craft_providers.lxd.lxd_instance module

LXD Instance Executor.

class craft_providers.lxd.lxd_instance.LXDInstance(*, name, default_command_environment=None, project='default', remote='local', lxc=None)[source]

Bases: craft_providers.executor.Executor

LXD Instance Lifecycle.

Parameters
  • name (str) –

  • default_command_environment (Optional[Dict[str, Optional[str]]]) –

  • project (str) –

  • remote (str) –

  • lxc (Optional[LXC]) –

delete(force=True)[source]

Delete instance.

Parameters

force (bool) – Delete even if running.

Raises

LXDError – On unexpected error.

Return type

None

execute_popen(command, *, cwd=None, env=None, **kwargs)[source]

Execute a command in instance, using subprocess.Popen().

The process’ environment will inherit the execution environment’s default environment (PATH, etc.), but can be additionally configured via env parameter.

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

  • env (Optional[Dict[str, Optional[str]]]) – Additional environment to set for process.

  • kwargs – Additional keyword arguments to pass.

  • cwd (Optional[Path]) –

Return type

Popen

Returns

Popen instance.

execute_run(command, *, cwd=None, env=None, **kwargs)[source]

Execute a command using subprocess.run().

The process’ environment will inherit the execution environment’s default environment (PATH, etc.), but can be additionally configured via env parameter.

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

  • env (Optional[Dict[str, Optional[str]]]) – Additional environment to set for process.

  • kwargs – Keyword args to pass to subprocess.run().

  • cwd (Optional[Path]) –

Return type

CompletedProcess

Returns

Completed process.

Raises

subprocess.CalledProcessError – if command fails and check is True.

exists()[source]

Check if instance exists.

Return type

bool

Returns

True if instance exists.

Raises

LXDError – On unexpected error.

is_mounted(*, host_source, target)[source]

Check if path is mounted at target.

Parameters
  • host_source (Path) – Host path to check.

  • target (PurePath) – Instance path to check.

Return type

bool

Returns

True if host_source is mounted at target.

Raises

LXDError – On unexpected error.

is_running()[source]

Check if instance is running.

Return type

bool

Returns

True if instance is running.

Raises

LXDError – On unexpected error.

launch(*, image, image_remote, map_user_uid=False, ephemeral=False, uid=None)[source]

Launch instance.

Parameters
  • image (str) – Image name to launch.

  • image_remote (str) – Image remote name.

  • map_user_id – Whether id mapping should be used.

  • uid (Optional[int]) – If map_user_id is True, the host user ID to map to instance root.

  • ephemeral (bool) – Flag to enable ephemeral instance.

  • map_user_uid (bool) –

Raises

LXDError – On unexpected error.

Return type

None

mount(*, host_source, target)[source]

Mount host source directory to target mount point.

Checks first to see if already mounted. The source will be mounted as a disk named “disk-{target.as_posix()}”.

Parameters
  • host_source (Path) – Host path to mount.

  • target (PurePath) – Instance path to mount to.

Raises

LXDError – On unexpected error.

Return type

None

pull_file(*, source, destination)[source]

Copy a file from the environment to host.

Parameters
  • source (PurePath) – Environment file to copy.

  • destination (Path) – Host file path to copy to. Parent directory (destination.parent) must exist.

Raises
  • FileNotFoundError – If source file or destination’s parent directory does not exist.

  • LXDError – On unexpected error copying file.

Return type

None

push_file(*, source, destination)[source]

Copy a file from the host into the environment.

The destination file is overwritten if it exists.

Parameters
  • source (Path) – Host file to copy.

  • destination (PurePath) – Target environment file path to copy to. Parent directory (destination.parent) must exist.

Raises
  • FileNotFoundError – If source file or destination’s parent directory does not exist.

  • LXDError – On unexpected error copying file.

Return type

None

push_file_io(*, destination, content, file_mode, group='root', user='root')[source]

Create or replace file with content and file mode.

Parameters
  • destination (PurePath) – Path to file.

  • content (BytesIO) – Contents of file.

  • file_mode (str) – File mode string (e.g. ‘0644’).

  • group (str) – File group owner/id.

  • user (str) – File user owner/id.

Raises

LXDError – On unexpected error.

Return type

None

start()[source]

Start instance.

Raises

LXDError – on unexpected error.

Return type

None

stop()[source]

Stop instance.

Raises

LXDError – on unexpected error.

Return type

None

supports_mount()[source]

Check if instance supports mounting from host.

Return type

bool

Returns

True if mount is supported.

unmount(target)[source]

Unmount mount target shared with host.

Parameters

target (Path) – Target shared with host to unmount.

Raises

LXDError – On failure to unmount target.

Return type

None

unmount_all()[source]

Unmount all mounts shared with host.

Raises

LXDError – On failure to unmount target.

Return type

None