craft_providers.lxd.lxc module

LXC wrapper.

class craft_providers.lxd.lxc.LXC(*, lxc_path=PosixPath('lxc'))[source]

Bases: object

Wrapper for lxc command-line interface.

Parameters

lxc_path (Path) –

config_device_add_disk(*, instance_name, source, path, device, project='default', remote='local')[source]

Mount host source directory to target mount point.

Parameters
  • instance_name (str) – Name of instance.

  • source (Path) – Host path.

  • path (PurePath) – Mount target in instance.

  • device (str) – Name of device.

  • project (str) – Name of LXD project.

  • remote (str) – Name of LXD remote.

Raises

LXDError – on unexpected error.

Return type

None

config_device_remove(*, instance_name, device, project='default', remote='local')[source]

Mount host source directory to target mount point.

Parameters
  • instance_name (str) – Name of instance.

  • device (str) – Name of device.

  • project (str) – Name of LXD project.

  • remote (str) – Name of LXD remote.

Raises

LXDError – on unexpected error.

Return type

None

config_device_show(*, instance_name, project='default', remote='local')[source]

Show full device configuration.

Parameters
  • instance_name (str) – Name of instance.

  • project (str) – Name of LXD project.

  • remote (str) – Name of LXD remote.

Raises

LXDError – on unexpected error.

Return type

Dict[str, Any]

config_set(*, instance_name, key, value, project='default', remote='local')[source]

Set instance_name configuration key.

Parameters
  • instance_name (str) – Name of instance.

  • key (str) – Config key name.

  • value (str) – Config key value.

  • project (str) – Name of LXD project.

  • remote (str) – Name of LXD remote.

Raises

LXDError – on unexpected error.

Return type

None

copy(*, source_remote='local', source_instance_name, destination_remote='local', destination_instance_name, project='default')[source]

Copy instances within or in between LXD servers.

Calls lxc copy <source_remote>:<source_instance_name> <destination_remote>: destination_instance_name>. A running instance can be copied but the manpages state “This may cause data corruption or data loss depending on the used filesystem and applications. Use with care.”

Parameters
  • source_remote (str) – Name of source LXD remote.

  • source_instance_name (str) – Name of instance to copy from.

  • destination_remote (str) – Name of remote LXD destination.

  • destination_instance_name (str) – Name of instance to copy to.

  • project (str) – Name of LXD project.

Raises

LXDError – on unexpected error.

Return type

None

delete(*, instance_name, force=False, project='default', remote='local')[source]

Delete instance.

Parameters
  • instance_name (str) – Name of instance.

  • force (bool) – Force deletion if running.

  • project (str) – Name of LXD project.

  • remote (str) – Name of LXD remote.

Raises

LXDError – on unexpected error.

Return type

None

exec(*, command, instance_name, cwd=None, mode=None, project='default', remote='local', runner=<function run>, **kwargs)[source]

Execute command in instance_name with specified runner.

Parameters
  • command (List[str]) – Command to execute in the instance.

  • instance_name (str) – Name of instance to execute in.

  • cwd (Optional[str]) – Optional current working directory for command.

  • mode (Optional[str]) – Override terminal mode Valid options include: “auto”, “interactive”, “non-interactive”. lxd default is “auto”.

  • project (str) – Name of LXD project.

  • remote (str) – Name of LXD remote.

  • runner (Callable) – Execution function to invoke, e.g. subprocess.run or Popen. First argument is finalized command with the attached kwargs.

  • kwargs – Additional kwargs for runner.

Returns

Runner’s instance.

file_pull(*, instance_name, source, destination, create_dirs=False, recursive=False, project='default', remote='local')[source]

Retrieve file from instance_name.

Parameters
  • instance_name (str) – Name of instance.

  • source (PurePath) – Path in environment to pull.

  • destination (Path) – Path in host to write to.

  • create_dirs (bool) – Create any directories necessary.

  • recursive (bool) – Recursively transfer files.

  • project (str) – Name of LXD project.

  • remote (str) – Name of LXD remote.

Raises

LXDError – on unexpected error.

Return type

None

file_push(*, instance_name, source, destination, create_dirs=False, recursive=False, gid=None, uid=None, mode=None, project='default', remote='local')[source]

Create file with content and file mode.

Parameters
  • instance_name (str) – Name of instance to push file to.

  • source (Path) – Path in host to push.

  • destination (PurePath) – Path in environment to write to.

  • create_dirs (bool) – Create any directories necessary.

  • recursive (bool) – Recursively transfer files.

  • gid (Optional[int]) – Optional gid to set on push (lxd’s default is -1).

  • uid (Optional[int]) – Optional uid to set on push (lxd’s default is -1).

  • mode (Optional[str]) – Optional file mode to set on file.

  • project (str) – Name of LXD project.

  • remote (str) – Name of LXD remote.

Raises

LXDError – on unexpected error.

Return type

None

has_image(image_name, *, project='default', remote='local')[source]

Check if image with given alias name is present.

Parameters
  • image_name – Name of image alias.

  • project (str) – Name of LXD project.

  • remote (str) – Name of LXD remote.

Return type

bool

image_copy(*, image, image_remote, alias=None, project='default', remote='local')[source]

Copy image.

Parameters
  • instance_name – Optional instance name.

  • alias (Optional[str]) – New alias to add to image.

  • image (str) – Image to copy.

  • project (str) – Name of LXD project.

  • remote (str) – Name of LXD remote.

  • image_remote (str) –

