pycloudlib.lxd.cloud module

LXD Cloud type.

class pycloudlib.lxd.cloud.LXD(*args, **kwargs)[source]

Bases: pycloudlib.lxd.cloud.LXDContainer

Old LXD Container Cloud Class (Kept for compatibility issues).

CONTAINER_HASH_KEY = 'combined_squashfs_sha256'
__init__(*args, **kwargs)[source]

Run LXDContainer constructor.

clone(base, new_instance_name)

Create copy of an existing instance or snapshot.

Uses the lxc copy command to create a copy of an existing instance or a snapshot. To clone a snapshot then the base is instance_name/snapshot_name otherwise if base is only an existing instance it will clone an instance.

Parameters:
  • base – base instance or instance/snapshot
  • new_instance_name – name of new instance
Returns:

The created LXD instance object

create_key_pair()

Create and set a ssh key pair for a cloud instance.

Returns:A tuple containing the public and private key created
create_profile(profile_name, profile_config, force=False)

Create a lxd profile.

Create a lxd profile and populate it with the given profile config. If the profile already exists, we will not recreate it, unless the force parameter is set to True.

Parameters:
  • profile_name – Name of the profile to be created
  • profile_config – Config to be added to the new profile
  • force – Force the profile creation if it already exists
daily_image(release, arch='amd64')

Find the LXD fingerprint of the latest daily image.

Parameters:
  • release – string, Ubuntu release to look for
  • arch – string, architecture to use
Returns:

string, LXD fingerprint of latest image

delete_image(image_id)

Delete the image.

Parameters:image_id – string, LXD image fingerprint
delete_instance(instance_name, wait=True)

Delete an instance.

Parameters:
  • instance_name – instance name to delete
  • wait – wait for delete to complete
get_instance(instance_id)

Get an existing instance.

Parameters:instance_id – instance name to get
Returns:The existing instance as a LXD instance object
image_serial(image_id)

Find the image serial of a given LXD image.

Parameters:image_id – string, LXD image fingerprint
Returns:string, serial of latest image
init(name, image_id, ephemeral=False, network=None, storage=None, inst_type=None, profile_list=None, user_data=None, config_dict=None, execute_via_ssh=True)

Init a container.

This will initialize a container, but not launch or start it. If no remote is specified pycloudlib default to daily images.

Parameters:
  • name – string, what to call the instance
  • image_id – string, [<remote>:]<image identifier>, the image to launch
  • ephemeral – boolean, ephemeral, otherwise persistent
  • network – string, optional, network name to use
  • storage – string, optional, storage name to use
  • inst_type – string, optional, type to use
  • profile_list – list, optional, profile(s) to use
  • user_data – used by cloud-init to run custom scripts/configuration
  • config_dict – dict, optional, configuration values to pass
  • execute_via_ssh – bool, optional, execute commands on the instance via SSH if True (the default)
Returns:

The created LXD instance object

launch(image_id, instance_type=None, user_data=None, wait=True, name=None, ephemeral=False, network=None, storage=None, profile_list=None, config_dict=None, execute_via_ssh=True, **kwargs)

Set up and launch a container.

This will init and start a container with the provided settings. If no remote is specified pycloudlib defaults to daily images.

Parameters:
  • image_id – string, [<remote>:]<image>, the image to launch
  • instance_type – string, type to use
  • user_data – used by cloud-init to run custom scripts/configuration
  • wait – boolean, wait for instance to start
  • name – string, what to call the instance
  • ephemeral – boolean, ephemeral, otherwise persistent
  • network – string, network name to use
  • storage – string, storage name to use
  • profile_list – list, profile(s) to use
  • config_dict – dict, configuration values to pass
  • execute_via_ssh – bool, optional, execute commands on the instance via SSH if True (the default)
Returns:

The created LXD instance object

list_keys()

List ssh key names present on the cloud for accessing instances.

Returns:A list of strings of key pair names accessible to the cloud.
released_image(release, arch='amd64')

Find the LXD fingerprint of the latest released image.

Parameters:
  • release – string, Ubuntu release to look for
  • arch – string, architecture to use
Returns:

string, LXD fingerprint of latest image

snapshot(instance, clean=True, name=None)

Take a snapshot of the passed in instance for use as image.

Parameters:
  • instance (LXDInstance) – The instance to create an image from
  • clean – Whether to call cloud-init clean before creation
  • wait – Whether to wait until before image is created before returning
  • name – Name of the new image
  • stateful – Whether to use an LXD stateful snapshot
use_key(public_key_path, private_key_path=None, name=None)

Use an existing key.

Parameters:
  • public_key_path – path to the public key to upload
  • private_key_path – path to the private key
  • name – name to reference key by
class pycloudlib.lxd.cloud.LXDContainer(tag, timestamp_suffix=True, config_file: Union[pathlib.Path, _io.StringIO] = None)[source]

Bases: pycloudlib.lxd.cloud._BaseLXD

LXD Containers Cloud Class.

