Class: Retag::Image

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/retag/image.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#cmd!, #dockerauth

Constructor Details

#initialize(image, tag, suffix: nil) ⇒ Image

Returns a new instance of Image.



10
11
12
13
14
15
# File 'lib/retag/image.rb', line 10

def initialize(image, tag, suffix: nil)
  @image = image
  @tag = tag
  @tag = "#{@tag}-#{suffix}" if suffix.present?
  @full = "#{@image}:#{@tag}"
end

Instance Attribute Details

#fullObject (readonly)

Returns the value of attribute full.



8
9
10
# File 'lib/retag/image.rb', line 8

def full
  @full
end

#imageObject (readonly)

Returns the value of attribute image.



8
9
10
# File 'lib/retag/image.rb', line 8

def image
  @image
end

#tagObject (readonly)

Returns the value of attribute tag.



8
9
10
# File 'lib/retag/image.rb', line 8

def tag
  @tag
end

Instance Method Details

#retag!(newtag = tag) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/retag/image.rb', line 17

def retag!(newtag = tag)
  uri = ::URI.parse("https://#{image}")
  repo = uri.path
  repo[0..0] = ''
  uri.path = ''

  content_type = 'application/vnd.docker.distribution.manifest.v2+json'

  Tempfile.create('manifest') do |file|
    manifest = JSON.parse(cmd!("curl -s -u '#{dockerauth(uri.host)}' -H 'Accept: #{content_type}' '#{uri}/v2/#{repo}/manifests/#{tag}' -o -", capture: true).strip)
    raise manifest.inspect unless manifest.dig('layers') || manifest.dig('config')

    file.write(manifest.to_json)
    file.flush
    result = cmd!("curl -s -u '#{dockerauth(uri.host)}' -H 'Content-Type: #{content_type}' -H 'Accept: #{content_type}' -X PUT --data-binary @#{file.path} '#{uri}/v2/#{repo}/manifests/#{newtag}' -o -", capture: true).strip
    raise result unless result.empty?
  end

  Image.new(image, newtag)
end