Class: DockerApi

Inherits:
Object
  • Object
show all
Defined in:
lib/support/docker_api.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, secure = true) ⇒ DockerApi

Returns a new instance of DockerApi.



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

def initialize(url, secure = true)
  # Docker.logger = Logger.new(STDOUT)
  # Docker.logger.level = Logger::DEBUG
  address = url.split("//").last.split(":").first
  port = url.split(":").last
  host = {address: address, docker_port: port, ssl: secure}
  @connection = DockerApi.connection(host)
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



25
26
27
# File 'lib/support/docker_api.rb', line 25

def connection
  @connection
end

Class Method Details

.connection(host) ⇒ Object

Build a connection to docker on a remote host. Need a connection to be able to make multiple docker calls within the app to different hosts. host: {address: “olex-qa2.openlogic.com”, docker_port: 4245 }



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/support/docker_api.rb', line 29

def self.connection(host)
  cert_path = ENV['DOCKER_CERT_PATH']
  scheme = "http"
  scheme = "https" if (ENV['DOCKER_TLS_VERIFY'] == "1")
  
  docker_connection_opts = {:client_cert=>"#{cert_path}/cert.pem", :client_key=>"#{cert_path}/key.pem",
    :ssl_ca_file=>"#{cert_path}/ca.pem", :scheme => scheme}
  
  # docker_connection_opts = {:client_cert=>"/Users/mikemoore/.docker/machine/machines/dev/cert.pem", :client_key=>"/Users/mikemoore/.docker/machine/machines/dev/key.pem",
  #   :ssl_ca_file=>"/Users/mikemoore/.docker/machine/machines/dev/ca.pem", :scheme=>"https"}
  docker_connection_opts[:scheme] = "http" if (host[:ssl] == false)
  docker_connection = Docker::Connection.new("tcp://#{host[:address]}:#{host[:docker_port]}", docker_connection_opts)
  return docker_connection
end

Instance Method Details

#find_container_with_name(wanted_name) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/support/docker_api.rb', line 53

def find_container_with_name(wanted_name)
  options = {
    all: true
  }
  all_containers = Docker::Container.all(options, @connection)
  all_containers.each do |container|
    json = container.json
    name = json['Name']
    return container if (name == "/#{wanted_name}")
  end
  return nil
end