more refactoring

This commit is contained in:
Spitap
2022-09-19 15:13:44 +02:00
parent f3811b4b39
commit d75d83f202
7 changed files with 44 additions and 50 deletions

View File

@@ -42,12 +42,13 @@ class Amavis(base.Installer):
def get_config_files(self): def get_config_files(self):
"""Return appropriate config files.""" """Return appropriate config files."""
if package.backend.FORMAT == "deb": if package.backend.FORMAT == "deb":
amavisCustomConf = os.path.join(self.restore, "custom/99-custom") amavis_custom_configuration = os.path.join(
if self.restore and os.path.isfile(amavisCustomConf): self.restore, "custom/99-custom")
utils.copy_file(amavisCustomConf, os.path.join( if self.restore and os.path.isfile(amavis_custom_configuration):
utils.copy_file(amavis_custom_configuration, os.path.join(
self.config_dir, "/conf.d")) self.config_dir, "/conf.d"))
utils.printcolor( utils.printcolor(
"Custom amavis configuration restored", utils.GREEN) "Custom amavis configuration restored.", utils.GREEN)
return [ return [
"conf.d/05-node_id", "conf.d/15-content_filter_mode", "conf.d/05-node_id", "conf.d/15-content_filter_mode",
"conf.d/50-user"] "conf.d/50-user"]
@@ -77,7 +78,7 @@ class Amavis(base.Installer):
"""Return schema path.""" """Return schema path."""
if self.restore: if self.restore:
utils.printcolor( utils.printcolor(
"Trying to restore amavis database from backup", utils.MAGENTA) "Trying to restore amavis database from backup.", utils.MAGENTA)
amavis_db_backup_path = os.path.join( amavis_db_backup_path = os.path.join(
self.restore, "databases/amavis.sql") self.restore, "databases/amavis.sql")
if os.path.isfile(amavis_db_backup_path): if os.path.isfile(amavis_db_backup_path):
@@ -85,7 +86,7 @@ class Amavis(base.Installer):
"Amavis database backup found ! Restoring...", utils.GREEN) "Amavis database backup found ! Restoring...", utils.GREEN)
return amavis_db_backup_path return amavis_db_backup_path
utils.printcolor( utils.printcolor(
"Amavis database backup not found, creating empty database", utils.RED) "Amavis database backup not found, creating empty database.", utils.RED)
version = package.backend.get_installed_version("amavisd-new") version = package.backend.get_installed_version("amavisd-new")
if version is None: if version is None:

View File

