Class: DockerBuilder::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/docker_builder/manager.rb

Class Method Summary collapse

Class Method Details

._bootstrap_container_prepare(settings) ⇒ Object



390
391
392
393
394
395
396
# File 'lib/docker_builder/manager.rb', line 390

def self._bootstrap_container_prepare(settings)
  require_relative '../../lib/docker_builder/provisioner/provisioner_chef'

  provisioner = DockerBuilder::Provisioner::ProvisionerChef.new(settings)
  provisioner.copy_config_file

end

._install_container_provision_host(settings) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/docker_builder/manager.rb', line 108

def self._install_container_provision_host(settings)
  script_type = (settings['install']['host']['script_type'] rescue nil)
  return true unless script_type

  # run provision script on the host machine
  if script_type=='chef_recipe'
    return _install_container_provision_host_chef_recipe(settings)
  else
    # do nothing
  end

  true
end

._install_container_provision_host_chef_recipe(settings) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/docker_builder/manager.rb', line 124

def self._install_container_provision_host_chef_recipe(settings)
  # run script on host machine
  script_name = settings['install']['host']['script'] || 'install_host'

  # check script exists
  #script_path = "#{settings.name}/cookbooks/#{settings.name}/recipes/#{script_name}.rb"
  #f = File.expand_path('.', script_path)

  #if !File.exists?(f)
  #  puts "script not found: #{f}. Skipping"
  #  return false
  #end

  #puts "pwd= #{Dir.pwd}"
  #puts "root = #{Config.root_path}"
  #exit

  #
  res_chef = run_chef_recipe_server_recipe(settings, script_name)
  #cmd %Q(SERVER_NAME=#{settings.name} chef-client -z -N #{settings.name} --override-runlist 'recipe[#{settings.name}::#{script_name}]' )

  return true
end

._install_service_container(settings) ⇒ Object

systemd service



355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/docker_builder/manager.rb', line 355

