def create_new_instance(self): """ Creates and boots a new server to host the worker instance.""" self.log.debug("function create_new_instance %s" % self.user.name) ec2 = boto3.client("ec2", region_name=SERVER_PARAMS["REGION"]) resource = boto3.resource("ec2", region_name=SERVER_PARAMS["REGION"]) boot_drive = {'DeviceName': '/dev/sda1', # this is to be the boot drive 'Ebs': {'VolumeSize': SERVER_PARAMS["WORKER_EBS_SIZE"], # size in gigabytes 'DeleteOnTermination': True, 'VolumeType': 'gp2', # This means General Purpose SSD # 'Iops': 1000 } # i/o speed for storage, default is 100, more is faster } } # create new instance reservation = yield retry( ec2.run_instances, ImageId=SERVER_PARAMS["WORKER_AMI"], MinCount=1, MaxCount=1, KeyName=SERVER_PARAMS["KEY_NAME"], InstanceType=SERVER_PARAMS["INSTANCE_TYPE"], SubnetId=SERVER_PARAMS["SUBNET_ID"], SecurityGroupIds=SERVER_PARAMS["WORKER_SECURITY_GROUPS"], BlockDeviceMappings=[boot_drive], ) instance_id = reservation["Instances"][0]["InstanceId"] instance = yield retry(resource.Instance, instance_id) Server.new_server(instance_id, self.user.name) yield retry(instance.wait_until_exists) # add server tags; tags cannot be added until server exists yield retry(instance.create_tags, Tags=WORKER_TAGS) yield retry(instance.create_tags, Tags=[{"Key": "User", "Value": self.user.name}]) # start server # blocking calls should be wrapped in a Future yield retry(instance.wait_until_running) return instance
def create_new_instance(self): """ Creates and boots a new server to host the worker instance.""" self.log_user("create_new_instance()") ec2 = boto3.client("ec2", region_name=SERVER_PARAMS["REGION"]) resource = boto3.resource("ec2", region_name=SERVER_PARAMS["REGION"]) BDM = [] boot_drive = {'DeviceName': '/dev/sda1', # this is to be the boot drive 'Ebs': {'VolumeSize': SERVER_PARAMS["WORKER_EBS_SIZE"], # size in gigabytes 'DeleteOnTermination': True, 'VolumeType': 'gp2', # This means General Purpose SSD # 'Iops': 1000 } # i/o speed for storage, default is 100, more is faster } } BDM = [boot_drive] if SERVER_PARAMS["USER_HOME_EBS_SIZE"] > 0: user_drive = {'DeviceName': '/dev/sdf', # this is to be the user data drive 'Ebs': {'VolumeSize': SERVER_PARAMS["USER_HOME_EBS_SIZE"], # size in gigabytes 'DeleteOnTermination': False, 'VolumeType': 'gp2', # General Purpose SSD } } BDM = [boot_drive, user_drive] # prepare userdata script to execute on the worker instance user_home_device = "xvdf" if SERVER_PARAMS["USER_HOME_EBS_SIZE"] > 0 else "" user_data_script = WORKER_USER_DATA.format(user=self.user.name, device=user_home_device) # create new instance reservation = yield retry( ec2.run_instances, ImageId=SERVER_PARAMS["WORKER_AMI"], MinCount=1, MaxCount=1, KeyName=SERVER_PARAMS["KEY_NAME"], InstanceType=SERVER_PARAMS["INSTANCE_TYPE"], SubnetId=SERVER_PARAMS["SUBNET_ID"], SecurityGroupIds=SERVER_PARAMS["WORKER_SECURITY_GROUPS"], BlockDeviceMappings=BDM, UserData=user_data_script, max_retries=30, ) self.log_user("result of retry(ec2.run_instances): %s" % reservation) instance_id = reservation["Instances"][0]["InstanceId"] instance = yield retry(resource.Instance, instance_id) Server.new_server(instance_id, self.user.name) yield retry(instance.wait_until_exists) # add server tags; tags cannot be added until server exists yield retry(instance.create_tags, Tags=WORKER_TAGS) yield retry(instance.create_tags, Tags=[{"Key": "owner", "Value": self.user.name}]) # start server # blocking calls should be wrapped in a Future yield retry(instance.wait_until_running) return instance
async def create_new_instance(self): """ Creates and boots a new server to host the worker instance.""" self.log.debug("function create_new_instance %s" % self.user.name) ec2 = boto3.client("ec2", region_name=SERVER_PARAMS["REGION"]) resource = boto3.resource("ec2", region_name=SERVER_PARAMS["REGION"]) BDM = [] boot_drive = { 'DeviceName': '/dev/sda1', # this is to be the boot drive 'Ebs': { 'VolumeSize': SERVER_PARAMS["WORKER_EBS_SIZE"], # size in gigabytes 'DeleteOnTermination': True, 'VolumeType': 'gp2', # This means General Purpose SSD # 'Iops': 1000 } # i/o speed for storage, default is 100, more is faster } } BDM = [boot_drive] # get user volume volume_id = await self.select_volume() # create new instance reservation = await retry( ec2.run_instances, ImageId=SERVER_PARAMS["WORKER_AMI"], MinCount=1, MaxCount=1, KeyName=SERVER_PARAMS["KEY_NAME"], InstanceType=self.user_options['INSTANCE_TYPE'], SubnetId=SERVER_PARAMS["SUBNET_ID"], SecurityGroupIds=SERVER_PARAMS["WORKER_SECURITY_GROUPS"], BlockDeviceMappings=BDM, ) self.log.debug(reservation) try: instance_id = reservation["Instances"][0]["InstanceId"] except TypeError as e: raise Exception( 'AWS sends weirdly formatted JSON. Please try again...') instance = await retry(resource.Instance, instance_id) #if an old volume is restored from a terminated instance, the user has to be updated, e.g. delted and newly saved # TODO: Support multiple volumes if self.user.volume: server = Server.get_server(self.user.name) Server.remove_server(server.server_id) Server.new_server(instance_id, self.user.name, volume_id) await retry(instance.wait_until_exists) # add server tags; tags cannot be added until server exists await retry(instance.create_tags, Tags=WORKER_TAGS) await retry(instance.create_tags, Tags=[{ "Key": "User", "Value": self.user.name }]) await retry(instance.create_tags, Tags=[{ "Key": "Name", "Value": SERVER_PARAMS["WORKER_SERVER_NAME"] + '_' + self.user.name }]) # start server # blocking calls should be wrapped in a Future await retry(instance.wait_until_running) # Attach persistent EBS await retry(instance.attach_volume, Device='/dev/sdf', VolumeId=volume_id, InstanceId=instance_id) return instance
def __init__(self, name,): self.name = name self.last_activity = '' self.server = SimpleNamespace(**{'ip' : '', 'base_url': ''}) self.url = '' self.settings = {} dummyUser = DummyUser(name='developmentUser') try: server = Server.get_server(user_id=dummyUser.name) except: server = Server.new_server(server_id = '' , user_id = dummyUser.name , ebs_volume_id = '') assert server.user_id == dummyUser.name dummyUserOptions = {'EBS_VOL_ID' : '', 'EBS_VOL_SIZE' : 3, 'INSTANCE_TYPE': 't2.nano'} dummyHubOptions = SimpleNamespace(**{'public_host' : '', 'api_url' : '', 'base_url' : ''}) dummyApiToken = open('api_token.txt','r').read() dummyOAuthID = '1234'