Class: StackBuilder::Chef::ContainerNodeManager
- Inherits:
-
NodeManager
- Object
- Stack::NodeManager
- NodeManager
- StackBuilder::Chef::ContainerNodeManager
- Defined in:
- lib/stackbuilder/chef/stack_container_node.rb
Constant Summary
Constants inherited from NodeManager
NodeManager::SSH_CREDENTIAL_KEYS
Instance Attribute Summary
Attributes inherited from NodeManager
#name, #node_id, #run_list, #run_on_event
Instance Method Summary collapse
- #delete(index) ⇒ Object
-
#initialize(id, node_config, repo_path, environment) ⇒ ContainerNodeManager
constructor
A new instance of ContainerNodeManager.
- #process(index, events, attributes, target = nil) ⇒ Object
Methods inherited from NodeManager
#config_knife, #create, #create_vm, #delete_vm, #get_name, #get_scale, #get_ssh_credentials, #node_attributes, #process_vm
Methods inherited from Stack::NodeManager
#create, #get_name, #get_scale, #node_attributes, #set_scale
Constructor Details
#initialize(id, node_config, repo_path, environment) ⇒ ContainerNodeManager
Returns a new instance of ContainerNodeManager.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/stackbuilder/chef/stack_container_node.rb', line 9 def initialize(id, node_config, repo_path, environment) super(id, node_config, repo_path, environment) @env_file_path = repo_path + '/environments/' + environment + '.rb' @dockerfiles_build_dir = repo_path + '/.build/docker/build' FileUtils.mkdir_p(@dockerfiles_build_dir) docker_image_dir = repo_path + '/.build/docker/' + environment FileUtils.mkdir_p(docker_image_dir) docker_image_filename = docker_image_dir + '/' + @name @docker_image_path = docker_image_filename + '.gz' @docker_image_target = docker_image_filename + '.target' end |
Instance Method Details
#delete(index) ⇒ Object
174 175 176 177 |
# File 'lib/stackbuilder/chef/stack_container_node.rb', line 174 def delete(index) super(index) end |
#process(index, events, attributes, target = nil) ⇒ Object
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 108 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/stackbuilder/chef/stack_container_node.rb', line 26 def process(index, events, attributes, target = nil) return unless events.include?('create') || events.include?('install') || events.include?('configure') || events.include?('update') target_node_instance = "#{target.node_id}-#{index}" node = Chef::Node.load(target_node_instance) if @knife_config.has_key?('ip_attribute') v = lambda { |h,k| k.size>1 ? v.call(h[k.shift],k) : h[k.shift] } ipaddress = v.call(node.attributes, @knife_config['ip_attribute'].split('.')) else ipaddress = node.attributes['ipaddress'] end ssh_user, ssh_password, ssh_identity_file = target.get_ssh_credentials(target_node_instance) ssh = ssh_create(ipaddress, ssh_user, ssh_password || ssh_identity_file) image_exists = @name==ssh_exec!(ssh, "docker images | awk '$1==\"@name\" { print $1 }'")[:out].strip # Copy image file to target if it has changed or does not exist on target if build_container(attributes) && !target.nil? && ( !File.exist?(@docker_image_target) || (File.mtime(@docker_image_path) > File.mtime(@docker_image_target)) || !image_exists ) puts "Uploading docker image to target '#{target_node_instance}'." ssh.open_channel do |channel| channel.exec('gunzip | sudo docker load') do |ch, success| channel.on_data do |ch, data| res << data end channel.send_data IO.binread(@docker_image_path) channel.eof! end end ssh.loop FileUtils.touch(@docker_image_target) end # Start container instances if @knife_config.has_key?('container_start') result = ssh_exec!(ssh, "sudo docker ps -a | awk '/#{@node_id}/ { print $0 }'") raise StackBuilder::Common::StackBuilderError, "Error determining running containers for #{@name}: #{result[:err]}" \ if result[:exit_code]>0 running_instances = result[:out].lines if running_instances.size > @scale (running_instances.size - 1).downto(@scale) do |i| container_node_id = "#{@node_id}-#{i}" running_instance = running_instances.select{ |ri| ri[/#{@node_id}-\d+/,0]==container_node_id } container_id = running_instance.first[/^[0-9a-z]+/,0] result = ssh_exec!(ssh, "sudo docker rm -f #{container_id}") if result[:exit_code]==0 remove_container_node_from_chef(container_node_id) container_port_map = Hash.new container_port_map.merge!(node.normal['container_port_map']) \ unless node.normal['container_port_map'].nil? container_port_map.each do |k,v| container_port_map[k].delete(container_node_id) end node.normal['container_port_map'] = container_port_map node.save else @logger.error("Unable to stop container instance #{running_instances[i]}: #{result[:err]}") end end elsif running_instances.size < @scale container_start = @knife_config['container_start'] container_ports = container_start['ports'] = container_start['options'] start_cmd = "sudo docker run -d " start_cmd += + ' ' \ unless .nil? start_cmd += container_ports.collect \ { |k,p| "-p :#{p=~/\d+\:\d+/ ? p.to_s : ':' + p.to_s}" }.join(' ') \ unless container_ports.nil? running_instances.size.upto(@scale - 1) do |i| container_node_id = "#{@node_id}-#{i}" remove_container_node_from_chef(container_node_id) result = ssh_exec!( ssh, "#{start_cmd} --name #{container_node_id} " + "-h #{container_node_id} -e \"CHEF_NODE_NAME=#{container_node_id}\" #{@name}") if result[:exit_code]==0 container_id = container_node_id container_port_map = Hash.new container_port_map.merge!(node.normal['container_port_map']) \ unless node.normal['container_port_map'].nil? container_ports.each do |k,p| port_map = container_port_map[k] if port_map.nil? port_map = { } container_port_map[k] = port_map end if p=~/\d+\:\d+/ port_map[container_id] = p[/(\d+)\:\d+/, 1] else result = ssh_exec!(ssh, "sudo docker port #{container_node_id} #{p}") @logger.error( "Unable to get host port for " + "'#{@node_id}-#{i}:#{p}': #{result[:err]}") \ if result[:exit_code]>0 port_map[container_id] = result[:out][/:(\d+)$/, 1] end end node.normal['container_port_map'] = container_port_map node.save else @logger.error("Unable to start container instance '#{@node_id}-#{i}': #{result[:err]}") end end end end super(index, events, attributes, target) end |