deploy.py working now

pull/37/head
Minh Doan 6 years ago
parent 488e35eb8e
commit 6b4a760105
  1. 113
      aws-experiment-launch/deploy.py

@ -1,12 +1,14 @@
import boto3
import argparse import argparse
import sys import base64
import boto3
import datetime
import json import json
import os
import sys
import time import time
import datetime
from threading import Thread
from Queue import Queue from Queue import Queue
import base64 from threading import Thread
from utils import utils from utils import utils
@ -51,50 +53,15 @@ def run_one_region_codedeploy(region_number, region_config, node_name_tag, commi
codedeploy = session.client('codedeploy') codedeploy = session.client('codedeploy')
application_name = APPLICATION_NAME application_name = APPLICATION_NAME
deployment_group = APPLICATION_NAME + "-" + str(commit_id)[6] + "-" + CURRENT_SESSION deployment_group = APPLICATION_NAME + "-" + commit_id[:6] + "-" + CURRENT_SESSION
repo = REPO repo = REPO
print("Setting up to deploy commit_id %s on region %s"%(commit_id,region_number)) print("Setting up to deploy commit_id %s on region %s" % (commit_id, region_number))
response = get_application(codedeploy, application_name) utils.get_application(codedeploy, application_name)
deployment_group = get_deployment_group( deployment_group = utils.create_deployment_group(
codedeploy, region_number, application_name, deployment_group) codedeploy, region_number, application_name, deployment_group, node_name_tag)
depId = deploy(codedeploy, application_name, deployment_id = deploy(codedeploy, application_name, deployment_group, repo, commit_id)
deployment_group, repo, commit_id) return region_number, deployment_id
return region_number, depId
def get_deployment_group(codedeploy, region_number,application_name, deployment_group):
NODE_NAME = region_number + "-" + NODE_NAME_SUFFIX
response = codedeploy.create_deployment_group(
applicationName=application_name,
deploymentGroupName=deployment_group,
deploymentConfigName='CodeDeployDefault.AllAtOnce',
serviceRoleArn='arn:aws:iam::656503231766:role/BenchMarkCodeDeployServiceRole',
deploymentStyle={
'deploymentType': 'IN_PLACE',
'deploymentOption': 'WITHOUT_TRAFFIC_CONTROL'
},
ec2TagFilters = [
{
'Key': 'Name',
'Value': NODE_NAME,
'Type': 'KEY_AND_VALUE'
}
]
)
return deployment_group
def get_application(codedeploy, application_name):
response = codedeploy.list_applications()
if application_name in response['applications']:
return response
else:
response = codedeploy.create_application(
applicationName=application_name,
computePlatform='Server'
)
return response
def deploy(codedeploy, application_name, deployment_group, repo, commit_id): def deploy(codedeploy, application_name, deployment_group, repo, commit_id):
@ -115,39 +82,39 @@ def deploy(codedeploy, application_name, deployment_group, repo, commit_id):
'revisionType': 'GitHub', 'revisionType': 'GitHub',
'gitHubLocation': { 'gitHubLocation': {
'repository': repo, 'repository': repo,
'commit_id': commit_id, 'commitId': commit_id,
} }
} }
) )
depId = res["deploymentId"] deployment_id = res["deploymentId"]
print("Deployment ID: " + depId) print("Deployment ID: " + deployment_id)
# The deployment is launched at this point, so exit unless asked to wait # The deployment is launched at this point, so exit unless asked to wait
# until it finishes # until it finishes
info = {'status': 'Created'} info = {'status': 'Created'}
start = time.time() start = time.time()
while info['status'] not in ('Succeeded', 'Failed', 'Stopped',) and (time.time() - start < 600.0): while info['status'] not in ('Succeeded', 'Failed', 'Stopped',) and (time.time() - start < 600.0):
info = codedeploy.get_deployment(deploymentId=depId)['deploymentInfo'] info = codedeploy.get_deployment(deploymentId=deployment_id)['deploymentInfo']
print(info['status']) print(info['status'])
time.sleep(15) time.sleep(15)
if info['status'] == 'Succeeded': if info['status'] == 'Succeeded':
print("\nDeploy Succeeded") print("\nDeploy Succeeded")
return depId
else: else:
print("\nDeploy Failed") print("\nDeploy Failed")
print(info) print(info)
return depId return deployment_id
def run_one_region_codedeploy_wrapper(region_number, commit_id, queue): def run_one_region_codedeploy_wrapper(region_number, region_config, node_name_tag, commit_id, queue):
region_number, depId = run_one_region_codedeploy(region_number, commit_id) region_number, deployment_id = run_one_region_codedeploy(region_number, region_config, node_name_tag, commit_id)
queue.put((region_number, depId)) queue.put((region_number, deployment_id))
def launch_code_deploy(region_list, commit_id): def launch_code_deploy(region_list, region_config, commit_id):
queue = Queue() queue = Queue()
jobs = [] jobs = []
for i in range(len(region_list)): for region_tuppple in region_list:
region_number = region_list[i] # node_name_tag comes first.
node_name_tag, region_number = region_tuppple
my_thread = Thread(target=run_one_region_codedeploy_wrapper, args=( my_thread = Thread(target=run_one_region_codedeploy_wrapper, args=(
region_number, commit_id, queue)) region_number, region_config, node_name_tag, commit_id, queue))
my_thread.start() my_thread.start()
jobs.append(my_thread) jobs.append(my_thread)
for my_thread in jobs: for my_thread in jobs:
@ -158,21 +125,21 @@ def launch_code_deploy(region_list, commit_id):
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description='This script helps you start instances across multiple regions') description='This script helps you start instances across multiple regions')
parser.add_argument('--regions', type=str, dest='regions', parser.add_argument('--instance_output', type=str, dest='instance_output',
default='3', help="Supply a csv list of all regions") default='instance_output.txt',
parser.add_argument('--instances', type=str, dest='numInstances', help='the file contains node_name_tag and region number of created instances.')
default='1', help='number of instances') parser.add_argument('--region_config', type=str, dest='region_config', default='configuration.txt')
parser.add_argument('--configuration', type=str,
dest='config', default='configuration.txt')
parser.add_argument('--commit_id', type=str, dest='commit_id', parser.add_argument('--commit_id', type=str, dest='commit_id',
default='1f7e6e7ca7cf1c1190cedec10e791c01a29971cf') default='1f7e6e7ca7cf1c1190cedec10e791c01a29971cf')
args = parser.parse_args() args = parser.parse_args()
config = utils.read_region_config(args.config)
region_list = [item.strip() for item in args.regions.split(',')]
instances_list = [item.strip() for item in args.numInstances.split(',')]
assert len(region_list) == len(instances_list), "number of regions: %d != number of instances per region: %d" % (
len(region_list), len(instances_list))
commit_id = args.commit_id commit_id = args.commit_id
results = launch_code_deploy(region_list, commit_id) if not os.path.isfile(args.instance_output) or not commit_id:
print(results) print "%s does not exist" % args.instance_output
sys.exit(1)
with open(args.instance_output, "r") as fin:
region_list = [line.split(" ") for line in fin.readlines()]
region_list = [(item[0].strip(), item[1].strip()) for item in region_list]
results = launch_code_deploy(region_list, args.region_config, commit_id)
print(results)

Loading…
Cancel
Save