pycloudlib.instance module

Base class for all instances to provide consistent set of functions.

class pycloudlib.instance.BaseInstance(key_pair, username: str | None = None)[source]

Bases: ABC

Base instance object.

__init__(key_pair, username: str | None = None)[source]

Set up instance.

add_network_interface(**kwargs) str[source]

Add nic to running instance.

clean()[source]

Clean an instance to make it look prestine.

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

console_log()[source]

Return the instance console log.

Raises NotImplementedError if the cloud does not support fetching the console log for this instance.

abstract delete(wait=True) List[Exception][source]

Delete the instance.

Parameters:

wait – wait for instance to be deleted

execute(command, stdin=None, description=None, *, use_sudo=False, no_log=False, **kwargs)[source]

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

get_boot_id()[source]

Get the instance boot_id.

Returns:

string with the boot UUID

abstract property id: str

Return instance id.

install(packages)[source]

Install specific packages.

Parameters:

packages – string or list of package(s) to install

Returns:

result from install

abstract property ip

Return IP address of instance.

abstract property name

Return instance name.

property private_ip: str

Return instance private ip.

pull_file(remote_path, local_path)[source]

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)[source]

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

ready_timeout = 10
remove_network_interface(ip_address: str)[source]

Remove nic from running instance.

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

Restart an instance.

run_script(script, description=None)[source]

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

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

Shutdown the instance.

Parameters:

wait – wait for the instance to shutdown

abstract start(wait=True)[source]

Start the instance.

Parameters:

wait – wait for the instance to start.

update()[source]

Run apt-get update/upgrade on instance.

Returns:

result from upgrade

wait(ready_timeout: int | None = None, **kwargs)[source]

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

Parameters:

ready_timeout (int) – maximum time to wait for the instance to be ready for ssh after instance provisioning is complete

Raises:

PycloudlibTimeoutError – If the instance is not ready after the timeout

abstract wait_for_delete(**kwargs)[source]

Wait for instance to be deleted.

wait_for_restart(old_boot_id)[source]

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

old_boot_id is the boot id prior to restart

abstract wait_for_stop(**kwargs)[source]

Wait for instance stop.