Added automx support.

see #98
This commit is contained in:
Antoine Nguyen
2017-02-09 14:40:39 +01:00
parent 48227fcf5f
commit 5f36d6231f
14 changed files with 273 additions and 54 deletions

View File

@@ -6,6 +6,14 @@ from . import package
from . import utils
def get_path(cmd, venv=None):
"""Return path to cmd."""
path = cmd
if venv:
path = os.path.join(venv, "bin", path)
return path
def get_pip_path(venv):
"""Return the full path to pip command."""
binpath = "pip"

View File

@@ -0,0 +1,92 @@
"""Automx related tasks."""
import os
import pwd
import shutil
import stat
from .. import python
from .. import utils
from . import base
class Automx(base.Installer):
"""Automx installation."""
appname = "automx"
config_files = ["automx.conf"]
no_daemon = True
packages = {
"deb": ["memcached", "unzip"],
"rpm": []
}
with_user = True
def __init__(self, config):
"""Get configuration."""
super(Automx, self).__init__(config)
self.venv_path = config.get("automx", "venv_path")
self.instance_path = config.get("automx", "instance_path")
def get_template_context(self):
"""Additional variables."""
context = super(Automx, self).get_template_context()
sql_dsn = "{}://{}:{}@{}/{}".format(
self.dbengine,
self.config.get("modoboa", "dbuser"),
self.config.get("modoboa", "dbpassword"),
self.dbhost,
self.config.get("modoboa", "dbname"))
if self.db_driver == "pgsql":
sql_query = (
"SELECT first_name || ' ' || last_name AS display_name, email "
"FROM core_user WHERE email='%s' AND is_active")
else:
sql_query = (
"SELECT concat(first_name, ' ', last_name) AS display_name, "
"email FROM core_user WHERE email='%s' AND is_active=1"
)
context.update({"sql_dsn": sql_dsn, "sql_query": sql_query})
return context
def _setup_venv(self):
"""Prepare a python virtualenv."""
python.setup_virtualenv(self.venv_path, sudo_user=self.user)
packages = [
"future", "lxml", "ipaddress", "sqlalchemy", "python-memcached",
"python-dateutil", "configparser"
]
python.install_packages(packages, self.venv_path, sudo_user=self.user)
target = "{}/master.zip".format(self.home_dir)
if os.path.exists(target):
os.unlink(target)
utils.exec_cmd(
"wget https://github.com/sys4/automx/archive/master.zip",
sudo_user=self.user, cwd=self.home_dir)
self.repo_dir = "{}/automx-master".format(self.home_dir)
if os.path.exists(self.repo_dir):
shutil.rmtree(self.repo_dir)
utils.exec_cmd(
"unzip master.zip", sudo_user=self.user, cwd=self.home_dir)
utils.exec_cmd(
"{} setup.py install".format(
python.get_path("python", self.venv_path)),
cwd=self.repo_dir)
def _deploy_instance(self):
"""Copy files to instance dir."""
if not os.path.exists(self.instance_path):
pw = pwd.getpwnam(self.user)
mode = (
stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP |
stat.S_IROTH | stat.S_IXOTH)
utils.mkdir(self.instance_path, mode, pw[2], pw[3])
path = "{}/src/automx_wsgi.py".format(self.repo_dir)
utils.exec_cmd("cp {} {}".format(path, self.instance_path),
sudo_user=self.user, cwd=self.home_dir)
def post_run(self):
"""Additional tasks."""
self._setup_venv()
self._deploy_instance()

View File

@@ -9,7 +9,6 @@ from .. import utils
class Installer(object):
"""Simple installer for one application."""
appname = None

View File

@@ -0,0 +1,38 @@
[automx]
provider = %domain
domains = *
# Protect against DoS
memcache = 127.0.0.1:11211
memcache_ttl = 600
client_error_limit = 20
rate_limit_exception_networks = 127.0.0.0/8, ::1/128
[global]
backend = sql
actions = settings
account_type = email
host = %sql_dsn
query = %sql_query
result_attrs = display_name, email
smtp = yes
smtp_server = %hostname
smtp_port = 587
smtp_encryption = starttls
smtp_auth = plaintext
smtp_refresh_ttl = 6
smtp_default = yes
imap = yes
imap_server = %hostname
imap_port = 143
imap_encryption = starttls
imap_auth = plaintext
imap_refresh_ttl = 6
pop = yes
pop_server = %hostname
pop_port = 110
pop_encryption = starttls
pop_auth = plaintext

