[go: up one dir, main page]

Menu

[bbac43]: / docker / aws.py  Maximize  Restore  History

Download this file

36 lines (25 with data), 1.2 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import os
import json
import re
from urllib.request import urlopen
def remove_special_chars(input_string):
return re.sub(r'[^a-zA-Z0-9_]', '', input_string)
def fetch_container_metadata(container_metadata_url, enpoint="taskWithTags"):
with urlopen(container_metadata_url + "/" + enpoint) as response:
body = response.read()
return json.loads(body)
def write_dotenv(tags, dotenv_path=os.getenv("DOTENV_PATH", os.path.curdir)):
with open(os.path.join(dotenv_path, ".env"), 'w') as env_file:
for key, value in tags.items():
env_file.write(remove_special_chars(key) + "=" + value+"\n")
def load_env_from_tag():
# https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v4.html
container_metadata_url = os.getenv("ECS_CONTAINER_METADATA_URI_V4", None)
if container_metadata_url is not None:
try:
container_metadata = fetch_container_metadata(container_metadata_url)
write_dotenv(container_metadata.get('TaskTags', {}))
except Exception as e:
print("Error when requesting or parsing aws metadata")
if __name__ == '__main__':
load_env_from_tag()