コード例 #1
0
def setup_demo(current_ip_address, ami_image_id, key_file_name):
    """
    Sets up prerequisites and creates instances used in the demo.
    When this function returns, the instances are running and ready to use.

    :param current_ip_address: The public IP address of the current computer.
    :param ami_image_id: The Amazon Machine Image (AMI) that is used to create the
                         instances for the demo.
    :param key_file_name: The name of a local file that contains the private key
                          that is used to connect to the instances using SSH.
    :return: The newly created instances, security groups, key pair, and
             Elastic IP object.
    """
    key_pair = ec2_setup.create_key_pair(make_unique_name('key'),
                                         key_file_name)
    print(
        f"Created a key pair {key_pair.key_name} and saved the private key to "
        f"{key_file_name}")

    ssh_sec_group = ec2_setup.setup_security_group(
        make_unique_name('ssh-group'),
        f'Demo group that allows SSH from {current_ip_address}.',
        current_ip_address)
    print(f"Created security group {ssh_sec_group.group_name} that allows SSH "
          f"access from {current_ip_address}.")

    no_ssh_sec_group = ec2_setup.setup_security_group(
        make_unique_name('no-ssh-group'),
        'Demo group that does not allow SSH.')
    print(
        f"Created security group {no_ssh_sec_group.group_name} that does not allow "
        f"SSH access.")

    ssh_instance = ec2_setup.create_instance(ami_image_id, 't2.micro',
                                             key_pair.key_name,
                                             [ssh_sec_group.group_name])
    print(f"Created instance {ssh_instance.instance_id} that can be accessed "
          f"through SSH.")

    no_ssh_instance = ec2_setup.create_instance(ami_image_id, 't2.micro',
                                                key_pair.key_name,
                                                [no_ssh_sec_group.group_name])
    print(
        f"Created instance {no_ssh_instance.instance_id} that cannot be accessed "
        f"through SSH.")

    elastic_ip = ec2_instance_management.allocate_elastic_ip()
    print(f"Allocated static Elastic IP address: {elastic_ip.public_ip}.")

    print(f"Waiting for instances to start...")
    ssh_instance.wait_until_running()
    print(f"Instance {ssh_instance.instance_id} is running.")

    no_ssh_instance.wait_until_running()
    print(f"Instance {no_ssh_instance.instance_id} is running.")

    return (ssh_instance, no_ssh_instance), (no_ssh_sec_group, ssh_sec_group),\
        key_pair, elastic_ip
コード例 #2
0
def test_create_key_pair(
        make_stubber, make_unique_name, pk_file_name, error_code):
    ec2_stubber = make_stubber(ec2_setup.ec2.meta.client)
    key_name = make_unique_name('key')
    key_material = 'test-key-material'

    ec2_stubber.stub_create_key_pair(key_name, key_material, error_code)

    if error_code is None:
        with patch('builtins.open', mock_open()) as mock_file:
            key_pair = ec2_setup.create_key_pair(key_name, pk_file_name)
            if pk_file_name is not None:
                mock_file.assert_called_with(pk_file_name, 'w')
        assert key_pair.name == key_name
        assert key_pair.key_material == key_material
    else:
        with pytest.raises(ClientError) as exc_info:
            ec2_setup.create_key_pair(key_name, pk_file_name)
        assert exc_info.value.response['Error']['Code'] == error_code