Class: Armada::Container

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image, docker_host, options) ⇒ Container

Returns a new instance of Container.



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

def initialize(image, docker_host, options)
  @docker_host = docker_host
  @id          = nil
  @image       = image
  @name        = options[:container_name]
  @container   = docker_host.get_container(@name)
  @options     = options

  now_in_ns = Integer(Time.now.to_f * 1000000.0)
  @options[:binds] ||= []
  @options[:binds] << "/var/log/containers/#{@name}/#{SecureRandom.uuid}:/home/service/logs"
end

Instance Attribute Details

#containerObject (readonly)

Returns the value of attribute container.



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

def container
  @container
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.create_container_config(image_id, container_name, host, options = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/armada/docker/container.rb', line 70

def self.create_container_config(image_id, container_name, host, options = {})
  container_config = options[:container_config] || {}
  options[:env_vars][:HOST] = host

  container_config['Image'] = image_id || options[:image]
  container_config['Hostname'] = options[:hostname] || host

  if options[:port_bindings]
    container_config['ExposedPorts'] ||= {}
    options[:port_bindings].keys.each do |port|
      container_config['ExposedPorts'][port] = {}
    end
  end

  if container_name
    container_config['name'] = container_name
    options[:env_vars][:SERVICE_NAME] = container_name
    #should we do soemthing if container name isnt set?
  end

  if options[:env_vars]
    container_config['Env'] = options[:env_vars].map { |k,v| "#{k}=#{v}" }
  end

  if options[:binds]
    container_config['Volumes'] = options[:binds].inject({}) do |memo, v|
      memo[v.split(/:/).last] = {}
      memo
    end
    container_config['VolumesFrom'] = 'parent'
  end

  if options[:restart_policy]
    container_config["RestartPolicy"] = options[:restart_policy]
  end

  container_config
end

.create_host_config(options) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/armada/docker/container.rb', line 61

def self.create_host_config(options)
  host_config = options[:host_config] || {}
  host_config['Binds'] = options[:binds] if options[:binds] && !options[:binds].empty?
  host_config['PortBindings'] = options[:port_bindings] if options[:port_bindings]
  host_config['PublishAllPorts'] = true
  host_config['Privileged'] = options[:privileged] || false
  host_config
end

Instance Method Details

#create(container_config) ⇒ Object



41
42
43
# File 'lib/armada/docker/container.rb', line 41

def create(container_config)
  ::Docker::Container.create(container_config, @docker_host.connection)
end

#killObject



45
46
47
48
49
# File 'lib/armada/docker/container.rb', line 45

def kill
  return if @container.nil?
  info "Stopping old container #{@container.id[0..7]} (#{@name})"
  @container.kill :v => @options[:volumes]
end

#portsObject



109
110
111
# File 'lib/armada/docker/container.rb', line 109

def ports
  return @container.json["NetworkSettings"]["Ports"]
end

#removeObject



51
52
53
54
55
56
57
58
59
# File 'lib/armada/docker/container.rb', line 51

def remove
  return if @container.nil?
  info "Deleting old container #{@container.id[0..7]} (#{@name})"
  begin
    @container.remove
  rescue Exception => e
    error "Could not remove container #{@container.id[0..7]} (#{@name}).\nException was: #{e.message}"
  end
end

#startObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/armada/docker/container.rb', line 28

def start
  info "Creating new container for image - #{@image.name}:#{@image.tag} with image id (#{@image.id}) with container name #{@name}"
  container_config = Armada::Container.create_container_config(@image.id, @name, @docker_host.host, @options)
  begin
    @container = create(container_config)
    @id = @container.id
    info "Starting new container #{@id[0..11]}"
    @container.start!(Armada::Container.create_host_config(@options))
  rescue Exception => e
    raise "Error occured on #{@docker_host.host}:#{@docker_host.port}: #{e.message}"
  end
end

#stopObject



18
19
20
21
22
23
24
25
26
# File 'lib/armada/docker/container.rb', line 18

def stop
  if @container
    info "Stopping the running container named - #{@name}"
    kill
    remove
  else
    warn "No container found with the name #{@name}"
  end
end