Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change in the strategy to report instances #547

Merged
merged 1 commit into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
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
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