class Brick::Docker::DockerClient

Attributes

base_url[RW]
connection[RW]

Public Class Methods

connection(base_url) click to toggle source
# File lib/brick/docker/docker_client.rb, line 29
def self.connection base_url
  
conn = nil
  
  @@lock.synchronize do
    conn ||= @@connection_pool[base_url.to_sym]
    
    if(conn.nil?)
      conn = ::Docker::Connection.new(base_url, {}) 
      @@connection_pool[base_url.to_sym] = @connection
    end
  end 
  
  conn
end
default() click to toggle source
# File lib/brick/docker/docker_client.rb, line 60
def self.default
  @@default_client ||= DockerClient.new
  #puts "client=#{@@default_client}"
  return @@default_client
end
new(options={}) click to toggle source
# File lib/brick/docker/docker_client.rb, line 45
def initialize(options={})
  
  unless(options[:base_url].nil?)
    @base_url = options[:base_url]
  end
  
  
    @base_url ||= 'unix:///var/run/docker.sock'
  
  
  @connection= self.class.connection @base_url
  
  puts "#{__method__} #{__LINE__} @connection=#{@connection}"
end

Public Instance Methods

build_from_dir(options={}) click to toggle source
# File lib/brick/docker/docker_client.rb, line 132
def build_from_dir options={}
  
  image_name = options[:image_name]
  
  dockerfile_path = options[:build_dir]
  
  project_dir = options[:project_dir]
  
  no_cache = options[:no_cache]
  
  dockerfile_path = determine_dockerfile_path(dockerfile_path, project_dir)
  
  
  image = ::Docker::Image.build_from_dir(dockerfile_path, {"t"=>image_name, "nocache" =>no_cache }) { 
  |chunk| h1 = ::JSON.parse(chunk); 
          
          value = h1.values[0].to_s;
          
          puts(::URI.unescape(value)) 
  
  }

  
  image
end
create(config_hash, name=nil) click to toggle source
# File lib/brick/docker/docker_client.rb, line 66
def create config_hash, name=nil
  
 docker_hash= create_config(config_hash)
  
 docker_hash['name'] = name unless name.nil?
  
  begin
    container = ::Docker::Container.create(docker_hash.dup, connection)
    #get full information of the container
    container = ::Docker::Container.get(container.id,connection)
  rescue ::Docker::Error::NotFoundError => exception
      if exception.message.include? 'No such image'
        ::Docker::Image.create({'fromImage'=> config_hash['image']},{}, connection)
         container = ::Docker::Container.create(docker_hash.dup, connection)
         #get full information of the container
         container = ::Docker::Container.get(container.id,connection)
      else
        raise exception
    end
    return container
  end
end
get_container_by_id(id) click to toggle source
# File lib/brick/docker/docker_client.rb, line 127
def get_container_by_id id
  ::Docker::Container.get(id,connection)
end
get_container_by_name(name=nil) click to toggle source
# File lib/brick/docker/docker_client.rb, line 115
def get_container_by_name  name=nil
  
  container = nil
  
  unless name.nil?
    container = ::Docker::Container.search_by_name name, connection
  end
  
  container.first
  
end
run(config_hash, name=nil) click to toggle source

if the container is already existed, reuse it if the container is not started, start it

# File lib/brick/docker/docker_client.rb, line 92
def run config_hash, name=nil
  #byebug
  container = get_container_by_name name
  
  
  if container.nil?
    container = create config_hash, name
  else
    Brick::CLI::logger.info "container #{name} has already existed."
  end
  
  Brick::CLI::logger.debug "container #{container}."
  
  
  unless container.is_running?
    container.start(start_config(config_hash))
  else
    Brick::CLI::logger.info "container #{name} is #{container.info["Status"]}"
  end
  
  container
end