From b5f38afbcae965e879e05ce19c4c9cf583b22177 Mon Sep 17 00:00:00 2001 From: catborise Date: Wed, 24 Oct 2018 16:19:30 +0300 Subject: [PATCH] clone instance: create db record first, then run clone process. delete db record if exception while cloning --- instances/views.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/instances/views.py b/instances/views.py index e98ca86..aff0e78 100644 --- a/instances/views.py +++ b/instances/views.py @@ -741,11 +741,16 @@ def instance(request, compute_id, vname): msg = _("Instance mac '%s' invalid format!" % clone_data['clone-net-mac-0']) error_messages.append(msg) else: - new_uuid = conn.clone_instance(clone_data) - new_instance = Instance(compute_id=compute_id, name=clone_data['name'], uuid=new_uuid) + new_instance = Instance(compute_id=compute_id, name=clone_data['name']) new_instance.save() - userinstance = UserInstance(instance_id=new_instance.id, user_id=request.user.id, - is_delete=True) + try: + new_uuid = conn.clone_instance(clone_data) + new_instance.uuid = new_uuid + new_instance.save() + except Exception as e: + new_instance.delete() + raise e + userinstance = UserInstance(instance_id=new_instance.id, user_id=request.user.id, is_delete=True) userinstance.save() msg = _("Clone of '%s'" % instance.name)