Skip to content
Merged
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
52 changes: 52 additions & 0 deletions cloudendure/cloudendure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down