-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecs-get-port-mapping.py
98 lines (66 loc) · 2.68 KB
/
ecs-get-port-mapping.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/python3
import os
import boto.utils
import boto3
import requests
import datetime
import time
def get_contents(filename):
with open(filename) as f:
return f.read()
def get_ecs_introspection_url(resource):
# 172.17.0.1 is the docker network bridge ip
return 'http://172.17.0.1:51678/v1/' + resource
def contains_key(d, key):
return key in d and d[key] is not None
def get_local_container_info():
# get the docker container id
# http://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-introspection.html
docker_id = os.path.basename(get_contents("/proc/1/cpuset")).strip()
if docker_id is None:
raise Exception("Unable to find docker id")
ecs_local_task = requests.get(get_ecs_introspection_url('tasks') + '?dockerid=' + docker_id).json()
task_arn = ecs_local_task['Arn']
if task_arn is None:
raise Exception("Unable to find task arn for container %s in ecs introspection api" % docker_id)
ecs_local_container = None
if contains_key(ecs_local_task, 'Containers'):
for c in ecs_local_task['Containers']:
if c['DockerId'] == docker_id:
ecs_local_container = c
if ecs_local_container is None:
raise Exception("Unable to find container %s in ecs introspection api" % docker_id)
return ecs_local_container['Name'], task_arn
def main():
metadata = boto.utils.get_instance_metadata()
region = metadata['placement']['availability-zone'][:-1] # last char is the zone, which we don't care about
ecs_metadata = requests.get(get_ecs_introspection_url('metadata')).json()
cluster = ecs_metadata['Cluster']
container_name, task_arn = get_local_container_info()
# Get the container info from ECS. This will give us the port mappings
ecs = boto3.client('ecs', region_name=region)
response = ecs.describe_tasks(
cluster=cluster,
tasks=[
task_arn,
]
)
task = None
if contains_key(response, 'tasks'):
for t in response['tasks']:
if t['taskArn'] == task_arn:
task = t
if task is None:
raise Exception("Unable to locate task %s" % task_arn)
container = None
if contains_key(task, 'containers'):
for c in task['containers']:
if c['name'] == container_name:
container = c
if container is None:
raise Exception("Unable to find ecs container %s" % container_name)
if contains_key(container, 'networkBindings'):
for b in container['networkBindings']:
print("export PORT_%s_%d=%d;" % (b['protocol'].upper(), b['containerPort'], b['hostPort']))
if __name__ == '__main__':
main()