From 1ad2f03b52d5f63393dc26bc3b1709307da9ef9c Mon Sep 17 00:00:00 2001 From: "Ing. Jan KRCMAR" Date: Tue, 13 Mar 2018 13:56:58 +0100 Subject: [PATCH] add delete_instance function in instances.view --- instances/views.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/instances/views.py b/instances/views.py index 196eaf3..e81f7c8 100644 --- a/instances/views.py +++ b/instances/views.py @@ -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 +