From 7243170c0c40b6ac749d8f3b216f40b10fc7e2a3 Mon Sep 17 00:00:00 2001 From: Oliver Mannion <125105+tekumara@users.noreply.github.com> Date: Sun, 14 Jan 2024 20:53:36 +1100 Subject: [PATCH] feat: rename instance (#444) eg: ``` aec ec2 rename alice alice2 ``` --- docs/ami.md | 4 ++-- docs/ec2.md | 23 +++++++++++++++-------- docs/ssm.md | 2 +- src/aec/command/ec2.py | 20 ++++++++++++++++++++ src/aec/command/ssm.py | 2 +- src/aec/main.py | 5 +++++ tests/test_ec2.py | 10 ++++++++++ 7 files changed, 54 insertions(+), 12 deletions(-) diff --git a/docs/ami.md b/docs/ami.md index ab3202cc..643b1952 100644 --- a/docs/ami.md +++ b/docs/ami.md @@ -39,8 +39,8 @@ aec ami describe Name ImageId CreationDate RootDeviceName Size ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── - ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-20170727 ami-1e749f67 2023-05-27T03:17:09.000Z /dev/sda1 15 - ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20170721 ami-785db401 2023-05-27T03:17:09.000Z /dev/sda1 15 + ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-20170727 ami-1e749f67 2024-01-09T03:17:41.000Z /dev/sda1 15 + ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20170721 ami-785db401 2024-01-09T03:17:41.000Z /dev/sda1 15 ``` diff --git a/docs/ec2.md b/docs/ec2.md index 9d0fbfdb..1109f251 100644 --- a/docs/ec2.md +++ b/docs/ec2.md @@ -15,13 +15,13 @@ from aec.main import build_parser cog.out(f"```\n{build_parser()._subparsers._actions[1].choices['ec2'].format_help()}```") ]]] --> ``` -usage: aec ec2 [-h] {create-key-pair,describe,launch,logs,modify,start,stop,tag,tags,status,templates,terminate,user-data} ... +usage: aec ec2 [-h] {create-key-pair,describe,launch,logs,modify,start,stop,rename,tag,tags,status,templates,terminate,user-data} ... optional arguments: -h, --help show this help message and exit subcommands: - {create-key-pair,describe,launch,logs,modify,start,stop,tag,tags,status,templates,terminate,user-data} + {create-key-pair,describe,launch,logs,modify,start,stop,rename,tag,tags,status,templates,terminate,user-data} create-key-pair Create a key pair. describe List EC2 instances in the region. launch Launch a tagged EC2 instance with an EBS volume. @@ -29,6 +29,7 @@ subcommands: modify Change an instance's type. start Start EC2 instance. stop Stop EC2 instance. + rename Rename EC2 instance(s). tag Tag EC2 instance(s). tags List EC2 instances or volumes with their tags. status Describe instances status checks. @@ -76,10 +77,10 @@ cog.out(f"```\n{docs('aec ec2 describe', ec2.describe(config))}\n```") ``` aec ec2 describe - InstanceId State Name Type DnsName LaunchTime ImageId - ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── - i-cf1d572b090e417ef running alice t3.small ec2-54-214-212-170.compute-1.amazonaws.com 2023-05-27 03:17:09+00:00 ami-03cf127a - i-0cbc08830d8779f3d running sam t3.small ec2-54-214-129-94.compute-1.amazonaws.com 2023-05-27 03:17:10+00:00 ami-03cf127a + InstanceId State Name Type DnsName LaunchTime ImageId + ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── + i-f5e6d94c9e2c3afc2 running alice t3.small ec2-54-214-149-72.compute-1.amazonaws.com 2024-01-09 03:17:41+00:00 ami-03cf127a + i-94b729fbf6d605870 running sam t3.small ec2-54-214-85-61.compute-1.amazonaws.com 2024-01-09 03:17:41+00:00 ami-03cf127a ``` @@ -117,8 +118,8 @@ aec ec2 describe -c Name,SubnetId,Volumes,Image.CreationDate Name SubnetId Volumes Image.CreationDate ────────────────────────────────────────────────────────────────────── - alice subnet-3f674ae5 ['Size=15 GiB'] 2023-05-27T03:17:09.000Z - sam subnet-3f674ae5 ['Size=15 GiB'] 2023-05-27T03:17:09.000Z + alice subnet-35665c57 ['Size=15 GiB'] 2024-01-09T03:17:41.000Z + sam subnet-35665c57 ['Size=15 GiB'] 2024-01-09T03:17:41.000Z ``` @@ -162,6 +163,12 @@ aec ec2 tag alice -t Project="top secret" -t keep=forever i-0f7f6a072d985fd2d alice top secret forever ``` +Rename an instance + +``` +aec ec2 rename alice alice2 +``` + Show output as csv instead of a table (works with any command) ``` diff --git a/docs/ssm.md b/docs/ssm.md index 3a1cd515..9928bec1 100644 --- a/docs/ssm.md +++ b/docs/ssm.md @@ -16,7 +16,7 @@ optional arguments: subcommands: {commands,compliance-summary,describe,invocations,output,patch,patch-summary,run} commands List commands by instance. - compliance-summary Compliance summary for running instances that have run the patch baseline. + compliance-summary Compliance summary for instances that have run the patch baseline. describe List running instances with the SSM agent. invocations List invocations of a command across instances. output Fetch output of a command from S3. diff --git a/src/aec/command/ec2.py b/src/aec/command/ec2.py index 42781b07..8d87abdd 100755 --- a/src/aec/command/ec2.py +++ b/src/aec/command/ec2.py @@ -313,6 +313,26 @@ def describe_tags( return instance_tags(config, ident, name_match, keys) +def rename( + config: Config, + ident: str, + new_name: str, +) -> list[Instance]: + """Rename EC2 instance(s).""" + ec2_client = boto3.client("ec2", region_name=config.get("region", None)) + + instances = describe(config, ident, include_terminated=True) + + ids = [i["InstanceId"] for i in instances] + + if not ids: + raise NoInstancesError(name=ident) + + ec2_client.create_tags(Resources=ids, Tags=[{"Key": "Name", "Value": new_name}]) + + return describe(config, new_name, include_terminated=True) + + def tag( config: Config, ident: str | None = None, diff --git a/src/aec/command/ssm.py b/src/aec/command/ssm.py index 6b1fa5b7..c965fc4f 100644 --- a/src/aec/command/ssm.py +++ b/src/aec/command/ssm.py @@ -93,7 +93,7 @@ def patch_summary(config: Config) -> Iterator[dict[str, Any]]: def compliance_summary(config: Config) -> list[dict[str, Any]]: - """Compliance summary for running instances that have run the patch baseline.""" + """Compliance summary for instances that have run the patch baseline.""" instances_names = describe_instances_names(config) client = boto3.client("ssm", region_name=config.get("region", None)) diff --git a/src/aec/main.py b/src/aec/main.py index 1cc564c4..11ce3f8d 100644 --- a/src/aec/main.py +++ b/src/aec/main.py @@ -86,6 +86,11 @@ def tag_arg_checker(tag: str) -> str: config_arg, Arg("ident", type=str, help="Name tag of instance or instance id") ]), + Cmd(ec2.rename, [ + config_arg, + Arg("ident", type=str, help="Name tag of instance or instance id"), + Arg("new_name", type=str, help="New name"), + ]), Cmd(ec2.tag, [ config_arg, Arg("ident", type=str, nargs="?", help="Filter to instances with this Name tag or instance id."), diff --git a/tests/test_ec2.py b/tests/test_ec2.py index 15590df1..587a9a13 100644 --- a/tests/test_ec2.py +++ b/tests/test_ec2.py @@ -19,6 +19,7 @@ launch, logs, modify, + rename, start, status, stop, @@ -282,6 +283,15 @@ def describe_instance0(region_name: str, instance_id: str): return instances["Reservations"][0]["Instances"][0] +def test_rename(mock_aws_config: Config): + launch(mock_aws_config, "alice", ami_id) + + instances = rename(mock_aws_config, "alice", "alice2") + + assert len(instances) == 1 + assert instances[0]["Name"] == "alice2" + + def test_tag(mock_aws_config: Config): launch(mock_aws_config, "alice", ami_id)