Skip to content

Commit

Permalink
Python FD plugin baseclass: added some events
Browse files Browse the repository at this point in the history
Consolidates events and methods from various classes in the baseclass.
  • Loading branch information
aussendorf committed Jun 23, 2015
1 parent 690e9ed commit fc96743
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 61 deletions.
148 changes: 97 additions & 51 deletions src/plugins/filed/BareosFdPluginBaseclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@

import bareosfd
from bareos_fd_consts import bVariable, bFileType, bRCs, bCFs
from bareos_fd_consts import bEventType, bIOPS
from bareos_fd_consts import bEventType, bIOPS, bJobMessageType
import os


class BareosFdPluginBaseclass(object):

''' Bareos python plugin base class '''

def __init__(self, context, plugindef):
def __init__(self, context, plugindef, mandatory_options=None):
bareosfd.DebugMessage(
context, 100, "Constructor called in module %s\n" %
(__name__))
Expand All @@ -43,6 +43,8 @@ def __init__(self, context, plugindef):
events.append(bEventType['bEventEndBackupJob'])
events.append(bEventType['bEventEndFileSet'])
events.append(bEventType['bEventHandleBackupFile'])
events.append(bEventType['bEventStartBackupJob'])
events.append(bEventType['bEventStartRestoreJob'])
bareosfd.RegisterEvents(context, events)
# get some static Bareos values
self.fdname = bareosfd.GetValue(context, bVariable['bVarFDName'])
Expand All @@ -52,39 +54,67 @@ def __init__(self, context, plugindef):
self.level = bareosfd.GetValue(context, bVariable['bVarLevel'])
self.jobName = bareosfd.GetValue(context, bVariable['bVarJobName'])
self.workingdir = bareosfd.GetValue(
context,
bVariable['bVarWorkingDir'])
context, bVariable['bVarWorkingDir'])
self.FNAME = "undef"
self.file = None
bareosfd.DebugMessage(
context, 100, "FDName = %s - BareosFdPluginBaseclass\n" %
(self.fdname))
context, 100, "FDName = %s - BareosFdPluginBaseclass\n"
% (self.fdname))
bareosfd.DebugMessage(
context, 100, "WorkingDir: %s JobId: %s\n" %
(self.workingdir, self.jobId))
context, 100, "WorkingDir: %s JobId: %s\n"
% (self.workingdir, self.jobId))
self.mandatory_options = mandatory_options

def __str__(self):
return "<%s:fdname=%s jobId=%s client=%s since=%d level=%c jobName=%s workingDir=%s>" % \
(self.__class__, self.fdname, self.jobId, self.client,
self.since, self.level, self.jobName, self.workingdir)
return ("<%s:fdname=%s jobId=%s client=%s since=%d level=%c jobName=%s workingDir=%s>"
% (self.__class__, self.fdname, self.jobId, self.client,
self.since, self.level, self.jobName, self.workingdir))

def parse_plugin_definition(self, context, plugindef):
bareosfd.DebugMessage(
context, 100, "plugin def parser called with %s\n" %
(plugindef))
context, 100,
"plugin def parser called with \"%s\"\n" % (plugindef))
# Parse plugin options into a dict
self.options = dict()
plugin_options = plugindef.split(":")
for current_option in plugin_options:
while plugin_options:
current_option = plugin_options.pop(0)
key, sep, val = current_option.partition("=")
bareosfd.DebugMessage(context, 100, "key:val = %s:%s" % (key, val))
# See if the last character is a escape of the value string
while val[-1:] == '\\':
val = val[:-1] + ':' + plugin_options.pop(0)
# Replace all '\\'
val = val.replace("\\", "")
bareosfd.DebugMessage(context, 100, "key:val = \"%s:%s\"\n" % (key, val))
if val == '':
continue
else:
self.options[key] = val
# you should overload this method with your own and do option checking
# here, return bRCs['bRC_Error'], if options are not ok
# or better call super.parse_plugin_definition in your own class and
# make sanity check on self.options afterwards
return bRCs['bRC_OK']

def check_options(self, context, mandatory_options=None):
'''
Check Plugin options
Here we just verify that eventual mandatory options are set.
If you have more to veriy, just overwrite ths method in your class
'''
if mandatory_options is None:
return bRCs['bRC_OK']

for option in mandatory_options:
if (option not in self.options):
bareosfd.DebugMessage(
context, 100,
"Mandatory option \'%s\' not defined.\n" % option)
bareosfd.JobMessage(
context, bJobMessageType['M_FATAL'],
"Mandatory option \'%s\' not defined.\n" % option)
return bRCs['bRC_Error']

bareosfd.DebugMessage(
context, 100,
"Using Option %s=%s\n" % (option, self.options[option]))

return bRCs['bRC_OK']

def plugin_io(self, context, IOP):
Expand Down Expand Up @@ -159,51 +189,71 @@ def handle_plugin_event(self, context, event):

elif event == bEventType['bEventEndFileSet']:
bareosfd.DebugMessage(
context,
100,
context, 100,
"handle_plugin_event called with bEventEndFileSet\n")

elif event == bEventType['bEventStartBackupJob']:
bareosfd.DebugMessage(
context, 100,
"handle_plugin_event() called with bEventStartBackupJob\n")

return self.start_backup_job(context)

elif event == bEventType['bEventStartRestoreJob']:
bareosfd.DebugMessage(
context, 100,
"handle_plugin_event() called with bEventStartRestoreJob\n")

return self.start_restore_job(context)

else:
bareosfd.DebugMessage(
context, 100,
"handle_plugin_event called with event %s\n" % (event))

return bRCs['bRC_OK']

