Class: Service::RegistryApi

Inherits:
Object
  • Object
show all
Defined in:
app/models/service/registry_api.rb

Constant Summary collapse

DOCKER_HUB =
'https://index.docker.io/'.freeze
DEFAULTS =
{
  url: 'http://localhost:5000'.freeze,
  connection: { omit_default_port: true,
                headers: { "Content-Type" => "application/json" }}
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ RegistryApi

Returns a new instance of RegistryApi.



13
14
15
16
17
18
# File 'app/models/service/registry_api.rb', line 13

def initialize(params = {})
  self.config = DEFAULTS.merge(params)
  self.url = config[:url]

  Docker.logger = logger if Rails.env.development? || Rails.env.test?
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



10
11
12
# File 'app/models/service/registry_api.rb', line 10

def config
  @config
end

#urlObject

Returns the value of attribute url.



10
11
12
# File 'app/models/service/registry_api.rb', line 10

def url
  @url
end

Class Method Details

.docker_hubObject



61
62
63
# File 'app/models/service/registry_api.rb', line 61

def self.docker_hub
  @@docker_hub ||= new(url: DOCKER_HUB)
end

Instance Method Details

#catalog(query) ⇒ Object

Some Registries might have this endpoint not implemented/enabled



41
42
43
44
45
# File 'app/models/service/registry_api.rb', line 41

def catalog(query)
  get('/v2/_catalog'.freeze)['repositories'].select do |image|
    image =~ /^#{query}/
  end.map { |image_name| { 'name' => image_name } }
end

#connectionObject



20
21
22
# File 'app/models/service/registry_api.rb', line 20

def connection
  @connection ||= ::Docker::Connection.new(url, default_connection_options.merge(credentials))
end

#get(path, params = nil) ⇒ Object



24
25
26
27
28
29
# File 'app/models/service/registry_api.rb', line 24

def get(path, params = nil)
  response = connection.get('/'.freeze, params,
                            connection.options.merge({ path: "#{path}" }))
  response = parse_json(response)
  response
end

#ok?Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
# File 'app/models/service/registry_api.rb', line 54

def ok?
  get('/v1/'.freeze).match("Docker Registry API")
rescue => e
  logger.warn "API v1 - Ping failed #{e.backtrace}"
  get('/v2/'.freeze).is_a? Hash
end

#search(query) ⇒ Object

Since the Registry API v2 does not support a search the v1 endpoint is used Newer registries will fail, the v2 catalog endpoint is used



33
34
35
36
37
38
# File 'app/models/service/registry_api.rb', line 33

def search(query)
  get('/v1/search'.freeze, { q: query })
rescue => e
  logger.warn "API v1 - Search failed #{e.backtrace}"
  { 'results' => catalog(query) }
end

#tags(image_name, query = nil) ⇒ Object



47
48
49
50
51
52
# File 'app/models/service/registry_api.rb', line 47

def tags(image_name, query = nil)
  result = get_tags(image_name)
  result = result.keys.map { |t| {'name' => t.to_s } } if result.is_a? Hash
  result = filter_tags(result, query) if query
  result
end