Skip to content

Commit

Permalink
fd-plugin: format code
Browse files Browse the repository at this point in the history
  • Loading branch information
franku committed Oct 7, 2020
1 parent 74c9d40 commit 9452f7d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 80 deletions.
52 changes: 15 additions & 37 deletions core/src/plugins/filed/python/postgres/BareosFdPluginPostgres.py
Expand Up @@ -37,9 +37,7 @@
from BareosFdPluginBaseclass import *


class BareosFdPluginPostgres(
BareosFdPluginLocalFilesBaseclass
): # noqa
class BareosFdPluginPostgres(BareosFdPluginLocalFilesBaseclass): # noqa
"""
Simple Bareos-FD-Plugin-Class that parses a file and backups all files
listed there Filename is taken from plugin argument 'filename'
Expand Down Expand Up @@ -90,9 +88,7 @@ def check_options(self, mandatory_options=None):
"""
Check for mandatory options and verify database connection
"""
result = super(BareosFdPluginPostgres, self).check_options(
mandatory_options
)
result = super(BareosFdPluginPostgres, self).check_options(mandatory_options)
if not result == bRC_OK:
return result
# Accurate may cause problems with plugins
Expand Down Expand Up @@ -134,8 +130,7 @@ def execute_SQL(self, sqlStatement):
self.dbCursor.execute(sqlStatement)
except Exception as e:
bareosfd.JobMessage(
M_ERROR,
'Query "%s" failed: "%s"' % (sqlStatement, e.message),
M_ERROR, 'Query "%s" failed: "%s"' % (sqlStatement, e.message),
)
return False
return True
Expand Down Expand Up @@ -168,8 +163,7 @@ def start_backup_job(self):
# )
## WARNING: JobMessages cause fatal errors at this stage
JobMessage(
M_INFO,
"Connected to Postgres version %d\n" % (self.pgVersion),
M_INFO, "Connected to Postgres version %d\n" % (self.pgVersion),
)
except:
bareosfd.JobMessage(
Expand Down Expand Up @@ -205,8 +199,7 @@ def start_backup_job(self):
switchLsnStmt = "SELECT pg_switch_xlog()"
if pgMajorVersion < 9:
bareosfd.JobMessage(
M_INFO,
"WAL switching not supported on Postgres Version < 9\n",
M_INFO, "WAL switching not supported on Postgres Version < 9\n",
)
else:
if self.execute_SQL(getLsnStmt):
Expand All @@ -225,8 +218,7 @@ def start_backup_job(self):
# Let Postgres write latest transaction into a new WAL file now
if not self.execute_SQL(switchLsnStmt):
bareosfd.JobMessage(
M_WARNING,
"Could not switch to next WAL segment\n",
M_WARNING, "Could not switch to next WAL segment\n",
)
if self.execute_SQL(getLsnStmt):
currentLSN = self.formatLSN(self.dbCursor.fetchone()[0])
Expand Down Expand Up @@ -296,25 +288,20 @@ def start_backup_job(self):
self.parseBackupLabelFile()
bareosfd.JobMessage(
M_FATAL,
'Another Postgres Backup Operation "%s" is in progress. '
% self.laLABEL
'Another Postgres Backup Operation "%s" is in progress. ' % self.laLABEL
+ "You may stop it using SELECT pg_stop_backup()",
)
return bRC_Error

bareosfd.DebugMessage(
100, "Send 'SELECT pg_start_backup' to Postgres\n"
)
bareosfd.DebugMessage(100, "Send 'SELECT pg_start_backup' to Postgres\n")
# We tell Postgres that we want to start to backup file now
self.backupStartTime = datetime.datetime.now(
tz=dateutil.tz.tzoffset(None, self.tzOffset)
)
if not self.execute_SQL(
"SELECT pg_start_backup('%s');" % self.backupLabelString
):
bareosfd.JobMessage(
M_FATAL, "pg_start_backup statement failed."
)
bareosfd.JobMessage(M_FATAL, "pg_start_backup statement failed.")
return bRC_Error
results = self.dbCursor.fetchall()
bareosfd.DebugMessage(150, "Start response: %s\n" % str(results))
Expand All @@ -331,8 +318,7 @@ def parseBackupLabelFile(self):
labelFile = open(self.labelFileName, "r")
except:
bareosfd.JobMessage(
M_ERROR,
"Could not open Label File %s" % (self.labelFileName),
M_ERROR, "Could not open Label File %s" % (self.labelFileName),
)

for labelItem in labelFile.read().splitlines():
Expand All @@ -352,9 +338,7 @@ def start_backup_file(self, savepkt):

# Plain files are handled by super class
if self.files_to_backup[-1] not in ["ROP"]:
return super(BareosFdPluginPostgres, self).start_backup_file(
savepkt
)
return super(BareosFdPluginPostgres, self).start_backup_file(savepkt)

# Here we create the restore object
self.file_to_backup = self.files_to_backup.pop()
Expand All @@ -368,7 +352,7 @@ def start_backup_file(self, savepkt):
savepkt.object_name = savepkt.fname
bareosfd.DebugMessage(150, "fname: " + savepkt.fname + "\n")
bareosfd.DebugMessage(150, "rop " + str(self.rop_data) + "\n")
savepkt.object = bytearray(json.dumps(self.rop_data),"utf-8")
savepkt.object = bytearray(json.dumps(self.rop_data), "utf-8")
savepkt.object_len = len(savepkt.object)
savepkt.object_index = int(time.time())
else:
Expand Down Expand Up @@ -431,9 +415,7 @@ def closeDbConnection(self):
)
self.PostgressFullBackupRunning = False
else:
bareosfd.JobMessage(
M_ERROR, "pg_stop_backup statement failed."
)
bareosfd.JobMessage(M_ERROR, "pg_stop_backup statement failed.")

def checkForWalFiles(self):
"""
Expand All @@ -460,9 +442,7 @@ def checkForWalFiles(self):
fileMtime.replace(tzinfo=dateutil.tz.tzoffset(None, self.tzOffset))
> self.backupStartTime
):
bareosfd.DebugMessage(
150, "Adding WAL file %s for backup\n" % fileName
)
bareosfd.DebugMessage(150, "Adding WAL file %s for backup\n" % fileName)
self.files_to_backup.append(fullPath)

