From a88d908f5481737a57fe47d9cecaa45b47839b9a Mon Sep 17 00:00:00 2001 From: Tom Warnock Date: Mon, 5 Aug 2019 09:19:05 -0400 Subject: [PATCH 1/2] copy_image and split_image support --- cloudendure/cloudendure.py | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/cloudendure/cloudendure.py b/cloudendure/cloudendure.py index df7f4eecc..27b3aea2e 100644 --- a/cloudendure/cloudendure.py +++ b/cloudendure/cloudendure.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- """Define the CloudEndure main entry logic.""" +import copy import datetime import json import os @@ -573,6 +574,49 @@ def create_ami( return False return True + def copy_image(self, image_id: str, kms_id: str): + """Copy a shared image to a account""" + _ec2_client = boto3.client('ec2', AWS_REGION) + + new_image = _ec2_client.copy_image(SourceImageId=image_id, SourceRegion=AWS_REGION, Name="test", Encrypted=True, KmsKeyId=kms_id) + + print(new_image) + return new_image['ImageId'] + + def split_image(self, image_id: str): + """Split the image into a root drive only AMI and a collection of snapshots.""" + print("Loading EC2 client for region: ", AWS_REGION) + _ec2_res = boto3.resource("ec2", AWS_REGION) + + # Access the image that needs to be split + image = _ec2_res.Image(image_id) + + root_drive = {} + drives = {} + + # separate the root drive from the other drives + devices = image.block_device_mappings + for device in devices: + if "Ebs" in device: + if device['DeviceName'] == image.root_device_name: + print (f"Found Root! {device}") + root_drive = device + else: + drives[device["DeviceName"]] = device["Ebs"] + + # have to remove the encrypted flag + del root_drive["Ebs"]["Encrypted"] + + # create a new AMI with only the root + response = _ec2_res.register_image(Architecture="x86_64", + BlockDeviceMappings=[root_drive], + Name="test_split", + RootDeviceName=image.root_device_name, + VirtualizationType="hvm") + + # return the AMI + drives['root_ami'] = response.id + return drives def main(): """Define the main entry method for the CLI.""" From f0fb7b93ba2e967e6907dff5f82102141796ff32 Mon Sep 17 00:00:00 2001 From: Mark Beacom Date: Mon, 5 Aug 2019 09:25:52 -0400 Subject: [PATCH 2/2] Lint and remove unused import --- cloudendure/cloudendure.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/cloudendure/cloudendure.py b/cloudendure/cloudendure.py index 27b3aea2e..55103bed3 100644 --- a/cloudendure/cloudendure.py +++ b/cloudendure/cloudendure.py @@ -1,7 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- """Define the CloudEndure main entry logic.""" -import copy import datetime import json import os @@ -576,12 +575,18 @@ def create_ami( def copy_image(self, image_id: str, kms_id: str): """Copy a shared image to a account""" - _ec2_client = boto3.client('ec2', AWS_REGION) - - new_image = _ec2_client.copy_image(SourceImageId=image_id, SourceRegion=AWS_REGION, Name="test", Encrypted=True, KmsKeyId=kms_id) + _ec2_client = boto3.client("ec2", AWS_REGION) + + new_image = _ec2_client.copy_image( + SourceImageId=image_id, + SourceRegion=AWS_REGION, + Name="test", + Encrypted=True, + KmsKeyId=kms_id, + ) print(new_image) - return new_image['ImageId'] + return new_image["ImageId"] def split_image(self, image_id: str): """Split the image into a root drive only AMI and a collection of snapshots.""" @@ -598,8 +603,8 @@ def split_image(self, image_id: str): devices = image.block_device_mappings for device in devices: if "Ebs" in device: - if device['DeviceName'] == image.root_device_name: - print (f"Found Root! {device}") + if device["DeviceName"] == image.root_device_name: + print(f"Found Root! {device}") root_drive = device else: drives[device["DeviceName"]] = device["Ebs"] @@ -608,16 +613,19 @@ def split_image(self, image_id: str): del root_drive["Ebs"]["Encrypted"] # create a new AMI with only the root - response = _ec2_res.register_image(Architecture="x86_64", - BlockDeviceMappings=[root_drive], - Name="test_split", - RootDeviceName=image.root_device_name, - VirtualizationType="hvm") + response = _ec2_res.register_image( + Architecture="x86_64", + BlockDeviceMappings=[root_drive], + Name="test_split", + RootDeviceName=image.root_device_name, + VirtualizationType="hvm", + ) # return the AMI - drives['root_ami'] = response.id + drives["root_ami"] = response.id return drives + def main(): """Define the main entry method for the CLI.""" # Run CloudEndure via the pipeline object for easy CLI access.