Create config file from template.

see #118
This commit is contained in:
Antoine Nguyen
2017-04-25 11:32:24 +02:00
parent 025c72ced0
commit dd57aa830d
3 changed files with 35 additions and 5 deletions

View File

@@ -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"):