|
| 1 | +import boto3 |
| 2 | +import os |
| 3 | +import shutil |
| 4 | + |
| 5 | +from botocore.exceptions import NoRegionError |
| 6 | +from ros2cli.verb import VerbExtension |
| 7 | + |
| 8 | +from ..util import instance_dir |
| 9 | + |
| 10 | + |
| 11 | +class ImageVerb(VerbExtension): |
| 12 | + |
| 13 | + def add_arguments(self, parser, cli_name): |
| 14 | + parser.add_argument( |
| 15 | + "name", |
| 16 | + type=str, |
| 17 | + nargs="*", |
| 18 | + help="Set instance name. Can be found when running 'ros2 fog list' command next to ssh_key after " |
| 19 | + "'FogROS2KEY-'", |
| 20 | + ) |
| 21 | + parser.add_argument( |
| 22 | + "--region", |
| 23 | + nargs="*", |
| 24 | + help="Set AWS region (overrides config/env settings)", |
| 25 | + ) |
| 26 | + parser.add_argument( |
| 27 | + "--dry-run", |
| 28 | + action="store_true", |
| 29 | + help="Show what would happen, but do not execute", |
| 30 | + ) |
| 31 | + |
| 32 | + def query_region(self, region, name): |
| 33 | + try: |
| 34 | + client = boto3.client("ec2", region) |
| 35 | + except NoRegionError: |
| 36 | + raise RuntimeError( |
| 37 | + "AWS is not configured! Please run `aws configure` first." |
| 38 | + ) |
| 39 | + print("Instance name: ", name) |
| 40 | + ec2_instances = client.describe_instances( |
| 41 | + Filters=[ |
| 42 | + { |
| 43 | + "Name": "instance.group-name", |
| 44 | + "Values": ["FOGROS2_SECURITY_GROUP"], |
| 45 | + }, |
| 46 | + {"Name": "tag:FogROS2-Name", "Values": name}, |
| 47 | + ] |
| 48 | + ) |
| 49 | + |
| 50 | + if len(ec2_instances["Reservations"]) == 0: |
| 51 | + print( |
| 52 | + "No EC2 instances found with the specified name; " |
| 53 | + "check list to be sure name is correct!" |
| 54 | + ) |
| 55 | + |
| 56 | + return [client, ec2_instances] |
| 57 | + |
| 58 | + def create_ami(self, client, ec2_instances, dry_run): |
| 59 | + image_count = 0 |
| 60 | + for res in ec2_instances["Reservations"]: |
| 61 | + for inst in res["Instances"]: |
| 62 | + tag_map = ( |
| 63 | + {t["Key"]: t["Value"] for t in inst["Tags"]} |
| 64 | + if "Tags" in inst |
| 65 | + else {} |
| 66 | + ) |
| 67 | + print( |
| 68 | + f"Converting {tag_map.get('FogROS2-Name', '(unknown)')} " |
| 69 | + f"{inst['InstanceId']} to AMI." |
| 70 | + ) |
| 71 | + name = tag_map["FogROS2-Name"] + "-image" |
| 72 | + inst_id = inst['InstanceId'] |
| 73 | + |
| 74 | + if not dry_run: |
| 75 | + response = client.create_image(InstanceId=inst_id, Name=name) |
| 76 | + if response["ResponseMetadata"]["HTTPStatusCode"] != 200: |
| 77 | + raise RuntimeError( |
| 78 | + f"Could not create image for {inst['KeyName']}!" |
| 79 | + ) |
| 80 | + |
| 81 | + print("done.") |
| 82 | + image_count += 1 |
| 83 | + |
| 84 | + return image_count |
| 85 | + |
| 86 | + def main(self, *, args): |
| 87 | + regions = args.region |
| 88 | + if regions is None or len(regions) == 0: |
| 89 | + regions = [None] |
| 90 | + elif "*" in regions or "all" in regions: |
| 91 | + client = boto3.client("ec2") |
| 92 | + response = client.describe_regions() |
| 93 | + regions = [r["RegionName"] for r in response["Regions"]] |
| 94 | + |
| 95 | + |
| 96 | + if len(regions) == 1: |
| 97 | + image_count = self.create_ami( |
| 98 | + *self.query_region(regions[0], args.name), args.dry_run |
| 99 | + ) |
| 100 | + else: |
| 101 | + from concurrent.futures import ThreadPoolExecutor |
| 102 | + |
| 103 | + with ThreadPoolExecutor(max_workers=len(regions)) as executor: |
| 104 | + futures = [ |
| 105 | + executor.submit(self.query_region, r, args.name) |
| 106 | + for r in regions |
| 107 | + ] |
| 108 | + image_count = sum( |
| 109 | + [ |
| 110 | + self.create_ami(*f.result(), args.dry_run) |
| 111 | + for f in futures |
| 112 | + ] |
| 113 | + ) |
| 114 | + |
| 115 | + if image_count == 0: |
| 116 | + print("No image was created") |
| 117 | + |
0 commit comments