View File

@@ -0,0 +1,16 @@
upstream automx {
server unix:%uwsgi_socket_path fail_timeout=0;
}
server {
listen 80;
server_name %hostname;
access_log /var/log/nginx/%{hostname}-access.log;
error_log /var/log/nginx/%{hostname}-error.log;
location /mail/config-v1.1.xml {
include uwsgi_params;
uwsgi_pass automx;
}
}

View File

@@ -11,7 +11,7 @@ server {
server {
listen 443 ssl;
server_name %hostname;
root %modoboa_instance_path;
root %app_instance_path;
ssl_certificate %tls_cert_file;
ssl_certificate_key %tls_key_file;

View File

@@ -0,0 +1,14 @@
[uwsgi]
uid = %app_user
gid = %app_user
plugins = python
home = %app_venv_path
chdir = %app_instance_path
module = automx_wsgi
master = true
vhost = true
harakiri = 60
processes = %nb_processes
socket = %uwsgi_socket_path
chmod-socket = 660
vacuum = true

View File

@@ -1,9 +1,9 @@
[uwsgi]
uid = %modoboa_user
gid = %modoboa_user
uid = %app_user
gid = %app_user
plugins = python
home = %modoboa_venv_path
chdir = %modoboa_instance_path
home = %app_venv_path
chdir = %app_instance_path
module = instance.wsgi:application
master = true
processes = %nb_processes

View File

@@ -11,7 +11,6 @@ from .. import python
from .. import utils
from . import base
from . import install
class Modoboa(base.Installer):
@@ -44,7 +43,7 @@ class Modoboa(base.Installer):
self.devmode = config.getboolean("modoboa", "devmode")
def _setup_venv(self):
"""Prepare a dedicated virtuelenv."""
"""Prepare a dedicated virtualenv."""
python.setup_virtualenv(self.venv_path, sudo_user=self.user)
packages = ["modoboa", "rrdtool"]
if self.dbengine == "postgres":
@@ -171,5 +170,3 @@ class Modoboa(base.Installer):
self._setup_venv()
self._deploy_instance()
self.apply_settings()
install("uwsgi", self.config)
install("nginx", self.config)

View File

@@ -11,7 +11,6 @@ from .uwsgi import Uwsgi
class Nginx(base.Installer):
"""Nginx installer."""
appname = "nginx"
@@ -20,21 +19,23 @@ class Nginx(base.Installer):
"rpm": ["nginx"]
}
def get_template_context(self):
def get_template_context(self, app):
"""Additionnal variables."""
context = super(Nginx, self).get_template_context()
context.update({
"modoboa_instance_path": (
self.config.get("modoboa", "instance_path")),
"uwsgi_socket_path": Uwsgi(self.config).socket_path
"app_instance_path": (
self.config.get(app, "instance_path")),
"uwsgi_socket_path": Uwsgi(self.config).get_socket_path(app)
})
return context
def post_run(self):
"""Additionnal tasks."""
hostname = self.config.get("general", "hostname")
context = self.get_template_context()
src = self.get_file_path("nginx.conf.tpl")
def _setup_config(self, app, hostname=None):
"""Custom app configuration."""
if hostname is None:
hostname = self.config.get("general", "hostname")
context = self.get_template_context(app)
context.update({"hostname": hostname})
src = self.get_file_path("{}.conf.tpl".format(app))
if package.backend.FORMAT == "deb":
dst = os.path.join(
self.config_dir, "sites-available", "{}.conf".format(hostname))
@@ -44,7 +45,7 @@ class Nginx(base.Installer):
if os.path.exists(link):
return
os.symlink(dst, link)
group = self.config.get("modoboa", "user")
group = self.config.get(app, "user")
user = "www-data"
else:
dst = os.path.join(
@@ -54,6 +55,13 @@ class Nginx(base.Installer):
user = "nginx"
system.add_user_to_group(user, group)
def post_run(self):
"""Additionnal tasks."""
self._setup_config("modoboa")
if self.config.getboolean("automx", "enabled"):
hostname = "autoconfig.{}".format(
self.config.get("general", "domain"))
self._setup_config("automx", hostname)
if not os.path.exists("{}/dhparam.pem".format(self.config_dir)):
cmd = "openssl dhparam -dsaparam -out dhparam.pem 4096"
utils.exec_cmd(cmd, cwd=self.config_dir)

View File

@@ -10,7 +10,6 @@ from . import base
class Uwsgi(base.Installer):
"""uWSGI installer."""
appname = "uwsgi"
@@ -19,22 +18,21 @@ class Uwsgi(base.Installer):
"rpm": ["uwsgi", "uwsgi-plugin-python"],
}
@property
def socket_path(self):
def get_socket_path(self, app):
"""Return socket path."""
if package.backend.FORMAT == "deb":
return "/run/uwsgi/app/modoboa_instance/socket"
return "/run/uwsgi/modoboa_instance.sock"
return "/run/uwsgi/app/{}_instance/socket".format(app)
return "/run/uwsgi/{}_instance.sock".format(app)
def get_template_context(self):
def get_template_context(self, app):
"""Additionnal variables."""
context = super(Uwsgi, self).get_template_context()
context.update({
"modoboa_user": self.config.get("modoboa", "user"),
"modoboa_venv_path": self.config.get("modoboa", "venv_path"),
"modoboa_instance_path": (
self.config.get("modoboa", "instance_path")),
"uwsgi_socket_path": self.socket_path,
"app_user": self.config.get(app, "user"),
"app_venv_path": self.config.get(app, "venv_path"),
"app_instance_path": (
self.config.get(app, "instance_path")),
"uwsgi_socket_path": self.get_socket_path(app),
})
return context
@@ -44,18 +42,28 @@ class Uwsgi(base.Installer):
return os.path.join(self.config_dir, "apps-available")
return "{}.d".format(self.config_dir)
def post_run(self):
"""Additionnal tasks."""
context = self.get_template_context()
src = self.get_file_path("uwsgi.ini.tpl")
dst = os.path.join(self.get_config_dir(), "modoboa_instance.ini")
def _enable_config_debian(self, dst):
"""Enable config file."""
link = os.path.join(
self.config_dir, "apps-enabled", os.path.basename(dst))
if os.path.exists(link):
return
os.symlink(dst, link)
def _setup_config(self, app):
"""Common setup code."""
context = self.get_template_context(app)
src = self.get_file_path("{}.ini.tpl".format(app))
dst = os.path.join(
self.get_config_dir(), "{}_instance.ini".format(app))
utils.copy_from_template(src, dst, context)
return dst
def _setup_modoboa_config(self):
"""Custom modoboa configuration."""
dst = self._setup_config("modoboa")
if package.backend.FORMAT == "deb":
link = os.path.join(
self.config_dir, "apps-enabled", os.path.basename(dst))
if os.path.exists(link):
return
os.symlink(dst, link)
self._enable_config_debian(dst)
else:
system.add_user_to_group(
"uwsgi", self.config.get("modoboa", "user"))
@@ -68,8 +76,32 @@ class Uwsgi(base.Installer):
utils.exec_cmd(
"perl -pi -e '{}' /etc/uwsgi.ini".format(pattern))
def _setup_automx_config(self):
"""Custom automx configuration."""
dst = self._setup_config("automx")
if package.backend.FORMAT == "deb":
self._enable_config_debian(dst)
else:
system.add_user_to_group(
"uwsgi", self.config.get("automx", "user"))
pattern = (
"s/emperor-tyrant = true/emperor-tyrant = false/")
utils.exec_cmd(
"perl -pi -e '{}' /etc/uwsgi.ini".format(pattern))
def post_run(self):
"""Additionnal tasks."""
self._setup_modoboa_config()
if self.config.getboolean("automx", "enabled"):
self._setup_automx_config()
def restart_daemon(self):
"""Restart daemon process."""
code, output = utils.exec_cmd("service uwsgi status modoboa_instance")
action = "start" if code else "restart"
utils.exec_cmd("service uwsgi {}".format(action))
instances = ["modoboa_instance"]
if self.config.getboolean("automx", "enabled"):
instances.append("automx_instance")
for instance in instances:
code, output = utils.exec_cmd("service uwsgi status {}".format(
instance))
action = "start" if code else "restart"
utils.exec_cmd("service uwsgi {}".format(action))