Skip to content

Commit

Permalink
Merge pull request #54 from lobbyboy-ssh/is-available
Browse files Browse the repository at this point in the history
Add a `Provider.is_available` method
  • Loading branch information
luxiaba committed Dec 14, 2021
2 parents 1ea3077 + 648157c commit 26b94d2
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 27 deletions.
19 changes: 9 additions & 10 deletions lobbyboy/contrib/provider/footloose.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path
from dataclasses import dataclass
import subprocess
import sys
from typing import List

from paramiko import Channel
Expand All @@ -27,17 +28,15 @@ class FootlooseConfig(LBConfigProvider):
class FootlooseProvider(BaseProvider):
config = FootlooseConfig

# TODO add to pre hook
def check_footloose_executable(self):
process = subprocess.run(["footloose", "-h"])
if process.returncode != 0:
raise FootlooseException(
(
"footloose executable is not exist! "
"Please install footloose via "
"`GO111MODULE=on go get github.com/weaveworks/footloose`"
)
def is_available(self) -> bool:
if not self.check_command(["footloose", "-h"]):
print(
"footloose executable is not exist! "
"Please install footloose via "
"`GO111MODULE=on go get github.com/weaveworks/footloose`",
file=sys.stderr,
)
return False
return True

def create_server(self, channel: Channel) -> LBServerMeta:
Expand Down
19 changes: 9 additions & 10 deletions lobbyboy/contrib/provider/ignite.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from pathlib import Path
import subprocess
import sys
from typing import List
from dataclasses import dataclass

Expand Down Expand Up @@ -30,17 +31,15 @@ class IgniteConfig(LBConfigProvider):
class IgniteProvider(BaseProvider):
config = IgniteConfig

# TODO add to pre hook
def check_ignite_executable(self):
process = subprocess.run(["ignite", "-h"])
if process.returncode != 0:
raise IgniteException(
(
"ignite executable is not exist! "
"Please install ignite via the instrution on "
"https://ignite.readthedocs.io/en/stable/installation/"
)
def is_available(self) -> bool:
if not self.check_command(["ignite", "-h"]):
print(
"ignite executable is not exist! "
"Please install ignite via the instrution on "
"https://ignite.readthedocs.io/en/stable/installation/",
file=sys.stderr,
)
return False
return True

def create_server(self, channel: Channel) -> LBServerMeta:
Expand Down
12 changes: 6 additions & 6 deletions lobbyboy/contrib/provider/multipass.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
from pathlib import Path
import subprocess
import sys
from typing import List
from dataclasses import dataclass

Expand Down Expand Up @@ -32,13 +33,12 @@ class MultipassConfig(LBConfigProvider):
class MultipassProvider(BaseProvider):
config = MultipassConfig

# TODO add to pre hook
def check_multipass_executable(self):
process = subprocess.run(["multipass", "-h"])
if process.returncode != 0:
raise MultipassException(
("multipass executable is not exist! " "Please install it via `snap install multipass`")
def is_available(self) -> bool:
if not self.check_command(["multipass", "-h"]):
print(
"multipass executable is not exist! " "Please install it via `snap install multipass`", file=sys.stderr
)
return False
return True

def create_server(self, channel: Channel) -> LBServerMeta:
Expand Down
4 changes: 3 additions & 1 deletion lobbyboy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ def load_providers(provider_configs: Dict[str, LBConfigProvider], head_workspace

module = importlib.import_module(module_path)
provider_cls = getattr(module, classname)
_providers[name] = provider_cls(name=name, config=config, workspace=head_workspace.joinpath(name))
provider = provider_cls(name=name, config=config, workspace=head_workspace.joinpath(name))
if provider.is_available():
_providers[name] = provider

logger.info(f"{len(_providers)} providers loaded: {_providers.keys()}")
return _providers
Expand Down
15 changes: 15 additions & 0 deletions lobbyboy/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from datetime import datetime
from pathlib import Path
import logging
import subprocess
import time
from typing import List, Dict, Callable

Expand Down Expand Up @@ -102,6 +103,13 @@ def load_raw_server(server_workspace: Path):
logger.debug(f"load server data from {_path}")
return json.load(f)

def is_available(self) -> bool:
"""
Returns:
bool: True if this provider is available, False to disable it
"""
return True

@abstractmethod
def create_server(self, channel: Channel) -> LBServerMeta:
"""
Expand Down Expand Up @@ -167,3 +175,10 @@ def ssh_server_command(self, meta: LBServerMeta, pri_key_path: Path = None) -> L

def get_bill(self):
...

def check_command(self, command: List[str]) -> bool:
try:
process = subprocess.run(command, capture_output=True)
except FileNotFoundError:
return False
return process.returncode == 0
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* [DigitalOcean Provider](#digitalocean-provider)
* [Linode Provider](#linode-provider)
* [Ignite(Firecracker) Provider](#ignitefirecracker-provider)
* [Multipass Provider](#multipass-provider)
* [Write Your Own Providers](#write-your-own-providers)
* [Publish Your Own Providers](#publish-your-own-providers)
* [FAQ](#faq)
Expand Down Expand Up @@ -260,6 +261,15 @@ To make a new Provider work, you need to extend base class
`lobbyboy.provider.BaseProvider``, implement 2 methods:

```python
def is_available(self) -> bool:
"""
Override this method to check for requirements of this provider
Returns:
bool: True if this provider is available, False to disable it
"""
return True

def create_server(self, channel: Channel) -> LBServerMeta:
"""
Args:
Expand Down

0 comments on commit 26b94d2

Please sign in to comment.