if self.files_to_backup:
Expand All @@ -475,9 +455,7 @@ def end_backup_file(self):
Here we return 'bRC_More' as long as our list files_to_backup is not
empty and bRC_OK when we are done
"""
bareosfd.DebugMessage(
100, "end_backup_file() entry point in Python called\n"
)
bareosfd.DebugMessage(100, "end_backup_file() entry point in Python called\n")
if self.files_to_backup:
return bRC_More
else:
Expand Down
Expand Up @@ -31,9 +31,7 @@
import stat


class BareosFdPluginLocalFilesBaseclass(
BareosFdPluginBaseclass
): # noqa
class BareosFdPluginLocalFilesBaseclass(BareosFdPluginBaseclass): # noqa
"""
Simple Bareos-FD-Plugin-Class that parses a file and backups all files
listed there Filename is taken from plugin argument 'filename'
Expand Down Expand Up @@ -90,8 +88,7 @@ def start_backup_file(self, savepkt):
except Exception as e:
bareosfd.JobMessage(
bareosfd.M_ERROR,
'Could net get stat-info for file %s: "%s"'
% (self.file_to_backup, e),
'Could net get stat-info for file %s: "%s"' % (self.file_to_backup, e),
)
# As of Bareos 19.2.7 attribute names in bareosfd.StatPacket differ from os.stat
# In this case we have to translate names
Expand All @@ -111,17 +108,15 @@ def start_backup_file(self, savepkt):
mystatp.st_atime = statp.st_atime
mystatp.st_mtime = statp.st_mtime
mystatp.st_ctime = statp.st_ctime
#bareosfd.JobMessage( bareosfd.M_ERROR, '\nmystatp: %s\nstatp: %s\n' % (mystatp,statp))
# bareosfd.JobMessage( bareosfd.M_ERROR, '\nmystatp: %s\nstatp: %s\n' % (mystatp,statp))

savepkt.fname = self.file_to_backup
# os.islink will detect links to directories only when
# there is no trailing slash - we need to perform checks
# on the stripped name but use it with trailing / for the backup itself
if os.path.islink(self.file_to_backup.rstrip("/")):
savepkt.type = bareosfd.FT_LNK
savepkt.link = os.readlink(
self.file_to_backup.rstrip("/")
)
savepkt.link = os.readlink(self.file_to_backup.rstrip("/"))
bareosfd.DebugMessage(150, "file type is: FT_LNK\n")
elif os.path.isfile(self.file_to_backup):
savepkt.type = bareosfd.FT_REG
Expand All @@ -137,8 +132,7 @@ def start_backup_file(self, savepkt):
bareosfd.DebugMessage(150, "file type is: FT_FIFO\n")
else:
bareosfd.JobMessage(
bareosfd.M_WARNING,
"File %s of unknown type" % (self.file_to_backup),
bareosfd.M_WARNING, "File %s of unknown type" % (self.file_to_backup),
)
return bareosfd.bRC_Skip

