Skip to content

Commit

Permalink
feat(provider): list provider instances
Browse files Browse the repository at this point in the history
Fixes #639
  • Loading branch information
lengau committed Aug 30, 2024
1 parent 7784b7b commit d101a11
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 0 deletions.
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]:
"""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

0 comments on commit d101a11

Please sign in to comment.