Class: PicsolveDockerBuilder::Helpers::Registry

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/picsolve_docker_builder/helpers/registry.rb

Overview

This represents a remote registry, that is queried for for informations about images

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

#base_dir, #config_file, #config_path, #create_logger, #default_config, #log, #read_config, #validate_config

Constructor Details

#initialize(registry) ⇒ Registry

Returns a new instance of Registry.



15
16
17
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 15

def initialize(registry)
  @registry = registry
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 13

def config
  @config
end

Class Method Details

.creds(*args) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 111

def self.creds(*args)
  auth = Base64.decode64(
    (
      *args
    )
  ).split(':')
  {
    'username' => auth[0],
    'password' => auth[1]
  }
end

.dockercfgObject



103
104
105
106
107
108
109
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 103

def self.dockercfg
  JSON.parse(
    File.open(
      dockercfg_path
    ).read
  )
end

.dockercfg_pathObject



99
100
101
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 99

def self.dockercfg_path
  File.expand_path '~/.dockercfg'
end

.get_login_basic(registry = 'docker.picsolve.net') ⇒ Object



123
124
125
126
127
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 123

def self.(registry = 'docker.picsolve.net')
  dockercfg[registry]['auth']
rescue StandardError
  nil
end

.repo_tag_unique(image_name) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 129

def self.repo_tag_unique(image_name)
  repo_tag = image_name.split(%r{/})[1..-1].join('/')
  repo_tag_split = repo_tag.split(/:/)
  repo = repo_tag_split[0]
  tag = repo_tag_split[1] || 'latest'

  headers = {}
   = 
  headers['Authorization'] = "Basic #{login_basic}" \
    unless .nil?

  connection = Excon.new(
    'https://docker.picsolve.net',
    headers: headers,
    persistent: true
  )
  response = connection.get(path: "/v1/repositories/#{repo}/tags")

  tags = JSON.parse(response.body)

  hash = tags[tag]

  tags.each do |t, h|
    next if h != hash
    next if t == tag
    next unless t.match(/jenkins-[0-9]+/)
    return {
      tag_unique: "#{image_name.split(':').first}:#{t}",
      hash: hash
    }
  end

  fail "Can not find a uniqe tag for #{image_name}"
end

Instance Method Details

#api_versionObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 23

def api_version
  return @api_version unless @api_version.nil?
  if connection.get(path: '/v2/').status == 200
    @api_version = :v2
  elsif connection.get(path: '/v1/_ping').status == 200
    @api_version = :v1
  else
    api_version_unsupported
  end
  log.info "Detected api version #{@api_version} on registry #{@registry}"
  @api_version
end

#api_version_unsupportedObject



19
20
21
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 19

def api_version_unsupported
  fail 'No supported version found at registry'
end

#connectionObject



92
93
94
95
96
97
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 92

def connection
  @connection ||= Excon.new(
    @registry,
    headers: headers
  )
end

#headersObject



86
87
88
89
90
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 86

def headers
  {
    'Host' => http_host
  }
end

#http_hostObject



82
83
84
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 82

def http_host
  URI(@registry).host
end

#list_tags(image) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 72

def list_tags(image)
  if api_version == :v1
    list_tags_v1(image)
  elsif api_version == :v2
    list_tags_v2(image)
  else
    api_version_unsupported
  end
end

#list_tags_v1(image) ⇒ Object



36
37
38
39
40
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 36

def list_tags_v1(image)
  endpoint = "/v1/repositories/#{image}/tags"
  r = connection.get(path: endpoint)
  JSON.parse(r.body)
end

#list_tags_v2(image) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 42

def list_tags_v2(image)
  endpoint = "/v2/#{image}/tags/list"
  r = connection.get(path: endpoint)
  o = {}
  JSON.parse(r.body)['tags'].each do |tag|
    endpoint = "/v2/#{image}/manifests/#{tag}"
    r = connection.get(path: endpoint)
    digest = Digest::SHA2.new(256)
    JSON.parse(r.body)['fsLayers'].each do |fs_hash|
      digest.update(fs_hash['blobSum'])
    end
    o[tag] = digest.to_s
  end
  o
end

#unique_tag(image, tag) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 58

def unique_tag(image, tag)
  tags = list_tags(image)
  fail "tag '#{tag}' not found" unless tags.include? tag
  tags.each do |my_tag, hash|
    # next if not right name
    next unless my_tag.match(/^jenkins-/)
    # next hash not matching
    next if hash != tags[tag]
    # return tag name now
    return my_tag
  end
  fail "no unique tag found for tag=#{tag}"
end