Expand All @@ -154,8 +148,7 @@ def create_file(self, restorepkt):
virtual files or similar
"""
bareosfd.DebugMessage(
100,
"create_file() entry point in Python called with %s\n" % (restorepkt),
100, "create_file() entry point in Python called with %s\n" % (restorepkt),
)
FNAME = restorepkt.ofname
if not FNAME:
Expand Down Expand Up @@ -194,8 +187,7 @@ def create_file(self, restorepkt):
os.mkfifo(FNAME, 0o600)
except Exception as e:
bareosfd.JobMessage(
bareosfd.M_ERROR,
'Could net create fifo %s: "%s"' % (FNAME, e),
bareosfd.M_ERROR, 'Could net create fifo %s: "%s"' % (FNAME, e),
)
restorepkt.create_status = bareosfd.CF_CREATED
else:
Expand Down Expand Up @@ -262,10 +254,13 @@ def end_restore_file(self):
+ "\n",
)
try:
os.chown(self.FNAME, self.statp[self.FNAME].st_uid, self.statp[self.FNAME].st_gid)
os.chown(
self.FNAME, self.statp[self.FNAME].st_uid, self.statp[self.FNAME].st_gid
)
os.chmod(self.FNAME, self.statp[self.FNAME].st_mode)
os.utime(
self.FNAME, (self.statp[self.FNAME].st_atime, self.statp[self.FNAME].st_mtime)
self.FNAME,
(self.statp[self.FNAME].st_atime, self.statp[self.FNAME].st_mtime),
)
# del sometimes leads to no-key errors, it seams that end_restore_file is sometimes called
# multipl times.
Expand All @@ -282,9 +277,7 @@ def end_backup_file(self):
Here we return 'bRC_More' as long as our list files_to_backup is not
empty and bRC_OK when we are done
"""
bareosfd.DebugMessage(
100, "end_backup_file() entry point in Python called\n"
)
bareosfd.DebugMessage(100, "end_backup_file() entry point in Python called\n")
if self.files_to_backup:
return bareosfd.bRC_More
else:
Expand Down
Expand Up @@ -31,9 +31,7 @@
import stat


class BareosFdPluginLocalFileset(
BareosFdPluginLocalFilesBaseclass
): # noqa
class BareosFdPluginLocalFileset(BareosFdPluginLocalFilesBaseclass): # noqa
"""
Simple Bareos-FD-Plugin-Class that parses a file and backups all files
listed there Filename is taken from plugin argument 'filename'
Expand All @@ -45,9 +43,7 @@ def __init__(self, plugindef, mandatory_options=None):
"Constructor called in module %s with plugindef=%s\n"
% (__name__, plugindef),
)
super(BareosFdPluginLocalFileset, self).__init__(
plugindef, mandatory_options
)
super(BareosFdPluginLocalFileset, self).__init__(plugindef, mandatory_options)

def filename_is_allowed(self, filename, allowregex, denyregex):
"""
Expand All @@ -65,12 +61,9 @@ def filename_is_allowed(self, filename, allowregex, denyregex):
else:
denied = True
if not allowed or denied:
bareosfd.DebugMessage(
100, "File %s denied by configuration\n" % (filename)
)
bareosfd.DebugMessage(100, "File %s denied by configuration\n" % (filename))
bareosfd.JobMessage(
bareosfd.M_ERROR,
"File %s denied by configuration\n" % (filename),
bareosfd.M_ERROR, "File %s denied by configuration\n" % (filename),
)
return False
else:
Expand All @@ -83,16 +76,14 @@ def start_backup_job(self):
in self.files_to_backup
"""
bareosfd.DebugMessage(
100,
"Using %s to search for local files\n" % self.options["filename"],
100, "Using %s to search for local files\n" % self.options["filename"],
)
if os.path.exists(self.options["filename"]):
try:
config_file = open(self.options["filename"], "r")
except:
bareosfd.DebugMessage(
100,
"Could not open file %s\n" % (self.options["filename"]),
100, "Could not open file %s\n" % (self.options["filename"]),
)
return bareosfd.bRC_Error
else:
Expand Down Expand Up @@ -120,22 +111,17 @@ def start_backup_job(self):
for topdir, dirNames, fileNames in os.walk(listItem):
for fileName in fileNames:
if self.filename_is_allowed(
os.path.join(topdir, fileName),
self.allow,
self.deny,
os.path.join(topdir, fileName), self.allow, self.deny,
):
self.append_file_to_backup(
os.path.join(topdir, fileName)
)
self.append_file_to_backup(os.path.join(topdir, fileName))
for dirName in dirNames:
fullDirName = os.path.join(topdir, dirName) + "/"
self.append_file_to_backup(fullDirName)
bareosfd.DebugMessage(150, "Filelist: %s\n" % (self.files_to_backup))

if not self.files_to_backup:
bareosfd.JobMessage(
bareosfd.M_ERROR,
"No (allowed) files to backup found\n",
bareosfd.M_ERROR, "No (allowed) files to backup found\n",
)
return bareosfd.bRC_Error
else:
Expand Down

0 comments on commit 9452f7d

Please sign in to comment.