diff --git a/craft_providers/lxd/installer.py b/craft_providers/lxd/installer.py index 71e3c4c8..d49b12c5 100644 --- a/craft_providers/lxd/installer.py +++ b/craft_providers/lxd/installer.py @@ -79,7 +79,7 @@ def install(sudo: bool = True) -> str: return lxd.version() -def is_initialized(*, remote: str, lxc: LXC) -> bool: +def is_initialized(*, remote: str, project: str, lxc: LXC) -> bool: """Verify that LXD has been initialized and configuration looks valid. If LXD has been installed but the user has not initialized it (lxd init), @@ -153,7 +153,11 @@ def is_user_permitted() -> bool: def ensure_lxd_is_ready( - *, remote: str = "local", lxc: LXC = LXC(), lxd: LXD = LXD() + *, + remote: str = "local", + project: str = "default", + lxc: LXC = LXC(), + lxd: LXD = LXD(), ) -> None: """Ensure LXD is ready for use. @@ -185,7 +189,7 @@ def ensure_lxd_is_ready( ), ) - if not is_initialized(lxc=lxc, remote=remote): + if not is_initialized(lxc=lxc, project=project, remote=remote): raise errors.LXDError( brief="LXD has not been properly initialized.", details=( diff --git a/craft_providers/lxd/lxd_provider.py b/craft_providers/lxd/lxd_provider.py index ac6b862e..bae50954 100644 --- a/craft_providers/lxd/lxd_provider.py +++ b/craft_providers/lxd/lxd_provider.py @@ -26,7 +26,7 @@ from craft_providers.base import Base from craft_providers.errors import BaseConfigurationError -from .errors import LXDError, LXDUnstableImageError +from .errors import LXD_INSTALL_HELP, LXDError, LXDUnstableImageError from .installer import ensure_lxd_is_ready, install, is_installed from .launcher import launch from .lxc import LXC @@ -126,6 +126,26 @@ def launched_environment( :raises LXDError: if instance cannot be configured and launched. """ + projects = self.lxc.project_list(remote=self.lxd_remote) + if self.lxd_project in projects: + devices = self.lxc.profile_show( + project=self.lxd_project, profile="default", remote=self.lxd_remote + ).get("devices") + if not devices: + # Project "rockcraft" exists but the default profile is ill-formed, + # delete the project and start over. + raise LXDError( + brief="LXD project has an ill-formed default profile.", + details=( + f"The default profile in the LXD project '{self.lxd_project}' " + "has an empty devices section." + ), + resolution=( + "Delete the 'rockcraft' LXD project, it will be recreated in the " + "next execution.\n" + LXD_INSTALL_HELP + ), + ) + if build_base: logger.warning( "Deprecated: Parameter 'build_base' is deprecated and should "