Method: InspecPlugins::Compliance::CLI#upload
- Defined in:
- lib/plugins/inspec-compliance/lib/inspec-compliance/cli.rb
#upload(path) ⇒ Object
rubocop:disable Metrics/MethodLength, Metrics/AbcSize, PerceivedComplexity, Metrics/CyclomaticComplexity
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/cli.rb', line 126 def upload(path) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, PerceivedComplexity, Metrics/CyclomaticComplexity config = InspecPlugins::Compliance::Configuration.new return if !loggedin(config) # set owner to config config['owner'] = ['owner'] || config['user'] unless File.exist?(path) puts "Directory #{path} does not exist." exit 1 end vendor_deps(path, ) if File.directory?(path) o = .dup configure_logger(o) # only run against the mock backend, otherwise we run against the local system o[:backend] = Inspec::Backend.create(target: 'mock://') o[:check_mode] = true o[:vendor_cache] = Inspec::Cache.new(o[:vendor_cache]) # check the profile, we only allow to upload valid profiles profile = Inspec::Profile.for_target(path, o) # start verification process error_count = 0 error = lambda { |msg| error_count += 1 puts msg } result = profile.check unless result[:summary][:valid] error.call('Profile check failed. Please fix the profile before upload.') else puts('Profile is valid') end # determine user information if (config['token'].nil? && config['refresh_token'].nil?) || config['user'].nil? error.call('Please login via `inspec compliance login`') end # read profile name from inspec.yml profile_name = profile.params[:name] # read profile version from inspec.yml profile_version = profile.params[:version] # check that the profile is not uploaded already, # confirm upload to the user (overwrite with --force) if InspecPlugins::Compliance::API.exist?(config, "#{config['owner']}/#{profile_name}##{profile_version}") && !['overwrite'] error.call('Profile exists on the server, use --overwrite') end # abort if we found an error if error_count > 0 puts "Found #{error_count} error(s)" exit 1 end # if it is a directory, tar it to tmp directory generated = false if File.directory?(path) generated = true archive_path = Dir::Tmpname.create([profile_name, '.tar.gz']) {} puts "Generate temporary profile archive at #{archive_path}" profile.archive({ output: archive_path, ignore_errors: false, overwrite: true }) else archive_path = path end puts "Start upload to #{config['owner']}/#{profile_name}" pname = ERB::Util.url_encode(profile_name) if InspecPlugins::Compliance::API.is_automate_server?(config) || InspecPlugins::Compliance::API.is_automate2_server?(config) puts 'Uploading to Chef Automate' else puts 'Uploading to Chef Compliance' end success, msg = InspecPlugins::Compliance::API.upload(config, config['owner'], pname, archive_path) # delete temp file if it was temporary generated File.delete(archive_path) if generated && File.exist?(archive_path) if success puts 'Successfully uploaded profile' else puts 'Error during profile upload:' puts msg exit 1 end end |