diff options
author | Joerg Steffens <joerg.steffens@bareos.com> | 2016-02-25 21:29:11 +0300 |
---|---|---|
committer | Joerg Steffens <joerg.steffens@bareos.com> | 2016-03-10 16:58:33 +0300 |
commit | a92ba26db9a83875115fd3c7fe01749758097db0 (patch) | |
tree | 0eaf149be787419cc7f3bde8d1a19e0344d96a3c | |
parent | 8492cd87478be32ca7e1d99d1de532b556b17039 (diff) |
initial support to communicate with bareos-fd
-rw-r--r-- | bareos/bsock/__init__.py | 9 | ||||
-rw-r--r-- | bareos/bsock/bsock.py | 9 | ||||
-rw-r--r-- | bareos/bsock/connectiontype.py | 6 | ||||
-rw-r--r-- | bareos/bsock/filedaemon.py | 18 | ||||
-rw-r--r-- | bareos/bsock/lowlevel.py | 7 | ||||
-rw-r--r-- | bareos/bsock/protocolmessages.py | 9 | ||||
-rwxr-xr-x | bin/bareos-fd-connect.py | 46 | ||||
-rw-r--r-- | setup.py | 2 |
8 files changed, 94 insertions, 12 deletions
diff --git a/bareos/bsock/__init__.py b/bareos/bsock/__init__.py index 41bb186..f1a845d 100644 --- a/bareos/bsock/__init__.py +++ b/bareos/bsock/__init__.py @@ -1,5 +1,6 @@ #__all__ = [ "bconsole" ] -from bareos.exceptions import * -from bareos.util.password import Password -from bareos.bsock.bsock import BSock -from bareos.bsock.bsockjson import BSockJson +from bareos.exceptions import * +from bareos.util.password import Password +from bareos.bsock.connectiontype import ConnectionType +from bareos.bsock.bsock import BSock +from bareos.bsock.bsockjson import BSockJson diff --git a/bareos/bsock/bsock.py b/bareos/bsock/bsock.py index 60a220b..d0fa3a8 100644 --- a/bareos/bsock/bsock.py +++ b/bareos/bsock/bsock.py @@ -2,6 +2,7 @@ Communicates with the bareos-director """ +from bareos.bsock.connectiontype import ConnectionType from bareos.bsock.lowlevel import LowLevel from bareos.exceptions import * import sys @@ -14,11 +15,13 @@ class BSock(LowLevel): port=9101, dirname=None, name="*UserAgent*", - password=None): + password=None, + type=ConnectionType.DIRECTOR): super(BSock, self).__init__() - self.connect(address, port, dirname) + self.connect(address, port, dirname, type) self.auth(name=name, password=password) - self._set_state_director_prompt() + if type == ConnectionType.DIRECTOR: + self._set_state_director_prompt() def call(self, command): diff --git a/bareos/bsock/connectiontype.py b/bareos/bsock/connectiontype.py new file mode 100644 index 0000000..027e12e --- /dev/null +++ b/bareos/bsock/connectiontype.py @@ -0,0 +1,6 @@ +class ConnectionType: + """ + """ + DIRECTOR = 1001 + STORAGE = 1002 + FILEDAEMON = 1003 diff --git a/bareos/bsock/filedaemon.py b/bareos/bsock/filedaemon.py new file mode 100644 index 0000000..a5845d7 --- /dev/null +++ b/bareos/bsock/filedaemon.py @@ -0,0 +1,18 @@ +""" +Communicates with the bareos-fd +""" + +from bareos.bsock.connectiontype import ConnectionType +from bareos.bsock import BSock + +class FileDaemon(BSock): + '''use to send and receive the response to Bareos File Daemon''' + + def __init__(self, + address="localhost", + port=9102, + dirname=None, + name=None, + password=None, + type=ConnectionType.FILEDAEMON): + super(FileDaemon, self).__init__(address, port, dirname, name, password, type) diff --git a/bareos/bsock/lowlevel.py b/bareos/bsock/lowlevel.py index dc63417..75ac8c1 100644 --- a/bareos/bsock/lowlevel.py +++ b/bareos/bsock/lowlevel.py @@ -9,6 +9,7 @@ from bareos.exceptions import * from bareos.util.bareosbase64 import BareosBase64 from bareos.util.password import Password from bareos.bsock.constants import Constants +from bareos.bsock.connectiontype import ConnectionType from bareos.bsock.protocolmessages import ProtocolMessages import hmac import logging @@ -32,9 +33,10 @@ class LowLevel(object): self.dirname = None self.socket = None self.auth_credentials_valid = False + self.connection_type = None - def connect(self, address="localhost", port=9101, dirname=None): + def connect(self, address="localhost", port=9101, dirname=None, type=ConnectionType.DIRECTOR): ''' connect to bareos-director ''' @@ -44,6 +46,7 @@ class LowLevel(object): self.dirname = dirname else: self.dirname = address + self.connection_type = type return self.__connect() @@ -74,7 +77,7 @@ class LowLevel(object): def __auth(self): - bashed_name = ProtocolMessages.hello(self.name) + bashed_name = ProtocolMessages.hello(self.name, type=self.connection_type) # send the bash to the director self.send(bashed_name) diff --git a/bareos/bsock/protocolmessages.py b/bareos/bsock/protocolmessages.py index 33d68cc..b898a50 100644 --- a/bareos/bsock/protocolmessages.py +++ b/bareos/bsock/protocolmessages.py @@ -2,13 +2,18 @@ Protocol messages between bareos-director and user-agent. """ +from bareos.bsock.connectiontype import ConnectionType + class ProtocolMessages(): """ strings defined by the protocol to talk to the Bareos Director. """ @staticmethod - def hello(name): - return "Hello %s calling\n" % (name) + def hello(name, type=ConnectionType.DIRECTOR): + if type == ConnectionType.FILEDAEMON: + return "Hello Director %s calling\n" % (name) + else: + return "Hello %s calling\n" % (name) #@staticmethod #def ok(): diff --git a/bin/bareos-fd-connect.py b/bin/bareos-fd-connect.py new file mode 100755 index 0000000..21a0de9 --- /dev/null +++ b/bin/bareos-fd-connect.py @@ -0,0 +1,46 @@ +#!/usr/bin/python + +import argparse +import bareos.bsock +from bareos.bsock.filedaemon import FileDaemon +import logging +import sys + +def getArguments(): + parser = argparse.ArgumentParser(description='Connect to Bareos File Daemon.' ) + parser.add_argument('-d', '--debug', action='store_true', help="enable debugging output") + parser.add_argument('--name', help="Name of the Director resource in the File Daemon", required=True) + parser.add_argument('-p', '--password', help="password to authenticate to a Bareos File Daemon", required=True) + parser.add_argument('--port', default=9102, help="Bareos File Daemon network port") + parser.add_argument('address', nargs='?', default="localhost", help="Bareos File Daemon network address") + parser.add_argument('command', nargs='?', help="Command to send to the Bareos File Daemon") + 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', '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 + bsock=FileDaemon(**parameter) + except RuntimeError as e: + print str(e) + sys.exit(1) + logger.debug( "authentication successful" ) + if args.command: + print bsock.call(args.command) @@ -8,7 +8,7 @@ setup( author='Joerg Steffens', author_email='joerg.steffens@bareos.com', packages=find_packages(), - scripts=['bin/bconsole.py', 'bin/bconsole-json.py'], + scripts=['bin/bconsole.py', 'bin/bconsole-json.py', 'bin/bareos-fd-connect.py'], url='https://github.com/joergsteffens/python-bareos/', # What does your project relate to? keywords='bareos', |