Skip to content

Commit 98f5edb

Browse files
authored
Merge pull request #102 from jumpstarter-dev/list-lease
Implement jmp client lease list/release
2 parents 1cd5a8f + f8639fd commit 98f5edb

File tree

5 files changed

+133
-21
lines changed

5 files changed

+133
-21
lines changed

jumpstarter/cli/client.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,46 @@ def client():
2222
pass
2323

2424

25+
@client.group(short_help="Managed leases held by client.")
26+
def lease():
27+
pass
28+
29+
30+
@lease.command("list")
31+
@click.argument("name", type=str, default="")
32+
def lease_list(name):
33+
if name:
34+
config = ClientConfigV1Alpha1.load(name)
35+
else:
36+
config = UserConfigV1Alpha1.load_or_create().config.current_client
37+
if not config:
38+
raise ValueError("no client specified")
39+
40+
for lease in config.list_leases():
41+
print(lease)
42+
43+
44+
@lease.command("release")
45+
@click.argument("name", type=str, default="")
46+
@click.option("-l", "--lease", "lease", type=str, default="")
47+
@click.option("--all", "all_leases", is_flag=True)
48+
def lease_release(name, lease, all_leases):
49+
if name:
50+
config = ClientConfigV1Alpha1.load(name)
51+
else:
52+
config = UserConfigV1Alpha1.load_or_create().config.current_client
53+
if not config:
54+
raise ValueError("no client specified")
55+
56+
if all_leases:
57+
for lease in config.list_leases():
58+
config.release_lease(lease)
59+
else:
60+
if not lease:
61+
raise ValueError("no lease specified")
62+
config.release_lease(lease)
63+
64+
2565
@click.command("create", short_help="Create a client configuration.")
2666
@click.argument("name")
2767
@click.option(

jumpstarter/config/client.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from jumpstarter.client import LeaseRequest
1212
from jumpstarter.common import MetadataFilter
13+
from jumpstarter.v1 import jumpstarter_pb2, jumpstarter_pb2_grpc
1314

1415
from .common import CONFIG_PATH
1516
from .env import JMP_DRIVERS_ALLOW, JMP_ENDPOINT, JMP_TOKEN
@@ -47,21 +48,40 @@ class ClientConfigV1Alpha1(BaseModel):
4748
kind: Literal["ClientConfig"] = Field(default="ClientConfig")
4849
client: ClientConfigV1Alpha1Client = Field(default_factory=ClientConfigV1Alpha1Client)
4950

51+
async def channel(self):
52+
credentials = grpc.composite_channel_credentials(
53+
grpc.ssl_channel_credentials(),
54+
grpc.access_token_call_credentials(self.client.token),
55+
)
56+
57+
return grpc.aio.secure_channel(self.client.endpoint, credentials)
58+
5059
@contextmanager
5160
def lease(self, metadata_filter: MetadataFilter):
5261
with start_blocking_portal() as portal:
5362
with portal.wrap_async_context_manager(self.lease_async(metadata_filter, portal)) as lease:
5463
yield lease
5564

65+
def list_leases(self):
66+
with start_blocking_portal() as portal:
67+
return portal.call(self.list_leases_async)
68+
69+
def release_lease(self, name):
70+
with start_blocking_portal() as portal:
71+
portal.call(self.release_lease_async, name)
72+
73+
async def list_leases_async(self):
74+
stub = jumpstarter_pb2_grpc.ControllerServiceStub(await self.channel())
75+
return (await stub.ListLeases(jumpstarter_pb2.ListLeasesRequest())).names
76+
77+
async def release_lease_async(self, name):
78+
stub = jumpstarter_pb2_grpc.ControllerServiceStub(await self.channel())
79+
await stub.ReleaseLease(jumpstarter_pb2.ReleaseLeaseRequest(name=name))
80+
5681
@asynccontextmanager
5782
async def lease_async(self, metadata_filter: MetadataFilter, portal: BlockingPortal):
58-
credentials = grpc.composite_channel_credentials(
59-
grpc.ssl_channel_credentials(),
60-
grpc.access_token_call_credentials(self.client.token),
61-
)
62-
6383
async with LeaseRequest(
64-
channel=grpc.aio.secure_channel(self.client.endpoint, credentials),
84+
channel=await self.channel(),
6585
metadata_filter=metadata_filter,
6686
portal=portal,
6787
) as lease:

jumpstarter/v1/jumpstarter_pb2.py

Lines changed: 18 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)