-
Notifications
You must be signed in to change notification settings - Fork 1
/
postfix_setup.py
executable file
·44 lines (39 loc) · 1.47 KB
/
postfix_setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import os
import shutil
import subprocess
external_config_dir = "/mnt/postfix-config"
config_dir = "/etc/postfix"
backup_dir = "/etc/postfix.orig"
def main():
"""Initialize Postfix configuration."""
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
if os.path.exists(backup_dir):
shutil.rmtree(config_dir)
else:
shutil.move(config_dir, backup_dir)
shutil.copytree(backup_dir, config_dir)
shell_scripts = []
for entry in os.listdir(external_config_dir):
abs_entry = os.path.join(external_config_dir, entry)
extension = os.path.splitext(entry)[1]
if extension == ".sh":
shell_scripts.append(abs_entry)
else:
logger.info("Copying %s to Postfix configuration.", entry)
shutil.copy(abs_entry, config_dir)
if extension == '':
if entry.startswith("aliases"):
logger.info("Running 'postalias %s'.", entry)
subprocess.call(["/usr/sbin/postalias", os.path.join(config_dir, entry)])
else:
logger.info("Running 'postmap %s'.", entry)
subprocess.call(["/usr/sbin/postmap", os.path.join(config_dir, entry)])
for shell_script in shell_scripts:
logger.info("Running %s.", shell_script)
subprocess.call(["/bin/sh", shell_script])
if __name__ == "__main__":
main()