1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-23 22:55:23 +00:00

add delete_instance function in instances.view

This commit is contained in:
Ing. Jan KRCMAR 2018-03-13 13:56:58 +01:00
parent 748a79d9db
commit 1ad2f03b52

View file

@ -926,3 +926,40 @@ def sshkeys(request, vname):
for k in keys:
instance_keys.append(k.keypublic)
return HttpResponse(json.dumps(instance_keys))
def delete_instance(instance, delete_disk=False):
compute = instance.compute
instance_name = instance.name
try:
conn = wvmInstance(compute.hostname,
compute.login,
compute.password,
compute.type,
instance.name)
del_userinstance = UserInstance.objects.filter(instance=instance)
if del_userinstance:
print("Deleting UserInstances")
print(del_userinstance)
del_userinstance.delete()
if conn.get_status() == 1:
print("Forcing shutdown")
conn.force_shutdown()
if delete_disk:
snapshots = sorted(conn.get_snapshot(), reverse=True)
for snap in snapshots:
print("Deleting snapshot {}".format(snap['name']))
conn.snapshot_delete(snap['name'])
print("Deleting disks")
conn.delete_disk()
conn.delete()
instance.delete()
print("Instance {} on compute {} sucessfully deleted".format(instance_name, compute.hostname))
except libvirtError as lib_err:
print("Error removing instance {} on compute {}".format(instance_name, compute.hostname))
raise lib_err