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, #config_paths, #create_logger, #default_config, #log, #read_config, #validate_config

Constructor Details

#initialize(registry) ⇒ Registry

Returns a new instance of Registry.



25
26
27
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 25

def initialize(registry)
  @registry = registry
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

Class Method Details

.creds(image = nil) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 16

def self.creds(image = nil)
  if image.nil?
    registry = 'https://docker.picsolve.net'
  else
    registry = "https://#{image.split('/')[0]}"
  end
  new(registry).creds
end

Instance Method Details

#api_versionObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 33

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



29
30
31
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 29

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

#connectionObject



106
107
108
109
110
111
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 106

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

#credsObject



125
126
127
128
129
130
131
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 125

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

#dockercfgObject



117
118
119
120
121
122
123
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 117

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

#dockercfg_pathObject



113
114
115
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 113

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

#headersObject



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

def headers
  {
    'Host' => http_host,
    'Authorization' => "Basic #{}"
  }
end

#http_hostObject



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

def http_host
  URI(@registry).host
end

#list_tags(image) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 84

def list_tags(image)
  image = Repository.new 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



46
47
48
49
50
51
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 46

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

#list_tags_v2(image) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 53

def list_tags_v2(image)
  base_url = "/v2/#{image.user}/#{image.repo}"
  endpoint = "#{base_url}/tags/list"
  r = connection.get(path: endpoint)
  o = {}
  JSON.parse(r.body)['tags'].each do |tag|
    endpoint = "#{base_url}/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

#login_basicObject



133
134
135
136
137
# File 'lib/picsolve_docker_builder/helpers/registry.rb', line 133

def 
  dockercfg[http_host]['auth']
rescue StandardError
  nil
end

#unique_tag(image, tag) ⇒ Object



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

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