class Docker::Maker

Attributes

docker[R]
name[R]

Public Class Methods

new(base, name, path="/usr/bin/docker") click to toggle source
# File lib/docker/maker.rb, line 11
def initialize base, name, path="/usr/bin/docker"
  @docker = path
  @name = name
  @ports = []
  @env = []
  
  _, s = _exec [docker, "images", "|", "grep", base]
  unless s
    msg, s = _exec [docker, "pull", base]      
    raise msg unless s
  end
  @img, s = _exec [docker, "run", "-d", base, "/bin/bash", "-c", "ls"]
  raise out unless s
  _commit
end

Public Instance Methods

_bash(cmd, input=nil) click to toggle source
# File lib/docker/maker.rb, line 62
def _bash cmd, input=nil
  cmd = [docker, "run", "-d", @name, "/bin/bash", "-c", cmd]    
  @img, s = _exec cmd, input
  raise @img unless s
  s
end
_commit() click to toggle source
# File lib/docker/maker.rb, line 42
def _commit
  c = [docker, "commit"]
  run = {"PortSpecs" => @ports,
    "Env" => @env}
  
  run['Cmd'] = @cmd if @cmd
  run['User'] = @user if @user
  if @maint
    c << "-m"
    c << @maint
  end
  out, s = _exec [docker, "commit", "-run", JSON.dump(run),  @img, @name]
  raise "commit failed: #{out}" unless s
end
_exec(args, input=nil) click to toggle source
# File lib/docker/maker.rb, line 27
def _exec args, input=nil
  puts "#{args.join(" ")}"
  Open3.popen3(*args) do |sin, sout, serr, wait|
    if input 
      while buff = input.read(4096)
        sin.write buff
      end
    end
    sin.close      
    status = wait.value
    m = sout.read.strip
    [m, status.success?]
  end    
end
_wait() click to toggle source
# File lib/docker/maker.rb, line 57
def _wait
  out, s = _exec [docker, "wait", @img]
  raise "commit failed: #{out}" unless s
end
bash(cmd) click to toggle source
# File lib/docker/maker.rb, line 69
def bash cmd
  _bash cmd
  _wait
  _commit
end
cmd(c) click to toggle source
# File lib/docker/maker.rb, line 75
def cmd c
  @cmd = Array(c)
  _commit    
end
env(hash) click to toggle source
# File lib/docker/maker.rb, line 95
def env hash
  @env = @env + hash.inject([]) {|a, (k, v)| a << "#{k}=#{v}"}
  _commit
end
expose(port) click to toggle source
# File lib/docker/maker.rb, line 85
def expose port
  puts "exposing #{port}"
  @ports << port
  _commit
end
maintainer(mnt) click to toggle source
# File lib/docker/maker.rb, line 80
def maintainer mnt
  @maint = mnt
  _commit
end
put(vals) click to toggle source

IMG=$(docker run -i -a stdin brianm/ruby /bin/bash -c “/bin/cat > /echo.rb” < ./echo.rb)

# File lib/docker/maker.rb, line 102
def put vals
  vals.each do |k, v|
    if File.directory? k
      # mkdir foo; bsdtar -cf - -C adir . | (bsdtar xpf - -C foo )
      open("|tar -cf - -C #{k} .") do |input|
        bash "mkdir -p #{v}"
        cmd = [docker, "run", "-i","-a", "stdin", @name, 
               "/bin/bash", "-c", "tar xpf - -C #{v}"]
        @img, s = _exec cmd, input
        raise @img unless s
        _wait
        _commit
      end
    else    
      File.open(k, "r") do |input|
        cmd = [docker, "run", "-i","-a", "stdin", @name, 
               "/bin/bash", "-c", "/bin/cat >  #{v}"]
        @img, s = _exec cmd, input
        raise @img unless s
        _wait
        _commit
      end
    end
  end
end
user(usr) click to toggle source
# File lib/docker/maker.rb, line 91
def user usr
  @user = usr
end