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

symphony/cli: removed state and use client provider instead

parent d487c932
......@@ -23,7 +23,7 @@
from typer import Typer, Option
from typing_extensions import Annotated
from simbricks.cli.commands import audit, admin, namespaces, runs, systems, simulations, instantiations, runners
from simbricks.cli.state import state
from simbricks.client.provider import client_provider
from simbricks.cli.utils import async_cli
app = Typer()
......@@ -43,8 +43,8 @@ async def amain(
ns: Annotated[str, Option(help="Namespace to operate in.")] = "foo/bar/baz",
runner_ident: Annotated[int, Option(help="Runner ident to operate on.")] = -1,
):
state.namespace = ns
state.runner_id = runner_ident
client_provider.namespace = ns
client_provider.runner_id = runner_ident
def main():
......
......@@ -22,8 +22,7 @@
from typer import Typer, Option
from typing_extensions import Annotated
from ..state import state
from simbricks.client.provider import client_provider
from ..utils import async_cli, print_namespace_table
app = Typer(help="SimBricks admin commands.")
......@@ -33,7 +32,7 @@ app = Typer(help="SimBricks admin commands.")
@async_cli()
async def ns_ls():
"""List all available namespaces."""
client = state.admin_client
client = client_provider.admin_client
namespaces = await client.get_all_ns()
print_namespace_table(namespaces)
......@@ -42,7 +41,7 @@ async def ns_ls():
@async_cli()
async def ns_ls_id(ident: int):
"""List namespace with given id ident."""
client = state.admin_client
client = client_provider.admin_client
namespace = await client.get_ns(ns_id=ident)
print_namespace_table([namespace])
......@@ -51,16 +50,16 @@ async def ns_ls_id(ident: int):
@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
client = client_provider.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}")
print(f"Creating namespace {name} in {client_provider.namespace}. New namespace: {ns_id}")
@app.command()
@async_cli()
async def ns_delete(ident: int):
"""Delete a namespace."""
client = state.admin_client
client = client_provider.admin_client
await client.delete(ns_id=ident)
print(f"Deleted namespace with id {ident}.")
......@@ -21,7 +21,6 @@
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from typer import Typer
from ..state import state
from ..utils import async_cli
app = Typer(
......
......@@ -21,7 +21,7 @@
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from typer import Typer
from ..state import state
from simbricks.client.provider import client_provider
from ..utils import async_cli
from ..utils import print_instantiations_table
......@@ -32,7 +32,7 @@ app = Typer(help="Managing SimBricks Instantiations.")
@async_cli()
async def ls():
"""List Instantiations."""
insts = await state.simbricks_client.get_instantiations()
insts = await client_provider.simbricks_client.get_instantiations()
print_instantiations_table(insts)
......@@ -40,7 +40,7 @@ async def ls():
@async_cli()
async def show(inst_id: int):
"""Show individual Instantiation."""
inst = await state.simbricks_client.get_instantiation(instantiation_id=inst_id)
inst = await client_provider.simbricks_client.get_instantiation(instantiation_id=inst_id)
print_instantiations_table([inst])
......@@ -48,5 +48,5 @@ async def show(inst_id: int):
@async_cli()
async def delete(inst_id: int):
"""Delete an individual Instantiation."""
client = state.simbricks_client
client = client_provider.simbricks_client
await client.delete_instantiation(inst_id=inst_id)
......@@ -21,7 +21,7 @@
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from typer import Typer
from ..state import state
from simbricks.client.provider import client_provider
from ..utils import async_cli, print_namespace_table, print_members_table
app = Typer(help="Managing SimBricks namespaces.")
......@@ -31,7 +31,7 @@ app = Typer(help="Managing SimBricks namespaces.")
@async_cli()
async def ls():
"""List available namespaces."""
client = state.ns_client
client = client_provider.ns_client
namespaces = await client.get_all()
print_namespace_table(namespaces)
......@@ -41,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 = client_provider.ns_client
namespace = await client.get_ns(ident)
print_namespace_table([namespace])
......@@ -51,7 +51,7 @@ async def ls_id(ident: int):
@async_cli()
async def ls_cur():
"""List current namespace."""
client = state.ns_client
client = client_provider.ns_client
namespace = await client.get_cur()
print_namespace_table([namespace])
......@@ -62,7 +62,7 @@ async def ls_cur():
async def create(name: str):
"""Create a new namespace."""
client = state.ns_client
client = client_provider.ns_client
# create namespace relative to current namespace
cur_ns = await client.get_cur()
......@@ -72,7 +72,7 @@ async def create(name: str):
namespace = await client.create(parent_id=cur_ns_id, name=name)
ns_id = namespace["id"]
print(f"Creating namespace {name} in {state.namespace}. New namespace: {ns_id}")
print(f"Creating namespace {name} in {client_provider.namespace}. New namespace: {ns_id}")
@app.command()
......@@ -80,7 +80,7 @@ async def create(name: str):
async def delete(ident: int):
"""Delete a namespace."""
client = state.ns_client
client = client_provider.ns_client
await client.delete_ns(ident)
print(f"Deleted namespace with id {ident}.")
......@@ -90,7 +90,7 @@ async def delete(ident: int):
async def members():
"""List all members."""
client = state.ns_client
client = client_provider.ns_client
members = await client.get_members()
print_members_table(members)
......@@ -100,6 +100,6 @@ async def members():
async def member_add(user: str, role: str):
"""Add member to namespace."""
client = state.ns_client
client = client_provider.ns_client
members = await client.add_member(role, user)
print(f"Added user {user} with role {role}.")
\ No newline at end of file
......@@ -22,7 +22,7 @@
from typer import Typer, Option
from typing_extensions import Annotated
from ..state import state
from simbricks.client.provider import client_provider
from ..utils import async_cli, print_runner_table, print_table_generic
......@@ -33,7 +33,7 @@ app = Typer(help="Managing SimBricks runners.")
@async_cli()
async def ls():
"""List runners."""
runs = await state.runner_client.list_runners()
runs = await client_provider.runner_client.list_runners()
print_runner_table(runs)
......@@ -41,7 +41,7 @@ async def ls():
@async_cli()
async def show(runner_id: int):
"""Show individual runner."""
runner = await state.runner_client.get_runner(runner_id=runner_id)
runner = await client_provider.runner_client.get_runner(runner_id=runner_id)
print_runner_table([runner])
......@@ -49,14 +49,14 @@ async def show(runner_id: int):
@async_cli()
async def delete(runner_id: int):
"""Delete an individual runner."""
await state.runner_client.delete_runner(runner_id=runner_id)
await client_provider.runner_client.delete_runner(runner_id=runner_id)
@app.command()
@async_cli()
async def create(label: str, tags: list[str]):
"""Update a runner with the the given label and tags."""
runner = await state.runner_client.create_runner(label=label, tags=tags)
runner = await client_provider.runner_client.create_runner(label=label, tags=tags)
print_runner_table([runner])
......@@ -67,7 +67,7 @@ async def create_event(
run_id: Annotated[int | None, Option("--run", "-r", help="Set event for specific run.")] = None,
):
"""Send a run related event to a runner (Available actions: kill, heartbeat, simulation_status, start_run)."""
event = await state.runner_client.create_runner_event(action=action, run_id=run_id)
event = await client_provider.runner_client.create_runner_event(action=action, run_id=run_id)
print_table_generic("Event", [event], "id", "runner_id", "action", "run_id", "event_status")
......@@ -75,7 +75,7 @@ async def create_event(
@async_cli()
async def delete_event(event_id: int):
"""Delete a runner event."""
await state.runner_client.delete_runner_event(event_id=event_id)
await client_provider.runner_client.delete_runner_event(event_id=event_id)
@app.command()
......@@ -91,7 +91,7 @@ async def update_event(
run_id: Annotated[int | None, Option("--run", "-r", help="Run to set.")] = None,
):
"""Update a runner event."""
event = await state.runner_client.update_runner_event(
event = await client_provider.runner_client.update_runner_event(
event_id=event_id, action=action, event_status=event_status, run_id=run_id
)
print_table_generic("Event", [event], "id", "runner_id", "action", "run_id", "event_status")
......@@ -106,5 +106,5 @@ async def ls_events(
limit: Annotated[int | None, Option("--limit", "-l", help="Limit results.")] = None,
):
"""List runner related events"""
events = await state.runner_client.get_events(action=action, run_id=run_id, event_status=event_status, limit=limit)
events = await client_provider.runner_client.get_events(action=action, run_id=run_id, event_status=event_status, limit=limit)
print_table_generic("Events", events, "id", "runner_id", "action", "run_id", "event_status")
......@@ -26,7 +26,7 @@ from pathlib import Path
import simbricks.utils.load_mod as load_mod
from typer import Typer, Argument, Option
from typing_extensions import Annotated
from ..state import state
from simbricks.client.provider import client_provider
from ..utils import async_cli
from rich.console import Console
......@@ -39,7 +39,7 @@ app = Typer(help="Managing SimBricks runs.")
@async_cli()
async def ls():
"""List runs."""
runs = await state.simbricks_client.get_runs()
runs = await client_provider.simbricks_client.get_runs()
table = Table()
table.add_column("Id")
......@@ -56,7 +56,7 @@ 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 client_provider.simbricks_client.get_run(run_id)
print(run)
......@@ -64,7 +64,7 @@ async def show(run_id: int):
@async_cli()
async def follow(run_id: int):
"""Follow individual run as it executes."""
client = state.simbricks_client
client = client_provider.simbricks_client
await client.follow_run(run_id)
......@@ -72,7 +72,7 @@ async def follow(run_id: int):
@async_cli()
async def delete(run_id: int):
"""Delete an individual run."""
client = state.simbricks_client
client = client_provider.simbricks_client
await client.delete_run(run_id)
......@@ -80,7 +80,7 @@ async def delete(run_id: int):
@async_cli()
async def set_input_tarball(run_id: int, source_file: str):
"""Set the tarball input for an individual run."""
client = state.simbricks_client
client = client_provider.simbricks_client
await client.set_run_input(run_id, source_file)
......@@ -88,7 +88,7 @@ async def set_input_tarball(run_id: int, source_file: str):
@async_cli()
async def set_output_artifact(run_id: int, source_file: str):
"""Set the tarball input for an individual run."""
client = state.simbricks_client
client = client_provider.simbricks_client
await client.set_run_artifact(run_id, source_file)
......@@ -96,7 +96,7 @@ async def set_output_artifact(run_id: int, source_file: str):
@async_cli()
async def get_output_artifact(run_id: int, destination_file: str):
"""Follow individual run as it executes."""
client = state.simbricks_client
client = client_provider.simbricks_client
await client.get_run_artifact(run_id, destination_file)
......@@ -104,7 +104,7 @@ async def get_output_artifact(run_id: int, destination_file: str):
@async_cli()
async def update_run(run_id: int, updates: str):
"""Update run with the 'updates' json string."""
client = state.simbricks_client
client = client_provider.simbricks_client
json_updates = json.loads(updates)
await client.update_run(run_id, updates=json_updates)
......@@ -128,7 +128,7 @@ async def submit_script(
):
"""Submit a SimBricks python simulation script to run."""
system_client = state.simbricks_client
system_client = client_provider.simbricks_client
experiment_mod = load_mod.load_module(module_path=path)
instantiations = experiment_mod.instantiations
......@@ -154,7 +154,7 @@ async def submit_script(
await system_client.set_run_input(run_id, input)
if start:
await state.runner_client.create_runner_event(action="start_run", run_id=run_id)
await client_provider.runner_client.create_runner_event(action="start_run", run_id=run_id)
if follow:
await system_client.follow_run(run_id)
......@@ -21,7 +21,7 @@
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from typer import Typer
from ..state import state
from simbricks.client.provider import client_provider
from ..utils import async_cli
from ..utils import print_simulations_table
......@@ -32,7 +32,7 @@ app = Typer(help="Managing SimBricks Simulations.")
@async_cli()
async def ls():
"""List Simulations."""
simulations = await state.simbricks_client.get_simulations()
simulations = await client_provider.simbricks_client.get_simulations()
print_simulations_table(simulations)
......@@ -40,7 +40,7 @@ async def ls():
@async_cli()
async def show(sim_id: int):
"""Show individual Simulation."""
sim = await state.simbricks_client.get_simulation(simulation_id=sim_id)
sim = await client_provider.simbricks_client.get_simulation(simulation_id=sim_id)
print_simulations_table([sim])
......@@ -48,5 +48,5 @@ async def show(sim_id: int):
@async_cli()
async def delete(sim_id: int):
"""Delete an individual SImulation."""
client = state.simbricks_client
client = client_provider.simbricks_client
await client.delete_simulation(sim_id=sim_id)
......@@ -21,7 +21,7 @@
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from typer import Typer
from ..state import state
from simbricks.client.provider import client_provider
from ..utils import async_cli
from ..utils import print_systems_table
......@@ -32,7 +32,7 @@ app = Typer(help="Managing SimBricks Systems.")
@async_cli()
async def ls():
"""List Systems."""
systems = await state.simbricks_client.get_systems()
systems = await client_provider.simbricks_client.get_systems()
print_systems_table(systems=systems)
......@@ -40,7 +40,7 @@ async def ls():
@async_cli()
async def show(system_id: int):
"""Show individual System."""
run = await state.simbricks_client.get_system(system_id=system_id)
run = await client_provider.simbricks_client.get_system(system_id=system_id)
print_systems_table([run])
......@@ -48,5 +48,5 @@ async def show(system_id: int):
@async_cli()
async def delete(system_id: int):
"""Delete an individual run."""
client = state.simbricks_client
client = client_provider.simbricks_client
await client.delete_system(sys_id=system_id)
# 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.
import os
from simbricks.client import BaseClient, AdminClient, NSClient, SimBricksClient, RunnerClient
class State:
def __init__(self):
self.namespace = ""
self.runner_id: int = -1
self._base_client: BaseClient | None = None
self._admin_client: AdminClient = None
self._ns_client: NSClient | None = None
self._simbricks_client: SimBricksClient | None = None
self._runner_client: RunnerClient | 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)
return self._ns_client
@property
def simbricks_client(self):
if self._simbricks_client is None:
self._simbricks_client = SimBricksClient(self.ns_client)
return self._simbricks_client
@property
def runner_client(self):
if self._runner_client is None:
self._runner_client = RunnerClient(self.ns_client, id=self.runner_id)
return self._runner_client
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