1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
#!/usr/bin/python
import argparse
import bareos.bsock
import inspect
import logging
# pip install python-jsonrpc
import pyjsonrpc
import sys
from types import MethodType
def add(a, b):
"""Test function"""
return a + b
class RequestHandler(pyjsonrpc.HttpRequestHandler):
# Register public JSON-RPC methods
methods = {
"add": add
}
class BconsoleMethods:
def __init__(self, bconsole):
self.logger = logging.getLogger()
self.logger.debug("init")
self.conn = bconsole
def execute(self, command):
"""
Generic function to call any bareos console command.
"""
self.logger.debug(command)
return self.conn.call(command)
def execute_fullresult(self, command):
"""
Generic function to call any bareos console commands,
and return the full result (also the pseudo jsonrpc header, not required here).
"""
self.logger.debug(command)
return self.conn.call_fullresult(command)
def list(self, command):
"""
Interface to the Bareos console list command.
"""
return self.execute("list " + command)
def call(self, command):
"""
legacy function, as call is a suboptimal name.
It is used internally by python-jsonrpc.
Use execute() instead.
"""
return self.execute(command)
def bconsole_methods_to_jsonrpc(bconsole_methods):
tuples = inspect.getmembers(bconsole_methods, predicate=inspect.ismethod)
methods = RequestHandler.methods
for i in tuples:
methods[i[0]] = getattr(bconsole_methods, i[0])
print i[0]
print methods
RequestHandler.methods=methods
def getArguments():
parser = argparse.ArgumentParser(description='Run Bareos Director JSON-RPC proxy.' )
parser.add_argument('-d', '--debug', action='store_true', help="enable debugging output")
parser.add_argument('--name', default="*UserAgent*", help="use this to access a specific Bareos director named console. Otherwise it connects to the default console (\"*UserAgent*\")")
parser.add_argument('-p', '--password', help="password to authenticate to a Bareos Director console", required=True)
parser.add_argument('--port', default=9101, help="Bareos Director network port")
parser.add_argument('--dirname', help="Bareos Director name")
parser.add_argument('address', nargs='?', default="localhost", help="Bareos Director network address")
args = parser.parse_args()
return args
if __name__ == '__main__':
logging.basicConfig(format='%(levelname)s %(module)s.%(funcName)s: %(message)s', level=logging.INFO)
logger = logging.getLogger()
args=getArguments()
if args.debug:
logger.setLevel(logging.DEBUG)
try:
options = [ 'address', 'port', 'dirname', 'name' ]
parameter = {}
for i in options:
if hasattr(args, i) and getattr(args,i) != None:
logger.debug( "%s: %s" %(i, getattr(args,i)))
parameter[i] = getattr(args, i)
else:
logger.debug( '%s: ""' %(i))
logger.debug('options: %s' % (parameter))
password = bareos.bsock.Password(args.password)
parameter['password']=password
director = bareos.bsock.DirectorConsoleJson(**parameter)
except RuntimeError as e:
print(str(e))
sys.exit(1)
logger.debug( "authentication successful" )
bconsole_methods = BconsoleMethods( director )
bconsole_methods_to_jsonrpc( bconsole_methods )
print bconsole_methods.call("list jobs last")
# Threading HTTP-Server
http_server = pyjsonrpc.ThreadingHttpServer(
server_address = ('localhost', 8080),
RequestHandlerClass = RequestHandler
)
print "Starting HTTP server ..."
print "URL: http://localhost:8080"
http_server.serve_forever()
|