Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(provider): list provider instances #645

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions craft_providers/lxd/lxd_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ def __init__(
else:
self.lxc = lxc

def __repr__(self) -> str:
return f"{self.__class__.__name__}(name={self.name!r}, default_command_environment={self.default_command_environment!r}, project={self.project!r}, remote={self.remote!r})"

def _set_instance_name(self) -> None:
"""Convert a name to a LXD-compatible name.

Expand Down
10 changes: 10 additions & 0 deletions craft_providers/lxd/lxd_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import contextlib
import logging
import pathlib
from collections.abc import Collection
from datetime import timedelta
from typing import Iterator

Expand Down Expand Up @@ -87,6 +88,15 @@ def is_provider_installed(cls) -> bool:
"""
return is_installed()

def list_instances(self) -> Collection[LXDInstance]:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Add a test, at least to make sure we detect that this function should be updated if/when the LXDInstance class is updated? (same for the multipass provider)

"""Get a collection of existing instances for this LXD provider."""
return [
LXDInstance(name=name, project=self.lxd_project, remote=self.lxd_remote)
for name in self.lxc.list_names(
project=self.lxd_project, remote=self.lxd_remote
)
]

def create_environment(self, *, instance_name: str) -> Executor:
"""Create a bare environment for specified base.

Expand Down
3 changes: 3 additions & 0 deletions craft_providers/multipass/multipass_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ def __init__(
else:
self._multipass = Multipass()

def __repr__(self) -> str:
return f"{self.__class__.__name__}(name={self.name!r})"

def _create_temp_file(self) -> str:
"""Create a temporary file inside the instance owned by the `ubuntu` user.

Expand Down
8 changes: 8 additions & 0 deletions craft_providers/multipass/multipass_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import contextlib
import logging
import pathlib
from collections.abc import Collection
from dataclasses import dataclass
from enum import Enum
from typing import Dict, Iterator
Expand Down Expand Up @@ -164,6 +165,13 @@ def ensure_provider_is_available(cls) -> None:
install()
ensure_multipass_is_ready()

def list_instances(self) -> Collection[MultipassInstance]:
"""Get a collection of existing multipass VMs."""
return [
MultipassInstance(name=name, multipass=self.multipass)
for name in self.multipass.list()
]

def create_environment(self, *, instance_name: str) -> Executor:
"""Create a bare environment for specified base.

Expand Down
5 changes: 5 additions & 0 deletions craft_providers/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import logging
import pathlib
from abc import ABC, abstractmethod
from collections.abc import Collection
from typing import Generator

from .base import Base
Expand Down Expand Up @@ -78,6 +79,10 @@ def is_provider_installed(cls) -> bool:
:returns: True if installed.
"""

@abstractmethod
def list_instances(self) -> Collection[Executor]:
"""Get a collection of existing instances for this provider."""

@abstractmethod
def create_environment(self, *, instance_name: str) -> Executor:
"""Create a bare environment for specified base.
Expand Down
Loading