LXD Container Management Substrate
ObjLxc is a concrete implementation of the ObjSubstrate base class, designed to interact with and manage LXD (Linux Containers). It uses the pylxd library to communicate with the local LXD daemon.
This class allows you to perform standard lifecycle operations on LXD containers, such as creating, starting, stopping, and deleting them, all through the unified ObjSubstrate interface.
ObjLxc connects to the local LXD daemon by default and does not require any specific configuration in config.yaml. However, the machine running the script must have pylxd installed and be properly configured to communicate with LXD (e.g., the user must be in the lxd group).
ObjLxc provides concrete implementations for all the abstract methods in ObjSubstrate:
| Method | LXD Implementation Details |
|---|---|
connect() |
Initializes the pylxd.Client() and verifies the connection to the local LXD daemon. |
list_instances() |
Lists all containers, returning their name, status, and primary IPv4 address. |
get_instance(name) |
Retrieves detailed information for a specific container, including its configuration, devices, and real-time state (CPU, memory). |
create_instance(config) |
Creates a new container. The config dictionary should specify a name, and can optionally include image, profile, and other LXD-specific configuration. |
start_instance(name) |
Starts a stopped container. |
stop_instance(name, force) |
Stops a running container. The force parameter determines whether to stop gracefully or forcefully. |
delete_instance(name, force) |
Deletes a container. If the container is running, it will be stopped first. |
execute_command(name, command) |
Executes a command inside a running container and returns its stdout, stderr, and exit_code. |
ObjLxc.py is also an executable script that provides a command-line interface for managing LXD containers.
list-allLists all available LXD containers with their status and IP address.
python3 factory.deploy/ObjLxc.py list-all
createCreates a new container.
python3 factory.deploy/ObjLxc.py create <container-name> --image <image-alias> --profile <profile-name>
--image / -i: (Optional) The image to use (e.g., ubuntu/22.04). Defaults to ubuntu/22.04.--profile / -p: (Optional) The LXD profile to apply. Defaults to default.startStarts a specified container.
python3 factory.deploy/ObjLxc.py start <container-name>
stopStops a specified container.
python3-run factory.deploy/ObjLxc.py stop <container-name> [--force]
--force / -f: (Optional) Force the container to stop instead of shutting down gracefully.deleteDeletes a specified container.
python3 factory.deploy/ObjLxc.py delete <container-name>
statusGets detailed status and configuration information for a container.
python3 factory.deploy/ObjLxc.py status <container-name>
executeExecutes a command within a running container.
python3 factory.deploy/ObjLxc.py execute <container-name> -- <command> [args...]
Example:
python3 factory.deploy/ObjLxc.py execute my-container -- ls -l /tmp
verifyChecks the connection to the LXD daemon.
python3 factory.deploy/ObjLxc.py verify