lib/dev-lxc/server.rb in dev-lxc-2.3.0 vs lib/dev-lxc/server.rb in dev-lxc-2.3.1

- old
+ new

@@ -33,22 +33,25 @@ def install_package(package_path) @container.install_package(package_path) end def start + return if @container.running? hwaddr = @container.config_item("lxc.network.0.hwaddr") - DevLXC.assign_ip_address(@ipaddress, @container.name, hwaddr) if @ipaddress + release_lingering_dhcp_ip_addresses(hwaddr) + assign_static_ip_address(hwaddr) if @ipaddress @container.sync_mounts(@mounts) @container.start @container.sync_ssh_keys(@ssh_keys) puts end - def stop + def shutdown hwaddr = @container.config_item("lxc.network.0.hwaddr") if @container.defined? - @container.stop - deregister_from_dhcp(hwaddr) + @container.shutdown + remove_static_ip_address(hwaddr) + release_lingering_dhcp_ip_addresses(hwaddr) end def snapshot(comment=nil) unless @container.defined? puts "WARNING: Skipping snapshot of '#{@container.name}' because it does not exist" @@ -143,13 +146,41 @@ if @container.defined? hwaddr = @container.config_item("lxc.network.0.hwaddr") @container.snapshot_list.each { |snapshot| @container.snapshot_destroy(snapshot.first) } end @container.destroy - deregister_from_dhcp(hwaddr) + remove_static_ip_address(hwaddr) + release_lingering_dhcp_ip_addresses(hwaddr) end - def deregister_from_dhcp(hwaddr) + def release_lingering_dhcp_ip_addresses(hwaddr) + dhcp_leases = IO.readlines('/var/lib/misc/dnsmasq.lxcbr0.leases') + dhcp_leases.each do |dhcp_lease| + if m = dhcp_lease.match(/ #{hwaddr} (\d+\.\d+\.\d+\.\d+) /) + mac_addr = hwaddr + ip_addr = m[1] + elsif m = dhcp_lease.match(/ (\w\w:\w\w:\w\w:\w\w:\w\w:\w\w) #{@ipaddress} /) + mac_addr = m[1] + ip_addr = @ipaddress + elsif m = dhcp_lease.match(/ (\w\w:\w\w:\w\w:\w\w:\w\w:\w\w) (\d+\.\d+\.\d+\.\d+) #{@container.name.sub(/\.lxc$/, '')} /) + mac_addr = m[1] + ip_addr = m[2] + end + if mac_addr && ip_addr + puts "Releasing lingering DHCP lease: #{dhcp_lease}" + system("dhcp_release lxcbr0 #{ip_addr} #{mac_addr}") + end + end + end + + def assign_static_ip_address(hwaddr) + puts "Assigning IP address #{@ipaddress} to '#{@container.name}' container's lxc.network.hwaddr #{hwaddr}" + DevLXC.search_file_delete_line("/etc/lxc/dhcp-hosts.conf", /(^#{hwaddr}|,#{@ipaddress}$)/) + DevLXC.append_line_to_file("/etc/lxc/dhcp-hosts.conf", "#{hwaddr},#{@ipaddress}\n") + DevLXC.reload_dnsmasq + end + + def remove_static_ip_address(hwaddr) if @ipaddress DevLXC.search_file_delete_line("/etc/lxc/dhcp-hosts.conf", /,#{@ipaddress}$/) end unless hwaddr.nil? DevLXC.search_file_delete_line("/etc/lxc/dhcp-hosts.conf", /^#{hwaddr}/)