Raises

LXDError – on unexpected error.

Return type

None

image_delete(*, image, project='default', remote='local')[source]

Delete image.

Parameters
  • image (str) – Image to delete.

  • project (str) – Name of LXD project.

  • remote (str) – Name of LXD remote.

Raises

LXDError – on unexpected error.

Return type

None

image_list(*, project='default', remote='local')[source]

List images.

Parameters
  • project (str) – Name of LXD project.

  • remote (str) – Name of LXD remote.

Return type

List[Dict[str, Any]]

info(*, instance_name=None, project='default', remote='local')[source]

Show instance or server information.

Parameters
  • instance_name (Optional[str]) – Optional instance name.

  • project (str) – Name of LXD project.

  • remote (str) – Name of LXD remote.

Raises

LXDError – on unexpected error.

Return type

Dict[str, Any]

launch(*, instance_name, image, image_remote, config_keys=None, ephemeral=False, project='default', remote='local')[source]

Launch instance.

Parameters
  • instance_name (str) – Name of instance to launch.

  • image (str) – Name of image to use.

  • image_remote (str) – Name of image’s remote.

  • config_keys (Optional[Dict[str, str]]) – Configuration keys to set.

  • ephemeral (bool) – Use ephemeral instance.

  • project (str) – Name of LXD project.

  • remote (str) – Name of LXD remote.

Raises

LXDError – on unexpected error.

Return type

None

list(*, project='default', remote='local')[source]

List instances and their status.

Parameters
  • project (str) – Name of LXD project.

  • remote (str) – Name of LXD remote.

Return type

List[Dict[str, Any]]

Returns

List of containers and their info.

Raises

LXDError – on unexpected error.

list_names(*, project='default', remote='local')[source]

List container names.

A helper to get a list of container names from list().

Parameters
  • project (str) – Name of LXD project.

  • remote (str) – Name of LXD remote.

Return type

List[str]

Returns

List of container names.

Raises

LXDError – on unexpected error.

profile_edit(*, profile, config, project='default', remote='local')[source]

Set profile configuration.

Parameters
  • profile (str) – Name of profile.

  • project (str) – Name of LXD project.

  • remote (str) – Name of LXD remote.

  • config (Dict[str, Any]) –

Raises

LXDError – on unexpected error.

Return type

None

profile_show(*, profile, project='default', remote='local')[source]

Get profile configuration.

Parameters
  • profile (str) – Name of profile.

  • project (str) – Name of LXD project.

  • remote (str) – Name of LXD remote.

Raises

LXDError – on unexpected error.

Return type

Dict[str, Any]

project_create(*, project, remote='local')[source]

Create project.

Parameters
  • project (str) – Name of LXD project to create.

  • remote (str) – Name of LXD remote to create project on.

Raises

LXDError – on unexpected error.

Return type

None

project_delete(*, project, remote='local')[source]

Delete project, if it exists.

Parameters
  • project (str) – Name of LXD project to delete.

  • remote (str) – Name of LXD remote.

Raises

LXDError – on unexpected error.

Return type

None

project_list(remote='local')[source]

Get list of projects.

Parameters

remote (str) – Name of LXD remote to query.

Return type

List[str]

Returns

List of project names.

Raises

LXDError – on unexpected error.

publish(*, instance_name, alias=None, force=False, image_remote='local', project='default', remote='local')[source]

Publish image from instance.

Parameters
  • instance_name (str) – Name of instance to publish image from.

  • alias (Optional[str]) – New alias to define at target.

  • force (bool) – Force publishing of image, even if container is running.

  • image_remote (str) – Name of remote to publish image to.

  • project (str) – Name of LXD project.

  • remote (str) – Name of LXD remote instance is found on.

Raises

LXDError – on unexpected error.

Return type

None

remote_add(*, remote, addr, protocol='simplestreams')[source]

Add a public remote.

Parameters
  • remote (str) – Name of remote to add.

  • addr (str) – Address of remote.

  • protocol (str) – Name of protocol (“simplestreams” or “lxd”).

Raises

LXDError – on unexpected error.

Return type

None

remote_list()[source]

Get list of remotes.

Return type

Dict[str, Any]

Returns

dictionary with remote name mapping to config.

start(*, instance_name, project='default', remote='local')[source]

Start container.

Parameters
  • instance_name (str) – Name of instance to start.

  • project (str) – Name of LXD project.

  • remote (str) – Name of LXD remote.

Raises

LXDError – on unexpected error.

Return type

None

stop(*, instance_name, force=False, timeout=- 1, project='default', remote='local')[source]

Stop container.

Parameters
  • instance_name (str) – Name of instance to stop.

  • force (bool) – Force instance to stop.

  • timeout (int) – Timeout in seconds. -1 is no timeout.

  • project (str) – Name of LXD project.

  • remote (str) – Name of LXD remote.

Raises

LXDError – on unexpected error.

Return type

None

class craft_providers.lxd.lxc.StdinType(value)[source]

Bases: enum.Enum

Mappings for input stream to pass to stdin for lxc commands.

INTERACTIVE = -3
NULL = None
craft_providers.lxd.lxc.load_yaml(data)[source]

Load yaml without additional resolvers.

LXD may return YAML that has datetimes that are not valid when parsed to datetime.datetime(). Instead just use the base loader and avoid resolving this type (and others).