Skip to content

Commit

Permalink
change in the strategy to report instances (#547)
Browse files Browse the repository at this point in the history
Use `Name` tag instead of `aws:cloudformation:stack-name` to filter instances.
  • Loading branch information
lmineiro authored and jmcs committed Oct 26, 2018
1 parent a2142b5 commit c429e24
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
55 changes: 30 additions & 25 deletions senza/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1271,43 +1271,48 @@ def instances(
if all:
filters = []
else:
# filter out instances not part of any stack
filters = [{"Name": "tag-key", "Values": ["aws:cloudformation:stack-name"]}]
# filter out instances without a Name tag
filters = [{"Name": "tag-key", "Values": ["Name"]}]

# Semantically equivalent to the less efficient client side filtering on != TERMINATED
if not terminated:
filters.append(
{"Name": "instance-state-name", "Values": ["pending", "running", "shutting-down", "stopping", "stopped"]})

opt_docker_column = " docker_source" if docker_image else ""

for _ in watching(w, watch):
rows = []

for instance in ec2.instances.filter(Filters=filters):
cf_stack_name = get_tag(instance.tags, "aws:cloudformation:stack-name")
cf_stack_name = get_tag(instance.tags, "Name")
stack_name = get_tag(instance.tags, "StackName")
stack_version = get_tag(instance.tags, "StackVersion")
if not stack_refs or matches_any(cf_stack_name, stack_refs):
instance_health = get_instance_health(cf_stack_name, region)
if instance.state["Name"].upper() != "TERMINATED" or terminated:
docker_source = (
get_instance_docker_image_source(instance)
if docker_image
else ""
)

rows.append(
{
"stack_name": stack_name or "",
"version": stack_version or "",
"resource_id": get_tag(
instance.tags, "aws:cloudformation:logical-id"
),
"instance_id": instance.id,
"public_ip": instance.public_ip_address,
"private_ip": instance.private_ip_address,
"state": instance.state["Name"].upper().replace("-", "_"),
"lb_status": instance_health.get(instance.id),
"docker_source": docker_source,
"launch_time": instance.launch_time.timestamp(),
}
)
docker_source = (
get_instance_docker_image_source(instance)
if docker_image
else ""
)

rows.append(
{
"stack_name": stack_name or "",
"version": stack_version or "",
"resource_id": get_tag(
instance.tags, "aws:cloudformation:logical-id"
),
"instance_id": instance.id,
"public_ip": instance.public_ip_address,
"private_ip": instance.private_ip_address,
"state": instance.state["Name"].upper().replace("-", "_"),
"lb_status": instance_health.get(instance.id),
"docker_source": docker_source,
"launch_time": instance.launch_time.timestamp()
}
)

rows.sort(key=lambda r: (r["stack_name"], r["version"], r["instance_id"]))

Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ def my_resource(rtype, *args):
instance.public_ip_address = '8.8.8.8'
instance.private_ip_address = '10.0.0.1'
instance.state = {'Name': 'Test-instance'}
instance.tags = [{'Key': 'aws:cloudformation:stack-name', 'Value': 'test-1'},
instance.tags = [{'Key': 'Name', 'Value': 'test-1'},
{'Key': 'aws:cloudformation:logical-id', 'Value': 'local-id-123'},
{'Key': 'StackName', 'Value': 'test'},
{'Key': 'StackVersion', 'Value': '1'}]
Expand Down

0 comments on commit c429e24

Please sign in to comment.