Skip to content

Instantly share code, notes, and snippets.

@deadanon
Last active July 5, 2017 20:45
Show Gist options
  • Save deadanon/c384211d419062cb25bed041b2579c83 to your computer and use it in GitHub Desktop.
Save deadanon/c384211d419062cb25bed041b2579c83 to your computer and use it in GitHub Desktop.
import boto3
import json
import os
import subprocess
boto_s3c = boto3.client('s3')
boto_s3 = boto3.resource('s3')
boto_sqs = boto3.client('sqs', region_name='us-east-1')
boto_r53d = boto3.client('route53domains', region_name='us-east-1')
boto_r53 = boto3.client('route53')
boto_elb = boto3.client('elb', region_name='us-east-1')
def download_dir(client, resource, dist, local='/tmp', bucket='your_bucket'):
paginator = client.get_paginator('list_objects')
for result in paginator.paginate(Bucket=bucket, Delimiter='/', Prefix=dist):
if result.get('CommonPrefixes') is not None:
for subdir in result.get('CommonPrefixes'):
download_dir(client, resource, subdir.get('Prefix'), local, bucket)
if result.get('Contents') is not None:
for file in result.get('Contents'):
if not os.path.exists(os.path.dirname(local + os.sep + file.get('Key'))):
os.makedirs(os.path.dirname(local + os.sep + file.get('Key')))
resource.meta.client.download_file(bucket, file.get('Key'), local + os.sep + file.get('Key'))
def mkELB(port):
elb_name = "WP-P-%s" % str(port)
response = boto_elb.create_load_balancer(
Listeners=[
{
'InstancePort': int(port),
'InstanceProtocol': 'HTTP',
'LoadBalancerPort': 80,
'Protocol': 'HTTP',
},
],
LoadBalancerName=elb_name,
SecurityGroups=[
'sg-80b705f1',
],
Subnets=[
'subnet-e13f1684',
'subnet-e23bfdce',
],
)
response2 = boto_elb.register_instances_with_load_balancer(
LoadBalancerName=elb_name,
Instances=[
{
'InstanceId': 'TRAF_PRO_INSTANCE_ID'
},
]
)
return response
def _main():
sqs_msg = boto_sqs.receive_message(QueueUrl="https://sqs.us-east-1.amazonaws.com/974658918049/trafpro_domains",MaxNumberOfMessages=1)
content = sqs_msg['Messages'][0]
body = json.loads(content['Body'])
msg_id = content['ReceiptHandle']
op = boto_r53d.get_operation_detail(OperationId=body['OpID'])
if op['Status'] == "SUCCESSFUL":
boto_sqs.delete_message(QueueUrl="https://sqs.us-east-1.amazonaws.com/974658918049/trafpro_domains",ReceiptHandle=msg_id)
site = "sites/%s/" % body['DomainName']
download_dir(boto_s3c, boto_s3, site, "./", "trafpro.com")
sub_cmd = "ecs-cli compose --project-name WP-P-%s --file /home/ec2-user/sites/%s/docker-compose.yml up" % (body['Port'],body['DomainName'])
os.system(sub_cmd)
#return "output: ", test1
#{u'DNSName': 'WP-P-169-325949982.us-east-1.elb.amazonaws.com', 'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': '5461f7e5-5cfc-11e7-9c28-41929053f5d3', 'HTTPHeaders': {'x-amzn-requestid': '5461f7e5-5cfc-11e7-9c28-41929053f5d3', 'date': 'Thu, 29 Jun 2017 18:54:07 GMT', 'content-length': '361', 'content-type': 'text/xml'}}}
ELB = mkELB(body['Port'])
create_website_records(ELB['DNSName'],body['DomainName'])
#return mkELB(body['Port'])
def create_website_records(elb,domain):
raw_cmd = "aws route53 list-hosted-zones --max-items 100 --query \"HostedZones[?Name=='%s.']\"" % domain
#print raw_cmd
r53_data = subprocess.check_output(raw_cmd, shell=True)
#print r53_data
#print type(r53_data)
r53_data = json.loads(r53_data)
#print r53_data
#print type(r53_data)
r53_data = r53_data[0]
#print r53_data
#print type(r53_data)
#print r53_data
#print type(r53_data)
#print r53_data["Id"]
#records = boto_r53.get_hosted_zone(Id=r53_data['Id'])
response = boto_r53.change_resource_record_sets(
HostedZoneId=r53_data['Id'],
ChangeBatch={
'Changes': [
{
'Action': 'CREATE',
'ResourceRecordSet': {
'Name': domain,
'Type': 'A',
'AliasTarget': {
'HostedZoneId': "Z35SXDOTRQ7X7K",
'DNSName': elb,
'EvaluateTargetHealth': False
},
}
},
]
}
)
#change = records.add_change('CREATE', zone.name, 'A',alias_hosted_zone_id='Z3AQBSTGFYJSTF',alias_dns_name='s3-website-us-east-1.amazonaws.com')
#change.add_value('ALIAS s3-website-us-east-1.amazonaws.com (Z3AQBSTGFYJSTF)')
#change = records.add_change('CREATE', 'www.' + zone.name, 'CNAME')
#change.add_value('%s' % buckets['www'].get_website_endpoint())
#records.commit()
return response
#print(_main())
print(_main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment