diff --git a/cloudendure/cloudendure.py b/cloudendure/cloudendure.py index df7f4eecc..55103bed3 100644 --- a/cloudendure/cloudendure.py +++ b/cloudendure/cloudendure.py @@ -573,6 +573,58 @@ 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."""