Module: Admiral::Docker

Defined in:
lib/admiral/docker.rb

Constant Summary collapse

@@core_parameters =
['docker', 'image', 'username', 'password', 'keyfile', 'pubkeyfile', 'registry', 'layers', 'tests', 'hostname']

Class Method Summary collapse

Class Method Details

.apply_layer(platform, layer_uid, ipaddress) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/admiral/docker.rb', line 282

def self.apply_layer(platform, layer_uid, ipaddress)

  begin
    require "admiral/layers/#{layer_uid}.rb"
  rescue LoadError
    STDERR.puts "Layer #{layer_uid} not found"
    return false
  end

  begin
    kclass = ::Admiral::Layers.const_get(Admiral::Layer.uid_to_name(layer_uid))
  rescue NameError
    STDERR.puts "Layer #{layer_uid} has a mistake"
    return false
  end
  layer = kclass.new(platform,ipaddress)

  valid = layer.verify()
  if not valid
    return false
  end

  return layer.run()
end

.apply_layers(platform, ipaddress) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/admiral/docker.rb', line 213

def self.apply_layers(platform, ipaddress)

  layers = platform['layers']

  layers.each do | layer_uid |
    begin
      require "admiral/layers/#{layer_uid}.rb"
    rescue LoadError
      STDERR.puts "Layer #{layer_uid} not found"
      return false
    end

    begin
      kclass = ::Admiral::Layers.const_get(Admiral::Layer.uid_to_name(layer_uid))
    rescue NameError
      STDERR.puts "Layer #{layer_uid} has a mistake"
      return false
    end
    layer = kclass.new(platform,ipaddress)

    valid = layer.verify()
    if not valid
      return false
    end

    success = layer.run()
    if not success
      return false
    end

  end
  return true
end

.apply_test_layers(platform, ipaddress) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/admiral/docker.rb', line 247

def self.apply_test_layers(platform, ipaddress)

  layers = platform['tests']

  layers.each do | layer_uid |
    begin
      require "admiral/layers/#{layer_uid}.rb"
    rescue LoadError
      STDERR.puts "Layer #{layer_uid} not found"
      return false
    end

    begin
      kclass = ::Admiral::Layers.const_get(Admiral::Layer.uid_to_name(layer_uid))
    rescue NameError
      STDERR.puts "Layer #{layer_uid} has a mistake"
      return false
    end
    layer = kclass.new(platform,ipaddress)

    valid = layer.verify()
    if not valid
      return false
    end

    success = layer.run()
    if not success
      return false
    end

  end
  return true
end

.create(platform) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/admiral/docker.rb', line 22

def self.create (platform)

  platform_name = platform['name']
  image         = platform['image']
  docker        = platform['docker']
  hostname      = platform['hostname']
  ssh_key_file  = platform['keyfile']
  username      = platform['username']
  password      = platform['password']
  volumes       = platform['volumes']

  last_container_id = get_container_id(platform_name)
  if last_container_id
    puts "Container exist : #{last_container_id}"
  else
    dockerfile = generate_dockerfile(platform)

    volumes_cmd = ''

    if volumes.kind_of?(Array)
      volumes.each do | volume |
        if not volume['guest']
          STDERR.puts "ERROR: Volume must have 'guest' parameter"
          exit!
        else
          if volume['host']
            volumes_cmd << "-v #{volume['host']}:#{volume['guest']} "
          else
            volumes_cmd << "-v #{volume['guest']} "
          end
        end
      end
    end

    puts "=== Create image ==="

    begin
      output = Admiral::Shell.local("docker -H #{docker} build --build-arg=USERNAME='#{username}' --build-arg=PASSWORD='#{password}' --no-cache -", {:input => dockerfile}, true)
    rescue Interrupt
        STDERR.puts "Creation interrupted"
        exit!
    end

    if output

      image_id = output.gsub(/.* /m, "")
      puts "Image ID : #{image_id}"

      Dir.mkdir(".states") unless File.exists?(".states")
      f = File.open(".states/#{platform_name}.image", "w")
      f.write("#{image_id}")
      f.close

      puts "=== Create container ==="
      container_id = Admiral::Shell.local("docker -H #{docker} run -d -p 22 -h #{hostname} --privileged --cap-add ALL #{volumes_cmd} #{image_id}")

      if container_id
        puts "Container ID : #{container_id}"
        Dir.mkdir(".states") unless File.exists?(".states")
        f = File.open(".states/#{platform_name}.container", "w")
        f.write("#{container_id}")
        f.close

        output = Admiral::Shell.local("docker  -H #{docker} inspect #{container_id}")
        if output
          ipaddress = extract_ipaddress(output)
          puts "=== Configuring container ==="
          success = self.apply_layers(platform, ipaddress)
          if not success
            STDERR.puts "Failed to apply configuration layers, run destroy"
            destroy(platform)
            exit!
          end
        else
          STDERR.puts "Failed to log in container, run destroy"
          destroy(platform)
          exit!
        end
      else
        STDERR.puts "Failed to create container, run destroy"
        destroy(platform)
        exit!
      end
    end
  end
end

.destroy(platform) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/admiral/docker.rb', line 144

