Better UX, use of os to concatenate path

This commit is contained in:
Spitap
2022-08-05 15:20:11 +02:00
parent e546d2cb23
commit 53e3e3ec58
8 changed files with 35 additions and 32 deletions

View File

@@ -42,10 +42,10 @@ 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 = self.restore + "custom/99-custom" amavisCustomConf = os.path.join(self.restore, "custom/99-custom")
if self.restore and os.path.isfile(amavisCustomConf): if self.restore and os.path.isfile(amavisCustomConf):
utils.printcolor("Restoring custom Amavis configuration", utils.MAGENTA) utils.copy_file(amavisCustomConf, os.path.join(self.config_dir, "/conf.d"))
utils.copy_file(amavisCustomConf, self.config_dir+"/conf.d") utils.printcolor("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"]
@@ -75,7 +75,7 @@ class Amavis(base.Installer):
"""Return schema path.""" """Return schema path."""
if self.restore: if self.restore:
utils.printcolor("Trying to restore amavis database from backup", utils.MAGENTA) utils.printcolor("Trying to restore amavis database from backup", utils.MAGENTA)
amavisDbBackupPath = self.restore + "databases/amavis.sql" amavisDbBackupPath = os.path.join(self.restore, "databases/amavis.sql")
if os.path.isfile(amavisDbBackupPath): if os.path.isfile(amavisDbBackupPath):
utils.printcolor("Amavis database backup found ! Restoring...", utils.GREEN) utils.printcolor("Amavis database backup found ! Restoring...", utils.GREEN)
return amavisDbBackupPath return amavisDbBackupPath

View File

