Skip to content
This repository has been archived by the owner on Apr 5, 2022. It is now read-only.

Commit

Permalink
Merge pull request #5 from hellofresh/patch/entry-script
Browse files Browse the repository at this point in the history
Entry script adjustments
  • Loading branch information
Anton Ustyuzhanin authored Jul 3, 2019
2 parents b48d61b + a26fc71 commit 70d2027
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ RUN apt-get update -y \
python3-boto3 \
python3-yaml \
&& rm -rf /var/cache/apt/* \
&& useradd -ms /bin/bash rds_exporter
&& useradd -ms /bin/bash rds_exporter \
&& mkdir /rds_exporter \
&& chown rds_exporter:rds_exporter /rds_exporter

USER rds_exporter

COPY --from=builder /go/src/github.com/hellofresh/rds_exporter/rds_exporter /
COPY --from=builder /go/src/github.com/hellofresh/rds_exporter/rds_exporter /rds_exporter/rds_exporter
COPY entry.py /

EXPOSE 9042
Expand Down
37 changes: 26 additions & 11 deletions entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
import subprocess


REGION = 'eu-west-1'
AWS_REGION = os.getenv('AWS_REGION', 'eu-west-1')
ENVIRONMENT = os.getenv('ENVIRONMENT', 'staging')

client = boto3.client('rds', region_name=AWS_REGION)

def get_db_instances(region, page_size=20):
client = boto3.client('rds', region_name=region)
def get_db_instances(page_size=20):
marker = ""
pool = []
while True:
Expand All @@ -27,20 +27,35 @@ def get_db_instances(region, page_size=20):
pool = result.get("DBInstances")


def get_rds_tags(instance_arn):
response = client.list_tags_for_resource(
ResourceName=instance_arn
)
return { tag['Key']: tag['Value'] for tag in response['TagList'] }


def is_applicable(instance):
instance_name = instance['DBInstanceIdentifier']
instance_arn = instance['DBInstanceArn']
instance_tags = get_rds_tags(instance_arn)
instance_enhanced_monitoring_arn = instance.get('EnhancedMonitoringResourceArn', None)
instance_environment_tag = instance_tags.get('Environment', '')
return instance_enhanced_monitoring_arn and (instance_name.lower().endswith(ENVIRONMENT.lower()) or instance_environment_tag.lower() == ENVIRONMENT.lower())


if __name__ == "__main__":
em_enabled_instances = {'instances': []}

for instance in get_db_instances(REGION):
instance_name = instance['DBInstanceIdentifier']
if (instance.get('EnhancedMonitoringResourceArn', None) and
instance_name.endswith(ENVIRONMENT)):
instance = {'instance': instance_name, 'region': REGION}
for instance in get_db_instances():
if is_applicable(instance):
instance_name = instance['DBInstanceIdentifier']
instance = {'instance': instance_name, 'region': AWS_REGION}
em_enabled_instances['instances'].append(instance)

with open('/config.yml', 'w') as yaml_file:
with open('/rds_exporter/config.yml', 'w') as yaml_file:
yaml.dump(em_enabled_instances, yaml_file, default_flow_style=False)

rds_exporter = '/rds_exporter \
--config.file=/config.yml'
rds_exporter = '/rds_exporter/rds_exporter \
--config.file=/rds_exporter/config.yml'

subprocess.run(rds_exporter, shell=True)

0 comments on commit 70d2027

Please sign in to comment.