Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image removal for azure and aws #465

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions wrapanapi/systems/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,35 @@ def list_templates(self, executable_by_me=True, owned_by_me=True, public=False):
return [EC2Image(system=self, raw=self.ec2_resource.Image(image["ImageId"]))
for image in images]

def list_free_images(self, image_list=None):
"""
Returns images which don't have a VM associated to it

Args:
image_list (list): List of ids of all images in resource group
"""
free_images = []
vm_list = self.list_vms()

if not vm_list:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block is unnecessary and repetitive.

for vm in vm_list:
    free_images.append()
return free_images 

This loop will noop when vm_list is an empty list, which the function is written to return.

# No VMs using the images, images are free
return image_list

for vm in vm_list:
if vm.raw.image_id not in image_list:
free_images.append(vm.raw.image_id)
return free_images

def delete_images(self, image_list=None):
"""
Deletes images by ID

Args:
image_list (list): ["imageID_1", "imageID_2"]
"""
for image in image_list:
img=EC2Image(system=self, raw=self.ec2_resource.Image(image)).delete()

def find_templates(self, name=None, id=None, executable_by_me=True, owned_by_me=True,
public=False, filters=None):
"""
Expand Down
51 changes: 51 additions & 0 deletions wrapanapi/systems/msazure.py
akhil-jha marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,42 @@ def list_compute_images(self):
return self.resource_client.resources.list(
filter="resourceType eq 'Microsoft.Compute/images'")

def list_compute_images_by_resource_group(self, resource_group=None, free_images=None):
"""
Args:
resource_group (str): Name of the resource group
free_images (bool): Whether to collect image which do not have any resource(VM) linked to it
"""
resource_group = resource_group or self.resource_group
image_list = list(
self.resource_client.resources.list(
filter=f"resourceType eq 'Microsoft.Compute/images' and resourceGroup eq '{resource_group}'"
)
)
mshriver marked this conversation as resolved.
Show resolved Hide resolved

if not free_images:
return image_list

vm_list = self.list_vms(resource_group=resource_group)
if not vm_list:
return image_list

images_used_by_vm = []
for vm_name in vm_list:
images_used_by_vm.append(
self.compute_client.virtual_machines.get(
resource_group_name=resource_group,
vm_name=vm_name
).storage_profile.image_reference.id
)

images_with_no_resources = []
for image in image_list:
if image.id not in images_used_by_vm:
images_with_no_resources.append(image)

return images_with_no_resources
mshriver marked this conversation as resolved.
Show resolved Hide resolved

def list_all_image_names(self):
blob_image_names = [item.name for item in self.find_templates()]
compute_image_names = [item.name for item in self.list_compute_images()]
Expand Down Expand Up @@ -1130,6 +1166,21 @@ def delete_stack_by_date(self, days_old, resource_group=None):
result)
return results

def delete_compute_image_by_resource_group(self, resource_group=None, image_list=None):
"""
Delete compute images by resource group

:resource_group: (str) Name of the resource group. "FooBar"
:image_list: (list) List of images. ["imagename1", "imagename2", "imagename3"]
"""
result = []
resource_group = resource_group or self.resource_group
for image in image_list:
self.logger.info("Deleting '%s' from '%s'", image, resource_group)
response = self.compute_client.images.delete(resource_group_name=resource_group, image_name=image)
result.append((image, response))
return result

def list_stack_resources(self, stack_name, resource_group=None):
self.logger.info("Checking Stack %s resources ", stack_name)
# todo: weird implementation to refactor this method later
Expand Down