alok 6 years ago
commit 2c09e8e7c0
  1. 34
      aws-experiment-launch/create_and_deploy_minh.py
  2. 16
      aws-experiment-launch/utils/utils.py

@ -10,7 +10,6 @@ import base64
from utils import utils from utils import utils
class InstanceResource: class InstanceResource:
ON_DEMAND = 1 ON_DEMAND = 1
SPOT_INSTANCE = 2 SPOT_INSTANCE = 2
@ -33,26 +32,31 @@ NODE_NAME_SUFFIX = "NODE-" + CURRENT_SESSION
def run_one_region_instances(config, region_number, number_of_instances, instance_resource=InstanceResource.ON_DEMAND): def run_one_region_instances(config, region_number, number_of_instances, instance_resource=InstanceResource.ON_DEMAND):
region_name = config[region_number][utils.REGION_NAME] region_name = config[region_number][utils.REGION_NAME]
# Create session.
session = boto3.Session(region_name=region_name) session = boto3.Session(region_name=region_name)
# Create a client.
ec2_client = session.client('ec2') ec2_client = session.client('ec2')
print utils.get_one_availability_zone(ec2_client)
if instance_resource == InstanceResource.ON_DEMAND: if instance_resource == InstanceResource.ON_DEMAND:
response, node_name_tag = create_instances( return 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_tag, region_number)) print("Created %s in region %s" % (node_name_tag, region_number))
else: else:
return None, node_name_tag return False
return response, node_name_tag
def create_instances(config, ec2_client, region_number, number_of_instances): def create_instances(config, ec2_client, region_number, number_of_instances):
node_name_tag = region_number + "-" + NODE_NAME_SUFFIX node_name_tag = region_number + "-" + NODE_NAME_SUFFIX
print("Creating node_name_tag: %s" % node_name_tag)
available_zone = utils.get_one_availability_zone(ec2_client)
print("Looking at zone %s to create instances." % available_zone)
response = ec2_client.run_instances( response = ec2_client.run_instances(
MinCount=number_of_instances, MinCount=number_of_instances,
MaxCount=number_of_instances, MaxCount=number_of_instances,
ImageId=config[region_number][utils.REGION_AMI], ImageId=config[region_number][utils.REGION_AMI],
Placement={ Placement={
'AvailabilityZone': utils.get_one_availability_zone(ec2_client), 'AvailabilityZone': available_zone,
}, },
SecurityGroups=[config[region_number][utils.REGION_SECURITY_GROUP]], SecurityGroups=[config[region_number][utils.REGION_SECURITY_GROUP]],
IamInstanceProfile={ IamInstanceProfile={
@ -73,7 +77,17 @@ def create_instances(config, ec2_client, region_number, number_of_instances):
}, },
], ],
) )
return response, node_name_tag
count = 0
while count < 40:
time.sleep(5)
print("Waiting ...")
ip_list = utils.collect_public_ips_from_ec2_client(ec2_client, node_name_tag)
if len(ip_list) == number_of_instances:
print("Created %d instances" % number_of_instances)
return True
count = count + 1
return False
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
@ -93,10 +107,8 @@ if __name__ == "__main__":
for i in range(len(region_list)): for i in range(len(region_list)):
region_number = region_list[i] region_number = region_list[i]
number_of_instances = instances_list[i] number_of_instances = instances_list[i]
response, node_name_tag = run_one_region_instances( if run_one_region_instances(config, region_number, number_of_instances, InstanceResource.ON_DEMAND):
config, region_number, number_of_instances, InstanceResource.ON_DEMAND) print("Created instances for region %s" % region_number )
if response:
print utils.get_ip_list(response)
# Enable the code below later. # Enable the code below later.
# results = launch_code_deploy(region_list, commitId) # results = launch_code_deploy(region_list, commitId)

@ -32,16 +32,24 @@ def get_ip_list(response):
else: else:
return [] return []
def collect_public_ips(region_number, node_name_tag, region_config): def create_ec2_client(region_number, region_config):
config = read_configuration_file(region_config) config = read_region_config(region_config)
region_name = config[region_number][REGION_NAME] region_name = config[region_number][REGION_NAME]
session = boto3.Session(region_name=region_name) session = boto3.Session(region_name=region_name)
ec2_client = session.client('ec2') return session.client('ec2')
def collect_public_ips_from_ec2_client(ec2_client, node_name_tag):
filters = [{'Name': 'tag:Name','Values': [node_name_tag]}] filters = [{'Name': 'tag:Name','Values': [node_name_tag]}]
response = ec2_client.describe_instances(Filters=filters) response = ec2_client.describe_instances(Filters=filters)
ip_list = [] ip_list = []
if response.get('Reservations'):
for reservation in response[u'Reservations']: for reservation in response[u'Reservations']:
ip_list.extend(instance['PublicIpAddress'] for instance in reservation['Instances']) ip_list.extend(instance['PublicIpAddress'] for instance in reservation['Instances'] if instance.get('PublicIpAddress'))
return ip_list
def collect_public_ips(region_number, node_name_tag, region_config):
ec2_client = create_ec2_client(region_number, region_config)
ip_list = collect_public_ips_from_ec2_client(ec2_client, node_name_tag)
return ip_list return ip_list
def generate_distribution_config2(region_number, node_name_tag, region_config, def generate_distribution_config2(region_number, node_name_tag, region_config,

Loading…
Cancel
Save