Class: DockerBuilder::Manager
- Inherits:
-
Object
- Object
- DockerBuilder::Manager
- Defined in:
- lib/docker_builder/manager.rb
Class Method Summary collapse
- ._install_container_provision_host(settings) ⇒ Object
- ._install_container_provision_host_chef_recipe(settings) ⇒ Object
-
._install_service_container(settings) ⇒ Object
systemd service.
-
._provision_container_chef_recipe(settings) ⇒ Object
provision.
- ._remove_service_container(settings) ⇒ Object
-
._run_container(settings) ⇒ Object
run.
-
._run_container_chef(settings) ⇒ Object
def self._run_container_docker(settings).
- .build_image(server_name, settings = nil) ⇒ Object
- .build_image_with_chef(settings) ⇒ Object
- .build_image_with_dockerfile(settings) ⇒ Object
- .chef_config_knife_path ⇒ Object
- .chef_recipe_path(p) ⇒ Object
- .chef_remove_data(settings) ⇒ Object
- .cmd(s) ⇒ Object
- .destroy_container(server_name, settings) ⇒ Object
- .destroy_container_chef(settings) ⇒ Object
- .destroy_image(server_name, settings = {}) ⇒ Object
- .destroy_image_chef(settings) ⇒ Object
-
.exec_task(server_name, recipe_name) ⇒ Object
run task on running container.
- .load_settings(server_name, opts = {}) ⇒ Object
-
.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
- .start_container(server_name) ⇒ Object
Class Method Details
._install_container_provision_host(settings) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/docker_builder/manager.rb', line 85 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
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/docker_builder/manager.rb', line 101 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
162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/docker_builder/manager.rb', line 162 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
provision
184 185 186 187 188 |
# File 'lib/docker_builder/manager.rb', line 184 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
177 178 179 |
# File 'lib/docker_builder/manager.rb', line 177 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_container(settings) ⇒ Object
run
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/docker_builder/manager.rb', line 126 def self._run_container(settings) puts "run container ..." script_type = (settings['install']['node']['script_type'] rescue nil) if script_type && script_type=='chef_recipe' # run container and provision with chef _run_container_chef(settings) # ??? #_provision_container_chef_recipe(settings) else #_run_container_docker(settings) # docker run cmd %Q(docker run -d --name #{settings.container_name} #{settings.docker_ports_string} #{settings.docker_volumes_string} #{settings.docker_volumes_from_string} #{settings.docker_links_string} #{settings.run_env_variables_string} #{settings.image_name} #{settings['docker']['command']} #{settings['docker']['run_options']}) end # fix hosts #puts "adding hosts..." #puts "a= #{settings.attributes}" container_hosts = settings.node['hosts'] || [] container_hosts.each do |r| cmd %Q(docker exec #{settings.container_name} bash -c 'echo "#{r[0]} #{r[1]}" >> /etc/hosts') end true end |
._run_container_chef(settings) ⇒ Object
def self._run_container_docker(settings)
end
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/docker_builder/manager.rb', line 197 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) #s_run = %Q(cd #{settings.name} && SERVER_NAME=#{settings.name} chef-client -z -j config.common.json -N #{settings.name} ../container.rb) #s_run = %Q(cd #{settings.name} && SERVER_NAME=#{settings.name} chef-client -z -N #{settings.name} ../container.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 |
.build_image(server_name, settings = nil) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/docker_builder/manager.rb', line 26 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) if settings['build']['build_type']=='dockerfile' return build_image_with_dockerfile(settings) elsif settings['build']['build_type']=='chef' return build_image_with_chef(settings) end end |
.build_image_with_chef(settings) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/docker_builder/manager.rb', line 49 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
40 41 42 43 44 45 46 47 |
# File 'lib/docker_builder/manager.rb', line 40 def self.build_image_with_dockerfile(settings) puts "build image with Dockerfile. Image: #{settings.image_name}" name = settings['name'] cmd %Q(cd #{name} && docker build -t #{settings.image_name} . ) end |
.chef_config_knife_path ⇒ Object
346 347 348 |
# File 'lib/docker_builder/manager.rb', line 346 def self.chef_config_knife_path "#{Config.dir_gem_root}/lib/docker_builder/chef/.chef/knife.rb" end |
.chef_recipe_path(p) ⇒ Object
350 351 352 |
# File 'lib/docker_builder/manager.rb', line 350 def self.chef_recipe_path(p) "#{Config.dir_gem_root}/lib/docker_builder/chef/#{p}" end |
.chef_remove_data(settings) ⇒ Object
355 356 357 358 359 360 361 362 |
# File 'lib/docker_builder/manager.rb', line 355 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 |
.cmd(s) ⇒ Object
323 324 325 326 327 328 329 330 331 332 |
# File 'lib/docker_builder/manager.rb', line 323 def self.cmd(s) puts "running: #{s}" res = nil Bundler.with_clean_env do res = `#{s}` end puts "#{res}" end |
.destroy_container(server_name, settings) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/docker_builder/manager.rb', line 257 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
278 279 280 281 282 283 284 285 286 |
# File 'lib/docker_builder/manager.rb', line 278 def self.destroy_container_chef(settings) # 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
216 217 218 219 220 221 222 223 224 |
# File 'lib/docker_builder/manager.rb', line 216 def self.destroy_image(server_name, settings={}) 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
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/docker_builder/manager.rb', line 227 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}) #cmd %Q(SERVER_NAME=#{settings.name} chef-client -z -N #{settings.name} chef_destroy_image.rb) #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_destroy_image.rb')} ) 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
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/docker_builder/manager.rb', line 299 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 |
.load_settings(server_name, opts = {}) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/docker_builder/manager.rb', line 5 def self.load_settings(server_name, opts={}) settings = Settings.load_settings_for_server(server_name, opts) settings.set 'name', server_name settings end |
.run_chef_recipe(settings, recipe_rb) ⇒ Object
helpers - chef
337 338 339 |
# File 'lib/docker_builder/manager.rb', line 337 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.image_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
341 342 343 |
# File 'lib/docker_builder/manager.rb', line 341 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.image_name} -c #{chef_config_knife_path} --override-runlist 'recipe[#{settings.name}::#{server_recipe}]' ) end |
.run_container(server_name, settings = {}) ⇒ Object
run
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/docker_builder/manager.rb', line 68 def self.run_container(server_name, settings={}) puts "creating and running container.." #settings = load_settings(server_name) # 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 |
.save_chef_config(settings) ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/docker_builder/manager.rb', line 13 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 |
.start_container(server_name) ⇒ Object
289 290 291 292 293 294 295 296 |
# File 'lib/docker_builder/manager.rb', line 289 def self.start_container(server_name) settings = load_settings(server_name) # cmd %Q(docker start #{settings.container_name} ) return true end |