Class: Retag::Image

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#cmd!, #dockerauth

Constructor Details

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

Returns a new instance of Image.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/retag/image.rb', line 10

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

  @uri = ::URI.parse("https://#{@image}").normalize
  @repo = @uri.path
  @repo[0..0] = ''
  @uri.path = ''
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



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

def api
  @api
end

#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

#repoObject (readonly)

Returns the value of attribute repo.



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

def repo
  @repo
end

#tagObject (readonly)

Returns the value of attribute tag.



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

def tag
  @tag
end

#uriObject (readonly)

Returns the value of attribute uri.



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

def uri
  @uri
end

Instance Method Details

#get_manifestObject



61
62
63
64
65
66
67
68
# File 'lib/retag/image.rb', line 61

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

  manifest
end

#retag!(newtag = tag, newproject: nil) ⇒ Object



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
52
53
54
55
56
57
58
59
# File 'lib/retag/image.rb', line 25

def retag!(newtag = tag, newproject: nil)
  project, *_rest = repo.split('/')
  registry = repo
  newrepo = "#{newproject}/#{registry}"

  Tempfile.create('manifest') do |file|
    manifest = get_manifest
    file.write(manifest.to_json)
    file.flush

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

    if newproject && project != newproject
      unless api == 'harbor'
        raise "Unable to promote image #{repo} -> #{newrepo} with #{api.inspect} api: Unimplemented"
      end

      newregistry = registry.split('/').join('-')
      newrepo = "#{newproject}/#{newregistry}"
      
      result = cmd!(
        "curl -s -u '#{dockerauth(uri.host)}' -X POST '#{uri}/api/v2.0/projects/#{newproject}/repositories/#{newregistry}/artifacts?from=#{repo}:#{newtag}' -o -", capture: true
      ).strip
    end

    raise result unless result.empty?
  end

  newuri = uri.dup
  newuri.path = "/#{newrepo}".gsub('//', '/')
  Image.new(newuri.hostname + newuri.path, newtag)
end