Skip to content

Commit

Permalink
Remove host dhcp entries from network on destroy. Delete network if e…
Browse files Browse the repository at this point in the history
…mpty.
  • Loading branch information
adrahon committed Oct 11, 2014
1 parent 718a197 commit fea8541
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/vagrant-kvm/driver/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def reset_volume_permission(userid, groupid)
end

def delete
cleanup_networks
domain = @conn.lookup_domain_by_uuid(@uuid)
definition = Util::VmDefinition.new(domain.xml_desc)
volume = @pool.lookup_volume_by_path(definition.attributes[:disk])
Expand Down Expand Up @@ -257,6 +258,39 @@ def read_machine_ip
get_default_ip
end

# Remove DHCP entries from Networks
def cleanup_networks
# Get list of NICs
domain = @conn.lookup_domain_by_uuid(@uuid)
definition = Util::VmDefinition.new(domain.xml_desc)
nics = definition.get_all_nics
nics.each do |nic|
# for each nic remove dhcp entry from network
network = @conn.lookup_network_by_name(nic[:network])
network_def = Util::NetworkDefinition.new(nic[:network], network.xml_desc)

@logger.info("Removing host entry from network #{nic[:network]}")
@logger.debug("Removing #{nic[:mac]} defined as:\n#{network_def.get_host_xml(nic[:mac])}")
# 2 should be Libvirt::Network::NETWORK_UPDATE_COMMAND_DELETE
# but fails as undefined? XXX
network.update(2,
Libvirt::Network::NETWORK_SECTION_IP_DHCP_HOST,
-1,
network_def.get_host_xml(nic[:mac]),
Libvirt::Network::NETWORK_UPDATE_AFFECT_CURRENT
)

# if no entries left, destroy and undefine the network
network = @conn.lookup_network_by_name(nic[:network])
network_def = Util::NetworkDefinition.new(nic[:network], network.xml_desc)
if network_def.hosts.count == 0
@logger.info("Network #{nic[:network]} has no hosts left, deleting.")
network.destroy
network.undefine
end
end
end

# Activate the driver's storage pool
def activate_storage_pool(pool_name)
# Get storage pool if it exists
Expand Down
11 changes: 11 additions & 0 deletions lib/vagrant-kvm/util/network_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ def as_host_xml
xml
end

# Returns xml definition for one host
def get_host_xml(mac)
xml = ""
hosts.each do |host|
if host[:mac] == mac
return "<host mac='#{host[:mac]}' name='#{host[:name]}' ip='#{host[:ip]}' />"
end
end
xml
end

def hosts
get(:hosts)
end
Expand Down
13 changes: 13 additions & 0 deletions lib/vagrant-kvm/util/vm_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,19 @@ def add_nic(new_nic)
set(:nics, nics)
end

# Return list of NICs including primary NIC
# FIXME we need to treat primary as ordinary NIC
def get_all_nics
nics = get(:nics)
nics << {
:mac => get(:mac),
:network => get(:network),
# :type => get(:type), XXX need to manage model
:model => get(:network_model)
}
nics
end

def get_memory(unit="bytes")
size_from_bytes(get(:memory), unit)
end
Expand Down

0 comments on commit fea8541

Please sign in to comment.