-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
547599f
commit 1b908ac
Showing
4 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from takler.client.cli import app | ||
|
||
|
||
if __name__ == "__main__": | ||
app() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import typer | ||
|
||
from takler.client.service_client import TaklerServiceClient | ||
|
||
|
||
app = typer.Typer() | ||
|
||
|
||
@app.command() | ||
def init( | ||
task_id: str = typer.Option(...), | ||
node_path: str = typer.Option(...), | ||
host: str = typer.Option(None, help="takler service host"), | ||
port: int = typer.Option(None, help="takler service port"), | ||
): | ||
client = TaklerServiceClient(host=host, port=port) | ||
client.start() | ||
client.run_command_init(node_path=node_path, task_id=task_id) | ||
client.shutdown() | ||
|
||
|
||
@app.command() | ||
def complete( | ||
node_path: str = typer.Option(...), | ||
host: str = typer.Option(None, help="takler service host"), | ||
port: int = typer.Option(None, help="takler service port"), | ||
): | ||
client = TaklerServiceClient(host=host, port=port) | ||
client.start() | ||
client.run_command_complete(node_path=node_path) | ||
client.shutdown() | ||
|
||
|
||
@app.command() | ||
def show( | ||
host: str = typer.Option(None, help="takler service host"), | ||
port: int = typer.Option(None, help="takler service port"), | ||
): | ||
client = TaklerServiceClient(host=host, port=port) | ||
client.start() | ||
client.run_request_show() | ||
client.shutdown() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import asyncio | ||
from typing import Optional | ||
|
||
import grpc | ||
|
||
from takler.server.protocol.takler_pb2_grpc import TaklerServerStub | ||
from takler.server.protocol import takler_pb2 | ||
from takler.logging import get_logger | ||
|
||
|
||
logger = get_logger("client") | ||
|
||
|
||
class TaklerServiceClient: | ||
def __init__(self, host: str = "localhost", port: int = 50051): | ||
self.host = host # type: str | ||
self.port = port # type: int | ||
self.channel = None # type: Optional[grpc.Channel] | ||
self.stub = None # type: Optional[TaklerServerStub] | ||
|
||
@property | ||
def listen_address(self) -> str: | ||
""" | ||
str: gRPC server's listen address | ||
""" | ||
return f'{self.host}:{self.port}' | ||
|
||
def create_channel(self): | ||
self.channel = grpc.insecure_channel(self.listen_address) | ||
|
||
def close_channel(self): | ||
self.channel.close() | ||
self.channel = None | ||
|
||
def create_stub(self): | ||
self.stub = TaklerServerStub(self.channel) | ||
return self.stub | ||
|
||
def start(self): | ||
self.create_channel() | ||
self.create_stub() | ||
|
||
def shutdown(self): | ||
self.close_channel() | ||
|
||
def run_command_init(self, node_path: str, task_id: str): | ||
response = self.stub.RunInitCommand( | ||
takler_pb2.InitCommand( | ||
child_options=takler_pb2.ChildCommandOptions( | ||
node_path=node_path, | ||
), | ||
task_id=task_id | ||
) | ||
) | ||
print(f"received: {response.flag}") | ||
|
||
def run_command_complete(self, node_path: str): | ||
response = self.stub.RunCompleteCommand( | ||
takler_pb2.CompleteCommand( | ||
child_options=takler_pb2.ChildCommandOptions( | ||
node_path=node_path, | ||
) | ||
) | ||
) | ||
print(f"received: {response.flag}") | ||
|
||
def run_request_show(self): | ||
response = self.stub.RunShowRequest( | ||
takler_pb2.ShowRequest() | ||
) | ||
print(response.output) |