Merge branch 'crans'

This commit is contained in:
root 2020-05-28 22:55:27 +02:00
commit 1856ef7f5e
5 changed files with 35 additions and 6 deletions

1
.gitignore vendored
View file

@ -3,3 +3,4 @@ config.ini
**.list **.list
generated/* generated/*
aliases_local aliases_local
virtuals_local

View file

@ -1,6 +1,6 @@
## Re2o - Switchs config ## Re2o - Switchs config
This service uses Re2o API to generate Swicths config files This service uses Re2o API to generate postfix config files
More info on https://gitlab.federez.net/re2o/switchs/wikis/home More info on https://gitlab.federez.net/re2o/switchs/wikis/home

28
main.py
View file

@ -9,7 +9,7 @@ from jinja2 import Environment, FileSystemLoader
import requests import requests
import base64 import base64
import json import json
from subprocess import call from subprocess import call, DEVNULL
import os import os
import sys import sys
@ -30,13 +30,15 @@ def generate(api_client):
all_users = api_client.list("localemail/users") all_users = api_client.list("localemail/users")
# Création de l'environnement Jinja # Création de l'environnement Jinja
env = Environment(loader=FileSystemLoader(path)) env = Environment(loader=FileSystemLoader(path))
template = env.get_template('templates/list')
# On commence par les alias
template = env.get_template('templates/list_alias')
aliases_rendered = template.render(data=all_users) aliases_rendered = template.render(data=all_users)
fichier = open(path+'/generated/aliases','w') fichier = open(path+'/generated/aliases','w', encoding='utf-8')
if os.path.isfile(path+'/aliases_local'): # if a local aliases file exist, add it's content at the beginning 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') local = open(path+'/aliases_local','r', encoding='utf-8')
for line in local.readlines(): for line in local.readlines():
fichier.write(line) fichier.write(line)
local.close() local.close()
@ -44,7 +46,23 @@ def generate(api_client):
fichier.write(aliases_rendered) fichier.write(aliases_rendered)
fichier.close() fichier.close()
call(["/usr/bin/newaliases"], stdout=open(os.devnull, 'wb')) # Update the aliases config file # 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', encoding='utf-8')
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', encoding='utf-8')
for line in local.readlines():
fichier.write(line)
local.close()
fichier.write(virtuals_rendered)
fichier.close()
call(["/usr/bin/newaliases"], stdout=DEVNULL) # Update the aliases config file
call(["/usr/sbin/postmap", path+"/generated/virtual"], stdout=DEVNULL) # Update the aliases config file
call(["/usr/sbin/postfix", "reload"]) # force the reloading now call(["/usr/sbin/postfix", "reload"]) # force the reloading now
for arg in sys.argv: for arg in sys.argv:

10
templates/list_virtual Normal file
View file

@ -0,0 +1,10 @@
# Liste d'association alias:addresse des users
{% for user in data -%}
{%- for alias in user.email_address -%}
{%- if user.local_email_redirect -%}
{{ alias.complete_email_address }} {{ user.email }}
{% endif %}
{%- endfor -%}
{%- endfor -%}