Skip to content

Commit 9654516

Browse files
authored
refactor python client
1 parent 586ac62 commit 9654516

File tree

1 file changed

+26
-32
lines changed

1 file changed

+26
-32
lines changed

client/fossildb-client

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,30 @@ from grpc_health.v1 import health_pb2
1111
from grpc_health.v1 import health_pb2_grpc
1212

1313
def main():
14+
full_address = '{}:{}'.format(args.address, args.port)
15+
16+
print('Connecting to FossilDB at', full_address)
17+
channel = grpc.insecure_channel(full_address)
18+
19+
def health():
20+
try :
21+
healthStub = health_pb2_grpc.HealthStub(channel)
22+
reply = healthStub.Check(health_pb2.HealthCheckRequest(service=''))
23+
SERVING = health_pb2._HEALTHCHECKRESPONSE_SERVINGSTATUS.values_by_name["SERVING"].number
24+
if reply.status != SERVING:
25+
raise Exception(reply.status)
26+
except Exception as e:
27+
print('Health check unsuccessful. FossilDB offline?')
28+
print(reply)
29+
print(e)
30+
sys.exit(1)
1431

1532
commands = {
16-
'backup': lambda stub:
17-
stub.Backup(proto.BackupRequest()),
18-
'restore': lambda stub:
19-
stub.RestoreFromBackup(proto.RestoreFromBackupRequest()),
20-
'health': None
33+
'backup': lambda:
34+
proto_rpc.FossilDBStub(channel).Backup(proto.BackupRequest()),
35+
'restore': lambda:
36+
proto_rpc.FossilDBStub(channel).RestoreFromBackup(proto.RestoreFromBackupRequest()),
37+
'health': health
2138
}
2239

2340
parser = argparse.ArgumentParser()
@@ -38,33 +55,10 @@ def main():
3855
parser.print_help()
3956
exit(20)
4057

41-
full_address = '{}:{}'.format(args.address, args.port)
42-
43-
print('Connecting to FossilDB at', full_address)
44-
45-
channel = grpc.insecure_channel(full_address)
46-
stub = proto_rpc.FossilDBStub(channel)
47-
healthStub = health_pb2_grpc.HealthStub(channel)
48-
49-
if args.command == 'health':
50-
try :
51-
reply = healthStub.Check(health_pb2.HealthCheckRequest(service=''))
52-
print(reply)
53-
UNKNOWN = 0;
54-
SERVING = 1;
55-
NOT_SERVING = 2;
56-
if reply.status != SERVING:
57-
raise Exception(reply.status)
58-
except Exception as e:
59-
print('Health check unsuccessful. FossilDB offline?')
60-
print(e)
61-
sys.exit(1)
62-
63-
else:
64-
reply = commands[args.command](stub)
65-
print(reply)
66-
if not reply.success:
67-
sys.exit(1)
58+
reply = commands[args.command]()
59+
print(reply)
60+
if hasattr(reply, 'success') and not reply.success:
61+
sys.exit(1)
6862

6963

7064
if __name__ == '__main__':

0 commit comments

Comments
 (0)