From d75d83f202811a00a948d56211ab480f158e5b23 Mon Sep 17 00:00:00 2001 From: Spitap Date: Mon, 19 Sep 2022 15:13:44 +0200 Subject: [PATCH] more refactoring --- modoboa_installer/scripts/amavis.py | 13 +++--- modoboa_installer/scripts/backup.py | 49 +++++++++++------------ modoboa_installer/scripts/base.py | 4 -- modoboa_installer/scripts/dovecot.py | 2 +- modoboa_installer/scripts/modoboa.py | 10 ++--- modoboa_installer/scripts/postwhite.py | 6 +-- modoboa_installer/scripts/spamassassin.py | 10 ++--- 7 files changed, 44 insertions(+), 50 deletions(-) diff --git a/modoboa_installer/scripts/amavis.py b/modoboa_installer/scripts/amavis.py index 23a508d..2e5b696 100644 --- a/modoboa_installer/scripts/amavis.py +++ b/modoboa_installer/scripts/amavis.py @@ -42,12 +42,13 @@ class Amavis(base.Installer): def get_config_files(self): """Return appropriate config files.""" if package.backend.FORMAT == "deb": - amavisCustomConf = os.path.join(self.restore, "custom/99-custom") - if self.restore and os.path.isfile(amavisCustomConf): - utils.copy_file(amavisCustomConf, os.path.join( + amavis_custom_configuration = os.path.join( + self.restore, "custom/99-custom") + if self.restore and os.path.isfile(amavis_custom_configuration): + utils.copy_file(amavis_custom_configuration, os.path.join( self.config_dir, "/conf.d")) utils.printcolor( - "Custom amavis configuration restored", utils.GREEN) + "Custom amavis configuration restored.", utils.GREEN) return [ "conf.d/05-node_id", "conf.d/15-content_filter_mode", "conf.d/50-user"] @@ -77,7 +78,7 @@ class Amavis(base.Installer): """Return schema path.""" if self.restore: 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( self.restore, "databases/amavis.sql") if os.path.isfile(amavis_db_backup_path): @@ -85,7 +86,7 @@ class Amavis(base.Installer): "Amavis database backup found ! Restoring...", utils.GREEN) return amavis_db_backup_path 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") if version is None: diff --git a/modoboa_installer/scripts/backup.py b/modoboa_installer/scripts/backup.py index 84071f9..0b3aa9e 100644 --- a/modoboa_installer/scripts/backup.py +++ b/modoboa_installer/scripts/backup.py @@ -30,17 +30,11 @@ class Backup: def __init__(self, config, silent_backup, backup_path, nomail): self.config = config - self.destinationPath = backup_path + self.backup_path = backup_path self.nomail = nomail self.silent_backup = silent_backup - def preparePath(self): - 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): + def validate_path(self, path): """Check basic condition for backup directory.""" path_exists = os.path.exists(path) @@ -52,10 +46,10 @@ class Backup: if not path_exists: 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() - 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") utils.mkdir_safe(path, stat.S_IRWXU | stat.S_IRWXG, pw[2], pw[3]) @@ -64,40 +58,44 @@ class Backup: if len(os.listdir(path)) != 0: 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() - 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) else: 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 def set_path(self): """Setup backup directory.""" 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") path = f"./modoboa_backup/backup_{date}/" - self.validatePath(path) + self.validate_path(path) else: - if not self.validatePath(self.destinationPath): + if not self.validate_path(self.backup_path): utils.printcolor( - f"Path provided : {self.destinationPath}", utils.BLUE) + f"Path provided : {self.backup_path}", utils.BLUE) sys.exit(1) else: 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( "Enter backup path, please provide an empty folder.", utils.MAGENTA) utils.printcolor("CTRL+C to cancel", utils.MAGENTA) user_value = utils.user_input("-> ") def config_file_backup(self): - utils.copy_file("installer.cfg", self.destinationPath) + utils.copy_file("installer.cfg", self.backup_path) def mail_backup(self): if self.nomail: @@ -114,7 +112,7 @@ class Backup: f" ({home_path}) seems not right...", utils.RED) else: - dst = os.path.join(self.destinationPath, "mails/") + dst = os.path.join(self.backup_path, "mails/") if os.path.exists(dst): shutil.rmtree(dst) @@ -131,7 +129,7 @@ class Backup: "Backing up some custom configuration...", utils.MAGENTA) custom_path = os.path.join( - self.destinationPath, "custom") + self.backup_path, "custom") # AMAVIS if (self.config.has_option("amavis", "enabled") and @@ -145,8 +143,7 @@ class Backup: # POSTWHITE if (self.config.has_option("postwhite", "enabled") and self.config.getboolean("postwhite", "enabled")): - postswhite_custom = os.path.join(self.config.get( - "postwhite", "config_dir", "postwhite.conf")) + postswhite_custom = "/etc/postwhite.conf" if os.path.isfile(postswhite_custom): utils.copy_file(postswhite_custom, custom_path) utils.printcolor( @@ -163,7 +160,7 @@ class Backup: 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) if app_name == "modoboa" or (self.config.has_option(app_name, "enabled") and @@ -176,7 +173,7 @@ class Backup: def backup_completed(self): utils.printcolor("Backup process done, your backup is availible here:" - f"--> {self.destinationPath}", utils.GREEN) + f"--> {self.backup_path}", utils.GREEN) def run(self): self.set_path() diff --git a/modoboa_installer/scripts/base.py b/modoboa_installer/scripts/base.py index 67e3f0b..d56fdb0 100644 --- a/modoboa_installer/scripts/base.py +++ b/modoboa_installer/scripts/base.py @@ -161,10 +161,6 @@ class Installer(object): """Tasks to execute before the installer starts.""" pass - def restore(self): - """Tasks to execute to restore files/databases.""" - pass - def post_run(self): """Additionnal tasks.""" pass diff --git a/modoboa_installer/scripts/dovecot.py b/modoboa_installer/scripts/dovecot.py index 241ee7e..cc353ed 100644 --- a/modoboa_installer/scripts/dovecot.py +++ b/modoboa_installer/scripts/dovecot.py @@ -91,7 +91,7 @@ class Dovecot(base.Installer): mail_dir = os.path.join(self.restore, "mails/") if self.restore and len(os.listdir(mail_dir)) > 0: 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): shutil.rmtree(self.home_dir) diff --git a/modoboa_installer/scripts/modoboa.py b/modoboa_installer/scripts/modoboa.py index 3f1f164..d9e7ad0 100644 --- a/modoboa_installer/scripts/modoboa.py +++ b/modoboa_installer/scripts/modoboa.py @@ -179,15 +179,15 @@ class Modoboa(base.Installer): def get_sql_schema_path(self): if self.restore: utils.printcolor( - "Trying to restore modoboa database from backup", utils.MAGENTA) - modoboaDbBackupPath = os.path.join( + "Trying to restore modoboa database from backup.", utils.MAGENTA) + modoboa_database_backup_path = os.path.join( self.restore, "databases/modoboa.sql") - if os.path.isfile(modoboaDbBackupPath): + if os.path.isfile(modoboa_database_backup_path): utils.printcolor( "Modoboa database backup found ! Restoring...", utils.GREEN) - return modoboaDbBackupPath + return modoboa_database_backup_path 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()() diff --git a/modoboa_installer/scripts/postwhite.py b/modoboa_installer/scripts/postwhite.py index d6e6a67..040a212 100644 --- a/modoboa_installer/scripts/postwhite.py +++ b/modoboa_installer/scripts/postwhite.py @@ -46,10 +46,10 @@ class Postwhite(base.Installer): self.install_from_archive(SPF_TOOLS_REPOSITORY, install_dir) postw_dir = self.install_from_archive( POSTWHITE_REPOSITORY, install_dir) - postwhiteBackupConf = os.path.join( + postwhite_backup_configuration = os.path.join( self.restore, "custom/postwhite.conf") - if self.restore and os.path.isfile(postwhiteBackupConf): - utils.copy_file(postwhiteBackupConf, "/etc") + if self.restore and os.path.isfile(postwhite_backup_configuration): + utils.copy_file(postwhite_backup_configuration, "/etc") utils.printcolor( "postwhite.conf restored from backup", utils.GREEN) else: diff --git a/modoboa_installer/scripts/spamassassin.py b/modoboa_installer/scripts/spamassassin.py index 00843ce..150f238 100644 --- a/modoboa_installer/scripts/spamassassin.py +++ b/modoboa_installer/scripts/spamassassin.py @@ -27,15 +27,15 @@ class Spamassassin(base.Installer): """Return SQL schema.""" if self.restore: utils.printcolor( - "Trying to restore spamassassin database from backup", utils.MAGENTA) - amavisDbBackupPath = os.path.join( + "Trying to restore spamassassin database from backup.", utils.MAGENTA) + sa_database_backup_path = os.path.join( self.restore, "databases/spamassassin.sql") - if os.path.isfile(amavisDbBackupPath): + if os.path.isfile(sa_database_backup_path): utils.printcolor( "Spamassassin database backup found ! Restoring...", utils.GREEN) - return amavisDbBackupPath + return sa_database_backup_path 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": fname = "bayes_pg.sql"