@@ -7,18 +7,11 @@ import sys
77import fossildbapi_pb2 as proto
88import fossildbapi_pb2_grpc as proto_rpc
99
10+ from grpc_health .v1 import health_pb2
11+ from grpc_health .v1 import health_pb2_grpc
1012
11- def main ():
12-
13- commands = {
14- 'backup' : lambda stub :
15- stub .Backup (proto .BackupRequest ()),
16- 'restore' : lambda stub :
17- stub .RestoreFromBackup (proto .RestoreFromBackupRequest ()),
18- 'health' : lambda stub :
19- stub .Health (proto .HealthRequest ())
20- }
2113
14+ def parse_args (commands ):
2215 parser = argparse .ArgumentParser ()
2316 parser .add_argument (
2417 'address' , metavar = 'address' , default = 'localhost' , nargs = '?' ,
@@ -31,23 +24,47 @@ def main():
3124 help = 'command to execute, one of {}' .format (list (commands .keys ())))
3225
3326 args = parser .parse_args ()
34-
3527 if args .command not in commands :
3628 print ("command {} is not available" .format (args .command ))
3729 parser .print_help ()
3830 exit (20 )
3931
32+ return args
33+
34+
35+ def health (channel ):
36+ try :
37+ healthStub = health_pb2_grpc .HealthStub (channel )
38+ reply = healthStub .Check (health_pb2 .HealthCheckRequest (service = '' ))
39+ STATUSMAP = health_pb2 ._HEALTHCHECKRESPONSE_SERVINGSTATUS .values_by_name
40+ SERVING = STATUSMAP ["SERVING" ].number
41+ if reply .status != SERVING :
42+ raise Exception (reply .status )
43+ except Exception as e :
44+ print ('Health check unsuccessful. FossilDB offline?' )
45+ print (e )
46+ sys .exit (1 )
47+ return reply
48+
49+
50+ def main ():
51+ commands = {
52+ 'backup' : lambda channel :
53+ proto_rpc .FossilDBStub (channel ).Backup (proto .BackupRequest ()),
54+ 'restore' : lambda channel :
55+ proto_rpc .FossilDBStub (channel ).RestoreFromBackup (proto .RestoreFromBackupRequest ()),
56+ 'health' : health
57+ }
58+
59+ args = parse_args (commands )
4060 full_address = '{}:{}' .format (args .address , args .port )
4161
4262 print ('Connecting to FossilDB at' , full_address )
43-
4463 channel = grpc .insecure_channel (full_address )
45- stub = proto_rpc .FossilDBStub (channel )
46-
47- reply = commands [args .command ](stub )
4864
65+ reply = commands [args .command ](channel )
4966 print (reply )
50- if not reply .success :
67+ if hasattr ( reply , 'success' ) and not reply .success :
5168 sys .exit (1 )
5269
5370
0 commit comments