Class: Centurion::DockerRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/centurion/docker_registry.rb

Constant Summary collapse

OFFICIAL_URL =
'https://registry.hub.docker.com'

Instance Method Summary collapse

Constructor Details

#initialize(base_uri, registry_user = nil, registry_password = nil) ⇒ DockerRegistry

Returns a new instance of DockerRegistry.



10
11
12
13
14
# File 'lib/centurion/docker_registry.rb', line 10

def initialize(base_uri, registry_user=nil, registry_password=nil)
  @base_uri = base_uri
  @user = registry_user
  @password = registry_password
end

Instance Method Details

#digest_for_tag(repository, tag) ⇒ Object



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

def digest_for_tag(repository, tag)
  path = "/v1/repositories/#{repository}/tags/#{tag}"
  uri = uri_for_repository_path(repository, path)
  $stderr.puts "GET: #{uri}"
  options = { headers: { "Content-Type" => "application/json" } }
  if @user
    options[:user] = @user
    options[:password] = @password
  end
  response = Excon.get(
    uri,
    options
  )
  raise response.inspect unless response.status == 200

  # This hack is stupid, and I hate it. But it works around the fact that
  # the Docker Registry will return a base JSON String, which the Ruby parser
  # refuses (possibly correctly) to handle
  JSON.load('[' + response.body + ']').first
end

#repository_tags(repository) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/centurion/docker_registry.rb', line 37

def repository_tags(repository)
  path = "/v1/repositories/#{repository}/tags"
  uri = uri_for_repository_path(repository, path)

  $stderr.puts "GET: #{uri.inspect}"

  # Need to workaround a bug in Docker Hub to now pass port in Host header
  options = { omit_default_port: true }

  if @user
    options[:user] = @user
    options[:password] = @password
  end

  response = Excon.get(uri, options)
  raise response.inspect unless response.status == 200

  tags = JSON.load(response.body)

  # The Docker Registry API[1]  specifies a result in the format
  # { "[tag]" : "[image_id]" }. However, the official Docker registry returns a
  # result like [{ "layer": "[image_id]", "name": "[tag]" }].
  #
  # So, we need to normalize the response to what the Docker Registry API
  # specifies should be returned.
  #
  # [1]: https://docs.docker.com/v1.1/reference/api/registry_api/

  if is_official_registry?(repository)
    tags.each_with_object({}) do |tag, hash|
      hash[tag['name']] = tag['layer']
    end
  else
    tags
  end
end