Class: Kitchen::Driver::Libvirtlxc
- Inherits:
-
SSHBase
- Object
- SSHBase
- Kitchen::Driver::Libvirtlxc
- Defined in:
- lib/kitchen/driver/libvirtlxc.rb
Instance Method Summary collapse
- #create(state) ⇒ Object
- #destroy(state) ⇒ Object
- #fixup_files(container_id, container_path) ⇒ Object
- #wait_for_lease(container_id) ⇒ Object
Instance Method Details
#create(state) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/kitchen/driver/libvirtlxc.rb', line 18 def create(state) state[:container_id] = SecureRandom.uuid info("Creating LXC container #{state[:container_id]}") new_container = File.join(config[:container_path], state[:container_id]) state[:container_path] = new_container run_command("cp -r #{File.join(config[:container_path], config[:base_container])} #{new_container}") run_command("mkdir -p #{new_container}/root/.ssh") run_command("chmod 0700 #{new_container}/root/.ssh") run_command("cat #{config[:ssh_public_key]} >> #{new_container}/root/.ssh/authorized_keys") run_command("chmod 0644 #{new_container}/root/.ssh/authorized_keys") fixup_files(state[:container_id], new_container) run_command("virt-install --connect lxc:/// --name #{state[:container_id]} --ram #{config[:customize][:memory]} --vcpu #{config[:customize][:vcpu]} --filesystem #{new_container}/,/ --noautoconsole") run_command("virsh --connect lxc:/// start #{state[:container_id]}", :returns => [0,1]) state[:hostname] = wait_for_lease(state[:container_id]) wait_for_sshd(state[:hostname]) end |
#destroy(state) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/kitchen/driver/libvirtlxc.rb', line 35 def destroy(state) if state[:container_id] && state[:container_path] run_command("virsh --connect lxc:/// destroy #{state[:container_id]}") run_command("rm -rf #{state[:container_path]}") run_command("rm /etc/libvirt/lxc/#{state[:container_id]}.xml") end end |
#fixup_files(container_id, container_path) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/kitchen/driver/libvirtlxc.rb', line 43 def fixup_files(container_id, container_path) etc_sysconfig_network = File.join(container_path, "etc", "sysconfig", "network") if File.exists?(etc_sysconfig_network) info("manipulating sysconfig") run_command("sed -i 's/HOSTNAME=.*/HOSTNAME=#{container_id}/g' #{etc_sysconfig_network}") end ifcfg_eth0 = File.join(container_path, "etc", "sysconfig", "network-scripts", "ifcfg-eth0") if File.exists?(etc_sysconfig_network) run_command("sed -i 's/BOOTPROTO=.*/BOOTPROTO=dhcp/g' #{ifcfg_eth0}") run_command("sed -i 's/IPADDR=.+//g' #{ifcfg_eth0}") end etc_sudoers = File.join(container_path, "etc", "sudoers") if File.exists?(etc_sudoers) run_command("sed -i 's/Defaults.*requiretty/# Defaults requiretty/g' #{etc_sudoers}") run_command("sed -i 's/Defaults.*visiblepw/# Defaults !visiblepw/g' #{etc_sudoers}") end end |
#wait_for_lease(container_id) ⇒ Object
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 |
# File 'lib/kitchen/driver/libvirtlxc.rb', line 61 def wait_for_lease(container_id) info("Determining mac address...") mac_address = nil ip_address = nil File.open("/etc/libvirt/lxc/#{container_id}.xml", "r") do |xml| xml.each_line do |line| if line =~ /mac address='(.+)'/ mac_address = $1 break end end end info("Mac addresss: #{mac_address}") tries = 30 while ip_address == nil && tries > 0 File.open("/var/lib/libvirt/dnsmasq/default.leases") do |leases| leases.each_line do |line| if line =~ /^\d+ #{mac_address} (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ ip_address = $1 end end end if ip_address.nil? tries = tries - 1 sleep 1 end end raise ActionFailed, "Cannot determine IP Address of '#{container_id}'" unless ip_address info("IP Address: #{ip_address}") return ip_address end |