Class: Armada::Image

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(docker_host, options) ⇒ Image

do not use this method directly, instead call create



6
7
8
9
10
11
12
13
14
# File 'lib/armada/docker/image.rb', line 6

def initialize(docker_host, options)
  @name        = options[:image]
  @tag         = options[:tag]
  @pull        = options[:pull]
  @docker_host = docker_host
  @image       = options[:docker_image]
  @id          = @image.id if @image
  @auth        = generate_auth(options)
end

Instance Attribute Details

#authObject (readonly)

Returns the value of attribute auth.



4
5
6
# File 'lib/armada/docker/image.rb', line 4

def auth
  @auth
end

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/armada/docker/image.rb', line 4

def id
  @id
end

#imageObject (readonly)

Returns the value of attribute image.



4
5
6
# File 'lib/armada/docker/image.rb', line 4

def image
  @image
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/armada/docker/image.rb', line 4

def name
  @name
end

#tagObject (readonly)

Returns the value of attribute tag.



4
5
6
# File 'lib/armada/docker/image.rb', line 4

def tag
  @tag
end

Instance Method Details

#error(message) ⇒ Object



82
83
84
# File 'lib/armada/docker/image.rb', line 82

def error(message)
  Armada.ui.error "#{@docker_host.host} -- #{message}"
end

#extract_credentials(options) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/armada/docker/image.rb', line 36

def extract_credentials(options)
  options_credentials = options.select { |k, v| [:username, :password, :email].include? k }
  unless options_credentials.empty?
    return options_credentials
  else
    dockercfg = options[:dockercfg].for_image @name if options[:dockercfg]
    if dockercfg.nil?
      return {}
    else
      return {
        :username => dockercfg.username,
        :password => dockercfg.password,
        :email    => dockercfg.email
      }
    end
  end
end

#generate_auth(options) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/armada/docker/image.rb', line 64

def generate_auth(options)
  credentials = extract_credentials(options)

  if valid_credentials? credentials
    return credentials
  else
    return nil
  end
end

#info(message) ⇒ Object



74
75
76
# File 'lib/armada/docker/image.rb', line 74

def info(message)
  Armada.ui.info "#{@docker_host.host} -- #{message}"
end

#pullObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/armada/docker/image.rb', line 20

def pull
  if @pull
    begin
      info "Pulling image [#{@name}] with tag [#{@tag}]"
      @image = ::Docker::Image.create({:fromImage => @name, :tag => @tag}, @auth, @docker_host.connection)
      @id = @image.id
    rescue Exception => e
      raise "An error occurred while trying to pull image [#{@name}] with tag [#{@tag}] -- #{e.message}"
    end
  else
    info "Not pulling image [#{@name}] with tag [#{@tag}] because `--no-pull` was specified."
    @id = "#{@name}:#{@tag}"
    raise "The image id is not set, you cannot proceed with the deploy until a valid image is found -- [#{@name}:#{@tag}]" unless valid?
  end
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  return @id && @image
end

#valid_credentials?(m) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
# File 'lib/armada/docker/image.rb', line 54

def valid_credentials?(m)
  [:username, :password].each do |k|
    unless m.include? k
      return false
    end
  end

  return true
end

#warn(message) ⇒ Object



78
79
80
# File 'lib/armada/docker/image.rb', line 78

def warn(message)
  Armada.ui.warn "#{@docker_host.host} -- #{message}"
end