re2o-mail-server/main.py
2018-08-09 16:43:55 +02:00

59 lines
1.8 KiB
Python
Executable file

#!/usr/bin/env python3
from configparser import ConfigParser
import socket
from re2oapi import Re2oAPIClient
from jinja2 import Environment, FileSystemLoader
import requests
import base64
import json
from subprocess import call
import os
import sys
path =(os.path.dirname(os.path.abspath(__file__)))
config = ConfigParser()
config.read(path+'/config.ini')
api_hostname = config.get('Re2o', 'hostname')
api_password = config.get('Re2o', 'password')
api_username = config.get('Re2o', 'username')
api_client = Re2oAPIClient(api_hostname, api_username, api_password, use_tls=False)
client_hostname = socket.gethostname().split('.', 1)[0]
def generate(api_client):
all_users = api_client.list("localemail/users")
# Création de l'environnement Jinja
env = Environment(loader=FileSystemLoader(path))
template = env.get_template('templates/list')
aliases_rendered = template.render(data=all_users)
fichier = open(path+'/generated/aliases','w')
if os.path.isfile(path+'/aliases_local'): # if a local aliases file exist, add it's content at the beginning
local = open(path+'/aliases_local','r')
for line in local.readlines():
fichier.write(line)
local.close()
fichier.write(aliases_rendered)
fichier.close()
call(["/usr/bin/newaliases"], stdout=open(os.devnull, 'wb')) # Update the aliases config file
call(["/usr/sbin/postfix", "reload"]) # force the reloading now
for arg in sys.argv:
if arg=="--force":
generate(api_client)
for service in api_client.list("services/regen/"):
if service['hostname'] == client_hostname and \
service['service_name'] == 'mail-server' and \
service['need_regen']:
generate(api_client)
api_client.patch(service['api_url'], data={'need_regen': False})