def self.destroy (platform)
  platform_name = platform['name']
  docker = platform['docker']

  last_container_id = get_container_id(platform_name)
  if last_container_id
    puts "Remove container #{last_container_id}"
    Admiral::Shell.local("docker  -H #{docker} rm -f #{last_container_id}")
    File.delete(".states/#{platform_name}.container")
  else
    puts "No container"
  end

  last_image_id = get_image_id(platform_name)

  if last_container_id
    puts "Remove image #{last_image_id}"
    Admiral::Shell.local("docker  -H #{docker} rmi -f #{last_image_id}")
    File.delete(".states/#{platform_name}.image")
  else
    puts "No image"
  end
end

.extract_ipaddress(container_info) ⇒ Object



361
362
363
364
# File 'lib/admiral/docker.rb', line 361

def self.extract_ipaddress(container_info)
  data  = YAML.load(container_info).first
  return data['NetworkSettings']['IPAddress']
end

.generate_dockerfile(platform) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/admiral/docker.rb', line 308

def self.generate_dockerfile(platform)
  image      = platform["image"]
  username   = platform['username']
  password   = platform['password']
  pubkeyfile = platform['pubkeyfile']
  registry   = platform['registry']

  begin
    f = File.open(pubkeyfile, 'r')
    public_key = f.read().chomp()
    f.close
  rescue Errno::ENOENT  => e
     STDERR.puts "Error with public key : #{e.message}"
     exit!
  end

  from = "FROM #{registry}/#{image}\n"

  user = <<-eos
    ARG USERNAME
    ARG PASSWORD
    RUN useradd -d /home/${USERNAME} -m -s /bin/bash ${USERNAME}
    RUN echo ${USERNAME}:${PASSWORD} | chpasswd
    RUN echo "${USERNAME} ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
  eos

  key = <<-eos
    RUN mkdir /home/${USERNAME}/.ssh
    RUN echo '#{public_key}' >> /home/${USERNAME}/.ssh/authorized_keys
  eos

  tmpdir = <<-eos
    RUN mkdir /tmp/${USERNAME}/
    RUN chown ${USERNAME}:${USERNAME} /tmp/${USERNAME}
  eos

  ssh_env = <<-eos
    RUN echo "AcceptEnv *" >> /etc/ssh/sshd_config
  eos

  [from, user, key, tmpdir, ssh_env].join("\n")
end

.get_container_id(platform_name) ⇒ Object



202
203
204
205
206
207
208
209
210
211
# File 'lib/admiral/docker.rb', line 202

def self.get_container_id(platform_name)
   if File.exists?(".states/#{platform_name}.container")
      f = File.open(".states/#{platform_name}.container", "r")
      container_id = f.read()
      f.close
     return container_id
   else
     return nil
   end
end

.get_image_id(platform_name) ⇒ Object



191
192
193
194
195
196
197
198
199
200
# File 'lib/admiral/docker.rb', line 191

def self.get_image_id(platform_name)
   if File.exists?(".states/#{platform_name}.image")
      f = File.open(".states/#{platform_name}.image", "r")
      image_id = f.read()
      f.close
     return image_id
   else
     return nil
   end
end

.get_ip_address(docker, container_id) ⇒ Object



351
352
353
354
355
356
357
358
359
# File 'lib/admiral/docker.rb', line 351

def self.get_ip_address(docker, container_id)
  container_info = Admiral::Shell.local("docker -H #{docker} inspect #{container_id}")
  if container_info
    data  = YAML.load(container_info).first
    return data['NetworkSettings']['IPAddress']
  else
    return nil
  end
end

.login(platform) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/admiral/docker.rb', line 168

def self.(platform)

  container_id = get_container_id(platform['name'])
  docker = platform['docker']

  if container_id
    output = Admiral::Shell.local("docker  -H #{docker} inspect #{container_id}")
    if output
      ipaddress = extract_ipaddress(output)
      username  = platform['username']
      keyfile   = platform['keyfile']
      cmd       = "/bin/bash"

      puts "Log in to #{ipaddress}"
      Admiral::Shell.remote(ipaddress, username, keyfile, cmd)
    else
      puts "Failed to get ip address"
    end
  else
     puts "No container"
  end
end

.test(platform) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/admiral/docker.rb', line 109

def self.test (platform)
  platform_name = platform['name']
  testdir = "test"
  if Dir.exists?(testdir)
    puts "=== Run tests ==="
    container_id = get_container_id(platform['name'])
    docker = platform['docker']

    if container_id
      output = Admiral::Shell.local("docker  -H #{docker} inspect #{container_id}")
      if output
        ipaddress = extract_ipaddress(output)
        success = self.apply_test_layers(platform, ipaddress)
        if not success
          STDERR.puts "One or more tests failed, run destroy"
          destroy(platform)
          exit!
        end
      else
        STDERR.puts "Failed to get IP address"
        destroy(platform)
        exit!
      end
    else
      STDERR.puts "Failed to container ID"
      destroy(platform)
      exit!
    end
  else
    STDERR.puts "Test directory not found"
    destroy(platform)
    exit!
  end
end

.verify(platform) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/admiral/docker.rb', line 13

def self.verify (platform)
  @@core_parameters.each do | parameter |
    if not platform.key?(parameter)
      STDERR.puts "Parameter #{parameter} not found"
      exit!
    end
  end
end