Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ycai
simbricks
Commits
f8c67ccc
Unverified
Commit
f8c67ccc
authored
Dec 02, 2024
by
Jakob Görgen
Browse files
added simple cli utility to create and receive namespaces as user
parent
67698ae0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
96 additions
and
64 deletions
+96
-64
symphony/cli/simbricks/cli/commands/namespaces.py
symphony/cli/simbricks/cli/commands/namespaces.py
+53
-5
symphony/client/simbricks/client/client.py
symphony/client/simbricks/client/client.py
+43
-59
No files found.
symphony/cli/simbricks/cli/commands/namespaces.py
View file @
f8c67ccc
...
...
@@ -23,21 +23,69 @@
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
..state
import
state
from
..utils
import
async_cli
app
=
Typer
(
help
=
"Managing SimBricks namespaces."
)
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."""
print
(
f
"Listing Namespaces in
{
state
.
namespace
}
:"
)
client
=
state
.
ns_client
()
namespaces
=
await
client
.
get_all
()
print_namespace_table
(
namespaces
)
@
app
.
command
()
@
async_cli
()
async
def
ls_id
(
ident
:
int
):
"""List namespace with given id ident."""
client
=
state
.
ns_client
()
namespace
=
await
client
.
get_ns
(
ident
)
print_namespace_table
([
namespace
])
@
app
.
command
()
@
async_cli
()
async
def
ls_cur
():
"""List current namespace."""
client
=
state
.
ns_client
()
namespace
=
await
client
.
get_cur
()
print_namespace_table
([
namespace
])
@
app
.
command
()
@
async_cli
()
async
def
create
(
name
:
str
):
"""Create a new namespace."""
print
(
f
"Creating namespace
{
name
}
in
{
state
.
namespace
}
"
)
\ No newline at end of file
client
=
state
.
ns_client
()
# create namespace relative to current namespace
cur_ns
=
await
client
.
get_cur
()
cur_ns_id
=
int
(
cur_ns
[
"id"
])
# create the actual namespace
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
}
"
)
symphony/client/simbricks/client/client.py
View file @
f8c67ccc
...
...
@@ -61,9 +61,7 @@ class BaseClient:
url
=
f
"
{
self
.
_base_url
}{
url
}
"
async
with
self
.
session
()
as
session
:
async
with
session
.
post
(
url
=
url
,
data
=
data
,
**
kwargs
)
as
resp
:
# TODO: handel connection error
async
with
session
.
post
(
url
=
url
,
data
=
data
,
**
kwargs
)
as
resp
:
# TODO: handel connection error
print
(
await
resp
.
text
())
resp
.
raise_for_status
()
# TODO: handel gracefully
yield
resp
...
...
@@ -76,9 +74,7 @@ class BaseClient:
url
=
f
"
{
self
.
_base_url
}{
url
}
"
async
with
self
.
session
()
as
session
:
async
with
session
.
put
(
url
=
url
,
data
=
data
,
**
kwargs
)
as
resp
:
# TODO: handel connection error
async
with
session
.
put
(
url
=
url
,
data
=
data
,
**
kwargs
)
as
resp
:
# TODO: handel connection error
print
(
await
resp
.
text
())
resp
.
raise_for_status
()
# TODO: handel gracefully
yield
resp
...
...
@@ -90,9 +86,8 @@ class BaseClient:
url
=
f
"
{
self
.
_base_url
}{
url
}
"
async
with
self
.
session
()
as
session
:
async
with
session
.
get
(
url
=
url
,
data
=
data
,
**
kwargs
)
as
resp
:
# TODO: handel connection error
async
with
session
.
get
(
url
=
url
,
data
=
data
,
**
kwargs
)
as
resp
:
# TODO: handel connection error
print
(
await
resp
.
text
())
resp
.
raise_for_status
()
# TODO: handel gracefully
yield
resp
...
...
@@ -114,18 +109,14 @@ class NSClient:
async
def
post
(
self
,
url
:
str
,
data
:
typing
.
Any
=
None
,
**
kwargs
:
typing
.
Any
)
->
typing
.
AsyncIterator
[
aiohttp
.
ClientResponse
]:
async
with
self
.
_base_client
.
post
(
url
=
self
.
_build_ns_prefix
(
url
=
url
),
data
=
data
,
**
kwargs
)
as
resp
:
async
with
self
.
_base_client
.
post
(
url
=
self
.
_build_ns_prefix
(
url
=
url
),
data
=
data
,
**
kwargs
)
as
resp
:
yield
resp
@
contextlib
.
asynccontextmanager
async
def
put
(
self
,
url
:
str
,
data
:
typing
.
Any
=
None
,
**
kwargs
:
typing
.
Any
)
->
typing
.
AsyncIterator
[
aiohttp
.
ClientResponse
]:
async
with
self
.
_base_client
.
put
(
url
=
self
.
_build_ns_prefix
(
url
=
url
),
data
=
data
,
**
kwargs
)
as
resp
:
async
with
self
.
_base_client
.
put
(
url
=
self
.
_build_ns_prefix
(
url
=
url
),
data
=
data
,
**
kwargs
)
as
resp
:
yield
resp
@
contextlib
.
asynccontextmanager
...
...
@@ -133,15 +124,33 @@ class NSClient:
self
,
url
:
str
,
data
:
typing
.
Any
=
None
,
**
kwargs
:
typing
.
Any
)
->
typing
.
AsyncIterator
[
aiohttp
.
ClientResponse
]:
async
with
self
.
_base_client
.
get
(
url
=
self
.
_build_ns_prefix
(
url
=
url
),
data
=
data
,
**
kwargs
)
as
resp
:
async
with
self
.
_base_client
.
get
(
url
=
self
.
_build_ns_prefix
(
url
=
url
),
data
=
data
,
**
kwargs
)
as
resp
:
yield
resp
async
def
info
(
self
):
async
with
self
.
get
(
url
=
"/info"
)
as
resp
:
return
await
resp
.
json
()
async
def
create
(
self
,
parent_id
:
int
,
name
:
str
):
namespace_json
=
{
"parent_id"
:
parent_id
,
"name"
:
name
}
async
with
self
.
post
(
url
=
"/"
,
json
=
namespace_json
)
as
resp
:
return
await
resp
.
json
()
# retrieve namespace ns_id, useful for retrieving a child the current namespace
async
def
get_ns
(
self
,
ns_id
:
int
):
async
with
self
.
get
(
url
=
f
"/one/
{
ns_id
}
"
)
as
resp
:
return
await
resp
.
json
()
# retrieve the current namespace
async
def
get_cur
(
self
):
async
with
self
.
get
(
url
=
"/"
)
as
resp
:
return
await
resp
.
json
()
# recursively retrieve all namespaces beginning with the current including all children
async
def
get_all
(
self
):
async
with
self
.
get
(
url
=
"/all"
)
as
resp
:
return
await
resp
.
json
()
class
SimBricksClient
:
...
...
@@ -165,13 +174,8 @@ class SimBricksClient:
async
with
self
.
_ns_client
.
get
(
url
=
f
"/systems/
{
system_id
}
"
)
as
resp
:
return
await
resp
.
json
()
async
def
create_simulation
(
self
,
system_db_id
:
int
,
simulation
:
simulation
.
Simulation
)
->
simulation
.
Simulation
:
json_obj
=
{
"system_id"
:
system_db_id
,
"sb_json"
:
simulation
.
toJSON
()
}
async
def
create_simulation
(
self
,
system_db_id
:
int
,
simulation
:
simulation
.
Simulation
)
->
simulation
.
Simulation
:
json_obj
=
{
"system_id"
:
system_db_id
,
"sb_json"
:
simulation
.
toJSON
()}
print
(
json_obj
)
async
with
self
.
_ns_client
.
post
(
url
=
"/simulations"
,
json
=
json_obj
)
as
resp
:
return
await
resp
.
json
()
...
...
@@ -179,31 +183,22 @@ class SimBricksClient:
async
def
get_simulation
(
self
,
simulation_id
:
int
)
->
dict
:
async
with
self
.
_ns_client
.
get
(
url
=
f
"/simulations/
{
simulation_id
}
"
)
as
resp
:
return
await
resp
.
json
()
async
def
get_simulations
(
self
)
->
list
[
dict
]:
async
with
self
.
_ns_client
.
get
(
url
=
"/simulations"
)
as
resp
:
return
await
resp
.
json
()
async
def
create_instantiation
(
self
,
sim_db_id
:
int
,
instantiation
:
simulation
.
Simulation
)
->
simulation
.
Simulation
:
json_obj
=
{
"simulation_id"
:
sim_db_id
,
"sb_json"
:
{}
# FIXME
}
async
def
create_instantiation
(
self
,
sim_db_id
:
int
,
instantiation
:
simulation
.
Simulation
)
->
simulation
.
Simulation
:
json_obj
=
{
"simulation_id"
:
sim_db_id
,
"sb_json"
:
{}}
# FIXME
print
(
json_obj
)
async
with
self
.
_ns_client
.
post
(
url
=
"/instantiations"
,
json
=
json_obj
)
as
resp
:
return
await
resp
.
json
()
async
def
get_instantiation
(
self
,
instantiation_id
:
int
)
->
instantiation
.
Instantiation
:
async
def
get_instantiation
(
self
,
instantiation_id
:
int
)
->
instantiation
.
Instantiation
:
async
with
self
.
_ns_client
.
get
(
url
=
f
"/instantiations/
{
instantiation_id
}
"
)
as
resp
:
return
await
resp
.
json
()
async
def
create_run
(
self
,
inst_db_id
:
int
)
->
dict
:
async
def
create_run
(
self
,
inst_db_id
:
int
)
->
dict
:
json_obj
=
{
"instantiation_id"
:
inst_db_id
,
"state"
:
"pending"
,
...
...
@@ -221,7 +216,6 @@ class SimBricksClient:
return
await
resp
.
json
()
class
RunnerClient
:
def
__init__
(
self
,
ns_client
,
id
:
int
)
->
None
:
...
...
@@ -235,18 +229,14 @@ class RunnerClient:
async
def
post
(
self
,
url
:
str
,
data
:
typing
.
Any
=
None
,
**
kwargs
:
typing
.
Any
)
->
typing
.
AsyncIterator
[
aiohttp
.
ClientResponse
]:
async
with
self
.
_ns_client
.
post
(
url
=
self
.
_build_prefix
(
url
=
url
),
data
=
data
,
**
kwargs
)
as
resp
:
async
with
self
.
_ns_client
.
post
(
url
=
self
.
_build_prefix
(
url
=
url
),
data
=
data
,
**
kwargs
)
as
resp
:
yield
resp
@
contextlib
.
asynccontextmanager
async
def
put
(
self
,
url
:
str
,
data
:
typing
.
Any
=
None
,
**
kwargs
:
typing
.
Any
)
->
typing
.
AsyncIterator
[
aiohttp
.
ClientResponse
]:
async
with
self
.
_ns_client
.
put
(
url
=
self
.
_build_prefix
(
url
=
url
),
data
=
data
,
**
kwargs
)
as
resp
:
async
with
self
.
_ns_client
.
put
(
url
=
self
.
_build_prefix
(
url
=
url
),
data
=
data
,
**
kwargs
)
as
resp
:
yield
resp
@
contextlib
.
asynccontextmanager
...
...
@@ -254,15 +244,10 @@ class RunnerClient:
self
,
url
:
str
,
data
:
typing
.
Any
=
None
,
**
kwargs
:
typing
.
Any
)
->
typing
.
AsyncIterator
[
aiohttp
.
ClientResponse
]:
async
with
self
.
_ns_client
.
get
(
url
=
self
.
_build_prefix
(
url
=
url
),
data
=
data
,
**
kwargs
)
as
resp
:
async
with
self
.
_ns_client
.
get
(
url
=
self
.
_build_prefix
(
url
=
url
),
data
=
data
,
**
kwargs
)
as
resp
:
yield
resp
async
def
next_run
(
self
)
->
dict
|
None
:
async
def
next_run
(
self
)
->
dict
|
None
:
async
with
self
.
get
(
f
"/next_run"
)
as
resp
:
if
resp
.
status
==
200
:
return
await
resp
.
json
()
...
...
@@ -271,7 +256,6 @@ class RunnerClient:
else
:
resp
.
raise_for_status
()
async
def
update_run
(
self
,
run_id
:
int
,
...
...
@@ -279,10 +263,10 @@ class RunnerClient:
output
:
str
,
)
->
None
:
obj
=
{
'
state
'
:
state
,
'
output
'
:
output
,
'
id
'
:
run_id
,
'
instantiation_id
'
:
42
,
"
state
"
:
state
,
"
output
"
:
output
,
"
id
"
:
run_id
,
"
instantiation_id
"
:
42
,
}
async
with
self
.
put
(
url
=
f
"/update_run/
{
run_id
}
"
,
json
=
obj
)
as
resp
:
ret
=
await
resp
.
json
()
\ No newline at end of file
ret
=
await
resp
.
json
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment