Class: Toaster::LXC
- Inherits:
-
Object
- Object
- Toaster::LXC
- Defined in:
- lib/toaster/util/lxc.rb
Constant Summary collapse
- LXC_ROOT_DIR =
"/lxc/"
Class Attribute Summary collapse
-
.initialized ⇒ Object
Returns the value of attribute initialized.
-
.last_init_time ⇒ Object
Returns the value of attribute last_init_time.
-
.lxc_init_delay ⇒ Object
Returns the value of attribute lxc_init_delay.
-
.semaphore ⇒ Object
Returns the value of attribute semaphore.
Class Method Summary collapse
- .clean ⇒ Object
- .copy_on_write? ⇒ Boolean
- .destroy_container(lxc) ⇒ Object
- .exec(lxc, cmd, print_output = false, append_time_to_output = false) ⇒ Object
- .get_container_names ⇒ Object
- .get_containers_for_host(host_machine_ip, include_prototypes = false) ⇒ Object
- .get_containers_for_hosts(host_machine_ips = [], include_prototypes = false) ⇒ Object
- .get_next_container_name ⇒ Object
- .get_next_container_num ⇒ Object
- .get_prototype_name(name) ⇒ Object
- .get_prototypes_for_host(host_machine_ip) ⇒ Object
- .get_prototypes_local ⇒ Object
- .is_prototype?(name) ⇒ Boolean
- .new_container(prototype_name = "default", num_retries = 1) ⇒ Object
- .new_prototype(host_machine_ip, name, os_distribution) ⇒ Object
- .prototype_container(prototype_name) ⇒ Object
- .run_chef(lxc, chef_node, node_attributes) ⇒ Object
- .run_chef_node(lxc, chef_node, run_list_hash, chef_node_attrs = {}) ⇒ Object
Class Attribute Details
.initialized ⇒ Object
Returns the value of attribute initialized.
25 26 27 |
# File 'lib/toaster/util/lxc.rb', line 25 def initialized @initialized end |
.last_init_time ⇒ Object
Returns the value of attribute last_init_time.
25 26 27 |
# File 'lib/toaster/util/lxc.rb', line 25 def last_init_time @last_init_time end |
.lxc_init_delay ⇒ Object
Returns the value of attribute lxc_init_delay.
25 26 27 |
# File 'lib/toaster/util/lxc.rb', line 25 def lxc_init_delay @lxc_init_delay end |
.semaphore ⇒ Object
Returns the value of attribute semaphore.
25 26 27 |
# File 'lib/toaster/util/lxc.rb', line 25 def semaphore @semaphore end |
Class Method Details
.clean ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/toaster/util/lxc.rb', line 181 def self.clean() if ARGV.include?("-v") puts "DEBUG: existing containers: #{get_container_names()}" end get_container_names().each do |lxc_name| puts "DEBUG: destroying LXC container #{lxc_name}" destroy_container(lxc_name) end Dir["#{LXC_ROOT_DIR}/lxc*"].each do |lxc_path| if lxc_path.match(/\.lock$/) # delete *.lock file `rm -f #{lxc_path}` else # delete LXC container directory if copy_on_write?() `/sbin/btrfs subvolume delete #{lxc_path}` end `rm -rf #{lxc_path}` end end end |
.copy_on_write? ⇒ Boolean
104 105 106 |
# File 'lib/toaster/util/lxc.rb', line 104 def self.copy_on_write?() return File.exist?("/mnt/btrfs/") end |
.destroy_container(lxc) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/toaster/util/lxc.rb', line 203 def self.destroy_container(lxc) lxc_name = lxc.kind_of?(Hash) ? lxc["lxc_id"] : lxc.to_s if !lxc_name.match(/[a-zA-Z0-9]+/).to_s == lxc_name puts "WARN: Unexpected LXC container name '#{lxc_name}'. Canceling destroy operation." return end `lxc-stop -n #{lxc_name} 2> /dev/null` `lxc-destroy -n #{lxc_name} 2> /dev/null` # if we are using docker, kill the container using docker... Toaster::Docker.kill_container(lxc_name) # check if copy-on-write (btrfs) is enabled if copy_on_write?() puts "DEBUG: removing previously created copy-on-write directory using btrfs: #{LXC_ROOT_DIR}/#{lxc_name}" `/sbin/btrfs subvolume delete #{LXC_ROOT_DIR}/#{lxc_name}` else puts "DEBUG: removing LXC directory: #{LXC_ROOT_DIR}/#{lxc_name}" `rm -r #{LXC_ROOT_DIR}/#{lxc_name}` end # remove lock file `rm -f #{LXC_ROOT_DIR}/#{lxc_name}.lock` end |
.exec(lxc, cmd, print_output = false, append_time_to_output = false) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/toaster/util/lxc.rb', line 115 def self.exec(lxc, cmd, print_output=false, append_time_to_output=false) ip = lxc["ip"] cmd = "ssh \"#{ip}\" #{cmd}" output = "" if append_time_to_output output = Util.(cmd) else output = `#{cmd} 2>&1` end if print_output puts "INFO: output of command '#{cmd}' (return code #{$?}): #{output}" end return output end |
.get_container_names ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/toaster/util/lxc.rb', line 238 def self.get_container_names() result = [] containers = [] containers.concat(Toaster::Docker.get_container_names()) containers.each do |c| if !is_prototype?(c) result << c end end return result end |
.get_containers_for_host(host_machine_ip, include_prototypes = false) ⇒ 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/toaster/util/lxc.rb', line 282 def self.get_containers_for_host(host_machine_ip, include_prototypes=false) out = "" begin # try connection using test service puts "DEBUG: Trying to connect to test service at #{host_machine_ip}" require "toaster/api" client = ToasterAppClient.new(host_machine_ip) out = client.lxc() rescue => ex # try connection using ssh puts "Unable to obtain LXC information from remote host using test service: #{ex}" cmd = 'sh -c \'cat #{LXC_ROOT_DIR}/*/config\'' cmd = "ssh -o BatchMode=yes #{host_machine_ip} \" lxc-ls -l; echo \\\"__--__ \\\"; #{cmd} \"" out = `#{cmd}` if !out || out.strip == "" || out.match(/Permission denied/) || out.match(/No route/) || out.match(/connect/) raise "Connection to host #{host_machine_ip} failed: #{out}" end end out = out.strip return get_containers_from_output(out, include_prototypes) end |
.get_containers_for_hosts(host_machine_ips = [], include_prototypes = false) ⇒ Object
250 251 252 253 254 255 256 257 258 259 |
# File 'lib/toaster/util/lxc.rb', line 250 def self.get_containers_for_hosts(host_machine_ips = [], include_prototypes=false) host_machine_ips = host_machine_ips.split(/, ;:/) if !host_machine_ips.kind_of?(Array) result = {} host_machine_ips.each do |ip| if ip.strip != "" result[ip] = get_containers_for_host(ip, include_prototypes) end end return result end |
.get_next_container_name ⇒ Object
226 227 228 |
# File 'lib/toaster/util/lxc.rb', line 226 def self.get_next_container_name() return "lxc#{get_next_container_num()}" end |
.get_next_container_num ⇒ Object
229 230 231 232 233 234 235 236 237 |
# File 'lib/toaster/util/lxc.rb', line 229 def self.get_next_container_num() #next_num = get_max_container_num() + 1 next_num = 1 while File.exist?("#{LXC_ROOT_DIR}/lxc#{next_num}") || File.exist?("#{LXC_ROOT_DIR}/lxc#{next_num}.lock") next_num += 1 end return next_num end |
.get_prototype_name(name) ⇒ Object
311 312 313 314 315 |
# File 'lib/toaster/util/lxc.rb', line 311 def self.get_prototype_name(name) name = "prototype_default" if name == "prototype" return nil if !name.match(/^prototype_.*/) return name.gsub(/^prototype_([a-zA-Z0-9_]+).*/, '\1') end |
.get_prototypes_for_host(host_machine_ip) ⇒ Object
261 262 263 264 265 266 267 268 269 |
# File 'lib/toaster/util/lxc.rb', line 261 def self.get_prototypes_for_host(host_machine_ip) lxcs = get_containers_for_host(host_machine_ip, true) lxcs.keys.dup.each do |name| if !get_prototype_name(name) lxcs.delete(name) end end return lxcs end |
.get_prototypes_local ⇒ Object
270 271 272 273 |
# File 'lib/toaster/util/lxc.rb', line 270 def self.get_prototypes_local() service_port = Config.get("service.port") return get_prototypes_for_host("localhost:#{service_port}") end |
.is_prototype?(name) ⇒ Boolean
307 308 309 |
# File 'lib/toaster/util/lxc.rb', line 307 def self.is_prototype?(name) return !get_prototype_name(name).nil? end |
.new_container(prototype_name = "default", num_retries = 1) ⇒ Object
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 |
# File 'lib/toaster/util/lxc.rb', line 28 def self.new_container(prototype_name = "default", num_retries = 1) file = nil name = nil num = nil # check if the prototype with the given name exists existing_protos = get_prototypes_local().keys if !existing_protos.include?(prototype_name) && !existing_protos.include?("prototype_#{prototype_name}") puts "WARN: Prototype with name #{prototype_name} does not exist. Existing: #{existing_protos}" new_proto = existing_protos[0] if new_proto new_proto = get_prototype_name(new_proto) puts "INFO: Using randomly chosen prototype '#{new_proto}' instead of '#{prototype_name}'." prototype_name = new_proto end end #puts "DEBUG: Thread #{Thread.current.object_id} synchronizing on semaphore #{semaphore}" semaphore.synchronize do name = get_next_container_name() num = get_next_container_num() #puts "DEBUG: Next LXC container number: #{num}. (locked on semaphore #{semaphore})" # # make sure the LXC directory and lock file is created, otherwise # a parallel thread might take the same container ID. # lxc_dir = "#{LXC_ROOT_DIR}/#{name}" lxc_lockfile = "#{LXC_ROOT_DIR}/#{name}.lock" puts "INFO: Creating LXC directory #{lxc_dir}" `mkdir -p #{lxc_dir}` `touch #{lxc_lockfile}` #puts "DEBUG: LXC directory #{lxc_dir} exists: #{File.exist?(lxc_dir)}." # sleep a while, if necessary... now = TimeStamp.now.to_i if (now - last_init_time) < lxc_init_delay time_passed = now - last_init_time sleep(lxc_init_delay - time_passed) end last_init_time = TimeStamp.now.to_i end #puts "DEBUG: Thread #{Thread.current.object_id} releasing semaphore #{semaphore}" puts "INFO: Creating new LXC container '#{name}' from prototype '#{prototype_name}'." key = "initLXC " + Util.generate_short_uid() TimeStamp.add(nil, key) output = `#{Util.toaster_executable} spawn "#{name}" "#{prototype_name}" 2>&1` TimeStamp.add(nil, key) ip = output.gsub("\n", " ").gsub(/.*root@(([0-9]{1,3}\.){3}[0-9]{1,3}).*/, '\1') if Util.is_ip_address(ip) puts "INFO: IP address of new LXC container '#{name}' is '#{ip}'." else puts "WARN: Cleaning up container, because output after creation was: #{output}" destroy_container({ "num" => num, "rootdir" => "#{LXC_ROOT_DIR}/#{name}/rootfs/", "lxc_id" => name }) if num_retries > 0 puts "INFO: re-trying to create new LXC container after failed attempt." return new_container(prototype_name, num_retries - 1) end end return { "num" => num, "rootdir" => "#{LXC_ROOT_DIR}/#{name}/rootfs/", "ip" => ip, "lxc_id" => name, "creation_stdout" => output } end |
.new_prototype(host_machine_ip, name, os_distribution) ⇒ Object
275 276 277 278 279 280 |
# File 'lib/toaster/util/lxc.rb', line 275 def self.new_prototype(host_machine_ip, name, os_distribution) require "toaster/api" client = ToasterAppClient.new(host_machine_ip) out = client.proto(name, os_distribution) return out end |
.prototype_container(prototype_name) ⇒ Object
108 109 110 111 112 113 |
# File 'lib/toaster/util/lxc.rb', line 108 def self.prototype_container(prototype_name) return { "rootdir" => "#{LXC_ROOT_DIR}/prototype_#{prototype_name}/rootfs/", "lxc_id" => "prototype_#{prototype_name}" } end |
.run_chef(lxc, chef_node, node_attributes) ⇒ Object
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 173 174 175 176 177 178 179 |
# File 'lib/toaster/util/lxc.rb', line 147 def self.run_chef(lxc, chef_node, node_attributes) # prepare variables output = "" lxc_dir = lxc["rootdir"] node_file_local = Util.mktmpfile("/tmp/chef_node_") node_file = "#{lxc_dir}/#{node_file_local}" # write config files Util.write(node_file, node_attributes.to_json(), true) #puts "DEBUG: node file #{node_file} : #{File.read(node_file)}" # IMPORTANT: redirect streams from/to a stream (" < /dev/null >& /some/file"), # because otherwise we might end up in a situation where the ssh command # hangs and never returns, as described here: # http://www.snailbook.com/faq/background-jobs.auto.html # UPDATE: for docker.io containers under Ubuntu 13.04 this seems to # be unnecessary. In fact, the workaround with piping does not work. out_file = "/tmp/toaster_out_#{Util.generate_short_uid()}" last_out_file = "/tmp/toaster_out_latest" cmd = "\"" + "toaster chefsolo #{node_file_local} " + # "grep -v 'FATAL: No cookbook found in' " + ">& #{last_out_file}; " + "cat #{last_out_file}; " + "\"" puts "INFO: Executing command in LXC #{lxc["ip"]}: #{cmd}" output = exec(lxc, cmd, false, true) return output end |
.run_chef_node(lxc, chef_node, run_list_hash, chef_node_attrs = {}) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/toaster/util/lxc.rb', line 130 def self.run_chef_node(lxc, chef_node, run_list_hash, chef_node_attrs = {}) chef_node_name = ChefUtil.extract_node_name(chef_node) run_list = ChefUtil.prepare_run_list(chef_node_name, run_list_hash) node_attributes = { "run_list" => run_list, "toaster" => {} } node_attributes.merge!(chef_node_attrs) Config.values.each do |key,value| if key != "chef" node_attributes["toaster"][key] = value end end puts "DEBUG: Chef automation #{chef_node}, node attributes: #{MarkupUtil.to_json(node_attributes)}" run_chef(lxc, chef_node, node_attributes) end |