Skip to content

Commit

Permalink
Merge pull request #502 from grycap/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
micafer authored Nov 23, 2017
2 parents 17da0bf + 09aaee9 commit f89379f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
2 changes: 1 addition & 1 deletion IM/CloudInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def get_cloud_list(auth_data):
res = []

for i, auth in enumerate(auth_data.auth_list):
if auth['type'] not in ['InfrastructureManager', 'VMRC']:
if 'type' in auth and auth['type'] not in ['InfrastructureManager', 'VMRC']:
cloud_item = CloudInfo()
cloud_item.type = auth['type']
if 'id' in auth.keys() and auth['id']:
Expand Down
47 changes: 28 additions & 19 deletions IM/connectors/Azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,11 +555,7 @@ def create_vms(self, inf, radl, requested_radl, num_vm, location, storage_accoun

# Delete Resource group and everything in it
if group_name:
self.log_info("Delete Resource group %s and everything in it." % group_name)
try:
resource_client.resource_groups.delete(group_name).wait()
except:
self.log_exception("Error deleting Resource group %s." % group_name)
self.delete_resource_group(group_name, resource_client)

i += 1

Expand Down Expand Up @@ -604,11 +600,7 @@ def launch(self, inf, radl, requested_radl, num_vm, auth_data):
).wait()
except:
self.log_exception("Error creating storage account: %s" % storage_account)
self.log_info("Delete Inf RG group %s" % "rg-%s" % inf.id)
try:
resource_client.resource_groups.delete("rg-%s" % inf.id)
except:
pass
self.delete_resource_group("rg-%s" % inf.id, resource_client)

subnets = self.create_nets(radl, credentials, subscription_id, "rg-%s" % inf.id)

Expand All @@ -635,11 +627,7 @@ def launch(self, inf, radl, requested_radl, num_vm, auth_data):

if remaining_vms > 0:
# Remove the general group
self.log_info("Delete Inf RG group %s" % "rg-%s" % inf.id)
try:
resource_client.resource_groups.delete("rg-%s" % inf.id)
except:
pass
self.delete_resource_group("rg-%s" % inf.id, resource_client)
else:
self.log_info("All VMs created successfully.")

Expand Down Expand Up @@ -757,16 +745,14 @@ def finalize(self, vm, last, auth_data):

# Delete Resource group and everything in it
if self.get_rg(group_name, credentials, subscription_id):
self.log_info("Removing RG: %s" % group_name)
resource_client.resource_groups.delete(group_name).wait()
self.delete_resource_group(group_name, resource_client)
else:
self.log_info("RG: %s does not exist. Do not remove." % group_name)

# if it is the last VM delete the RG of the Inf
if last:
if self.get_rg("rg-%s" % vm.inf.id, credentials, subscription_id):
self.log_info("Removing Inf. RG: %s" % "rg-%s" % vm.inf.id)
resource_client.resource_groups.delete("rg-%s" % vm.inf.id)
self.delete_resource_group("rg-%s" % vm.inf.id, resource_client)
else:
self.log_info("RG: %s does not exist. Do not remove." % "rg-%s" % vm.inf.id)

Expand Down Expand Up @@ -831,3 +817,26 @@ def alterVM(self, vm, radl, auth_data):
return False, "Error altering the VM: " + str(ex)

return (True, "")

def delete_resource_group(self, group_name, resource_client, max_retries=3):
"""
Delete a RG with retries
"""
cont = 0
deleted = False

self.log_info("Delete RG %s." % group_name)
while cont < max_retries and not deleted:
cont += 1
try:
resource_client.resource_groups.delete(group_name).wait()
deleted = True
except:
self.log_exception("Error deleting Resource group %s (%d/%d)." % (group_name, cont, max_retries))

if not deleted:
self.log_error("Resource group %s cannot be deleted!!!" % group_name)
else:
self.log_info("Resource group %s successfully deleted." % group_name)

return deleted

0 comments on commit f89379f

Please sign in to comment.