Skip to content

Commit

Permalink
fetch ec2 tags from aws instead of from facter command
Browse files Browse the repository at this point in the history
  • Loading branch information
liyaqin1 committed Feb 6, 2024
1 parent cda241c commit 96241b0
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions deploy-agent/deployd/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import socket
import traceback
import json
import boto3

from deployd.client.base_client import BaseClient
from deployd.client.restfulclient import RestfulClient
Expand Down Expand Up @@ -171,11 +172,22 @@ def _read_host_info(self):
# so need to read ec2_tags from facter and parse Autoscaling tag to cover this case
if not self._autoscaling_group:
ec2_tags = facter_data.get(ec2_tags_key)
if ec2_tags:
ec2_tags['availability_zone'] = self._availability_zone
self._ec2_tags = json.dumps(ec2_tags) if ec2_tags else None
self._autoscaling_group = ec2_tags.get(asg_tag_key) if ec2_tags else None

# Obtain all EC2 tags, inclusive of those that are outside the scope of the 'facter' command.
self._ec2_tags = None
try:
ec2 = boto3.resource('ec2', region_name='us-east-1')
ec2instance = ec2.Instance(self._id)
all_tags = {}
for tag in ec2instance.tags:
all_tags[tag["Key"]] = tag["Value"]
all_tags["availability_zone"] = self._availability_zone
self._ec2_tags = json.dumps(all_tags)
except Exception:
log.warn("Failed to get host {} tags.".format(self._id))
pass

if not self._stage_type and not self._stage_type_fetched:
self._stage_type = facter_data.get(stage_type_key, None)
self._stage_type_fetched = True
Expand Down

0 comments on commit 96241b0

Please sign in to comment.