Class: DockerBuilder::ManagerContainer
- Inherits:
-
Object
- Object
- DockerBuilder::ManagerContainer
- Defined in:
- lib/docker_builder/manager_container.rb
Class Method Summary collapse
-
._install_service_container(settings) ⇒ Object
systemd service.
- ._remove_service_container(settings) ⇒ Object
-
.assert(expression, string = "Assert failed") ⇒ Object
common helpers.
- .assert_container_running(container_name) ⇒ Object
- .chef_config_knife_path ⇒ Object
- .chef_recipe_path(p) ⇒ Object
- .chef_remove_data(settings) ⇒ Object
- .clear_cache(name, settings) ⇒ Object
- .cmd(s) ⇒ Object
- .create_container(settings) ⇒ Object
- .destroy_container(server_name, settings) ⇒ Object
- .destroy_container_chef(settings) ⇒ Object
-
.exec_task(server_name, recipe_name) ⇒ Object
run task on running container.
-
.run_chef_recipe(settings, recipe_rb) ⇒ Object
helpers - chef.
- .run_chef_recipe_server_recipe(settings, server_recipe) ⇒ Object
-
.run_container(server_name, settings = {}) ⇒ Object
run.
- .save_chef_config(settings) ⇒ Object
- .save_config_json(settings) ⇒ Object
-
.setup_container_after_start(settings) ⇒ Object
def self._prepare_provision_before_start_chef(settings, script) puts “_prepare_provision_before_start_chef”.
- .setup_network(settings) ⇒ Object
- .start_container(name, settings) ⇒ Object
-
.stop_container(server_name, settings) ⇒ Object
stop container.
- .wait_until_running(container_name) ⇒ Object
Class Method Details
._install_service_container(settings) ⇒ Object
systemd service
195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/docker_builder/manager_container.rb', line 195 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 |
._remove_service_container(settings) ⇒ Object
210 211 212 |
# File 'lib/docker_builder/manager_container.rb', line 210 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 |
.assert(expression, string = "Assert failed") ⇒ Object
common helpers
369 370 371 372 373 |
# File 'lib/docker_builder/manager_container.rb', line 369 def self.assert(expression, string = "Assert failed") unless expression throw Exception.new string end end |
.assert_container_running(container_name) ⇒ Object
146 147 148 149 |
# File 'lib/docker_builder/manager_container.rb', line 146 def self.assert_container_running(container_name) res = system("docker exec #{container_name} true") assert res, "Container #{container_name} is not running" end |
.chef_config_knife_path ⇒ Object
349 350 351 |
# File 'lib/docker_builder/manager_container.rb', line 349 def self.chef_config_knife_path "#{Config.dir_gem_root}/lib/docker_builder/chef/.chef/knife.rb" end |
.chef_recipe_path(p) ⇒ Object
353 354 355 |
# File 'lib/docker_builder/manager_container.rb', line 353 def self.chef_recipe_path(p) "#{Config.dir_gem_root}/lib/docker_builder/chef/#{p}" end |
.chef_remove_data(settings) ⇒ Object
358 359 360 361 362 363 364 365 |
# File 'lib/docker_builder/manager_container.rb', line 358 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
315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/docker_builder/manager_container.rb', line 315 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
332 333 334 |
# File 'lib/docker_builder/manager_container.rb', line 332 def self.cmd(s) Command.cmd(s) end |
.create_container(settings) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/docker_builder/manager_container.rb', line 58 def self.create_container(settings) # create = "" networks = settings['docker'].fetch('network', {}).fetch('networks', []) if networks && networks[0] network = networks[0] #puts "network=#{network}" << "--net #{network['net']} " << "--ip #{network['ip']} " if network['ip'] << "--mac-address #{network['mac_address']} " if network['mac_address'] end 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.} #{settings.run_env_variables_string} #{settings.image_name} #{settings['docker']['command']} #{settings['docker']['run_options']}) # network setup_network(settings) end |
.destroy_container(server_name, settings) ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/docker_builder/manager_container.rb', line 240 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
261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/docker_builder/manager_container.rb', line 261 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 |
.exec_task(server_name, recipe_name) ⇒ Object
run task on running container
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/docker_builder/manager_container.rb', line 291 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.('.', 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_chef_recipe(settings, recipe_rb) ⇒ Object
helpers - chef
340 341 342 |
# File 'lib/docker_builder/manager_container.rb', line 340 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
344 345 346 |
# File 'lib/docker_builder/manager_container.rb', line 344 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
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 |
# File 'lib/docker_builder/manager_container.rb', line 32 def self.run_container(server_name, settings={}) puts "creating and running container.." #settings = load_settings(server_name) # generate config save_config_json(settings) # destroy destroy_container(server_name, settings) # create create_container(settings) # START && run provision after start start_container(name, settings) # TODO: systemd service #res_service = _install_service_container(settings) true end |
.save_chef_config(settings) ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/docker_builder/manager_container.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 |
# File 'lib/docker_builder/manager_container.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 true end |
.setup_container_after_start(settings) ⇒ Object
def self._prepare_provision_before_start_chef(settings, script)
puts "_prepare_provision_before_start_chef"
require_relative '../../lib/docker_builder/provisioner/provisioner_chef'
provisioner = DockerBuilder::Provisioner::Chef.new(settings)
provisioner.copy_config_file
end
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_container.rb', line 164 def self.setup_container_after_start(settings) # default gateway 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 # 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
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/docker_builder/manager_container.rb', line 78 def self.setup_network(settings) container_name = settings.container_name # networks networks = settings['docker'].fetch('network', {}).fetch('networks', []) if networks ind = 0 networks.each do |net| ind = ind + 1 # next if net['action']=='remove' # skip first network next if ind==1 # connect 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
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/docker_builder/manager_container.rb', line 112 def self.start_container(name, settings) ### BEFORE START # run setup provision scripts DockerBuilder::Provisioner::Base.run_provision_scripts_setup(settings) ### start cmd %Q(docker start #{settings.container_name}) # wait wait_until_running(settings.container_name) ### AFTER START # setup setup_container_after_start(settings) # provision after start # run bootstrap provision scripts DockerBuilder::Provisioner::Base.run_provision_scripts_bootstrap(settings) end |
.stop_container(server_name, settings) ⇒ Object
stop container
279 280 281 282 283 284 285 286 287 |
# File 'lib/docker_builder/manager_container.rb', line 279 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
136 137 138 139 140 141 142 143 144 |
# File 'lib/docker_builder/manager_container.rb', line 136 def self.wait_until_running(container_name) retries = 10 until system("docker exec #{container_name} true") || retries < 0 sleep 1 retries = retries - 1 end assert_container_running(container_name) end |