pycloudlib.ec2.instance module

EC2 instance.

class pycloudlib.ec2.instance.EC2Instance(key_pair, client, instance)[source]

Bases: pycloudlib.instance.BaseInstance

EC2 backed instance.

__init__(key_pair, client, instance)[source]

Set up instance.

Parameters:
  • key_pair – SSH key object
  • client – boto3 client object
  • instance – created boto3 instance object
add_network_interface() → str[source]

Add network interface to instance.

Creates an ENI device and attaches it to the running instance. This is effectively a hot-add of a network device. Returns the IP address of the added network interface as a string.

See the AWS documentation for more info: https://boto3.readthedocs.io/en/latest/reference/services/ec2.html?#EC2.Client.create_network_interface https://boto3.readthedocs.io/en/latest/reference/services/ec2.html?#EC2.Client.attach_network_interface

add_volume(size=8, drive_type='gp2')[source]

Add storage volume to instance.

Creates an EBS volume and attaches it to the running instance. This is effectively a hot-add of a storage device.

See AWS documentation for more info: https://boto3.readthedocs.io/en/latest/reference/services/ec2.html?#EC2.Client.create_volume https://boto3.readthedocs.io/en/latest/reference/services/ec2.html?#EC2.Client.attach_volume

Parameters:
  • size – Size in GB of the drive to add
  • drive_type – Type of EBS volume to add
availability_zone

Return availability zone.

clean()

Clean an instance to make it look prestine.

This will clean out specifically the cloud-init files and system logs.

console_log()[source]

Collect console log from instance.

The console log is buffered and not always present, therefore may return empty string.

Returns:The console log or error message
delete(wait=True)[source]

Delete instance.

execute(command, stdin=None, description=None, *, use_sudo=False, **kwargs)

Execute command in instance, recording output, error and exit code.

Assumes functional networking and execution with the target filesystem being available at /.

Parameters:
  • command – the command to execute as root inside the image. If command is a string, then it will be executed as: [‘sh’, ‘-c’, command]
  • stdin – bytes content for standard in
  • description – purpose of command
  • use_sudo – boolean to run the command as sudo
Returns:

Result object

Raises SSHException if there are any problem with the ssh connection

id

Return id of instance.

image_id

Return id of instance.

install(packages)

Install specific packages.

Parameters:packages – string or list of package(s) to install
Returns:result from install
ip

Return IP address of instance.

name

Return id of instance.

pull_file(remote_path, local_path)

Copy file at ‘remote_path’, from instance to ‘local_path’.

Parameters:
  • remote_path – path on remote instance
  • local_path – local path

Raises SSHException if there are any problem with the ssh connection

push_file(local_path, remote_path)

Copy file at ‘local_path’ to instance at ‘remote_path’.

Parameters:
  • local_path – local path
  • remote_path – path on remote instance

Raises SSHException if there are any problem with the ssh connection

remove_network_interface(ip_address)[source]

Remove network interface based on IP address.

Find the NIC from the IP, detach from the instance, then delete the NIC.

restart(wait=True, **kwargs)[source]

Restart the instance.

run_script(script, description=None)

Run script in target and return stdout.

Parameters:
  • script – script contents
  • description – purpose of script
Returns:

result from script execution

Raises SSHException if there are any problem with the ssh connection

shutdown(wait=True, **kwargs)[source]

Shutdown the instance.

Parameters:wait – wait for the instance shutdown
start(wait=True)[source]

Start the instance.

Parameters:wait – wait for the instance to start.
update()

Run apt-get update/upgrade on instance.

Returns:result from upgrade
wait()

Wait for instance to be up and cloud-init to be complete.

wait_for_delete()[source]

Wait for instance to be deleted.

wait_for_stop()[source]

Wait for instance stop.