2018-06-27 19:00:53 +02:00
|
|
|
#!/usr/bin/env python3
|
2018-05-24 15:27:42 +02:00
|
|
|
from configparser import ConfigParser
|
2018-05-26 23:04:14 +02:00
|
|
|
import socket
|
2018-05-24 15:27:42 +02:00
|
|
|
|
|
|
|
from re2oapi import Re2oAPIClient
|
|
|
|
|
2018-07-02 10:40:45 +02:00
|
|
|
from jinja2 import Environment, FileSystemLoader
|
|
|
|
|
2018-07-11 02:35:13 +02:00
|
|
|
import requests
|
|
|
|
import base64
|
|
|
|
import json
|
2018-07-19 12:53:35 +02:00
|
|
|
from subprocess import call
|
2018-08-09 16:43:55 +02:00
|
|
|
import os
|
2018-08-08 19:52:04 +02:00
|
|
|
import sys
|
|
|
|
|
2018-08-09 15:42:52 +02:00
|
|
|
path =(os.path.dirname(os.path.abspath(__file__)))
|
2018-07-11 02:35:13 +02:00
|
|
|
|
2018-05-24 15:27:42 +02:00
|
|
|
config = ConfigParser()
|
2018-08-09 15:42:52 +02:00
|
|
|
config.read(path+'/config.ini')
|
2018-05-24 15:27:42 +02:00
|
|
|
|
2018-05-26 23:04:14 +02:00
|
|
|
api_hostname = config.get('Re2o', 'hostname')
|
|
|
|
api_password = config.get('Re2o', 'password')
|
|
|
|
api_username = config.get('Re2o', 'username')
|
|
|
|
|
2018-08-09 15:43:37 +02:00
|
|
|
api_client = Re2oAPIClient(api_hostname, api_username, api_password, use_tls=False)
|
2018-05-26 23:04:14 +02:00
|
|
|
|
|
|
|
client_hostname = socket.gethostname().split('.', 1)[0]
|
|
|
|
|
2018-08-07 17:38:12 +02:00
|
|
|
def generate(api_client):
|
2018-08-08 17:22:01 +02:00
|
|
|
all_users = api_client.list("localemail/users")
|
2018-08-07 17:38:12 +02:00
|
|
|
# Création de l'environnement Jinja
|
2018-08-09 16:16:42 +02:00
|
|
|
env = Environment(loader=FileSystemLoader(path))
|
2018-08-11 03:52:54 +02:00
|
|
|
|
|
|
|
# On commence par les alias
|
|
|
|
template = env.get_template('templates/list_alias')
|
2018-08-07 17:38:12 +02:00
|
|
|
aliases_rendered = template.render(data=all_users)
|
2018-07-02 10:40:45 +02:00
|
|
|
|
2018-08-09 15:42:52 +02:00
|
|
|
fichier = open(path+'/generated/aliases','w')
|
2018-07-02 10:40:45 +02:00
|
|
|
|
2018-08-09 16:07:39 +02:00
|
|
|
if os.path.isfile(path+'/aliases_local'): # if a local aliases file exist, add it's content at the beginning
|
2018-08-09 15:42:52 +02:00
|
|
|
local = open(path+'/aliases_local','r')
|
2018-08-07 17:38:12 +02:00
|
|
|
for line in local.readlines():
|
|
|
|
fichier.write(line)
|
|
|
|
local.close()
|
2018-07-08 19:59:13 +02:00
|
|
|
|
2018-08-07 17:38:12 +02:00
|
|
|
fichier.write(aliases_rendered)
|
|
|
|
fichier.close()
|
2018-07-08 19:59:13 +02:00
|
|
|
|
2018-08-11 03:52:54 +02:00
|
|
|
# On enchaine avec les redirections
|
|
|
|
template = env.get_template('templates/list_virtual')
|
|
|
|
virtuals_rendered = template.render(data=all_users)
|
|
|
|
|
|
|
|
fichier = open(path+'/generated/virtual','w')
|
|
|
|
|
|
|
|
if os.path.isfile(path+'/virtuals_local'): # if a local aliases file exist, add it's content at the beginning
|
|
|
|
local = open(path+'/virtuals_local','r')
|
|
|
|
for line in local.readlines():
|
|
|
|
fichier.write(line)
|
|
|
|
local.close()
|
|
|
|
|
|
|
|
fichier.write(virtuals_rendered)
|
|
|
|
fichier.close()
|
|
|
|
|
2018-08-09 16:43:55 +02:00
|
|
|
call(["/usr/bin/newaliases"], stdout=open(os.devnull, 'wb')) # Update the aliases config file
|
2018-08-11 03:52:54 +02:00
|
|
|
call(["/usr/sbin/postmap", path+"/generated/virtual"], stdout=open(os.devnull, 'wb')) # Update the aliases config file
|
2018-08-09 16:37:34 +02:00
|
|
|
call(["/usr/sbin/postfix", "reload"]) # force the reloading now
|
2018-08-07 17:38:12 +02:00
|
|
|
|
2018-08-09 15:42:52 +02:00
|
|
|
for arg in sys.argv:
|
|
|
|
if arg=="--force":
|
|
|
|
generate(api_client)
|
|
|
|
|
2018-08-07 17:38:12 +02:00
|
|
|
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})
|