pycloudlib.lxd.instance module

LXD instance.

class pycloudlib.lxd.instance.LXDInstance(name, key_pair=None, execute_via_ssh=True, series=None, ephemeral=None, *, username: str | None = None)[source]

Bases: BaseInstance

LXD backed instance.

__init__(name, key_pair=None, execute_via_ssh=True, series=None, ephemeral=None, *, username: str | None = None)[source]

Set up instance.

Parameters:
  • name – name of instance

  • key_pair – SSH key object

  • execute_via_ssh – Boolean True to use ssh instead of lxc exec for all operations.

  • series – Ubuntu release name: xenial, bionic etc.

  • ephemeral – Boolean True if instance is ephemeral. If left unspecified, ephemeral type will be determined and cached by the ephemeral method.

  • username – username to use when connecting via SSH

console_log()[source]

Return console log.

Uses the ‘–show-log’ option of console to get the console log from an instance.

Returns:

bytes of this instance’s console

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

Delete the current instance.

By default this will use the ‘–force’ option to prevent the need to always stop the instance first. This makes it easier to work with ephemeral instances as well, which are deleted on stop.

Parameters:

wait – wait for delete

delete_snapshot(snapshot_name)[source]

Delete a snapshot of the instance.

Parameters:

snapshot_name – the name to delete

edit(key, value)[source]

Edit the config of the instance.

Parameters:
  • key – The config key to edit

  • value – The new value to set the key to

property ephemeral

Return boolean if ephemeral or not.

Will return False if unknown.

Returns:

boolean if ephemeral

property id: str

Return instance id.

property ip

Return IP address of instance.

Returns:

IP address assigned to instance.

Raises: PycloudlibTimeoutError when exhausting retries trying to parse

lxc list for ip addresses.

property is_vm

Return boolean if vm type or not.

Will return False if unknown.

Returns:

boolean if virtual-machine

local_snapshot(snapshot_name, stateful=False)[source]

Create an LXD snapshot (not a launchable image).

Parameters:
  • snapshot_name – name to call snapshot

  • stateful – boolean, stateful snapshot or not

property name

Return instance name.

parse_ip(query: dict)[source]

Return ip address from lxd query.

Returns None if no address found

pull_file(remote_path, local_path)[source]

Pull file from an instance.

The remote path must be absolute path with LXD due to the way files are pulled off. Specifically, the format is ‘name/path’ with path assumed to start from ‘/’.

Parameters:
  • remote_path – path to remote file to pull down

  • local_path – local path to put the file

push_file(local_path, remote_path)[source]

Push file to an instance.

The remote path must be absolute path with LXD due to the way files are pulled off. Specifically, the format is ‘name/path’ with path assumed to start from ‘/’.

Parameters:
  • local_path – local path to file to push up

  • remote_path – path to push file

restore(snapshot_name)[source]

Restore instance from a specific snapshot.

Parameters:

snapshot_name – Name of snapshot to restore from

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

Shutdown instance.

Parameters:
  • wait – boolean, wait for instance to shutdown

  • force – boolean, force instance to shutdown

snapshot(snapshot_name)[source]

Create an image snapshot.

Snapshot is a bit of a misnomer here. Since “snapshot” in the context of clouds means “create a launchable container from this instance”, we actually need to do a publish here. If you need the lxd “snapshot” functionality, use local_snapshot

Parameters:

snapshot_name – name to call snapshot

start(wait=True)[source]

Start instance.

Parameters:

wait – boolean, wait for instance to fully start

property state

Return current status of instance.

If unable to get status will return ‘Unknown’.

Returns:

Reported status from lxc info

wait_for_delete()[source]

Wait for delete.

Not used for LXD.

wait_for_state(desired_state: str, num_retries: int = 100)[source]

Wait for instance to reach desired state value.

Parameters:
  • desired_state – String representing one of lxc instance states seen by lxc ls -s. For example, ACTIVE, FROZEN, RUNNING, STOPPED

  • retries – Integer for number of retry attempts before raising a PycloudlibTimeoutError.

wait_for_stop()[source]

Wait for cloud instance to transition to stop state.

class pycloudlib.lxd.instance.LXDVirtualMachineInstance(name, key_pair=None, execute_via_ssh=True, series=None, ephemeral=None, *, username: str | None = None)[source]

Bases: LXDInstance

LXD Virtual Machine backed instance.