Welcome to mirror list, hosted at ThFree Co, Russian Federation.

bsock.py « bsock « bareos - github.com/bareos/python-bareos.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e2357b87336c393131eeb2d9224590ed24d50b1d (plain)
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
"""
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 = b''
        try:
            self.send(bytearray(command, 'utf-8'))
            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":
            # Python2: raw_input, Python3: input
            try:
                myinput = raw_input
            except NameError:
                myinput = input
            command = myinput(">>")
            resultmsg = self.call(command)
            sys.stdout.write(resultmsg.decode('utf-8'))
        return True