CONTAINER_HASH_KEY = 'combined_squashfs_sha256'
__init__(tag, timestamp_suffix=True, config_file: Union[pathlib.Path, _io.StringIO] = None)

Initialize base cloud class.

Parameters:
  • tag – string used to name and tag resources with
  • timestamp_suffix – Append a timestamped suffix to the tag string.
  • config_file – path to pycloudlib configuration file
clone(base, new_instance_name)

Create copy of an existing instance or snapshot.

Uses the lxc copy command to create a copy of an existing instance or a snapshot. To clone a snapshot then the base is instance_name/snapshot_name otherwise if base is only an existing instance it will clone an instance.

Parameters:
  • base – base instance or instance/snapshot
  • new_instance_name – name of new instance
Returns:

The created LXD instance object

create_key_pair()

Create and set a ssh key pair for a cloud instance.

Returns:A tuple containing the public and private key created
create_profile(profile_name, profile_config, force=False)

Create a lxd profile.

Create a lxd profile and populate it with the given profile config. If the profile already exists, we will not recreate it, unless the force parameter is set to True.

Parameters:
  • profile_name – Name of the profile to be created
  • profile_config – Config to be added to the new profile
  • force – Force the profile creation if it already exists
daily_image(release, arch='amd64')

Find the LXD fingerprint of the latest daily image.

Parameters:
  • release – string, Ubuntu release to look for
  • arch – string, architecture to use
Returns:

string, LXD fingerprint of latest image

delete_image(image_id)

Delete the image.

Parameters:image_id – string, LXD image fingerprint
delete_instance(instance_name, wait=True)

Delete an instance.

Parameters:
  • instance_name – instance name to delete
  • wait – wait for delete to complete
get_instance(instance_id)

Get an existing instance.

Parameters:instance_id – instance name to get
Returns:The existing instance as a LXD instance object
image_serial(image_id)

Find the image serial of a given LXD image.

Parameters:image_id – string, LXD image fingerprint
Returns:string, serial of latest image
init(name, image_id, ephemeral=False, network=None, storage=None, inst_type=None, profile_list=None, user_data=None, config_dict=None, execute_via_ssh=True)

Init a container.

This will initialize a container, but not launch or start it. If no remote is specified pycloudlib default to daily images.

Parameters:
  • name – string, what to call the instance
  • image_id – string, [<remote>:]<image identifier>, the image to launch
  • ephemeral – boolean, ephemeral, otherwise persistent
  • network – string, optional, network name to use
  • storage – string, optional, storage name to use
  • inst_type – string, optional, type to use
  • profile_list – list, optional, profile(s) to use
  • user_data – used by cloud-init to run custom scripts/configuration
  • config_dict – dict, optional, configuration values to pass
  • execute_via_ssh – bool, optional, execute commands on the instance via SSH if True (the default)
Returns:

The created LXD instance object

launch(image_id, instance_type=None, user_data=None, wait=True, name=None, ephemeral=False, network=None, storage=None, profile_list=None, config_dict=None, execute_via_ssh=True, **kwargs)

Set up and launch a container.

This will init and start a container with the provided settings. If no remote is specified pycloudlib defaults to daily images.

Parameters:
  • image_id – string, [<remote>:]<image>, the image to launch
  • instance_type – string, type to use
  • user_data – used by cloud-init to run custom scripts/configuration
  • wait – boolean, wait for instance to start
  • name – string, what to call the instance
  • ephemeral – boolean, ephemeral, otherwise persistent
  • network – string, network name to use
  • storage – string, storage name to use
  • profile_list – list, profile(s) to use
  • config_dict – dict, configuration values to pass
  • execute_via_ssh – bool, optional, execute commands on the instance via SSH if True (the default)
Returns:

The created LXD instance object

list_keys()

List ssh key names present on the cloud for accessing instances.

Returns:A list of strings of key pair names accessible to the cloud.
released_image(release, arch='amd64')

Find the LXD fingerprint of the latest released image.

Parameters:
  • release – string, Ubuntu release to look for
  • arch – string, architecture to use
Returns:

string, LXD fingerprint of latest image

snapshot(instance, clean=True, name=None)

Take a snapshot of the passed in instance for use as image.

Parameters:
  • instance (LXDInstance) – The instance to create an image from
  • clean – Whether to call cloud-init clean before creation
  • wait – Whether to wait until before image is created before returning
  • name – Name of the new image
  • stateful – Whether to use an LXD stateful snapshot
use_key(public_key_path, private_key_path=None, name=None)

Use an existing key.

Parameters:
  • public_key_path – path to the public key to upload
  • private_key_path – path to the private key
  • name – name to reference key by
class pycloudlib.lxd.cloud.LXDVirtualMachine(tag, timestamp_suffix=True, config_file: Union[pathlib.Path, _io.StringIO] = None)[source]

Bases: pycloudlib.lxd.cloud._BaseLXD

LXD Virtual Machine Cloud Class.

