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 431
def content_upload_resource
  ::HammerCLIForeman.foreman_resource(:content_uploads)
end
execute() click to toggle source
# File lib/hammer_cli_katello/repository.rb, line 414
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 410
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 508
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 540
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 CyclomaticComplexity rubocop:disable PerceivedComplexity

silence_warnings() { || ... } click to toggle source

rubocop:enable CyclomaticComplexity rubocop:enable PerceivedComplexity

# File lib/hammer_cli_katello/repository.rb, line 595
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 561
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 519
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 479
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