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):
"""Return appropriate config files."""
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):
utils.printcolor("Restoring custom Amavis configuration", utils.MAGENTA)
utils.copy_file(amavisCustomConf, self.config_dir+"/conf.d")
utils.copy_file(amavisCustomConf, os.path.join(self.config_dir, "/conf.d"))
utils.printcolor("Custom amavis configuration restored", utils.GREEN)
return [
"conf.d/05-node_id", "conf.d/15-content_filter_mode",
"conf.d/50-user"]
@@ -75,7 +75,7 @@ class Amavis(base.Installer):
"""Return schema path."""
if self.restore:
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):
utils.printcolor("Amavis database backup found ! Restoring...", utils.GREEN)
return amavisDbBackupPath

View File

@@ -43,15 +43,12 @@ class Backup():
def preparePath(self):
pw = pwd.getpwnam("root")
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):
"""Check basic condition for backup directory"""
if path[-1] != "/":
path += "/"
try :
pathExists = os.path.exists(path)
except:
@@ -126,7 +123,7 @@ class Backup():
f" ({home_path}) seems not right...", utils.RED)
else:
dst = self.destinationPath + "mails/"
dst = os.path.join(self.destinationPath, "mails/")
if os.path.exists(dst):
shutil.rmtree(dst)
@@ -142,7 +139,7 @@ class Backup():
Feel free to suggest to add others!"""
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_custom = "/etc/amavis/conf.d/99-custom"
@@ -162,14 +159,14 @@ class Backup():
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)
"""Modoboa"""
dbname = self.config.get("modoboa", "dbname")
dbuser = self.config.get("modoboa", "dbuser")
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"""
if (self.config.has_option("amavis", "enabled") and
@@ -177,7 +174,7 @@ class Backup():
dbname = self.config.get("amavis", "dbname")
dbuser = self.config.get("amavis", "dbuser")
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"""
if (self.config.has_option("spamassassin", "enabled") and
@@ -185,7 +182,7 @@ class Backup():
dbname = self.config.get("spamassassin", "dbname")
dbuser = self.config.get("spamassassin", "dbuser")
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):
utils.printcolor("Backup process done, your backup is availible here:"

View File

@@ -88,13 +88,19 @@ class Dovecot(base.Installer):
def post_run(self):
"""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)
if os.path.exists(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:
utils.printcolor("It seems that mails were not backed up, skipping mail restoration.", utils.MAGENTA)

View File

@@ -1,6 +1,5 @@
"""Modoboa related tasks."""
from genericpath import isfile
import json
import os
import pwd
@@ -181,7 +180,7 @@ 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 = self.restore + "databases/modoboa.sql"
modoboaDbBackupPath = os.path.join(self.restore, "databases/modoboa.sql")
if os.path.isfile(modoboaDbBackupPath):
utils.printcolor("Modoboa database backup found ! Restoring...", utils.GREEN)
return modoboaDbBackupPath

View File

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

View File

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

View File

@@ -27,7 +27,7 @@ class Spamassassin(base.Installer):
"""Return SQL schema."""
if self.restore:
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):
utils.printcolor("Spamassassin database backup found ! Restoring...", utils.GREEN)
return amavisDbBackupPath

12
run.py
View File

@@ -3,8 +3,7 @@
"""An installer for Modoboa."""
import argparse
from ast import parse
from ctypes import util
import os
try:
import configparser
except ImportError:
@@ -56,7 +55,7 @@ def restore_disclamer():
"""Display restore disclamer. """
utils.printcolor(
"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)
def main(input_args):
@@ -123,9 +122,10 @@ def main(input_args):
isRestoring = False
if args.restore != None:
isRestoring = True
if args.restore[-1] != "/":
args.restore += "/"
args.configfile = args.restore + "installer.cfg"
args.configfile = os.path.join(args.restore, "installer.cfg")
if not os.path.exists(args.configfile):
utils.printcolor("installer.cfg from backup not found!", utils.RED)
sys.exit(1)
utils.printcolor("Welcome to Modoboa installer!\n", utils.GREEN)
wasConfigFileAlreadyThere = utils.check_config_file(args.configfile, args.interactive, args.upgrade, args.backup, isRestoring)