review fix

This commit is contained in:
Spitap
2022-11-03 10:54:06 +01:00
parent 5c9d5c9a03
commit 554611b366
6 changed files with 21 additions and 17 deletions

View File

@@ -1,6 +1,8 @@
import random import random
import string import string
from constants import DEFAULT_BACKUP_DIRECTORY
def make_password(length=16): def make_password(length=16):
"""Create a random password.""" """Create a random password."""
@@ -441,10 +443,10 @@ ConfigDictTemplate = [
}, },
{ {
"name": "backup", "name": "backup",
"values": [ "values": [
{ {
"option": "default_path", "option": "default_path",
"default": "./modoboa_backup/" "default": DEFAULT_BACKUP_DIRECTORY
} }
] ]
} }

View File

@@ -0,0 +1 @@
DEFAULT_BACKUP_DIRECTORY = "./modoboa_backup/"

View File

@@ -9,18 +9,19 @@ import datetime
from .. import database from .. import database
from .. import utils from .. import utils
from ..constants import DEFAULT_BACKUP_DIRECTORY
class Backup: class Backup:
""" """
Backup structure ( {optional} ): Backup structure ( {optional} ):
{{backup_folder}} {{backup_directory}}
|| ||
||--> installer.cfg ||--> installer.cfg
||--> custom ||--> custom
|--> { (copy of) /etc/amavis/conf.d/99-custom } |--> { (copy of) /etc/amavis/conf.d/99-custom }
|--> { (copy of) /etc/postfix/custom_whitelist.cidr } |--> { (copy of) /etc/postfix/custom_whitelist.cidr }
|--> { (copy of) dkim folder } |--> { (copy of) dkim directory }
|--> {dkim.pem}... |--> {dkim.pem}...
|--> { (copy of) radicale home_dir } |--> { (copy of) radicale home_dir }
||--> databases ||--> databases
@@ -50,22 +51,22 @@ class Backup:
if not path_exists: if not path_exists:
if not self.silent_backup: if not self.silent_backup:
create_dir = 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 (create_dir == "y" or create_dir == "yes")): if self.silent_backup or (not self.silent_backup and create_dir.startswith("y")):
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])
else: else:
utils.printcolor( utils.printcolor(
"Error, backup dir not present.", utils.RED "Error, backup directory not present.", utils.RED
) )
return False return False
if len(os.listdir(path)) != 0: if len(os.listdir(path)) != 0:
if not self.silent_backup: if not self.silent_backup:
delete_dir = input( delete_dir = input(
"Warning : backup folder is not empty, it will be purged if you continue... [Y/n]\n").lower() "Warning : backup directory is not empty, it will be purged if you continue... [Y/n]\n").lower()
if self.silent_backup or (not self.silent_backup and (delete_dir == "y" or delete_dir == "yes")): if self.silent_backup or (not self.silent_backup and (delete_dir == "y" or delete_dir == "yes")):
try: try:
@@ -99,7 +100,7 @@ class Backup:
if self.config.has_option("backup", "default_path"): if self.config.has_option("backup", "default_path"):
path = self.config.get("backup", "default_path") path = self.config.get("backup", "default_path")
else: else:
path = f"./modoboa_backup/" path = DEFAULT_BACKUP_DIRECTORY
date = datetime.datetime.now().strftime("%m_%d_%Y_%H_%M") date = datetime.datetime.now().strftime("%m_%d_%Y_%H_%M")
path = os.path.join(path, f"backup_{date}") path = os.path.join(path, f"backup_{date}")
self.validate_path(path) self.validate_path(path)
@@ -112,7 +113,7 @@ class Backup:
user_value = None user_value = None
while user_value == "" or user_value is None or not self.validate_path(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 (it must be an empty directory)", 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("-> ")

View File

@@ -17,10 +17,10 @@ class Restore:
"Provided path is not a directory !", utils.RED) "Provided path is not a directory !", utils.RED)
sys.exit(1) sys.exit(1)
modobasql_file = os.path.join(restore, "databases/modoboa.sql") modoba_sql_file = os.path.join(restore, "databases/modoboa.sql")
if not os.path.isfile(modobasql_file): if not os.path.isfile(modoba_sql_file):
utils.printcolor( utils.printcolor(
modobasql_file + " not found, please check your backup", utils.RED) modoba_sql_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

@@ -172,9 +172,9 @@ def copy_from_template(template, dest, context):
def check_config_file(dest, interactive=False, upgrade=False, backup=False, restore=False): def check_config_file(dest, interactive=False, upgrade=False, backup=False, restore=False):
"""Create a new installer config file if needed.""" """Create a new installer config file if needed."""
isPresent = True is_present = True
if os.path.exists(dest): if os.path.exists(dest):
return isPresent return is_present
if upgrade: if upgrade:
printcolor( printcolor(
"You cannot upgrade an existing installation without a " "You cannot upgrade an existing installation without a "

4
run.py
View File

@@ -123,10 +123,10 @@ def main(input_args):
sys.exit(1) sys.exit(1)
utils.printcolor("Welcome to Modoboa installer!\n", utils.GREEN) utils.printcolor("Welcome to Modoboa installer!\n", utils.GREEN)
is_config_file_availible = utils.check_config_file( is_config_file_available = utils.check_config_file(
args.configfile, args.interactive, args.upgrade, args.backup, is_restoring) args.configfile, args.interactive, args.upgrade, args.backup, is_restoring)
if is_config_file_availible and args.backup: if is_config_file_available and args.backup:
utils.printcolor("No config file found,", utils.RED) utils.printcolor("No config file found,", utils.RED)
return return