@@ -30,17 +30,11 @@ class Backup:
def __init__(self, config, silent_backup, backup_path, nomail): def __init__(self, config, silent_backup, backup_path, nomail):
self.config = config self.config = config
self.destinationPath = backup_path self.backup_path = backup_path
self.nomail = nomail self.nomail = nomail
self.silent_backup = silent_backup self.silent_backup = silent_backup
def preparePath(self): def validate_path(self, path):
pw = pwd.getpwnam("root")
for dir in ["custom/", "databases/"]:
utils.mkdir_safe(os.path.join(self.destinationPath, dir),
stat.S_IRWXU | stat.S_IRWXG, pw[2], pw[3])
def validatePath(self, path):
"""Check basic condition for backup directory.""" """Check basic condition for backup directory."""
path_exists = os.path.exists(path) path_exists = os.path.exists(path)
@@ -52,10 +46,10 @@ class Backup:
if not path_exists: if not path_exists:
if not self.silent_backup: if not self.silent_backup:
createDir = input( create_dir = input(
f"\"{path}\" doesn't exists, would you like to create it ? [Y/n]\n").lower() f"\"{path}\" doesn't exists, would you like to create it ? [Y/n]\n").lower()
if self.silent_backup or (not self.silent_backup and (createDir == "y" or createDir == "yes")): if self.silent_backup or (not self.silent_backup and (create_dir == "y" or create_dir == "yes")):
pw = pwd.getpwnam("root") pw = pwd.getpwnam("root")
utils.mkdir_safe(path, stat.S_IRWXU | utils.mkdir_safe(path, stat.S_IRWXU |
stat.S_IRWXG, pw[2], pw[3]) stat.S_IRWXG, pw[2], pw[3])
@@ -64,40 +58,44 @@ class Backup:
if len(os.listdir(path)) != 0: if len(os.listdir(path)) != 0:
if not self.silent_backup: if not self.silent_backup:
delDir = input( delete_dir = input(
"Warning : backup folder is not empty, it will be purged if you continue... [Y/n]\n").lower() "Warning : backup folder is not empty, it will be purged if you continue... [Y/n]\n").lower()
if self.silent_backup or (not self.silent_backup and (delDir == "y" or delDir == "yes")): if self.silent_backup or (not self.silent_backup and (delete_dir == "y" or delete_dir == "yes")):
shutil.rmtree(path) shutil.rmtree(path)
else: else:
return False return False
self.destinationPath = path self.backup_path = path
pw = pwd.getpwnam("root")
for dir in ["custom/", "databases/"]:
utils.mkdir_safe(os.path.join(self.backup_path, dir),
stat.S_IRWXU | stat.S_IRWXG, pw[2], pw[3])
self.preparePath()
return True return True
def set_path(self): def set_path(self):
"""Setup backup directory.""" """Setup backup directory."""
if self.silent_backup: if self.silent_backup:
if self.destinationPath is None: if self.backup_path is None:
date = datetime.datetime.now().strftime("%m_%d_%Y_%H_%M") date = datetime.datetime.now().strftime("%m_%d_%Y_%H_%M")
path = f"./modoboa_backup/backup_{date}/" path = f"./modoboa_backup/backup_{date}/"
self.validatePath(path) self.validate_path(path)
else: else:
if not self.validatePath(self.destinationPath): if not self.validate_path(self.backup_path):
utils.printcolor( utils.printcolor(
f"Path provided : {self.destinationPath}", utils.BLUE) f"Path provided : {self.backup_path}", utils.BLUE)
sys.exit(1) sys.exit(1)
else: else:
user_value = None user_value = None
while user_value == "" or user_value is None or not self.validatePath(user_value): while user_value == "" or user_value is None or not self.validate_path(user_value):
utils.printcolor( utils.printcolor(
"Enter backup path, please provide an empty folder.", utils.MAGENTA) "Enter backup path, please provide an empty folder.", utils.MAGENTA)
utils.printcolor("CTRL+C to cancel", utils.MAGENTA) utils.printcolor("CTRL+C to cancel", utils.MAGENTA)
user_value = utils.user_input("-> ") user_value = utils.user_input("-> ")
def config_file_backup(self): def config_file_backup(self):
utils.copy_file("installer.cfg", self.destinationPath) utils.copy_file("installer.cfg", self.backup_path)
def mail_backup(self): def mail_backup(self):
if self.nomail: if self.nomail:
@@ -114,7 +112,7 @@ class Backup:
f" ({home_path}) seems not right...", utils.RED) f" ({home_path}) seems not right...", utils.RED)
else: else:
dst = os.path.join(self.destinationPath, "mails/") dst = os.path.join(self.backup_path, "mails/")
if os.path.exists(dst): if os.path.exists(dst):
shutil.rmtree(dst) shutil.rmtree(dst)
@@ -131,7 +129,7 @@ class Backup:
"Backing up some custom configuration...", utils.MAGENTA) "Backing up some custom configuration...", utils.MAGENTA)
custom_path = os.path.join( custom_path = os.path.join(
self.destinationPath, "custom") self.backup_path, "custom")
# AMAVIS # AMAVIS
if (self.config.has_option("amavis", "enabled") and if (self.config.has_option("amavis", "enabled") and
@@ -145,8 +143,7 @@ class Backup:
# POSTWHITE # POSTWHITE
if (self.config.has_option("postwhite", "enabled") and if (self.config.has_option("postwhite", "enabled") and
self.config.getboolean("postwhite", "enabled")): self.config.getboolean("postwhite", "enabled")):
postswhite_custom = os.path.join(self.config.get( postswhite_custom = "/etc/postwhite.conf"
"postwhite", "config_dir", "postwhite.conf"))
if os.path.isfile(postswhite_custom): if os.path.isfile(postswhite_custom):
utils.copy_file(postswhite_custom, custom_path) utils.copy_file(postswhite_custom, custom_path)
utils.printcolor( utils.printcolor(
@@ -163,7 +160,7 @@ class Backup:
def database_dump(self, app_name): def database_dump(self, app_name):
dump_path = os.path.join(self.destinationPath, "backup") dump_path = os.path.join(self.backup_path, "backup")
backend = database.get_backend(self.config) backend = database.get_backend(self.config)
if app_name == "modoboa" or (self.config.has_option(app_name, "enabled") and if app_name == "modoboa" or (self.config.has_option(app_name, "enabled") and
@@ -176,7 +173,7 @@ class Backup:
def backup_completed(self): def backup_completed(self):
utils.printcolor("Backup process done, your backup is availible here:" utils.printcolor("Backup process done, your backup is availible here:"
f"--> {self.destinationPath}", utils.GREEN) f"--> {self.backup_path}", utils.GREEN)
def run(self): def run(self):
self.set_path() self.set_path()

View File

@@ -161,10 +161,6 @@ class Installer(object):
"""Tasks to execute before the installer starts.""" """Tasks to execute before the installer starts."""
pass pass
def restore(self):
"""Tasks to execute to restore files/databases."""
pass
def post_run(self): def post_run(self):
"""Additionnal tasks.""" """Additionnal tasks."""
pass pass

View File

@@ -91,7 +91,7 @@ class Dovecot(base.Installer):
mail_dir = os.path.join(self.restore, "mails/") mail_dir = os.path.join(self.restore, "mails/")
if self.restore and len(os.listdir(mail_dir)) > 0: if self.restore and len(os.listdir(mail_dir)) > 0:
utils.printcolor( utils.printcolor(
"Copying mail backup over dovecot directory", utils.GREEN) "Copying mail backup over dovecot directory.", utils.GREEN)
if os.path.exists(self.home_dir): if os.path.exists(self.home_dir):
shutil.rmtree(self.home_dir) shutil.rmtree(self.home_dir)

View File

@@ -179,15 +179,15 @@ class Modoboa(base.Installer):
def get_sql_schema_path(self): def get_sql_schema_path(self):
if self.restore: if self.restore:
utils.printcolor( utils.printcolor(
"Trying to restore modoboa database from backup", utils.MAGENTA) "Trying to restore modoboa database from backup.", utils.MAGENTA)
modoboaDbBackupPath = os.path.join( modoboa_database_backup_path = os.path.join(
self.restore, "databases/modoboa.sql") self.restore, "databases/modoboa.sql")
if os.path.isfile(modoboaDbBackupPath): if os.path.isfile(modoboa_database_backup_path):
utils.printcolor( utils.printcolor(
"Modoboa database backup found ! Restoring...", utils.GREEN) "Modoboa database backup found ! Restoring...", utils.GREEN)
return modoboaDbBackupPath return modoboa_database_backup_path
utils.printcolor( utils.printcolor(
"Modoboa database backup not found, creating empty database", utils.RED) "Modoboa database backup not found, creating empty database.", utils.RED)
return super().get_sql_schema_path()() return super().get_sql_schema_path()()

View File

@@ -46,10 +46,10 @@ class Postwhite(base.Installer):
self.install_from_archive(SPF_TOOLS_REPOSITORY, install_dir) self.install_from_archive(SPF_TOOLS_REPOSITORY, install_dir)
postw_dir = self.install_from_archive( postw_dir = self.install_from_archive(
POSTWHITE_REPOSITORY, install_dir) POSTWHITE_REPOSITORY, install_dir)
postwhiteBackupConf = os.path.join( postwhite_backup_configuration = os.path.join(
self.restore, "custom/postwhite.conf") self.restore, "custom/postwhite.conf")
if self.restore and os.path.isfile(postwhiteBackupConf): if self.restore and os.path.isfile(postwhite_backup_configuration):
utils.copy_file(postwhiteBackupConf, "/etc") utils.copy_file(postwhite_backup_configuration, "/etc")
utils.printcolor( utils.printcolor(
"postwhite.conf restored from backup", utils.GREEN) "postwhite.conf restored from backup", utils.GREEN)
else: else:

View File

@@ -27,15 +27,15 @@ class Spamassassin(base.Installer):
"""Return SQL schema.""" """Return SQL schema."""
if self.restore: if self.restore:
utils.printcolor( utils.printcolor(
"Trying to restore spamassassin database from backup", utils.MAGENTA) "Trying to restore spamassassin database from backup.", utils.MAGENTA)
amavisDbBackupPath = os.path.join( sa_database_backup_path = os.path.join(
self.restore, "databases/spamassassin.sql") self.restore, "databases/spamassassin.sql")
if os.path.isfile(amavisDbBackupPath): if os.path.isfile(sa_database_backup_path):
utils.printcolor( utils.printcolor(
"Spamassassin database backup found ! Restoring...", utils.GREEN) "Spamassassin database backup found ! Restoring...", utils.GREEN)
return amavisDbBackupPath return sa_database_backup_path
utils.printcolor( utils.printcolor(
"Spamassassin database backup not found, creating empty database", utils.RED) "Spamassassin database backup not found, creating empty database.", utils.RED)
if self.dbengine == "postgres": if self.dbengine == "postgres":
fname = "bayes_pg.sql" fname = "bayes_pg.sql"