DISK1_HASH_KEY = 'combined_disk1-img_sha256'
DISK_KVM_HASH_KEY = 'combined_disk-kvm-img_sha256'
DISK_UEFI1_KEY = 'combined_uefi1-img_sha256'
__init__(tag, timestamp_suffix=True, config_file: Union[pathlib.Path, _io.StringIO] = None)

Initialize base cloud class.

Parameters:
  • tag – string used to name and tag resources with
  • timestamp_suffix – Append a timestamped suffix to the tag string.
  • config_file – path to pycloudlib configuration file
build_necessary_profiles(image_id)[source]

Build necessary profiles to launch the LXD instance.

Parameters:image_id – string, [<remote>:]<release>, the image to build profiles for
Returns:A list containing the profiles created
clone(base, new_instance_name)

Create copy of an existing instance or snapshot.

Uses the lxc copy command to create a copy of an existing instance or a snapshot. To clone a snapshot then the base is instance_name/snapshot_name otherwise if base is only an existing instance it will clone an instance.

Parameters:
  • base – base instance or instance/snapshot
  • new_instance_name – name of new instance
Returns:

The created LXD instance object

create_key_pair()

Create and set a ssh key pair for a cloud instance.

Returns:A tuple containing the public and private key created
create_profile(profile_name, profile_config, force=False)

Create a lxd profile.

Create a lxd profile and populate it with the given profile config. If the profile already exists, we will not recreate it, unless the force parameter is set to True.

Parameters:
  • profile_name – Name of the profile to be created
  • profile_config – Config to be added to the new profile
  • force – Force the profile creation if it already exists
daily_image(release, arch='amd64')

Find the LXD fingerprint of the latest daily image.

Parameters:
  • release – string, Ubuntu release to look for
  • arch – string, architecture to use
Returns:

string, LXD fingerprint of latest image

delete_image(image_id)

Delete the image.

Parameters:image_id – string, LXD image fingerprint
delete_instance(instance_name, wait=True)

Delete an instance.

Parameters:
  • instance_name – instance name to delete
  • wait – wait for delete to complete
get_instance(instance_id)

Get an existing instance.

Parameters:instance_id – instance name to get
Returns:The existing instance as a LXD instance object
image_serial(image_id)

Find the image serial of a given LXD image.

Parameters:image_id – string, LXD image fingerprint
Returns:string, serial of latest image
init(name, image_id, ephemeral=False, network=None, storage=None, inst_type=None, profile_list=None, user_data=None, config_dict=None, execute_via_ssh=True)

Init a container.

This will initialize a container, but not launch or start it. If no remote is specified pycloudlib default to daily images.

Parameters:
  • name – string, what to call the instance
  • image_id – string, [<remote>:]<image identifier>, the image to launch
  • ephemeral – boolean, ephemeral, otherwise persistent
  • network – string, optional, network name to use
  • storage – string, optional, storage name to use
  • inst_type – string, optional, type to use
  • profile_list – list, optional, profile(s) to use
  • user_data – used by cloud-init to run custom scripts/configuration
  • config_dict – dict, optional, configuration values to pass
  • execute_via_ssh – bool, optional, execute commands on the instance via SSH if True (the default)
Returns:

The created LXD instance object

launch(image_id, instance_type=None, user_data=None, wait=True, name=None, ephemeral=False, network=None, storage=None, profile_list=None, config_dict=None, execute_via_ssh=True, **kwargs)

Set up and launch a container.

This will init and start a container with the provided settings. If no remote is specified pycloudlib defaults to daily images.

Parameters:
  • image_id – string, [<remote>:]<image>, the image to launch
  • instance_type – string, type to use
  • user_data – used by cloud-init to run custom scripts/configuration
  • wait – boolean, wait for instance to start
  • name – string, what to call the instance
  • ephemeral – boolean, ephemeral, otherwise persistent
  • network – string, network name to use
  • storage – string, storage name to use
  • profile_list – list, profile(s) to use
  • config_dict – dict, configuration values to pass
  • execute_via_ssh – bool, optional, execute commands on the instance via SSH if True (the default)
Returns:

The created LXD instance object

list_keys()

List ssh key names present on the cloud for accessing instances.

Returns:A list of strings of key pair names accessible to the cloud.
released_image(release, arch='amd64')

Find the LXD fingerprint of the latest released image.

Parameters:
  • release – string, Ubuntu release to look for
  • arch – string, architecture to use
Returns:

string, LXD fingerprint of latest image

snapshot(instance, clean=True, name=None)

Take a snapshot of the passed in instance for use as image.

Parameters:
  • instance (LXDInstance) – The instance to create an image from
  • clean – Whether to call cloud-init clean before creation
  • wait – Whether to wait until before image is created before returning
  • name – Name of the new image
  • stateful – Whether to use an LXD stateful snapshot
use_key(public_key_path, private_key_path=None, name=None)

Use an existing key.

Parameters:
  • public_key_path – path to the public key to upload
  • private_key_path – path to the private key
  • name – name to reference key by