def start_backup_file(self, context, savepkt):
def start_backup_job(self, context):
'''
Base method, we do not add anything, overload this method with your
implementation to add files to backup fileset
Start of Backup Job. Called just before backup job really start.
Overload this to arrange whatever you have to do at this time.
'''
return bRCs['bRC_OK']

def start_backup_file(self, context, savepkt):
'''
Base method, we do not add anything, overload this method with your
implementation to add files to backup fileset
'''
bareosfd.DebugMessage(context, 100, "start_backup called\n")
return bRCs['bRC_Skip']

def end_backup_file(self, context):
bareosfd.DebugMessage(
context,
100,
"end_backup_file() entry point in Python called\n")
context, 100, "end_backup_file() entry point in Python called\n")
return bRCs['bRC_OK']

def start_restore_job(self, context):
'''
Start of Restore Job. Called , if you have Restore objects.
Overload this to handle restore objects, if applicable
'''
return bRCs['bRC_OK']

def start_restore_file(self, context, cmd):
bareosfd.DebugMessage(
context, 100,
"start_restore_file() entry point in Python called with %s\n"
% (cmd))
"start_restore_file() entry point in Python called with %s\n" % (cmd))
return bRCs['bRC_OK']

def end_restore_file(self, context):
bareosfd.DebugMessage(
context,
100,
"end_restore_file() entry point in Python called\n")
context, 100, "end_restore_file() entry point in Python called\n")
return bRCs['bRC_OK']

def restore_object_data(self, context, ROP):
bareosfd.DebugMessage(
context,
100,
"restore_object_data called with " + str(ROP) + "\n")
context, 100, "restore_object_data called with " + str(ROP) + "\n")
return bRCs['bRC_OK']

def create_file(self, context, restorepkt):
Expand All @@ -214,16 +264,13 @@ def create_file(self, context, restorepkt):
'''
bareosfd.DebugMessage(
context, 100,
"create_file() entry point in Python called with %s\n"
% (restorepkt))
"create_file() entry point in Python called with %s\n" % (restorepkt))
FNAME = restorepkt.ofname
dirname = os.path.dirname(FNAME)
if not os.path.exists(dirname):
bareosfd.DebugMessage(
context,
200,
"Directory %s does not exist, creating it now\n" %
dirname)
context, 200,
"Directory %s does not exist, creating it now\n" % dirname)
os.makedirs(dirname)
# open creates the file, if not yet existing, we close it again right
# aways it will be opened again in plugin_io.
Expand All @@ -237,40 +284,39 @@ def create_file(self, context, restorepkt):

def set_file_attributes(self, context, restorepkt):
bareosfd.DebugMessage(
context, 100,
"set_file_attributes() entry point in Python called with %s\n" %
context, 100, "set_file_attributes() entry point in Python called with %s\n" %
(str(restorepkt)))
return bRCs['bRC_OK'];
return bRCs['bRC_OK']

def check_file(self, context, fname):
bareosfd.DebugMessage(
context, 100,
"check_file() entry point in Python called with %s\n" % (fname))
context, 100, "check_file() entry point in Python called with %s\n" % (fname))
return bRCs['bRC_OK']

def get_acl(self, context, acl):
bareosfd.DebugMessage(
context, 100, "get_acl() entry point in Python called with %s\n" % (acl))
return bRCs['bRC_OK'];
return bRCs['bRC_OK']

def set_acl(self, context, acl):
bareosfd.DebugMessage(
context, 100, "set_acl() entry point in Python called with %s\n" % (acl))
return bRCs['bRC_OK'];
return bRCs['bRC_OK']

def get_xattr(self, context, xattr):
bareosfd.DebugMessage(
context, 100, "get_xattr() entry point in Python called with %s\n" % (xattr))
return bRCs['bRC_OK'];
return bRCs['bRC_OK']

def set_xattr(self, context, xattr):
bareosfd.DebugMessage(
context, 100, "set_xattr() entry point in Python called with %s\n" % (xattr))
return bRCs['bRC_OK'];
return bRCs['bRC_OK']

def handle_backup_file(self, context, savepkt):
bareosfd.DebugMessage(
context, 100, "handle_backup_file() entry point in Python called with %s\n" % (savepkt))
context, 100,
"handle_backup_file() entry point in Python called with %s\n" % (savepkt))
return bRCs['bRC_OK']

# vim: ts=4 tabstop=4 expandtab shiftwidth=4 softtabstop=4
23 changes: 13 additions & 10 deletions src/plugins/filed/BareosFdPluginLocalFileset.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,21 @@ class BareosFdPluginLocalFileset(BareosFdPluginBaseclass.BareosFdPluginBaseclass
listed there Filename is taken from plugin argument 'filename'
'''

def parse_plugin_definition(self, context, plugindef):
def __init__(self, context, plugindef):
bareosfd.DebugMessage(
context, 100,
"Constructor called in module %s with plugindef=%s\n"
% (__name__, plugindef))
# Last argument of super constructor is a list of mandatory arguments
super(BareosFdPluginLocalFileset, self).__init__(context, plugindef, ['filename'])

def start_backup_job(self, context):
'''
Parses the plugin argmuents and reads files from file given by
argument 'filename'
At this point, plugin options were passed and checked already.
We try to read from filename and setup the list of file to backup
in self.files_to_backup
'''
# BareosFdPluginBaseclass.parse_plugin_definition(self, context, plugindef); # noqa
super(BareosFdPluginLocalFileset, self).parse_plugin_definition(
context, plugindef)
if ('filename' not in self.options):
bareosfd.DebugMessage(context, 100,
"Option \'filename\' not defined.\n")
return bRCs['bRC_Error']

bareosfd.DebugMessage(context, 100,
"Using %s to search for local files\n"
% (self.options['filename']))
Expand Down

0 comments on commit fc96743

Please sign in to comment.