class Decking::Container

Attributes

config[R]
container[R]
name[R]

Public Class Methods

[](name) click to toggle source
# File lib/decking/containers.rb, line 47
def [](name)
  instances[name] ||= new(name, @containers[name])
end
add(params) click to toggle source
# File lib/decking/containers.rb, line 42
def add params
  containers.update params.name => params
  self[params.name]
end
attach_all() click to toggle source
# File lib/decking/containers.rb, line 26
def attach_all 
  run_with_threads_multiplexed :attach, instances
end
containers() click to toggle source
# File lib/decking/containers.rb, line 34
def containers
  @containers ||= Hash.new
end
create_all() click to toggle source
# File lib/decking/containers.rb, line 18
def create_all ; map{|n, c| c.create  }; end
create_all!() click to toggle source
# File lib/decking/containers.rb, line 19
def create_all!; map{|n, c| c.create! }; end
delete_all() click to toggle source
# File lib/decking/containers.rb, line 15
def delete_all ; map{|n, c| c.delete  }; end
delete_all!() click to toggle source
# File lib/decking/containers.rb, line 16
def delete_all!; map{|n, c| c.delete! }; end
each(&block) click to toggle source
# File lib/decking/containers.rb, line 51
def each &block
  @instances.each(&block)
end
instances() click to toggle source
# File lib/decking/containers.rb, line 38
def instances
  @instances ||= Hash.new
end
new(name, params) click to toggle source
# File lib/decking/containers.rb, line 58
def initialize name, params
  @name   = name
  @config = params
end
start_all() click to toggle source
# File lib/decking/containers.rb, line 21
def start_all  ; map{|n, c| c.start   }; end
stop_all() click to toggle source
# File lib/decking/containers.rb, line 23
def stop_all   ; map{|n, c| c.stop    }; end
stop_all!() click to toggle source
# File lib/decking/containers.rb, line 24
def stop_all!  ; map{|n, c| c.stop!   }; end
tail_all_logs(*args) click to toggle source
# File lib/decking/containers.rb, line 30
def tail_all_logs *args
  run_with_threads_multiplexed :tail_logs, instances, *args
end

Public Instance Methods

