Class: OCIRegistry::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/oci_registry/utils.rb

Class Method Summary collapse

Class Method Details

.calculate_sha256(file_path) ⇒ Object

Function to calculate SHA256 hash of a file



27
28
29
# File 'lib/oci_registry/utils.rb', line 27

def self.calculate_sha256(file_path)
  Digest::SHA256.file(file_path).hexdigest
end

.copy(src_user:, src_pass:, dst_user:, dst_pass:, src_oci:, src_commit:, dst_oci:, dst_commit:) ⇒ Object

TODO: Copy FROM registry->local then local->registry. This avoids an error where a slow vs fast registry [causes a timeout](github.com/containers/image/issues/1083).



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/oci_registry/utils.rb', line 86

def self.copy(src_user:, src_pass:, dst_user:, dst_pass:, src_oci:, src_commit:, dst_oci:, dst_commit:)
  retries = 0
  begin
    details = {}
    # We need a tempdir to write the policy file
    Dir.mktmpdir do |dir|
      OCIRegistry::Utils.write_policy(filename: File.join(dir, "policy.json"))
      cmd = %Q[skopeo  --policy #{dir}/policy.json copy --src-creds #{src_user}:#{src_pass} --dest-creds #{dst_user}:#{dst_pass} docker://#{src_oci}:#{src_commit} docker://#{dst_oci}:#{dst_commit}]
      details = run_command(cmd)
      # Dir.mktmpdir do |tar|
      #   cmd = %Q[skopeo  --policy #{dir}/policy.json copy --src-creds  #{src_user}:#{src_pass} docker://#{src_oci}:#{src_commit} dir:#{tar}]
      #   details = run_command(cmd)
      #   cmd = %Q[skopeo  --policy #{dir}/policy.json copy --dest-creds #{dst_user}:#{dst_pass} dir:#{tar} docker://#{dst_oci}:#{dst_commit}]
      #   details = run_command(cmd)
      # end
    end
    if details[:status] == 1
      # Error occured
      # Logger: ("Error copying image:\n#{details[:err]}")
      raise "Error copying image from registry to registry #{details[:err]}."
    end
    details
  rescue Exception => e
    # Logger: ("Error caught copying image: #{e.message}.")
    if (retries += 1) < 3
      # Logger: ("Retry... ##{retries}.")
      sleep 10 * 2**retries # Exponential backoff
      retry
    end
    raise e
  end
end

.copy_image(src_user:, src_pass:, dst_user:, dst_pass:, src_oci:, src_commit:, dst_oci:, dst_commit:) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/oci_registry/utils.rb', line 66

def self.copy_image(src_user:, src_pass:, dst_user:, dst_pass:, src_oci:, src_commit:, dst_oci:, dst_commit:)
  src_user = Shellwords.escape(src_user)
  src_pass = Shellwords.escape(src_pass)
  dst_user = Shellwords.escape(dst_user)
  dst_pass = Shellwords.escape(dst_pass)

  copy(src_user: src_user, src_pass: src_pass, dst_user: dst_user, dst_pass: dst_pass, src_oci: src_oci, src_commit: src_commit, dst_oci: dst_oci, dst_commit: dst_commit)
  cmd = %Q[skopeo inspect --creds #{dst_user}:#{dst_pass} docker://#{dst_oci}:#{dst_commit}]
  details = run_command(cmd)
  if details[:status] == 1
    # Error occured
    # Logger: ("Error inspecting image:\n#{details[:err]}")
    raise "Image not found in registry #{details[:err]}."
  end
  inspect = JSON.parse(details[:out])
  inspect['Digest']
end

.get_image_digest(image_name) ⇒ Object



23
24
# File 'lib/oci_registry/utils.rb', line 23

def self.get_image_digest(image_name)
end

.run_command(cmd) ⇒ Object

Helper method to run shell commands



14
15
16
17
18
19
20
21
# File 'lib/oci_registry/utils.rb', line 14

def self.run_command(cmd)
  stdout, stderr, status = Open3.capture3(cmd)
  {
    out: stdout,
    err: stderr,
    status: status.exitstatus
  }
end

.sum_layers(skopeo_inspect) ⇒ Object

Takes the output of skopeo inspect and returns the size of the image by adding its layers. cmd = %Q[skopeo inspect oci:#td/tmp_stack:latest] skopeo_inspect = JSON.parse(‘#cmd`)



55
56
57
58
59
60
61
62
63
64
# File 'lib/oci_registry/utils.rb', line 55

def self.sum_layers(skopeo_inspect)
  # Ensure we treat as JSON...
  skopeo_inspect = JSON.parse(skopeo_inspect) if skopeo_inspect.is_a?(String)
  checksum = skopeo_inspect['Digest']
  if skopeo_inspect['LayersData'].nil?
    puts "skopeo_inspect: #{skopeo_inspect.to_yaml}"
    raise "No layers found."
  end
  size = skopeo_inspect['LayersData'].map { |l| l['Size'] }.sum
end

.write_policy(filename: "policy.json") ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/oci_registry/utils.rb', line 31

def self.write_policy(filename: "policy.json")
  File.open(filename, "w") do |f|
    f.write <<~EOF
      {
          "default": [
              {
                  "type": "insecureAcceptAnything"
              }
          ],
          "transports":
              {
                  "docker-daemon":
                      {
                          "": [{"type":"insecureAcceptAnything"}]
                      }
              }
      }
    EOF
  end
end