/
bsock.py
64 lines (55 loc) · 1.87 KB
/
bsock.py
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
"""
Communicates with the bareos-director
"""
from bareos.bsock.connectiontype import ConnectionType
from bareos.bsock.lowlevel import LowLevel
from bareos.exceptions import *
import sys
class BSock(LowLevel):
'''use to send and receive the response from director'''
def __init__(self,
address="localhost",
port=9101,
dirname=None,
name="*UserAgent*",
password=None,
type=ConnectionType.DIRECTOR):
super(BSock, self).__init__()
self.connect(address, port, dirname, type)
self.auth(name=name, password=password)
if type == ConnectionType.DIRECTOR:
self._set_state_director_prompt()
def call(self, command):
'''
call a bareos-director user agent command
'''
return self.__call(command, 0)
def __call(self, command, count):
'''
Call a bareos-director user agent command.
If connection is lost, try to reconnect.
'''
result = ''
try:
self.send(str(command))
result = self.recv_msg()
except (SocketEmptyHeader, ConnectionLostError) as e:
self.logger.error("connection problem (%s): %s" % (type(e).__name__, str(e)))
if count == 0:
if self.reconnect():
return self.__call(command, count+1)
return result
def send_command(self, commamd):
return self.call(command)
def interactive(self):
"""
Enter the interactive mode.
Exit via typing "exit" or "quit".
"""
self._set_state_director_prompt()
command = ""
while command != "exit" and command != "quit":
command = raw_input(">>")
resultmsg = self.call(command)
sys.stdout.write(resultmsg)
return True