@@ -43,15 +43,12 @@ class Backup():
def preparePath(self): def preparePath(self):
pw = pwd.getpwnam("root") pw = pwd.getpwnam("root")
for dir in self.BACKUPDIRECTORY: for dir in self.BACKUPDIRECTORY:
utils.mkdir_safe(self.destinationPath + dir, stat.S_IRWXU | stat.S_IRWXG, pw[2], pw[3]) utils.mkdir_safe(os.path.join(self.destinationPath,dir),
stat.S_IRWXU | stat.S_IRWXG, pw[2], pw[3])
def validatePath(self, path): def validatePath(self, path):
"""Check basic condition for backup directory""" """Check basic condition for backup directory"""
if path[-1] != "/":
path += "/"
try : try :
pathExists = os.path.exists(path) pathExists = os.path.exists(path)
except: except:
@@ -126,7 +123,7 @@ class Backup():
f" ({home_path}) seems not right...", utils.RED) f" ({home_path}) seems not right...", utils.RED)
else: else:
dst = self.destinationPath + "mails/" dst = os.path.join(self.destinationPath, "mails/")
if os.path.exists(dst): if os.path.exists(dst):
shutil.rmtree(dst) shutil.rmtree(dst)
@@ -142,7 +139,7 @@ class Backup():
Feel free to suggest to add others!""" Feel free to suggest to add others!"""
utils.printcolor("Backing up some custom configuration...", utils.MAGENTA) utils.printcolor("Backing up some custom configuration...", utils.MAGENTA)
custom_path = self.destinationPath + self.BACKUPDIRECTORY[0] custom_path = os.path.join(self.destinationPath, self.BACKUPDIRECTORY[0])
"""AMAVIS""" """AMAVIS"""
amavis_custom = "/etc/amavis/conf.d/99-custom" amavis_custom = "/etc/amavis/conf.d/99-custom"
@@ -162,14 +159,14 @@ class Backup():
utils.printcolor("Backing up databases...", utils.MAGENTA) utils.printcolor("Backing up databases...", utils.MAGENTA)
dump_path = self.destinationPath + self.BACKUPDIRECTORY[1] dump_path = os.path.join(self.destinationPath, self.BACKUPDIRECTORY[1])
backend = database.get_backend(self.config) backend = database.get_backend(self.config)
"""Modoboa""" """Modoboa"""
dbname = self.config.get("modoboa", "dbname") dbname = self.config.get("modoboa", "dbname")
dbuser = self.config.get("modoboa", "dbuser") dbuser = self.config.get("modoboa", "dbuser")
dbpasswd = self.config.get("modoboa", "dbpassword") dbpasswd = self.config.get("modoboa", "dbpassword")
backend.dumpDatabase(dbname, dbuser, dbpasswd, dump_path+"modoboa.sql") backend.dumpDatabase(dbname, dbuser, dbpasswd, os.path.join(dump_path,"modoboa.sql"))
"""Amavis""" """Amavis"""
if (self.config.has_option("amavis", "enabled") and if (self.config.has_option("amavis", "enabled") and
@@ -177,7 +174,7 @@ class Backup():
dbname = self.config.get("amavis", "dbname") dbname = self.config.get("amavis", "dbname")
dbuser = self.config.get("amavis", "dbuser") dbuser = self.config.get("amavis", "dbuser")
dbpasswd = self.config.get("amavis", "dbpassword") dbpasswd = self.config.get("amavis", "dbpassword")
backend.dumpDatabase(dbname, dbuser, dbpasswd, dump_path+"amavis.sql") backend.dumpDatabase(dbname, dbuser, dbpasswd, os.path.join(dump_path,"amavis.sql"))
"""SpamAssassin""" """SpamAssassin"""
if (self.config.has_option("spamassassin", "enabled") and if (self.config.has_option("spamassassin", "enabled") and
@@ -185,7 +182,7 @@ class Backup():
dbname = self.config.get("spamassassin", "dbname") dbname = self.config.get("spamassassin", "dbname")
dbuser = self.config.get("spamassassin", "dbuser") dbuser = self.config.get("spamassassin", "dbuser")
dbpasswd = self.config.get("spamassassin", "dbpassword") dbpasswd = self.config.get("spamassassin", "dbpassword")
backend.dumpDatabase(dbname, dbuser, dbpasswd, dump_path+"spamassassin.sql") backend.dumpDatabase(dbname, dbuser, dbpasswd, os.path.join(dump_path,"spamassassin.sql"))
def backupCompletion(self): def backupCompletion(self):
utils.printcolor("Backup process done, your backup is availible here:" utils.printcolor("Backup process done, your backup is availible here:"

View File

@@ -88,13 +88,19 @@ class Dovecot(base.Installer):
def post_run(self): def post_run(self):
"""Additional tasks.""" """Additional tasks."""
if self.restore and len(os.listdir(self.restore + "mails")) > 0: 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) utils.printcolor("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)
shutil.copytree(self.restore+"mails/", self.home_dir) shutil.copytree(mail_dir, self.home_dir)
#Resetting permission for vmail
for dirpath, dirnames, filenames in os.walk(self.home_dir):
shutil.chown(dirpath, self.user, self.user)
for filename in filenames:
shutil.chown(os.path.join(dirpath, filename), self.user, self.user)
elif self.restore: elif self.restore:
utils.printcolor("It seems that mails were not backed up, skipping mail restoration.", utils.MAGENTA) utils.printcolor("It seems that mails were not backed up, skipping mail restoration.", utils.MAGENTA)

View File

@@ -1,6 +1,5 @@
"""Modoboa related tasks.""" """Modoboa related tasks."""
from genericpath import isfile
import json import json
import os import os
import pwd import pwd
@@ -181,7 +180,7 @@ class Modoboa(base.Installer):
def get_sql_schema_path(self): def get_sql_schema_path(self):
if self.restore: if self.restore:
utils.printcolor("Trying to restore modoboa database from backup", utils.MAGENTA) utils.printcolor("Trying to restore modoboa database from backup", utils.MAGENTA)
modoboaDbBackupPath = self.restore + "databases/modoboa.sql" modoboaDbBackupPath = os.path.join(self.restore, "databases/modoboa.sql")
if os.path.isfile(modoboaDbBackupPath): if os.path.isfile(modoboaDbBackupPath):
utils.printcolor("Modoboa database backup found ! Restoring...", utils.GREEN) utils.printcolor("Modoboa database backup found ! Restoring...", utils.GREEN)
return modoboaDbBackupPath return modoboaDbBackupPath

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 = self.restore+"custom/postwhite.conf" postwhiteBackupConf = os.path.join(self.restore, "custom/postwhite.conf")
if self.restore and os.path.isfile(postwhiteBackupConf): if self.restore and os.path.isfile(postwhiteBackupConf):
utils.printcolor("Restoring postwhite.conf backup.", utils.GREEN)
utils.copy_file(postwhiteBackupConf, "/etc") utils.copy_file(postwhiteBackupConf, "/etc")
utils.printcolor("postwhite.conf restored from backup", utils.GREEN)
else: else:
utils.copy_file(os.path.join(postw_dir, "postwhite.conf"), "/etc") utils.copy_file(os.path.join(postw_dir, "postwhite.conf"), "/etc")
postw_bin = os.path.join(postw_dir, "postwhite") postw_bin = os.path.join(postw_dir, "postwhite")

View File

@@ -18,11 +18,12 @@ class Restore:
sys.exit(1) sys.exit(1)
try: try:
if not os.path.isfile(restore+"databases/modoboa.sql"): modobasql_file = os.path.join(restore, "databases/modoboa.sql")
utils.printcolor(restore+"databases/modoboa.sql not found, please check your backup", utils.RED) if not os.path.isfile(modobasql_file):
utils.printcolor(modobasql_file +" not found, please check your backup", utils.RED)
sys.exit(1) sys.exit(1)
except: except:
utils.printcolor(restore+"databases/modoboa.sql not found, please check your backup", utils.RED) utils.printcolor(modobasql_file + " not found, please check your backup", utils.RED)
sys.exit(1) sys.exit(1)
#Everything seems allright here, proceding... #Everything seems allright here, proceding...

View File

@@ -27,7 +27,7 @@ class Spamassassin(base.Installer):
"""Return SQL schema.""" """Return SQL schema."""
if self.restore: if self.restore:
utils.printcolor("Trying to restore spamassassin database from backup", utils.MAGENTA) utils.printcolor("Trying to restore spamassassin database from backup", utils.MAGENTA)
amavisDbBackupPath = self.restore + "databases/spamassassin.sql" amavisDbBackupPath = os.path.join(self.restore, "databases/spamassassin.sql")
if os.path.isfile(amavisDbBackupPath): if os.path.isfile(amavisDbBackupPath):
utils.printcolor("Spamassassin database backup found ! Restoring...", utils.GREEN) utils.printcolor("Spamassassin database backup found ! Restoring...", utils.GREEN)
return amavisDbBackupPath return amavisDbBackupPath

14
run.py
View File

@@ -3,8 +3,7 @@
"""An installer for Modoboa.""" """An installer for Modoboa."""
import argparse import argparse
from ast import parse import os
from ctypes import util
try: try:
import configparser import configparser
except ImportError: except ImportError:
@@ -56,7 +55,7 @@ def restore_disclamer():
"""Display restore disclamer. """ """Display restore disclamer. """
utils.printcolor( utils.printcolor(
"You are about to restore a previous installation of Modoboa." "You are about to restore a previous installation of Modoboa."
"Is a new version has been released in between, please update your database !", "If a new version has been released in between, please update your database !",
utils.BLUE) utils.BLUE)
def main(input_args): def main(input_args):
@@ -123,10 +122,11 @@ def main(input_args):
isRestoring = False isRestoring = False
if args.restore != None: if args.restore != None:
isRestoring = True isRestoring = True
if args.restore[-1] != "/": args.configfile = os.path.join(args.restore, "installer.cfg")
args.restore += "/" if not os.path.exists(args.configfile):
args.configfile = args.restore + "installer.cfg" utils.printcolor("installer.cfg from backup not found!", utils.RED)
sys.exit(1)
utils.printcolor("Welcome to Modoboa installer!\n", utils.GREEN) utils.printcolor("Welcome to Modoboa installer!\n", utils.GREEN)
wasConfigFileAlreadyThere = utils.check_config_file(args.configfile, args.interactive, args.upgrade, args.backup, isRestoring) wasConfigFileAlreadyThere = utils.check_config_file(args.configfile, args.interactive, args.upgrade, args.backup, isRestoring)