The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/aws-experiment-launch/utils/utils.py

144 lines
5.6 KiB

import boto3
import datetime
import json
import sys
import time
REGION_NAME = 'region_name'
REGION_KEY = 'region_key'
REGION_SECURITY_GROUP = 'region_security_group'
REGION_HUMAN_NAME = 'region_human_name'
INSTANCE_TYPE = 't2.micro'
REGION_AMI = 'region_ami'
7 years ago
def read_region_config(region_config='configuration.txt'):
config = {}
with open(region_config, 'r') as f:
for myline in f:
7 years ago
mylist = [item.strip() for item in myline.strip().split(',')]
region_num = mylist[0]
config[region_num] = {}
config[region_num][REGION_NAME] = mylist[1]
config[region_num][REGION_KEY] = mylist[2]
config[region_num][REGION_SECURITY_GROUP] = mylist[3]
config[region_num][REGION_HUMAN_NAME] = mylist[4]
config[region_num][REGION_AMI] = mylist[5]
return config
7 years ago
def get_ip_list(response):
if response.get('Instances', None):
return [instance.get('PublicIpAddress', None) for instance in response['Instances']]
else:
return []
7 years ago
def create_ec2_client(region_number, region_config):
config = read_region_config(region_config)
region_name = config[region_number][REGION_NAME]
session = boto3.Session(region_name=region_name)
7 years ago
return session.client('ec2'), session
7 years ago
7 years ago
def collect_public_ips_from_ec2_client(ec2_client, node_name_tag):
filters = [{'Name': 'tag:Name','Values': [node_name_tag]}]
response = ec2_client.describe_instances(Filters=filters)
ip_list = []
7 years ago
if response.get('Reservations'):
for reservation in response[u'Reservations']:
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):
7 years ago
ec2_client, _ = create_ec2_client(region_number, region_config)
7 years ago
ip_list = collect_public_ips_from_ec2_client(ec2_client, node_name_tag)
return ip_list
7 years ago
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 create_deployment_group(codedeploy, region_number,application_name, deployment_group, node_name_tag):
_ = 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_tag,
'Type': 'KEY_AND_VALUE'
}
]
)
# Checking??
return deployment_group
7 years ago
def generate_distribution_config2(region_number, node_name_tag, region_config,
7 years ago
shard_number, client_number, distribution_config):
ip_list = collect_public_ips(region_number, node_name_tag, region_config)
7 years ago
generate_distribution_config(shard_number, client_number, ip_list, distribution_config)
7 years ago
def generate_distribution_config3(shard_number, client_number, ip_list_file, distribution_config):
7 years ago
with open(ip_list_file, "r") as fin:
lines = fin.readlines()
ip_list = [line.strip() for line in lines]
7 years ago
generate_distribution_config(shard_number, client_number, ip_list, distribution_config)
7 years ago
7 years ago
def generate_distribution_config(shard_number, client_number, ip_list, distribution_config):
if len(ip_list) < shard_number * 2 + client_number:
print("Not enough nodes to generate a config file")
return False
# Create ip for clients.
client_id, leader_id, validator_id = 0, 0, 0
7 years ago
with open(distribution_config, "w") as fout:
for i in range(len(ip_list)):
7 years ago
if client_id < client_number:
fout.write("%s 9000 client %d\n" % (ip_list[i], client_id % shard_number))
client_id = client_id + 1
7 years ago
elif leader_id < shard_number:
fout.write("%s 9000 leader %d\n" % (ip_list[i], leader_id))
leader_id = leader_id + 1
else:
7 years ago
fout.write("%s 9000 validator %d\n" % (ip_list[i], validator_id % shard_number))
validator_id = validator_id + 1
def get_availability_zones(ec2_client):
response = ec2_client.describe_availability_zones()
all_zones = []
if response.get('AvailabilityZones', None):
all_zones = [info['ZoneName'] for info in response.get('AvailabilityZones') if info['State'] == 'available']
return all_zones
7 years ago
def get_one_availability_zone(ec2_client):
all_zones = get_availability_zones(ec2_client)
if len(all_zones) > 0:
return all_zones[0]
else:
return None
7 years ago
# Get instance_ids from describe_instances_response.
def get_instance_ids(describe_instances_response):
instance_ids = []
if describe_instances_response["Reservations"]:
for reservation in describe_instances_response["Reservations"]:
instance_ids.extend(instance["InstanceId"] for instance in reservation["Instances"] if instance.get("InstanceId"))
return instance_ids
# used for testing only.
# if __name__ == "__main__":
# ip_list = collect_public_ips('4', "4-NODE-23-36-01-2018-07-05", "configuration.txt")
# print ip_list
# generate_distribution_config(2, 1, ip_list, "config_test.txt")