Class: Armada::Container
- Inherits:
-
Object
- Object
- Armada::Container
- Defined in:
- lib/armada/docker/container.rb
Instance Attribute Summary collapse
-
#container ⇒ Object
readonly
Returns the value of attribute container.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .create_container_config(image_id, container_name, host, options = {}) ⇒ Object
- .create_host_config(options) ⇒ Object
Instance Method Summary collapse
- #create(container_config) ⇒ Object
-
#initialize(image, docker_host, options) ⇒ Container
constructor
A new instance of Container.
- #kill ⇒ Object
- #ports ⇒ Object
- #remove ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
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, ) @docker_host = docker_host @id = nil @image = image @name = [:container_name] @container = docker_host.get_container(@name) @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
#container ⇒ Object (readonly)
Returns the value of attribute container.
4 5 6 |
# File 'lib/armada/docker/container.rb', line 4 def container @container end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
4 5 6 |
# File 'lib/armada/docker/container.rb', line 4 def id @id end |
#name ⇒ Object (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, = {}) container_config = [:container_config] || {} [:env_vars][:HOST] = host container_config['Image'] = image_id || [:image] container_config['Hostname'] = [:hostname] || host if [:port_bindings] container_config['ExposedPorts'] ||= {} [:port_bindings].keys.each do |port| container_config['ExposedPorts'][port] = {} end end if container_name container_config['name'] = container_name [:env_vars][:SERVICE_NAME] = container_name #should we do soemthing if container name isnt set? end if [:env_vars] container_config['Env'] = [:env_vars].map { |k,v| "#{k}=#{v}" } end if [:binds] container_config['Volumes'] = [:binds].inject({}) do |memo, v| memo[v.split(/:/).last] = {} memo end container_config['VolumesFrom'] = 'parent' end if [:restart_policy] container_config["RestartPolicy"] = [: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() host_config = [:host_config] || {} host_config['Binds'] = [:binds] if [:binds] && ![:binds].empty? host_config['PortBindings'] = [:port_bindings] if [:port_bindings] host_config['PublishAllPorts'] = true host_config['Privileged'] = [: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 |
#kill ⇒ Object
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 |
#ports ⇒ Object
109 110 111 |
# File 'lib/armada/docker/container.rb', line 109 def ports return @container.json["NetworkSettings"]["Ports"] end |
#remove ⇒ Object
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.}" end end |
#start ⇒ Object
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.}" end end |
#stop ⇒ Object
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 |