Skip to content

Commit

Permalink
feat: rename instance (#444)
Browse files Browse the repository at this point in the history
eg:
```
aec ec2 rename alice alice2
```
  • Loading branch information
tekumara committed Jan 14, 2024
1 parent baf011c commit 7243170
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 12 deletions.
4 changes: 2 additions & 2 deletions docs/ami.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
<!-- [[[end]]] -->

Expand Down
23 changes: 15 additions & 8 deletions docs/ec2.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@ 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.
logs Show the system logs.
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.
Expand Down Expand Up @@ -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
```
<!-- [[[end]]] -->

Expand Down Expand Up @@ -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
```
<!-- [[[end]]] -->

Expand Down Expand Up @@ -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)

```
Expand Down
2 changes: 1 addition & 1 deletion docs/ssm.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions src/aec/command/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/aec/command/ssm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 5 additions & 0 deletions src/aec/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."),
Expand Down
10 changes: 10 additions & 0 deletions tests/test_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
launch,
logs,
modify,
rename,
start,
status,
stop,
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 7243170

Please sign in to comment.