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 447 def content_upload_resource ::HammerCLIForeman.foreman_resource(:content_uploads) end
execute()
click to toggle source
# File lib/hammer_cli_katello/repository.rb, line 430 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 426 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 524 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 556 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, async: true } params[:content_type] = options["option_content_type"] if options["option_content_type"] if options["option_ostree_repository_name"] params[:ostree_repository_name] = options["option_ostree_repository_name"] end results = if options["option_async"] resource.call(:import_uploads, params) else task_progress(resource.call(:import_uploads, params)) end results end
print_results(name, results, async)
click to toggle source
rubocop:disable PerceivedComplexity
# File lib/hammer_cli_katello/repository.rb, line 584 def print_results(name, results, async) if !%w(error warning).include?(results) && !async # task successful && no async flag used upload_results = results.dig('output', 'upload_results') || [] if upload_results.empty? print_message _("Successfully uploaded file %s") % name else 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 %s") % name end end end elsif results&.dig('id') && async # async flag used print_message _("Content is being uploaded in task #{results['id']}.") else print_message _("Could not upload the content.") end end
silence_warnings() { || ... }
click to toggle source
rubocop:enable PerceivedComplexity
# File lib/hammer_cli_katello/repository.rb, line 609 def silence_warnings original_verbose = $VERBOSE $VERBOSE = nil begin yield ensure $VERBOSE = original_verbose end end
task_progress(task_or_id)
click to toggle source
Calls superclass method
# File lib/hammer_cli_katello/repository.rb, line 577 def task_progress(task_or_id) super task_id = task_or_id.is_a?(Hash) ? task_or_id['id'] : task_or_id load_task(task_id) end
update_content_upload(upload_id, repo_id, file)
click to toggle source
# File lib/hammer_cli_katello/repository.rb, line 535 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 495 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 opts[:ostree_repository_name] = options["option_ostree_repository_name"] opts[:filename] = filename results = import_uploads([ { id: upload_id, content_unit_id: content_unit_id, name: filename, size: file.size, checksum: checksum }], opts) print_results(filename, results, options["option_async"]) ensure if upload_id content_upload_resource.call(:destroy, :repository_id => get_identifier, :id => upload_id) end end