class HammerCLIKatello::Repository::UploadContentCommand
rubocop:disable ClassLength
Constants
- CONTENT_CHUNK_SIZE
Public Instance Methods
content_upload_resource()
click to toggle source
# File lib/hammer_cli_katello/repository.rb, line 374 def content_upload_resource ::HammerCLIForeman.foreman_resource(:content_uploads) end
execute()
click to toggle source
# File lib/hammer_cli_katello/repository.rb, line 357 def execute @failure = false files = option_content.sort if files.length.zero? output.print_error _("Could not find any files matching PATH") return HammerCLI::EX_NOINPUT end files.each do |file_path| last_file = file_path == files.last File.open(file_path, 'rb') { |file| upload_file(file, last_file: last_file) } end @failure ? HammerCLI::EX_DATAERR : HammerCLI::EX_OK end
request_headers()
click to toggle source
# File lib/hammer_cli_katello/repository.rb, line 353 def request_headers {:content_type => 'multipart/form-data'} end
Private Instance Methods
create_content_upload(size, checksum, content_type)
click to toggle source
# File lib/hammer_cli_katello/repository.rb, line 441 def create_content_upload(size, checksum, content_type) params = { :repository_id => get_identifier, :size => size, :checksum => checksum, :content_type => content_type } response = content_upload_resource.call(:create, params) response end
import_uploads(uploads, opts = {})
click to toggle source
# File lib/hammer_cli_katello/repository.rb, line 473 def import_uploads(uploads, opts = {}) publish_repository = opts.fetch(:last_file, false) sync_capsule = opts.fetch(:last_file, false) params = {:id => get_identifier, :uploads => uploads, publish_repository: publish_repository, sync_capsule: sync_capsule } params[:content_type] = options["option_content_type"] if options["option_content_type"] resource.call(:import_uploads, params) end
print_results(name, results)
click to toggle source
# File lib/hammer_cli_katello/repository.rb, line 485 def print_results(name, results) if results.empty? || results.dig('output', 'upload_results').empty? print_message _("Successfully uploaded file '%{name}'") % { :name => name } else upload_results = results.dig('output', 'upload_results') || [] upload_results.each do |result| if result['type'] == 'docker_manifest' print_message( _("Successfully uploaded manifest file '%{name}' with digest '%{digest}'") % { :name => name, :digest => result['digest'] }) else print_message _("Successfully uploaded file '%{name}'") % { :name => name } end end end end
silence_warnings() { || ... }
click to toggle source
# File lib/hammer_cli_katello/repository.rb, line 506 def silence_warnings original_verbose = $VERBOSE $VERBOSE = nil begin yield ensure $VERBOSE = original_verbose end end
update_content_upload(upload_id, repo_id, file)
click to toggle source
# File lib/hammer_cli_katello/repository.rb, line 452 def update_content_upload(upload_id, repo_id, file) offset = 0 while (content = file.read(CONTENT_CHUNK_SIZE)) params = { :offset => offset, :id => upload_id, :content => content, :size => file.size, :repository_id => repo_id, :multipart => true } # To workaround rest-client bug with false negative warnings, # see https://github.com/rest-client/rest-client/pull/670 for more details silence_warnings do content_upload_resource.call(:update, params, request_headers) end offset += CONTENT_CHUNK_SIZE end end
upload_file(file, opts = {})
click to toggle source
# File lib/hammer_cli_katello/repository.rb, line 414 def upload_file(file, opts = {}) total_size = File.size(file) checksum = Digest::SHA256.hexdigest(File.read(file)) content_type = options["option_content_type"] ? options["option_content_type"] : nil filename = File.basename(file.path) upload_create_response = create_content_upload(total_size, checksum, content_type) upload_id = upload_create_response["upload_id"] || "duplicate" content_unit_id = upload_create_response["content_unit_href"] unless content_unit_id repo_id = get_identifier update_content_upload(upload_id, repo_id, file) end results = import_uploads([ { id: upload_id, content_unit_id: content_unit_id, name: filename, size: file.size, checksum: checksum }], opts) print_results(filename, results) ensure if upload_id content_upload_resource.call(:destroy, :repository_id => get_identifier, :id => upload_id) end end