Skip to content

Commit

Permalink
fix: handle ill-formed profiles created by older rockcraft
Browse files Browse the repository at this point in the history
Older versions of rockcraft may have created a rockcraft lxd project
containing a broken default profile. If this is the case, tell the
user to delete the project and start over.

Signed-off-by: Claudio Matsuoka <claudio.matsuoka@canonical.com>
  • Loading branch information
cmatsuoka committed Jun 24, 2024
1 parent 0b8d058 commit 8b00682
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
10 changes: 7 additions & 3 deletions craft_providers/lxd/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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=(
Expand Down
22 changes: 21 additions & 1 deletion craft_providers/lxd/lxd_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 "
Expand Down

0 comments on commit 8b00682

Please sign in to comment.