def self._install_service_container(settings)
  # not work
  #cmd %Q(SERVER_NAME=#{settings.name} chef-client -z -N #{settings.name} install_container_service.rb )

  # work
  #cmd %Q(SERVER_NAME=#{settings.name} chef-client -z -N #{settings.name} -j config_run_install_container_service.json )

  # work
  #cmd %Q(SERVER_NAME=#{settings.name} chef-client -z -N #{settings.name} --override-runlist 'recipe[server-api::install_container_service]' )

  #
  cmd %Q(SERVER_NAME=#{settings.name} chef-client -z -N #{settings.name} -j config/config-#{settings.name}.json --override-runlist 'recipe[server-api::install_container_service]' )
end

._provision_container_chef_recipe(settings) ⇒ Object



398
399
400
401
402
# File 'lib/docker_builder/manager.rb', line 398

def self._provision_container_chef_recipe(settings)
  puts "provisioning container #{settings.container_name}"

  #cmd %Q(SERVER_NAME=#{settings.name} chef-client -z -N #{settings.name} #{settings.name}/cookbooks/#{settings.name}/ )
end

._remove_service_container(settings) ⇒ Object



370
371
372
# File 'lib/docker_builder/manager.rb', line 370

def self._remove_service_container(settings)
  cmd %Q(SERVER_NAME=#{settings.name} chef-client -z -N #{settings.name} -j config/config-#{settings.name}.json --override-runlist 'recipe[server-api::remove_container_service]' )
end

._run_bootstrap_script(settings, script) ⇒ Object



330
331
332
333
334
335
336
# File 'lib/docker_builder/manager.rb', line 330

def self._run_bootstrap_script(settings, script)
  if script['type']=='shell' && script['run_from']=='host'
    return _run_bootstrap_script_shell_from_host(settings, script)
  end

  return nil
end

._run_bootstrap_script_shell_from_host(settings, script) ⇒ Object



339
340
341
342
# File 'lib/docker_builder/manager.rb', line 339

def self._run_bootstrap_script_shell_from_host(settings, script)
  cmd %Q(cd #{settings.dir_server_root} && #{script['script']} )

end

._run_container(settings) ⇒ Object

run



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/docker_builder/manager.rb', line 149

def self._run_container(settings)
  puts "run container ..."

  # generate config
  save_config_json(settings)

  # create
  create_container(settings)

  setup_network(settings)


  ### BEFORE START

  # run setup provision scripts
  setup_scripts = (settings['provision']['setup'] rescue [])
  if setup_scripts
    setup_scripts.each do |script|
      _run_setup_script(settings, script)
    end
  end


  # ??? TODO: remove it ?
  # run some provision scripts - for chef
  bootstrap_type = (settings['install']['bootstrap']['type'] rescue nil)

  # before start
  if bootstrap_type && bootstrap_type=='chef'
    _bootstrap_container_prepare(settings)
  end


  ### START && run provision after start
  start_container(name, settings)

  true
end

._run_container_chef(settings) ⇒ Object

def self._run_container_docker(settings)

end


411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/docker_builder/manager.rb', line 411

def self._run_container_chef(settings)
  # generate json config for chef
  save_chef_config(settings)

  # run chef
  #s_run = %Q(cd #{settings.name} && chef-client -z -j config.json -c ../.chef/knife.rb -N #{settings.name} ../lib/chef_container_run.rb)

  # good - 2016-nov-19
  #cmd %Q(SERVER_NAME=#{settings.name} chef-client -z -N #{settings.name} chef_run_container.rb)

  #
  res_chef = run_chef_recipe(settings, 'chef_run_container.rb')

  res_chef
end

._run_setup_script(settings, script) ⇒ Object

provision



377
378
379
380
381
382
383
# File 'lib/docker_builder/manager.rb', line 377

def self._run_setup_script(settings, script)
  if script['type']=='shell'
    return _run_setup_script_shell_from_host(settings, script)
  end

  return nil
end

._run_setup_script_shell_from_host(settings, script) ⇒ Object



385
386
387
# File 'lib/docker_builder/manager.rb', line 385

def self._run_setup_script_shell_from_host(settings, script)
  cmd %Q(cd #{settings.dir_server_root} && #{script['script']} )
end

.assert(expression, string = "Assert failed") ⇒ Object

common helpers



609
610
611
612
613
# File 'lib/docker_builder/manager.rb', line 609

def self.assert(expression, string = "Assert failed")
  unless expression
    throw Exception.new string
  end
end

.assert_container_running(container_name) ⇒ Object



244
245
246
247
# File 'lib/docker_builder/manager.rb', line 244

def self.assert_container_running(container_name)
  res = system("docker exec #{container_name} true")
  assert res, "Container #{container_name} is not running"
end

.build_image(server_name, settings = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/docker_builder/manager.rb', line 30

def self.build_image(server_name, settings=nil)
  puts "building image for #{server_name}..."
  #puts "settings: #{settings}"
  #puts "debug: #{settings['attributes']}"

  #settings = load_settings(server_name)

  t = settings['build']['build_type']
  if t=='' || t=='none'
    #
    puts "no build needed..."

  elsif t.downcase=='dockerfile'
    return build_image_with_dockerfile(settings)
  elsif t=='chef'
    return build_image_with_chef(settings)
  elsif t=='packer'
    return build_image_with_packer(settings)
  end
end

.build_image_with_chef(settings) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/docker_builder/manager.rb', line 59

def self.build_image_with_chef(settings)
  puts "build image with chef"

  # config json
  save_chef_config(settings)

  # check node
  cmd %Q(cd #{Config.root_path} && chef exec knife node show #{settings.chef_node_name} -c #{chef_config_knife_path})


  #cmd %Q(SERVER_NAME=#{settings.name} SERVER_PATH=#{settings.dir_server_root} chef exec chef-client -z -N #{settings.image_name} -j #{settings.filename_config_json} -c #{chef_config_knife_path} #{chef_recipe_path('chef_build_image.rb')} )
  res_recipe = run_chef_recipe(settings, 'chef_build_image.rb')
end

.build_image_with_dockerfile(settings) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/docker_builder/manager.rb', line 51

def self.build_image_with_dockerfile(settings)
  puts "build image with Dockerfile"

  #cmd %Q(cd #{name} && docker build -t #{settings.image_name} . )
  cmd %Q(docker build -t #{settings.image_name} #{settings.dir_server_root} )

end

.build_image_with_packer(settings) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/docker_builder/manager.rb', line 74

def self.build_image_with_packer(settings)
  require_relative '../../lib/docker_builder/builder/packer'

  puts "build image with packer"

  builder = DockerBuilder::Builder::Packer.new(settings)
  builder.build
end

.chef_config_knife_pathObject



589
590
591
# File 'lib/docker_builder/manager.rb', line 589

def self.chef_config_knife_path
  "#{Config.dir_gem_root}/lib/docker_builder/chef/.chef/knife.rb"
end

.chef_recipe_path(p) ⇒ Object



593
594
595
# File 'lib/docker_builder/manager.rb', line 593

def self.chef_recipe_path(p)
  "#{Config.dir_gem_root}/lib/docker_builder/chef/#{p}"
end

.chef_remove_data(settings) ⇒ Object



598
599
600
601
602
603
604
605
# File 'lib/docker_builder/manager.rb', line 598

def self.chef_remove_data(settings)
  #
  cmd %Q(cd #{Config.root_path} && chef exec knife node delete #{settings.chef_node_name}  -y -c #{chef_config_knife_path})

  # clean chef client, node
  cmd %Q(cd #{Config.root_path} && rm -f #{settings.filename_chef_node_json} )
  cmd %Q(cd #{Config.root_path} && rm -f #{settings.filename_chef_client_json} )
end

.clear_cache(name, settings) ⇒ Object



542
543
544
545
546
547
548
549
550
551
552
553
554
555
# File 'lib/docker_builder/manager.rb', line 542

def self.clear_cache(name, settings)
  # common cache
  cmd("rm -rf ~/.chef/cache")
  cmd("rm -rf ~/.chef/local-mode-cache")

  # cache for server
  cmd("rm -rf #{settings.dir_server_root}/.chef/local-mode-cache")
  #cmd("rm -rf ~/.chef/package-cache")

  # cache in gem
  cmd("rm -rf #{Config.dir_gem_root}/lib/docker_builder/.chef/local-mode-cache")


end

.cmd(s) ⇒ Object



559
560
561
# File 'lib/docker_builder/manager.rb', line 559

def self.cmd(s)
  Command.cmd(s)
end

.create_container(settings) ⇒ Object



188
189
190
191
# File 'lib/docker_builder/manager.rb', line 188

def self.create_container(settings)
  # create
  cmd %Q(docker create --name #{settings.container_name} #{settings.docker_ports_string} #{settings.docker_volumes_string} #{settings.docker_volumes_from_string} #{settings.docker_links_string}  #{settings.run_extra_options_string} #{settings.run_env_variables_string} #{settings.image_name} #{settings['docker']['command']} #{settings['docker']['run_options']})
end

.destroy_container(server_name, settings) ⇒ Object



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# File 'lib/docker_builder/manager.rb', line 468

def self.destroy_container(server_name, settings)
  puts "destroying container #{server_name}..."

  # TODO: stop, remove systemd service
  #res_service = _remove_service_container(settings)

  #
  cmd %Q(docker rm -f #{settings.container_name} )



  # if chef
  if settings['build']['build_type']=='chef'
    return destroy_container_chef(settings)
  end

  #
  return true
end

.destroy_container_chef(settings) ⇒ Object



489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/docker_builder/manager.rb', line 489

def self.destroy_container_chef(settings)
  # destroy temp container
  cmd %Q(docker rm -f chef-converge.#{settings.image_name} )

  #
  res_chef = run_chef_recipe(settings, 'chef_destroy_container.rb')
  #cmd %Q(SERVER_NAME=#{settings.name} chef-client -z -N #{settings.name} chef_destroy_container.rb)

  #
  chef_remove_data(settings)

end

.destroy_image(server_name, settings = {}) ⇒ Object



428
429
430
431
432
433
434
435
436
437
438
# File 'lib/docker_builder/manager.rb', line 428

def self.destroy_image(server_name, settings={})
  puts "destroying image for server #{server_name}"

  cmd %Q(docker rmi #{settings.image_name} )
  cmd %Q(docker rm -f chef.converge.#{settings.image_name} )

  # delete chef data
  if settings['build']['build_type']=='chef'
    return destroy_image_chef(settings)
  end
end

.destroy_image_chef(settings) ⇒ Object



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/docker_builder/manager.rb', line 441

def self.destroy_image_chef(settings)
  puts "destroying image with chef..."

  # config json
  save_chef_config(settings)

  # destroy temp container
  cmd %Q(docker rm -f chef-converge.#{settings.image_name} )

  #
  cmd %Q(cd #{Config.root_path} && chef exec knife node delete #{settings.chef_node_name}  -y -c #{chef_config_knife_path})

  res_recipe = run_chef_recipe(settings, 'chef_destroy_image.rb')

  chef_remove_data(settings)

  # work - before 2016-nov-19
  #cmd %Q(cd #{Config.root_path} && chef exec knife node delete #{settings.chef_node_name}  -y -c #{chef_config_knife_path})

  # clean chef client, node
  #cmd %Q(cd #{Config.root_path} && rm -f #{settings.filename_chef_node_json} )
  #cmd %Q(cd #{Config.root_path} && rm -f #{settings.filename_chef_client_json} )
end

.exec_task(server_name, recipe_name) ⇒ Object

run task on running container



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'lib/docker_builder/manager.rb', line 519

def self.exec_task(server_name, recipe_name)
  #raise 'not implemented'

  settings = load_settings(server_name)

  # check script exists
  script_path = "#{settings.name}/cookbooks/#{settings.name}/recipes/#{recipe_name}.rb"
  f = File.expand_path('.', script_path)

  if !File.exists?(f)
    puts "script not found: #{f}. Skipping"
    return false
  end

  #
  cmd %Q(SERVER_NAME=#{settings.name} chef-client -z --override-runlist 'recipe[server-api::exec_container]' )
  #cmd %Q(SERVER_NAME=#{settings.name} chef-client -z -N #{settings.name} --override-runlist 'recipe[#{settings.name}::#{recipe_name}]' )
  #cmd %Q(SERVER_NAME=#{settings.name} chef-client -z -N #{settings.name} chef_exec_container.rb )

  return true
end

.run_bootstrap_shell_script_in_container(settings, script_path) ⇒ Object



345
346
347
348
# File 'lib/docker_builder/manager.rb', line 345

def self.run_bootstrap_shell_script_in_container(settings, script_path)
  # exec
  cmd %Q(docker exec #{settings.container_name} #{script_path} )
end

.run_chef_recipe(settings, recipe_rb) ⇒ Object

helpers - chef



580
581
582
# File 'lib/docker_builder/manager.rb', line 580

def self.run_chef_recipe(settings, recipe_rb)
  cmd %Q(cd #{Config.root_path} && SERVER_NAME=#{settings.name} SERVER_PATH=#{settings.dir_server_root} chef exec chef-client -z -N #{settings.container_name} -j #{settings.filename_config_json} -c #{chef_config_knife_path} #{chef_recipe_path(recipe_rb)} )
end

.run_chef_recipe_server_recipe(settings, server_recipe) ⇒ Object



584
585
586
# File 'lib/docker_builder/manager.rb', line 584

def self.run_chef_recipe_server_recipe(settings, server_recipe)
  cmd %Q(cd #{Config.root_path} && SERVER_NAME=#{settings.name} SERVER_PATH=#{settings.dir_server_root} chef exec chef-client -z -N #{settings.container_name} -c #{chef_config_knife_path} --override-runlist 'recipe[#{settings.name}::#{server_recipe}]' )
end

.run_container(server_name, settings = {}) ⇒ Object

run



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/docker_builder/manager.rb', line 87

def self.run_container(server_name, settings={})
  puts "creating and running container.."
  #settings = load_settings(server_name)

  # destroy
  destroy_container(server_name, settings)


  # provision host before running container
  res_install = _install_container_provision_host(settings)

  # run container
  res_run = _run_container(settings)


  # TODO: systemd service
  #res_service = _install_service_container(settings)


end

.run_provision_after_start(settings) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/docker_builder/manager.rb', line 276

def self.run_provision_after_start(settings)
  # run bootstrap provision scripts
  bootstrap_scripts = (settings['provision']['bootstrap'] rescue [])
  if bootstrap_scripts
    bootstrap_scripts.each do |script|
      _run_bootstrap_script(settings, script)
    end
  end


=begin
# commented - 2017-02-22

  #
  install_node_script_type = (settings['install']['node']['script_type'] rescue nil)
  install_bootstrap_script = (settings['install']['bootstrap']['script'] rescue nil)

  if install_node_script_type && install_node_script_type=='chef_recipe'
    # run container and provision with chef
    #_run_container_chef(settings)

    # ???
    #_provision_container_chef_recipe(settings)

  elsif install_node_script_type && install_node_script_type=='shell'
    # docker run
    #create_and_run_container(settings)

    # provision with shell script
    run_shell_script_in_container(settings, "install.sh")

  else
    # no script for provision
    #_run_container_docker(settings)

    # docker run
    #create_and_run_container(settings)

  end

  # bootstrap
  if install_bootstrap_script
    #script = settings['install']['bootstrap']['script'] || '/opt/bootstrap/bootstrap.sh'

    # bootstsrap with shell script
    run_bootstrap_shell_script_in_container(settings, install_bootstrap_script)
  end
=end



  true
end

.run_shell_script_in_container(settings, script_name) ⇒ Object

helpers - shell



566
567
568
569
570
571
572
573
574
575
# File 'lib/docker_builder/manager.rb', line 566

def self.run_shell_script_in_container(settings, script_name)
  script_path = settings.make_path_full("scripts/#{script_name}")

  # copy
  cmd %Q(cd #{Config.root_path} && docker cp #{script_path} #{settings.container_name}:/tmp/#{script_name} )

  # exec
  cmd %Q(docker exec #{settings.container_name} chmod +x /tmp/#{script_name} )
  cmd %Q(docker exec #{settings.container_name} /tmp/#{script_name} )
end

.save_chef_config(settings) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/docker_builder/manager.rb', line 4

def self.save_chef_config(settings)
  require 'json'
  filename = settings.filename_chef_config
  FileUtils.mkdir_p(File.dirname(filename))
  File.open(filename,"w+") do |f|
    f.write(settings.all_attributes.to_json)
  end

  true
end

.save_config_json(settings) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/docker_builder/manager.rb', line 16

def self.save_config_json(settings)
  require 'json'
  filename = settings.filename_config_json
  FileUtils.mkdir_p(File.dirname(filename))
  File.open(filename,"w+") do |f|
    f.write(settings.all_attributes.to_json)
  end

  #puts "f==#{filename}"

  true
end

.setup_container_after_start(settings) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/docker_builder/manager.rb', line 249

def self.setup_container_after_start(settings)

  # default gateway
=begin
  network = settings['docker']['network']
  if network
    gateway = network['default_gateway']

    if gateway
      # fix default gateway
      #cmd %Q(docker exec #{settings.container_name} ip route change default via #{gateway} dev eth1)
      cmd %Q(docker exec #{settings.container_name} ip route change default via #{gateway})
    end
  end
=end


  # fix hosts
  container_hosts = settings['docker']['hosts'] || []
  container_hosts.each do |r|
    #cmd %Q(docker exec #{settings.container_name} bash -c 'echo "#{r[0]} #{r[1]}" >>  /etc/hosts')
    cmd %Q(docker exec #{settings.container_name} sh -c 'echo "#{r[0]} #{r[1]}" >>  /etc/hosts')
  end
end

.setup_network(settings) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/docker_builder/manager.rb', line 194

def self.setup_network(settings)
  container_name = settings.container_name

  # networks
  networks = settings['docker'].fetch('network', {}).fetch('networks', [])
  if networks
    networks.each do |net|
      next if net['action']=='remove'

      ip = net['ip']
      s_ip = "--ip #{ip}" if ip
      #puts %Q(docker network connect #{s_ip}  #{net['net']} #{settings.container_name})
      cmd %Q(docker network connect #{s_ip}  #{net['net']} #{settings.container_name})
    end

    # remove
    networks.each do |net|
      next unless net['action']=='remove'
      cmd %Q(docker network disconnect #{net['net']} #{settings.container_name})
    end
  end
end

.start_container(name, settings) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/docker_builder/manager.rb', line 219

def self.start_container(name, settings)
  # start
  cmd %Q(docker start #{settings.container_name})

  # wait
  wait_until_running(settings.container_name)

  # setup
  setup_container_after_start(settings)

  # provision after start
  run_provision_after_start(settings)
end

.stop_container(server_name, settings) ⇒ Object

stop container



507
508
509
510
511
512
513
514
515
# File 'lib/docker_builder/manager.rb', line 507

def self.stop_container(server_name, settings)
  puts "stopping container #{server_name}..."

  #
  cmd %Q(docker stop #{settings.container_name} )

  #
  return true
end

.wait_until_running(container_name) ⇒ Object



234
235
236
237
238
239
240
241
242
# File 'lib/docker_builder/manager.rb', line 234

def self.wait_until_running(container_name)
  retries = 20
  until system("docker exec #{container_name} true") || retries < 0
    sleep 0.5
    retries == retries - 1
  end

  assert_container_running(container_name)
end