Class: Docker::Maker

Inherits:
Object
  • Object
show all
Defined in:
lib/docker/maker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, name, path = "/usr/bin/docker") ⇒ Maker

Returns a new instance of Maker.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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

Instance Attribute Details

#dockerObject (readonly)

Returns the value of attribute docker.



9
10
11
# File 'lib/docker/maker.rb', line 9

def docker
  @docker
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/docker/maker.rb', line 9

def name
  @name
end

Instance Method Details

#_bash(cmd, input = nil) ⇒ Object

Raises:

  • (@img)


62
63
64
65
66
67
# 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

#_commitObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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

#_waitObject



57
58
59
60
# File 'lib/docker/maker.rb', line 57

def _wait
  out, s = _exec [docker, "wait", @img]
  raise "commit failed: #{out}" unless s
end

#bash(cmd) ⇒ Object



69
70
71
72
73
# File 'lib/docker/maker.rb', line 69

def bash cmd
  _bash cmd
  _wait
  _commit
end

#cmd(c) ⇒ Object



75
76
77
78
# File 'lib/docker/maker.rb', line 75

def cmd c
  @cmd = Array(c)
  _commit    
end

#env(hash) ⇒ Object



95
96
97
98
# File 'lib/docker/maker.rb', line 95

def env hash
  @env = @env + hash.inject([]) {|a, (k, v)| a << "#{k}=#{v}"}
  _commit
end

#expose(port) ⇒ Object



85
86
87
88
89
# File 'lib/docker/maker.rb', line 85

def expose port
  puts "exposing #{port}"
  @ports << port
  _commit
end

#maintainer(mnt) ⇒ Object



80
81
82
83
# File 'lib/docker/maker.rb', line 80

def maintainer mnt
  @maint = mnt
  _commit
end

#put(vals) ⇒ Object

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# 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) ⇒ Object



91
92
93
# File 'lib/docker/maker.rb', line 91

def user usr
  @user = usr
end