Merge pull request #122 from modoboa/feature/generate_config_file
Create config file from template.
This commit is contained in:
@@ -23,7 +23,7 @@ password =
|
||||
|
||||
[mysql]
|
||||
user = root
|
||||
password = password
|
||||
password = $mysql_password
|
||||
charset = utf8
|
||||
collation = utf8_general_ci
|
||||
|
||||
@@ -35,7 +35,7 @@ instance_path = %(home_dir)s/instance
|
||||
timezone = Europe/Paris
|
||||
dbname = modoboa
|
||||
dbuser = modoboa
|
||||
dbpassword = password
|
||||
dbpassword = $modoboa_password
|
||||
# Extensions to install
|
||||
# also available: modoboa-radicale modoboa-dmarc modoboa-imap-migration
|
||||
extensions = modoboa-amavis modoboa-pdfcredentials modoboa-postfix-autoreply modoboa-sievefilters modoboa-stats modoboa-webmail
|
||||
@@ -58,7 +58,7 @@ max_servers = 1
|
||||
|
||||
dbname = amavis
|
||||
dbuser = amavis
|
||||
dbpassword = password
|
||||
dbpassword = $amavis_password
|
||||
|
||||
[clamav]
|
||||
enabled = true
|
||||
@@ -95,7 +95,7 @@ config_dir = /etc/mail/spamassassin
|
||||
|
||||
dbname = spamassassin
|
||||
dbuser = spamassassin
|
||||
dbpassword = password
|
||||
dbpassword = $sa_password
|
||||
|
||||
[uwsgi]
|
||||
enabled = true
|
||||
@@ -5,6 +5,7 @@ import datetime
|
||||
import glob
|
||||
import os
|
||||
import platform
|
||||
import random
|
||||
import shutil
|
||||
import string
|
||||
import subprocess
|
||||
@@ -82,6 +83,13 @@ def mkdir(path, mode, uid, gid):
|
||||
os.chown(path, uid, gid)
|
||||
|
||||
|
||||
def make_password(length=16):
|
||||
"""Create a random password."""
|
||||
return "".join(
|
||||
random.SystemRandom().choice(
|
||||
string.letters + string.digits) for _ in range(length))
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def settings(**kwargs):
|
||||
"""Context manager to declare temporary settings."""
|
||||
@@ -131,6 +139,25 @@ def copy_from_template(template, dest, context):
|
||||
fp.write(ConfigFileTemplate(buf).substitute(context))
|
||||
|
||||
|
||||
def check_config_file(dest):
|
||||
"""Create a new installer config file if needed."""
|
||||
if os.path.exists(dest):
|
||||
return
|
||||
printcolor(
|
||||
"Configuration file {} not found, creating new one."
|
||||
.format(dest), YELLOW)
|
||||
with open("installer.cfg.default") as fp:
|
||||
buf = fp.read()
|
||||
context = {
|
||||
"mysql_password": make_password(),
|
||||
"modoboa_password": make_password(),
|
||||
"amavis_password": make_password(),
|
||||
"sa_password": make_password()
|
||||
}
|
||||
with open(dest, "w") as fp:
|
||||
fp.write(string.Template(buf).substitute(context))
|
||||
|
||||
|
||||
def has_colours(stream):
|
||||
"""Check if terminal supports colors."""
|
||||
if not hasattr(stream, "isatty"):
|
||||
|
||||
5
run.py
5
run.py
@@ -21,6 +21,8 @@ def main():
|
||||
help="Enable debug output")
|
||||
parser.add_argument("--force", action="store_true", default=False,
|
||||
help="Force installation")
|
||||
parser.add_argument("--configfile", default="installer.cfg",
|
||||
help="Configuration file to use")
|
||||
parser.add_argument("domain", type=str,
|
||||
help="The main domain of your future mail server")
|
||||
args = parser.parse_args()
|
||||
@@ -28,8 +30,9 @@ def main():
|
||||
if args.debug:
|
||||
utils.ENV["debug"] = True
|
||||
utils.printcolor("Welcome to Modoboa installer!", utils.GREEN)
|
||||
utils.check_config_file(args.configfile)
|
||||
config = configparser.SafeConfigParser()
|
||||
with open("installer.cfg") as fp:
|
||||
with open(args.configfile) as fp:
|
||||
config.readfp(fp)
|
||||
if not config.has_section("general"):
|
||||
config.add_section("general")
|
||||
|
||||
Reference in New Issue
Block a user