Unverified Commit e91118f4 authored by Jakob Görgen's avatar Jakob Görgen
Browse files

cli/simbricks/cli/state: added admin client + turned functions into...

cli/simbricks/cli/state: added admin client + turned functions into properties; cli/simbricks/cli/commands/admin: added admin commands
parent 1d77610f
......@@ -22,7 +22,7 @@
from typer import Typer, Option
from typing_extensions import Annotated
from simbricks.cli.commands import audit, namespaces, runs
from simbricks.cli.commands import audit, admin, namespaces, runs
from simbricks.cli.state import state
from simbricks.cli.utils import async_cli
......@@ -30,16 +30,20 @@ app = Typer()
app.add_typer(namespaces.app, name="ns")
app.add_typer(runs.app, name="runs")
app.add_typer(audit.app, name="audit")
app.add_typer(admin.app, name="admin")
@app.callback()
@async_cli()
async def amain(
ns: Annotated[str, Option(help='Namespace to operate in.')] = 'foo/bar/baz',
ns: Annotated[str, Option(help="Namespace to operate in.")] = "foo/bar/baz",
):
state.namespace = ns
def main():
app()
if __name__ == "__main__":
main()
# Copyright 2024 Max Planck Institute for Software Systems, and
# National University of Singapore
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from typer import Typer, Option
from typing_extensions import Annotated
from ..state import state
from ..utils import async_cli, print_namespace_table
app = Typer(help="SimBricks admin commands.")
@app.command()
@async_cli()
async def ns_ls():
"""List all available namespaces."""
client = state.admin_client
namespaces = await client.get_all_ns()
print_namespace_table(namespaces)
@app.command()
@async_cli()
async def ns_ls_id(ident: int):
"""List namespace with given id ident."""
client = state.admin_client
namespace = await client.get_ns(ns_id=ident)
print_namespace_table([namespace])
@app.command()
@async_cli()
async def ns_create(name: str, parent_id: Annotated[int, Option(help="optional parent namesapce")] = None):
"""Create a new namespace."""
client = state.admin_client
namespace = await client.create_ns(parent_id=parent_id, name=name)
ns_id = namespace["id"]
print(f"Creating namespace {name} in {state.namespace}. New namespace: {ns_id}")
@app.command()
@async_cli()
async def ns_delete(ident: int):
"""Delete a namespace."""
client = state.admin_client
await client.delete(ns_id=ident)
print(f"Deleted namespace with id {ident}.")
......@@ -20,9 +20,7 @@
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from pathlib import Path
from typer import Typer, Option
from typing_extensions import Annotated
from typer import Typer
from ..state import state
from ..utils import async_cli
......
......@@ -20,34 +20,18 @@
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from pathlib import Path
from typer import Typer, Option
from typing_extensions import Annotated
from rich.table import Table
from rich.console import Console
from typer import Typer
from ..state import state
from ..utils import async_cli
from ..utils import async_cli, print_namespace_table
app = Typer(help="Managing SimBricks namespaces.")
def print_namespace_table(namespaces) -> None:
table = Table()
table.add_column("Id")
table.add_column("name")
table.add_column("parent")
for ns in namespaces:
table.add_row(str(ns["id"]), str(ns["name"]), str(ns["parent_id"]))
console = Console()
console.print(table)
@app.command()
@async_cli()
async def ls():
"""List available namespaces."""
client = state.ns_client()
client = state.ns_client
namespaces = await client.get_all()
print_namespace_table(namespaces)
......@@ -57,7 +41,7 @@ async def ls():
@async_cli()
async def ls_id(ident: int):
"""List namespace with given id ident."""
client = state.ns_client()
client = state.ns_client
namespace = await client.get_ns(ident)
print_namespace_table([namespace])
......@@ -67,7 +51,7 @@ async def ls_id(ident: int):
@async_cli()
async def ls_cur():
"""List current namespace."""
client = state.ns_client()
client = state.ns_client
namespace = await client.get_cur()
print_namespace_table([namespace])
......@@ -78,7 +62,7 @@ async def ls_cur():
async def create(name: str):
"""Create a new namespace."""
client = state.ns_client()
client = state.ns_client
# create namespace relative to current namespace
cur_ns = await client.get_cur()
......@@ -96,6 +80,6 @@ async def create(name: str):
async def delete(ident: int):
"""Delete a namespace."""
client = state.ns_client()
client = state.ns_client
await client.delete(ident)
print(f"Deleted namespace with id {ident}.")
......@@ -39,7 +39,7 @@ app = Typer(
@async_cli()
async def ls():
"""List runs."""
runs = await state.simbricks_client().get_runs()
runs = await state.simbricks_client.get_runs()
table = Table()
table.add_column('Id')
......@@ -56,13 +56,13 @@ async def ls():
@async_cli()
async def show(run_id: int):
"""Show individual run."""
run = await state.simbricks_client().get_run(run_id)
run = await state.simbricks_client.get_run(run_id)
print(run)
async def follow_run(run_id: int):
last_run = None
while True:
run = await state.simbricks_client().get_run(run_id)
run = await state.simbricks_client.get_run(run_id)
if not last_run or last_run['state'] != run['state']:
print(f'State:', run['state'])
if not last_run or (
......@@ -92,7 +92,7 @@ async def submit_script(
):
"""Submit a SimBricks python simulation script to run."""
system_client = state.simbricks_client()
system_client = state.simbricks_client
experiment_mod = load_mod.load_module(module_path=path)
instantiations = experiment_mod.instantiations
......
......@@ -21,29 +21,40 @@
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import os
from simbricks.client import BaseClient, NSClient, SimBricksClient
from simbricks.client import BaseClient, AdminClient, NSClient, SimBricksClient
class State():
class State:
def __init__(self):
self.namespace = ''
self.namespace = ""
self._base_client: BaseClient | None = None
self._admin_client: AdminClient = None
self._ns_client: NSClient | None = None
self._simbricks_client: SimBricksClient | None = None
@property
def base_client(self):
if self._base_client is None:
self._base_client = BaseClient()
return self._base_client
@property
def admin_client(self):
if self._admin_client is None:
self._admin_client = AdminClient(base_client=self.base_client)
return self._admin_client
@property
def ns_client(self):
if self._ns_client is None:
self._ns_client = NSClient(base_client=self.base_client(),
namespace=self.namespace)
self._ns_client = NSClient(base_client=self.base_client, namespace=self.namespace)
return self._ns_client
@property
def simbricks_client(self):
if self._simbricks_client is None:
self._simbricks_client = SimBricksClient(self.ns_client())
self._simbricks_client = SimBricksClient(self.ns_client)
return self._simbricks_client
state = State()
\ No newline at end of file
state = State()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment