mirror of
https://github.com/vagrant-libvirt/vagrant-libvirt.git
synced 2025-02-25 18:55:27 -06:00
Handle network that vagrant created missing when destroy is run. Closes #128
This commit is contained in:
parent
6e8c43e300
commit
1484a9fb36
@ -18,7 +18,7 @@ module VagrantPlugins
|
|||||||
# data directory, created_networks file holds UUIDs of each network.
|
# data directory, created_networks file holds UUIDs of each network.
|
||||||
created_networks_file = env[:machine].data_dir + 'created_networks'
|
created_networks_file = env[:machine].data_dir + 'created_networks'
|
||||||
|
|
||||||
@logger.info 'Attepmt destroy network'
|
@logger.info 'Checking if any networks were created'
|
||||||
# If created_networks file doesn't exist, there are no networks we
|
# If created_networks file doesn't exist, there are no networks we
|
||||||
# need to remove.
|
# need to remove.
|
||||||
unless File.exist?(created_networks_file)
|
unless File.exist?(created_networks_file)
|
||||||
@ -26,39 +26,45 @@ module VagrantPlugins
|
|||||||
return @app.call(env)
|
return @app.call(env)
|
||||||
end
|
end
|
||||||
|
|
||||||
@logger.info 'file with network exists'
|
@logger.info 'File with created networks exists'
|
||||||
|
|
||||||
# Iterate over each created network UUID and try to remove it.
|
# Iterate over each created network UUID and try to remove it.
|
||||||
created_networks = []
|
created_networks = []
|
||||||
file = File.open(created_networks_file, 'r')
|
file = File.open(created_networks_file, 'r')
|
||||||
file.readlines.each do |network_uuid|
|
file.readlines.each do |network_uuid|
|
||||||
@logger.info network_uuid
|
@logger.info "Checking for #{network_uuid}"
|
||||||
|
# lookup_network_by_uuid throws same exception
|
||||||
|
# if there is an error or if the network just doesn't exist
|
||||||
begin
|
begin
|
||||||
libvirt_network = env[:libvirt_compute].client.lookup_network_by_uuid(
|
libvirt_network = env[:libvirt_compute].client.lookup_network_by_uuid(
|
||||||
network_uuid)
|
network_uuid)
|
||||||
rescue
|
rescue Libvirt::RetrieveError => e
|
||||||
raise network_uuid
|
# this network is already destroyed, so move on
|
||||||
next
|
if e.message =~ /Network not found/
|
||||||
|
@logger.info "It is already undefined"
|
||||||
|
next
|
||||||
|
# some other error occured, so raise it again
|
||||||
|
else
|
||||||
|
raise e
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Maybe network doesn't exist anymore.
|
|
||||||
next unless libvirt_network
|
|
||||||
|
|
||||||
# Skip removing if network has still active connections.
|
# Skip removing if network has still active connections.
|
||||||
xml = Nokogiri::XML(libvirt_network.xml_desc)
|
xml = Nokogiri::XML(libvirt_network.xml_desc)
|
||||||
connections = xml.xpath('/network/@connections').first
|
connections = xml.xpath('/network/@connections').first
|
||||||
@logger.info connections
|
|
||||||
if connections != nil
|
if connections != nil
|
||||||
|
@logger.info "Still has connections so will not undefine"
|
||||||
created_networks << network_uuid
|
created_networks << network_uuid
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
|
|
||||||
# Shutdown network first.
|
|
||||||
libvirt_network.destroy
|
|
||||||
|
|
||||||
|
# Shutdown network first.
|
||||||
# Undefine network.
|
# Undefine network.
|
||||||
begin
|
begin
|
||||||
|
libvirt_network.destroy
|
||||||
libvirt_network.undefine
|
libvirt_network.undefine
|
||||||
|
@logger.info "Undefined it"
|
||||||
rescue => e
|
rescue => e
|
||||||
raise Error::DestroyNetworkError,
|
raise Error::DestroyNetworkError,
|
||||||
network_name: libvirt_network.name,
|
network_name: libvirt_network.name,
|
||||||
@ -68,13 +74,16 @@ module VagrantPlugins
|
|||||||
file.close
|
file.close
|
||||||
|
|
||||||
# Update status of created networks after removing some/all of them.
|
# Update status of created networks after removing some/all of them.
|
||||||
|
# Not sure why we are doing this, something else seems to always delete the file
|
||||||
if created_networks.length > 0
|
if created_networks.length > 0
|
||||||
File.open(created_networks_file, 'w') do |file|
|
File.open(created_networks_file, 'w') do |file|
|
||||||
|
@logger.info "Writing new created_networks file"
|
||||||
created_networks.each do |network_uuid|
|
created_networks.each do |network_uuid|
|
||||||
file.puts network_uuid
|
file.puts network_uuid
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
@logger.info "Deleting created_networks file"
|
||||||
File.delete(created_networks_file)
|
File.delete(created_networks_file)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user