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