1""" 2Reimplementation of the bconsole program in python. 3""" 4 5from bareos.bsock.directorconsole import DirectorConsole 6from pprint import pformat, pprint 7import json 8 9class DirectorConsoleJson(DirectorConsole): 10 """ 11 use to send and receive the response from director 12 """ 13 14 def __init__(self, *args, **kwargs): 15 super(DirectorConsoleJson, self).__init__(*args, **kwargs) 16 17 def _init_connection(self): 18 # older version did not support compact mode, 19 # therfore first set api mode to json (which should always work in bareos >= 15.2.0) 20 # and then set api mode json compact (which should work with bareos >= 15.2.2) 21 self.logger.debug(self.call(".api json")) 22 self.logger.debug(self.call(".api json compact=yes")) 23 24 25 def call(self, command): 26 json = self.call_fullresult(command) 27 if json == None: 28 return 29 if 'result' in json: 30 result = json['result'] 31 else: 32 # TODO: or raise an exception? 33 result = json 34 return result 35 36 37 def call_fullresult(self, command): 38 resultstring = super(DirectorConsoleJson, self).call(command) 39 data = None 40 if resultstring: 41 try: 42 data = json.loads(resultstring.decode('utf-8')) 43 except ValueError as e: 44 # in case result is not valid json, 45 # create a JSON-RPC wrapper 46 data = { 47 'error': { 48 'code': 2, 49 'message': str(e), 50 'data': resultstring 51 }, 52 } 53 return data 54 55 56 def _show_result(self, msg): 57 pprint(msg) 58