Class: Actions::Pulp3::Repository::UploadFile

Inherits:
AbstractAsyncTask show all
Defined in:
app/lib/actions/pulp3/repository/upload_file.rb

Instance Method Summary collapse

Methods inherited from AbstractAsyncTask

#cancel, #cancel!, #combined_tasks, #done?, #external_task, #humanized_state, #new_or_existing_objects, #pulp_tasks, #rescue_external_task, #run, #task_groups

Methods inherited from Abstract

#smart_proxy

Instance Method Details

#check_error_detailsObject



62
63
64
65
66
67
68
69
# File 'app/lib/actions/pulp3/repository/upload_file.rb', line 62

def check_error_details
  output[:pulp_tasks].each do |pulp_task|
    error_details = pulp_task.try(:[], "error")
    if error_details && !error_details.nil? && !unique_error(error_details)
      fail _("An error occurred during upload \n%{error_message}") % {:error_message => error_details}
    end
  end
end

#check_for_errorsObject



71
72
73
74
75
76
77
78
79
# File 'app/lib/actions/pulp3/repository/upload_file.rb', line 71

def check_for_errors
  combined_tasks.each do |task|
    if unique_error task.error
      warn _("Duplicate artifact detected")
    else
      super
    end
  end
end

#external_task=(tasks) ⇒ Object

rubocop:enable Metrics/MethodLength



54
55
56
# File 'app/lib/actions/pulp3/repository/upload_file.rb', line 54

def external_task=(tasks)
  super
end

#finalizeObject



58
59
60
# File 'app/lib/actions/pulp3/repository/upload_file.rb', line 58

def finalize
  check_error_details
end

#invoke_external_taskObject

rubocop:disable Metrics/MethodLength



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/lib/actions/pulp3/repository/upload_file.rb', line 10

def invoke_external_task
  repo = ::Katello::Repository.find(input[:repository_id])
  repo_backend_service = repo.backend_service(smart_proxy)
  upload_class = repo_backend_service.core_api.upload_class
  uploads_api = repo_backend_service.core_api.uploads_api
  offset = 0
  response = nil
  File.open(input[:file], "rb") do |file|
    total_size = File.size(file)
    upload_href = uploads_api.create(upload_class.new(size: total_size)).pulp_href
    sha256 = Digest::SHA256.hexdigest(File.read(file))
    until file.eof?
      chunk = file.read(upload_chunk_size)
      begin
        filechunk = Tempfile.new('filechunk', :encoding => 'ascii-8bit')
        filechunk.write(chunk)
        filechunk.flush
        actual_chunk_size = File.size(filechunk)
        response = uploads_api.update(content_range(offset, offset + actual_chunk_size - 1, total_size), upload_href, filechunk)
        offset += actual_chunk_size
      ensure
        filechunk.close
        filechunk.unlink
      end
    end

    if response
      upload_href = response.pulp_href
      #Check for any duplicate artifacts created in parallel subtasks
      duplicate_sha_artifact_list = ::Katello::Pulp3::Api::Core.new(smart_proxy).artifacts_api.list("sha256": sha256)
      duplicate_sha_artifact_href = duplicate_sha_artifact_list&.results&.first&.pulp_href
      if duplicate_sha_artifact_href
        uploads_api.delete(upload_href)
        output[:artifact_href] = duplicate_sha_artifact_href
        output[:pulp_tasks] = nil
      else
        output[:artifact_href] = nil
        output[:pulp_tasks] = [uploads_api.commit(upload_href, sha256: sha256)]
      end
    end
  end
end

#plan(repository, smart_proxy, file) ⇒ Object



5
6
7
# File 'app/lib/actions/pulp3/repository/upload_file.rb', line 5

def plan(repository, smart_proxy, file)
  plan_self(:repository_id => repository.id, :smart_proxy_id => smart_proxy.id, :file => file)
end