# Conflicts:
#	aws-experiment-launch/create_and_deploy.py
pull/31/head
Richard Liu 6 years ago
commit 27efed47e2
  1. 97
      aws-experiment-launch/create_and_deploy.py

@ -21,10 +21,11 @@ REGION_SECURITY_GROUP = 'region_security_group'
REGION_HUMAN_NAME = 'region_human_name' REGION_HUMAN_NAME = 'region_human_name'
INSTANCE_TYPE = 't2.small' INSTANCE_TYPE = 't2.small'
REGION_AMI = 'region_ami' REGION_AMI = 'region_ami'
# USER_DATA = 'user-data.sh' with open("user-data.sh", "r") as userdata_file:
USER_DATA = userdata_file.read()
# UserData must be base64 encoded for spot instances. # UserData must be base64 encoded for spot instances.
with open("user-data.sh", "rb") as userdata_file: USER_DATA_BASE64 = base64.b64encode(USER_DATA)
USER_DATA = base64.b64encode(userdata_file.read())
IAM_INSTANCE_PROFILE = 'BenchMarkCodeDeployInstanceProfile' IAM_INSTANCE_PROFILE = 'BenchMarkCodeDeployInstanceProfile'
REPO = "simple-rules/harmony-benchmark" REPO = "simple-rules/harmony-benchmark"
@ -37,8 +38,7 @@ NODE_NAME_SUFFIX = "NODE-" + CURRENT_SESSION
""" """
TODO: TODO:
NODE = region_number + NODE_VALUE
Use that to retrieve ids, only deploy on specific nodes (right now it deploys everywere), remove placement group.
save NODE to disk, so that you can selectively only run deploy (not recreate instances). save NODE to disk, so that you can selectively only run deploy (not recreate instances).
Right now all instances have "NODE" so this has uninted consequences of running on instances that were previous created. Right now all instances have "NODE" so this has uninted consequences of running on instances that were previous created.
Build (argparse,functions) support for Build (argparse,functions) support for
@ -60,8 +60,9 @@ def run_one_region_instances(config, region_number, number_of_instances, instanc
session = boto3.Session(region_name=region_name) session = boto3.Session(region_name=region_name)
ec2_client = session.client('ec2') ec2_client = session.client('ec2')
if instance_type == InstanceType.ON_DEMAND: if instance_type == InstanceType.ON_DEMAND:
response = create_instances( NODE_NAME = create_instances(
config, ec2_client, region_number, int(number_of_instances)) config, ec2_client, region_number, int(number_of_instances))
print("Created %s in region %s"%(NODE_NAME,region_number)) ##REPLACE ALL print with logger
elif instance_type == InstanceType.SPOT_INSTANCE: elif instance_type == InstanceType.SPOT_INSTANCE:
response = request_spot_instances( response = request_spot_instances(
config, ec2_client, region_number, int(number_of_instances)) config, ec2_client, region_number, int(number_of_instances))
@ -109,7 +110,7 @@ def create_instances(config, ec2_client, region_number, number_of_instances):
# } # }
# } # }
) )
return response return NODE_NAME
def request_spot_instances(config, ec2_client, region_number, number_of_instances): def request_spot_instances(config, ec2_client, region_number, number_of_instances):
@ -123,7 +124,7 @@ def request_spot_instances(config, ec2_client, region_number, number_of_instance
'IamInstanceProfile': { 'IamInstanceProfile': {
'Name': IAM_INSTANCE_PROFILE 'Name': IAM_INSTANCE_PROFILE
}, },
'UserData': USER_DATA, 'UserData': USER_DATA_BASE64,
'ImageId': config[region_number][REGION_AMI], 'ImageId': config[region_number][REGION_AMI],
'InstanceType': INSTANCE_TYPE, 'InstanceType': INSTANCE_TYPE,
'KeyName': config[region_number][REGION_KEY], 'KeyName': config[region_number][REGION_KEY],
@ -223,67 +224,57 @@ def run_one_region_codedeploy(region_number, commitId):
NODE_NAME = region_number + "-" + NODE_NAME_SUFFIX NODE_NAME = region_number + "-" + NODE_NAME_SUFFIX
session = boto3.Session(region_name=region_name) session = boto3.Session(region_name=region_name)
ec2_client = session.client('ec2') ec2_client = session.client('ec2')
filters = [ filters = [{'Name': 'tag:Name','Values': [NODE_NAME]}]
{ instance_ids = get_instance_ids(ec2_client.describe_instances(Filters=filters))
'Name': 'tag:Name',
'Values': [NODE_NAME] print("Number of instances: %d" % len(instance_ids))
}
]
print("Waiting for all instances to start running") print("Waiting for all %d instances in region %s to start running"%(len(instance_ids),region_number))
waiter = ec2_client.get_waiter('instance_running') waiter = ec2_client.get_waiter('instance_running')
waiter.wait(Filters=filters) waiter.wait(InstanceIds=instance_ids)
print("Waiting for all instances to be status ok") print("Waiting for all %d instances in region %s to be INSTANCE STATUS OK"%(len(instance_ids),region_number))
waiter = ec2_client.get_waiter('instance_status_ok') waiter = ec2_client.get_waiter('instance_status_ok')
waiter.wait(Filters=filters) waiter.wait(InstanceIds=instance_ids)
print("Waiting for system to be status ok") print("Waiting for all %d instances in region %s to be SYSTEM STATUS OK"%(len(instance_ids),region_number))
waiter = ec2_client.get_waiter('system_status_ok') waiter = ec2_client.get_waiter('system_status_ok')
waiter.wait(Filters=filters) waiter.wait(InstanceIds=instance_ids)
codedeploy = session.client('codedeploy') codedeploy = session.client('codedeploy')
application_name = APPLICATION_NAME application_name = APPLICATION_NAME
deployment_group = APPLICATION_NAME + "-" + str(commitId) deployment_group = APPLICATION_NAME + "-" + str(commitId)[6] + "-" + CURRENT_SESSION
repo = REPO repo = REPO
print("Setting up to deploy commitId %s on region %s"%(commitId,region_number))
response = get_application(codedeploy, application_name) response = get_application(codedeploy, application_name)
response = get_deployment_group( deployment_group = get_deployment_group(
codedeploy, application_name, deployment_group) codedeploy, region_number, application_name, deployment_group)
depId = deploy(codedeploy, application_name, depId = deploy(codedeploy, application_name,
deployment_group, repo, commitId) deployment_group, repo, commitId)
return region_number, depId return region_number, depId
def get_deployment_group(codedeploy, application_name, deployment_group): def get_deployment_group(codedeploy, region_number,application_name, deployment_group):
NODE_NAME = region_number + "-" + NODE_NAME_SUFFIX NODE_NAME = region_number + "-" + NODE_NAME_SUFFIX
response = codedeploy.list_deployment_groups( response = codedeploy.create_deployment_group(
applicationName=application_name applicationName=application_name,
) deploymentGroupName=deployment_group,
if deployment_group in response['deploymentGroups']: deploymentConfigName='CodeDeployDefault.AllAtOnce',
return response serviceRoleArn='arn:aws:iam::656503231766:role/BenchMarkCodeDeployServiceRole',
else: deploymentStyle={
response = codedeploy.create_deployment_group( 'deploymentType': 'IN_PLACE',
applicationName=application_name, 'deploymentOption': 'WITHOUT_TRAFFIC_CONTROL'
deploymentGroupName=deployment_group, },
deploymentConfigName='CodeDeployDefault.AllAtOnce', ec2TagFilters = [
serviceRoleArn='arn:aws:iam::656503231766:role/BenchMarkCodeDeployServiceRole', {
deploymentStyle={ 'Key': 'Name',
'deploymentType': 'IN_PLACE', 'Value': NODE_NAME,
'deploymentOption': 'WITHOUT_TRAFFIC_CONTROL' 'Type': 'KEY_AND_VALUE'
},
ec2TagSet={
'ec2TagSetList': [
[
{
'Key': 'Name',
'Value': NODE_NAME,
'Type': 'KEY_AND_VALUE'
},
],
]
} }
) ]
return response )
return deployment_group
def get_application(codedeploy, application_name): def get_application(codedeploy, application_name):
@ -338,12 +329,10 @@ def deploy(codedeploy, application_name, deployment_group, repo, commitId):
print(info) print(info)
return depId return depId
def run_one_region_codedeploy_wrapper(region_number, commitId, queue): def run_one_region_codedeploy_wrapper(region_number, commitId, queue):
region_number, depId = run_one_region_codedeploy(region_number, commitId) region_number, depId = run_one_region_codedeploy(region_number, commitId)
queue.put((region_number, depId)) queue.put((region_number, depId))
def launch_code_deploy(region_list, commitId): def launch_code_deploy(region_list, commitId):
queue = Queue() queue = Queue()
jobs = [] jobs = []
@ -368,7 +357,6 @@ def get_instance_ids(describe_instances_response):
instance_ids.append(instance["InstanceId"]) instance_ids.append(instance["InstanceId"])
return instance_ids return instance_ids
def read_configuration_file(filename): def read_configuration_file(filename):
config = {} config = {}
with open(filename, 'r') as f: with open(filename, 'r') as f:
@ -383,7 +371,6 @@ def read_configuration_file(filename):
config[region_num][REGION_AMI] = mylist[5] config[region_num][REGION_AMI] = mylist[5]
return config return config
def get_commitId(commitId): def get_commitId(commitId):
if commitId is None: if commitId is None:
commitId = run("git rev-list --max-count=1 HEAD", commitId = run("git rev-list --max-count=1 HEAD",

Loading…
Cancel
Save