def ec2_instance( request, ec2_client, ec2_resource, ec2_instance_type, ec2_key_name, ec2_instance_role_name, ec2_instance_ami, region, ): use_instances_flag = request.config.getoption( "--use-instances") if request.config.getoption( "--use-instances") else None if use_instances_flag: instances_file = request.config.getoption("--use-instances") run(f"touch {instances_file}", warn=True) instances_dict = YamlHandler.load_yaml(instances_file) LOGGER.info(f"instances_dict: {instances_dict}") instances = instances_dict.get(request.node.name.split("[")[0], "") LOGGER.info(f"instances: {instances}") assert instances != "", f"Could not find instance details corresponding to test: {request.node.name.split('[')[0]}" instance_details = instances.get(ec2_instance_type, "") assert instance_details != "", f"Could not obtain details for instance type: {ec2_instance_type}" instance_id = instance_details.get("instance_id", "") assert instance_id != "", f"Missing instance_id" key_filename = instance_details.get("key_filename", "") assert key_filename != "", f"Missing key_filename" LOGGER.info( f"For test: {request.node.name}; Using instance_id: {instance_id} and key_filename: {key_filename}" ) return instance_id, key_filename key_filename = ec2_utils.generate_ssh_keypair(ec2_client, ec2_key_name) params = { "KeyName": ec2_key_name, "ImageId": ec2_instance_ami, "InstanceType": ec2_instance_type, "IamInstanceProfile": { "Name": ec2_instance_role_name }, "TagSpecifications": [ { "ResourceType": "instance", "Tags": [{ "Key": "Name", "Value": f"TS Benchmark {ec2_key_name}" }] }, ], "MaxCount": 1, "MinCount": 1, "BlockDeviceMappings": [{ "DeviceName": "/dev/sda1", "Ebs": { "VolumeSize": 220 } }], } try: instances = ec2_resource.create_instances(**params) except ClientError as e: if e.response["Error"]["Code"] == "InsufficientInstanceCapacity": LOGGER.warning( f"Failed to launch {ec2_instance_type} in {region} because of insufficient capacity" ) raise instance_id = instances[0].id LOGGER.info(f"Created instance: TS Benchmark {ec2_key_name}") # Define finalizer to terminate instance after this fixture completes def terminate_ec2_instance(): ec2_client.terminate_instances(InstanceIds=[instance_id]) def delete_ssh_keypair(): ec2_utils.destroy_ssh_keypair(ec2_client, key_filename) do_not_terminate_flag = request.config.getoption("--do-not-terminate") LOGGER.info(f"do_not_terminate_flag: {do_not_terminate_flag}") instances_file = os.path.join(os.getcwd(), "instances.yaml") run(f"touch {instances_file}", warn=True) if not do_not_terminate_flag: request.addfinalizer(terminate_ec2_instance) request.addfinalizer(delete_ssh_keypair) if do_not_terminate_flag and not use_instances_flag: instances_dict = YamlHandler.load_yaml(instances_file) if not instances_dict: instances_dict = {} update_dictionary = { request.node.name.split("[")[0]: { ec2_instance_type: { "instance_id": instance_id, "key_filename": key_filename } } } instances_dict.update(update_dictionary) YamlHandler.write_yaml(instances_file, instances_dict) ec2_utils.check_instance_state(instance_id, state="running", region=region) ec2_utils.check_system_state(instance_id, system_status="ok", instance_status="ok", region=region) return instance_id, key_filename
def ec2_instance( request, ec2_client, ec2_resource, ec2_instance_type, ec2_key_name, ec2_instance_role_name, ec2_instance_ami, region, ): key_filename = ec2_utils.generate_ssh_keypair(ec2_client, ec2_key_name) def delete_ssh_keypair(): ec2_utils.destroy_ssh_keypair(ec2_client, key_filename) request.addfinalizer(delete_ssh_keypair) params = { "KeyName": ec2_key_name, "ImageId": ec2_instance_ami, "InstanceType": ec2_instance_type, "IamInstanceProfile": { "Name": ec2_instance_role_name }, "TagSpecifications": [ { "ResourceType": "instance", "Tags": [{ "Key": "Name", "Value": f"TS Benchmark {ec2_key_name}" }] }, ], "MaxCount": 1, "MinCount": 1, "BlockDeviceMappings": [{ "DeviceName": "/dev/sda1", "Ebs": { "VolumeSize": 120 } }], } try: instances = ec2_resource.create_instances(**params) except ClientError as e: if e.response["Error"]["Code"] == "InsufficientInstanceCapacity": LOGGER.warning( f"Failed to launch {ec2_instance_type} in {region} because of insufficient capacity" ) raise instance_id = instances[0].id LOGGER.info(f"Created instance: TS Benchmark {ec2_key_name}") # Define finalizer to terminate instance after this fixture completes def terminate_ec2_instance(): ec2_client.terminate_instances(InstanceIds=[instance_id]) request.addfinalizer(terminate_ec2_instance) ec2_utils.check_instance_state(instance_id, state="running", region=region) ec2_utils.check_system_state(instance_id, system_status="ok", instance_status="ok", region=region) return instance_id, key_filename