Skip to content

Commit

Permalink
initial support to communicate with bareos-fd
Browse files Browse the repository at this point in the history
  • Loading branch information
joergsteffens committed Mar 10, 2016
1 parent 8492cd8 commit a92ba26
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 12 deletions.
9 changes: 5 additions & 4 deletions 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
9 changes: 6 additions & 3 deletions bareos/bsock/bsock.py
Expand Up @@ -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
Expand All @@ -14,11 +15,13 @@ def __init__(self,
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):
Expand Down
6 changes: 6 additions & 0 deletions bareos/bsock/connectiontype.py
@@ -0,0 +1,6 @@
class ConnectionType:
"""
"""
DIRECTOR = 1001
STORAGE = 1002
FILEDAEMON = 1003
18 changes: 18 additions & 0 deletions 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)
7 changes: 5 additions & 2 deletions bareos/bsock/lowlevel.py
Expand Up @@ -9,6 +9,7 @@
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
Expand All @@ -32,9 +33,10 @@ def __init__(self):
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
'''
Expand All @@ -44,6 +46,7 @@ def connect(self, address="localhost", port=9101, dirname=None):
self.dirname = dirname
else:
self.dirname = address
self.connection_type = type
return self.__connect()


Expand Down Expand Up @@ -74,7 +77,7 @@ def auth(self, password, name="*UserAgent*"):


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)

Expand Down
9 changes: 7 additions & 2 deletions bareos/bsock/protocolmessages.py
Expand Up @@ -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():
Expand Down
46 changes: 46 additions & 0 deletions 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)
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -8,7 +8,7 @@
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',
Expand Down

0 comments on commit a92ba26

Please sign in to comment.