create(force = false) click to toggle source
# File lib/decking/container/create.rb, line 3
def create force = false
  run_with_progress("#{"Forcefully " if force}Creating #{name}") do
    begin
      exposed_ports = Hash.new
      port.each do |val|
        vars = val.split(':')
        if vars.size == 3
          exposed_ports[vars[-2]] = Hash.new
        else
          exposed_ports[vars[-1]] = Hash.new
        end
      end
      @container = Docker::Container.create 'name'         => name,
                                            'Image'        => image,
                                            'Hostname'     => hostname,
                                            'Domainname'   => domainname,
                                            'Entrypoint'   => entrypoint,
                                            'Memory'       => memory,
                                            'MemorySwap'   => memory_swap,
                                            'CpuShares'    => cpu_shares,
                                            'Cpuset'       => cpu_set,
                                            'AttachStdout' => attach_stdout,
                                            'AttachStderr' => attach_stderr,
                                            'AttachStdin'  => attach_stdin,
                                            'Tty'          => tty,
                                            'OpenStdin'    => open_stdin,
                                            'StdinOnce'    => stdin_once,
                                            'Cmd'          => command.scan(/(?:"(?:\\.|[^"])*"|'(?:\\.|[^'])*'|[^'" ])+/).map{|val| val.gsub(/^['"]/,"").gsub(/['"]$/,"")},
                                            'Env'          => env.map { |k, v| "#{k}=#{v}" },
                                            'ExposedPorts' => exposed_ports
    rescue Docker::Error::ConflictError
      clear_progressline
      puts "Container #{name} already exists".yellow
      if force
        delete!
        retry
      else
        exit
      end
    rescue Docker::Error::NotFoundError
      clear_progressline
      puts "Image #{image} not found".yellow
      exit
    rescue Docker::Error::ServerError => e
      clear_progressline
      puts "Container #{name} encountered a ServerError".red
      puts e.message.red
      exit
    rescue Exception => e
      clear_progressline
      puts "Unhandled Exception #{e.message}"
      e.backtrace.map{|msg| puts "  #{msg}"}
      exit
    end
  end
end
create!() click to toggle source
# File lib/decking/container/create.rb, line 60
def create!
  create true
end
delete(force = false) click to toggle source
# File lib/decking/container/delete.rb, line 3
def delete force = false
  run_with_progress("#{'Forcefully ' if force}Deleting #{name}") do
    begin
      Docker::Container.get(name).remove('force' => force)
    rescue Docker::Error::NotFoundError
      clear_progressline
      @@logger.warn "Container #{name} does not exist, nothing to delete".yellow
    rescue Docker::Error::ServerError => e
      clear_progressline
      @@logger.error e.message.red
    end
  end
end
delete!() click to toggle source
# File lib/decking/container/delete.rb, line 17
def delete!
  delete true
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/decking/containers.rb, line 63
def method_missing method, *args, &block
  if config.key? method
    config[method]
  else
    super
  end
end
start() click to toggle source
# File lib/decking/container/start.rb, line 3
def start
  run_with_progress("Starting #{name}") do
    begin
      port_bindings = Hash.new
      port.each do |val|
        vars = val.split(':')
        case vars.size
        when 3
          port_bindings[vars[-1]] = [ { 'HostIp' => vars[0], 'HostPort' => vars[1] } ]
        when 2
          port_bindings[vars[-1]] = [ { 'HostIp' => '',      'HostPort' => vars[0] } ]
        else
          port_bindings[vars[0]]  = [ { 'HostPort' => vars[0] } ]
        end
      end

      start_links = Array.new
      if links.is_a? Array
        links.each do |val|
          start_links << val.dep + ':' + val.alias
        end
      end

      Docker::Container.get(name).start! 'Links'        => start_links,
                                         'Binds'        => binds,
                                         'LxcConf'      => lxc_conf,
                                         'PortBindings' => port_bindings
    rescue Docker::Error::NotFoundError
      clear_progressline
      puts "Container #{name} not found".red
      exit
    rescue Docker::Error::ServerError => e
      clear_progressline
      puts "Container #{name} encountered a ServerError".red
      puts e.message.red
      exit
    rescue Exception => e
      clear_progressline
      puts "Unhandled Exception #{e.message}"
      e.backtrace.map{|msg| puts "  #{msg}"}
      exit
    end
  end
end
stop(time_to_kill = 30) click to toggle source
# File lib/decking/container/stop.rb, line 4
def stop time_to_kill = 30
  run_with_progress("Stopping #{name}") do
    begin
      Docker::Container.get(name).stop('t' => time_to_kill)
    rescue Docker::Error::NotFoundError
      clear_progressline
      puts "Container #{name} does not exist, nothing to stop".yellow
    rescue Docker::Error::ServerError => e
      clear_progressline
      puts "Container #{name} encountered a ServerError".red
      puts e.message.red
      exit
    end
  end
end
stop!() click to toggle source
# File lib/decking/container/stop.rb, line 20
def stop!
  stop 1      
end
tail_logs(timestamps = false, lines = 0, follow = true) click to toggle source
# File lib/decking/container/attach.rb, line 4
def tail_logs timestamps = false, lines = 0, follow = true
  @@logger.info "Grabbing logs from #{name}... stop with ^C".yellow
  begin
    Docker::Container.get(name).streaming_logs(follow: follow, stdout: true, stderr: true, tail: lines, timestamps: timestamps) do |stream, chunk|
      case stream
        when :stdout
          $stdout.print "(#{name}) #{chunk}"
          $stdout.flush
        when :stderr
          $stdout.print "(#{name}) #{chunk}".red
          $stdout.flush
      end
    end
  rescue Docker::Error::NotFoundError
    @@logger.error "Container #{name} does not exist"
  end
end