Class: Hippo::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/hippo/image.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ Image

Returns a new instance of Image.



9
10
11
12
# File 'lib/hippo/image.rb', line 9

def initialize(name, options)
  @name = name
  @options = options
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/hippo/image.rb', line 14

def name
  @name
end

Instance Method Details

#can_check_for_existence?Boolean

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/hippo/image.rb', line 53

def can_check_for_existence?
  @options['existenceCheck'].nil? ||
    @options['existenceCheck'] == true
end

#exists?Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hippo/image.rb', line 58

def exists?
  return true unless tag.is_a?(RepositoryTag)
  return true if host.nil?
  return true unless can_check_for_existence?

  credentials = Hippo.config.dig('docker', 'credentials', host)
  http = Net::HTTP.new(host, 443)
  http.use_ssl = true
  request = Net::HTTP::Head.new("/v2/#{image_name}/manifests/#{tag}")
  if credentials
    request.basic_auth(credentials['username'], credentials['password'])
  end
  response = http.request(request)

  case response
  when Net::HTTPOK
    true
  when Net::HTTPUnauthorized
    raise Error, "Could not authenticate to #{host} to verify image existence"
  when Net::HTTPNotFound
    false
  else
    raise Error, "Got #{response.code} status when verifying imag existence with #{host}"
  end
end

#hostObject



16
17
18
# File 'lib/hippo/image.rb', line 16

def host
  @options['host']
end

#image_nameObject



20
21
22
# File 'lib/hippo/image.rb', line 20

def image_name
  @options['name']
end

#image_urlObject



36
37
38
39
40
41
42
# File 'lib/hippo/image.rb', line 36

def image_url
  if host
    "#{host}/#{image_name}:#{tag}"
  else
    "#{image_name}:#{tag}"
  end
end

#tagObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/hippo/image.rb', line 24

def tag
  @tag ||= begin
    if @options['tag'].is_a?(Hash) && repo = @options['tag']['fromRepository']
      RepositoryTag.new(repo)
    elsif @options['tag'].nil?
      'latest'
    else
      @options['tag'].to_s
    end
  end
end

#template_varsObject



44
45
46
47
48
49
50
51
# File 'lib/hippo/image.rb', line 44

def template_vars
  @template_vars ||= {
    'host' => host,
    'name' => image_name,
    'tag' => tag.to_s,
    'url' => image_url
  }
end