Skip to content

Commit

Permalink
display sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
hhuuggoo committed Apr 1, 2024
1 parent 217f131 commit 3185f36
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
11 changes: 10 additions & 1 deletion saturn_client/cli/commands.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
import pprint

from saturn_client.run import batch, setup_file_syncs, split

Expand All @@ -21,7 +22,7 @@
ResourceStatus,
SaturnConnection,
ResourceType,
SaturnHTTPError,
SaturnHTTPError, ServerOptionTypes,
)


Expand Down Expand Up @@ -476,6 +477,14 @@ def batch_cli(input_file):
batch_info = deserialize(input_file)
batch(batch_info)

@cli.command("options")
@click.option("--option-type", default=ServerOptionTypes.SIZES)
@click.option("--glob")
def options_cli(option_type: str = ServerOptionTypes.SIZES, glob: Optional[str] = None):
client = SaturnConnection()
results = client.list_options(option_type, glob=glob)
for r in results:
click.echo(pprint.pformat(r))

@cli.command("split")
@click.argument("recipe_template")
Expand Down
28 changes: 28 additions & 0 deletions saturn_client/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ def lookup(cls, value: str):
raise SaturnError(f'resource type "{value}" not found')


class ServerOptionTypes:
AUTO_SHUTOFF = "auto_shutoff"
DISK_SPACE = "disk_space"
SIZES = "sizes"

@classmethod
def values(cls) -> List[str]:
return [cls.AUTO_SHUTOFF, cls.DISK_SPACE, cls.SIZES]


class ResourceStatus:
"""
Enum for resource statuses
Expand Down Expand Up @@ -206,6 +216,24 @@ def __init__(
# test connection to raise errors early
self._saturn_version = self._get_saturn_version()

def list_options(self, option_type: str, glob: Optional[str] = None) -> List:
if option_type not in ServerOptionTypes.values():
raise ValueError(f"unknown option {option_type}. must be one of {ServerOptionTypes.values()}")
url = urljoin(self.url, "api/info/servers")
response = requests.get(url, headers=self.settings.headers)
if not response.ok:
raise SaturnHTTPError.from_response(response)
results = response.json()[option_type]
if option_type != ServerOptionTypes.SIZES:
if glob:
results = [x for x in results if fnmatch(x, glob)]
else:
results = results.values()
if glob:
results = [x for x in results if fnmatch(x["name"], glob)]
results = sorted(results, key=lambda x: (x['gpu'], x['cores']))
return results

@property
def orgs(self) -> List[Dict[str, Any]]:
url = urljoin(self.url, "api/orgs")
Expand Down

0 comments on commit 3185f36

Please sign in to comment.