From 96241b049bdb83ec048775a7094d1d114317c353 Mon Sep 17 00:00:00 2001 From: YaqinLi Date: Tue, 6 Feb 2024 22:30:57 +0000 Subject: [PATCH] fetch ec2 tags from aws instead of from facter command --- deploy-agent/deployd/client/client.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/deploy-agent/deployd/client/client.py b/deploy-agent/deployd/client/client.py index c25ac5ccf2..76eff87629 100644 --- a/deploy-agent/deployd/client/client.py +++ b/deploy-agent/deployd/client/client.py @@ -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 @@ -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