Last active
August 29, 2015 14:16
-
-
Save peoplemerge/53cae713a4e7f1476c51 to your computer and use it in GitHub Desktop.
Map Amazon Elastic Container Service (ECS) tasks to the hosts by tag.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import boto | |
import boto.ec2 | |
cluster_name='default' | |
ecs_conn=boto.connect_ec2containerservice(host='ecs.us-west-2.amazonaws.com') | |
task_response = ecs_conn.list_tasks(cluster=cluster_name) | |
tasks = task_response['ListTasksResponse']['ListTasksResult']['taskArns'] | |
task_details_response = ecs_conn.describe_tasks(cluster=cluster_name, tasks=tasks) | |
task_details_list = task_details_response['DescribeTasksResponse']['DescribeTasksResult']['tasks'] | |
iarn_task_definitions = [ (task_details['containerInstanceArn'], task_details['taskArn'], task_details['taskDefinitionArn'] ) for task_details in task_details_list ] | |
#now that we have iarn, get the instance names for them | |
instances = ecs_conn.list_container_instances(cluster=cluster_name) | |
arns_list = instances['ListContainerInstancesResponse']['ListContainerInstancesResult']['containerInstanceArns'] | |
# The following code doesn't work because the AWS interface is busted | |
#arns_str = ' '.join(arns_list) | |
#container_instances = ecs_conn.describe_container_instances(arns_str, cluster=cluster_name) | |
container_instances_resp = [ (arn, ecs_conn.describe_container_instances(arn, cluster=cluster_name)) for arn in arns_list ] | |
container_instances = [ (inst['ec2InstanceId'],arn) for (arn, container) in container_instances_resp for inst in container['DescribeContainerInstancesResponse']['DescribeContainerInstancesResult']['containerInstances'] ] | |
ec2_conn = boto.ec2.connect_to_region('us-west-2') | |
# | |
instances = [(container_instance,i,arn) for (container_instance, arn) in container_instances for r in ec2_conn.get_all_instances(instance_ids=container_instance) for i in r.instances] | |
iarns_instances_names = [(arn,(container_instance,instance.tags['Name'])) for (container_instance,instance,arn) in instances ] | |
instances_dict = dict(iarns_instances_names) | |
# We have both iarn_task_definition and instances_names_iarns, so join on iarn for a mega-tuple | |
for (iarn,task,definition) in iarn_task_definitions: | |
(container_instance,instance_name) = instances_dict[iarn] | |
print("%(iarn)s %(task)s %(definition)s %(container_instance)s %(instance_name)s" % locals()) | |
print("\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"_comment": "this example assumes a nice fat EBS volume mounted on /srv/volumes/solr, and a tag named whatever is in your family", | |
"family": "solr-server1", | |
"containerDefinitions": [ | |
{ | |
"name": "solr", | |
"image": "makuk66/docker-solr", | |
"essential": true, | |
"portMappings": [ | |
{ | |
"containerPort": 8983, | |
"hostPort": 8983 | |
} | |
], | |
"command": [ | |
"/bin/bash", | |
"-c", | |
"/opt/solr/bin/solr -c -h YOUR_HOSTNAME_HERE -m 6g -f -s /srv/volumes/solr -z YOUR_ZKSERVER1_HERE:2181,YOUR_ZKSERVER2_HERE:2181,YOUR_ZKSERVER3_HERE:2181,YOUR_ZKSERVER4_HERE:2181,YOUR_ZKSERVER5_HERE:2181" | |
], | |
"environment": [ | |
], | |
"mountPoints" : [ | |
{ | |
"sourceVolume": "solr", | |
"containerPath": "/srv/volumes/solr" | |
} | |
], | |
"memory": 12000, | |
"cpu": 256 | |
} | |
], | |
"volumes": [ | |
{ | |
"name": "solr", | |
"host": { | |
"sourcePath": "/srv/volumes/solr" | |
} | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
apt-get update | |
#see if we should format the volume | |
if [[ "`sudo file -s /dev/xvdf`" == '/dev/xvdf: data' ]]; then sudo mkfs.ext4 /dev/xvdf ;fi | |
#mount volume | |
mkdir -p /srv/volumes/solr | |
mount /dev/xvdf /srv/volumes/solr | |
#install eni utility - doesn't exist on Ubuntu | |
#get docker (insecurely) | |
apt-get install -y build-essential | |
curl -sSL https://get.docker.com/ubuntu/ | sudo sh | |
#start ecs container agent | |
mkdir /etc/ecs | |
echo ECS_CLUSTER=default >> /etc/ecs/ecs.config | |
docker run --name ecs-agent -d -v /var/run/docker.sock:/var/run/docker.sock -v /var/log/ecs/:/log -p 127.0.0.1:51678:51678 -v /var/lib/ecs/data:/data -e ECS_LOGFILE=/log/ecs-agent.log -e ECS_LOGLEVEL=info -e ECS_DATADIR=/data -e ECS_CLUSTER=default amazon/amazon-ecs-agent:latest | |
#mkdir /srv/volumes/solr/... | |
#run apt again to encourage a delay for the instance to finish registering | |
apt-get install -y python-pip | |
pip install awscli boto | |
python - <<END | |
import json | |
import urllib2 | |
import boto.utils | |
import boto.ec2 | |
response = urllib2.urlopen('http://localhost:51678/v1/metadata').read() | |
meta = json.loads(response) | |
container_instance = meta['ContainerInstanceArn'] | |
cluster_name='default' | |
instance_id = boto.utils.get_instance_metadata()['instance-id'] | |
ecs_conn=boto.connect_ec2containerservice(host='ecs.us-west-2.amazonaws.com') | |
ec2_conn = boto.ec2.connect_to_region('us-west-2') | |
reservation = ec2_conn.get_all_instances(instance_ids=instance_id) | |
if 'start-docker-tasks' in reservation[0].instances[0].tags: | |
for task in reservation[0].instances[0].tags['start-docker-tasks'].split(): | |
started = ecs_conn.start_task(task_definition=task, container_instances=[container_instance], cluster=cluster_name) | |
print(task + " " + container_instance + " " + cluster_name) | |
print(started) | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment