Skip to content

Instantly share code, notes, and snippets.

@chris-smith-zocdoc
Created December 19, 2016 18:59
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save chris-smith-zocdoc/126db78651046c67ac66dbd87393b1dc to your computer and use it in GitHub Desktop.
Save chris-smith-zocdoc/126db78651046c67ac66dbd87393b1dc to your computer and use it in GitHub Desktop.
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()
#!/bin/sh
set -o errexit
set -o xtrace
export EC2_HOST=$(wget -O - http://169.254.169.254/latest/meta-data/local-ipv4 2> /dev/null)
result="$(python /opt/bin/ecs-get-port-mapping.py)"
eval "$result"
@svperfecta
Copy link

svperfecta commented Dec 21, 2016

Thanks very much for posting this! I'm not completely clear how to invoke it though. Do you guys fork the official Mashape Kong and replace the current entrypoint? Or do you upload this to your base ami and specify this in the entrypoint in your container-definition

@chris-smith-zocdoc
Copy link
Author

@GenExp I'm not familiar with Kong, but our container specifies entry_point.sh as the entrypoint. These scripts are part of our container image.

@dgs-genzt
Copy link

Hi,
I am trying to do a port resolve too for selenium grid in ECS. and i have a few questions.

  1. your entry_point.sh does not have REMOTE_HOST set nor is it running the xvfb-run command to invoke the browser in the container. Can you give a little detail on this please or how your python code works ??
    2)should i install python in the container??

@chris-smith-zocdoc
Copy link
Author

chris-smith-zocdoc commented Feb 11, 2017

@dgs-genzt Ah yes I left that out because this script isn't specific to Selenium Grid, its useful for many applications.

Yes you'll need python inside the container if you want to invoke the python script.

The rest of the script for selenium grid would be

export REMOTE_HOST="http://$EC2_HOST:$PORT_TCP_5555"
exec /opt/bin/entry_point.sh

The specific variables you need to set vary depending on version. My docker file is based on selenium/node-chrome-debug:2.53.1-beryllium. I know the variables changed in Grid 3.

@mwedgwood-rmn
Copy link

@chris-smith-zocdoc Is this Gist free for others to use? If so, would you mind adding a license file?

@robertoriv
Copy link

robertoriv commented Oct 27, 2017

@chris-smith-zocdoc my team is working on open-sourcing a Selenium on ECS reference implementation. We'd like to include a slightly-modified version of this gist as part of our code/images. Is that ok?

@chris-smith-zocdoc
Copy link
Author

Sure, consider it under Apache 2. I'll add a file in a couple days when I'm at a computer

https://www.apache.org/licenses/LICENSE-2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment