def call(env)
env[:ui].info(I18n.t('vagrant_libvirt.destroy_domain'))
libvirt_domain = env[:machine].provider.driver.connection.client.lookup_domain_by_uuid(
env[:machine].id)
libvirt_domain.list_snapshots.each do |name|
@logger.info("Deleting snapshot '#{name}'")
begin
libvirt_domain.lookup_snapshot_by_name(name).delete
rescue => e
raise Errors::DeleteSnapshotError, error_message: e.message
end
end
if libvirt_domain.has_managed_save?
libvirt_domain.managed_save_remove
end
domain = env[:machine].provider.driver.connection.servers.get(env[:machine].id.to_s)
if env[:machine].provider_config.disks.empty?
domain.destroy(destroy_volumes: true)
else
domain.destroy(destroy_volumes: false)
env[:machine].provider_config.disks.each do |disk|
next if disk[:allow_existing]
diskname = libvirt_domain.name + '-' + disk[:device] + '.' + disk[:type].to_s
libvirt_disk = domain.volumes.select do |x|
x.name == diskname
end.first
if libvirt_disk
libvirt_disk.destroy
elsif disk[:path]
poolname = env[:machine].provider_config.storage_pool_name
libvirt_disk = domain.volumes.select do |x|
x.path =~ /\/#{disk[:path]}$/ && x.pool_name == poolname
end.first
libvirt_disk.destroy if libvirt_disk
end
end
root_disk = domain.volumes.select do |x|
x.name == libvirt_domain.name + '.img'
end.first
root_disk.destroy if root_disk
end
@app.call(env)
end