| 
									
										
										
										
											2018-07-26 21:57:21 +02:00
										 |  |  | #!/usr/bin/python3 | 
					
						
							| 
									
										
										
										
											2017-09-24 14:34:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import jinja2 | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import socket | 
					
						
							|  |  |  | import glob | 
					
						
							|  |  |  | import shutil | 
					
						
							| 
									
										
										
										
											2018-10-07 01:39:02 +03:00
										 |  |  | import tenacity | 
					
						
							| 
									
										
										
										
											2018-07-26 21:57:21 +02:00
										 |  |  | import multiprocessing | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-07 00:32:05 +03:00
										 |  |  | from tenacity import retry | 
					
						
							| 
									
										
										
										
											2018-07-26 21:57:21 +02:00
										 |  |  | from podop import run_server | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def start_podop(): | 
					
						
							|  |  |  |     os.setuid(100) | 
					
						
							| 
									
										
										
										
											2018-09-27 14:53:58 +02:00
										 |  |  |     run_server(3 if "DEBUG" in os.environ else 0, "postfix", "/tmp/podop.socket", [ | 
					
						
							| 
									
										
										
										
											2018-07-26 21:57:21 +02:00
										 |  |  | 		("transport", "url", "http://admin/internal/postfix/transport/§"), | 
					
						
							|  |  |  | 		("alias", "url", "http://admin/internal/postfix/alias/§"), | 
					
						
							| 
									
										
										
										
											2018-09-26 00:14:46 +02:00
										 |  |  | 		("domain", "url", "http://admin/internal/postfix/domain/§"), | 
					
						
							| 
									
										
										
										
											2018-07-26 21:57:21 +02:00
										 |  |  |         ("mailbox", "url", "http://admin/internal/postfix/mailbox/§"), | 
					
						
							| 
									
										
										
										
											2018-10-07 01:52:01 +02:00
										 |  |  |         ("sender", "url", "http://admin/internal/postfix/sender/§") | 
					
						
							| 
									
										
										
										
											2018-07-26 21:57:21 +02:00
										 |  |  |     ]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-24 14:34:46 +02:00
										 |  |  | convert = lambda src, dst: open(dst, "w").write(jinja2.Template(open(src).read()).render(**os.environ)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-07 02:10:13 +03:00
										 |  |  | @retry(stop=tenacity.stop_after_attempt(100), wait=tenacity.wait_random(min=2, max=5)) | 
					
						
							| 
									
										
										
										
											2018-10-07 00:32:05 +03:00
										 |  |  | def resolve(): | 
					
						
							|  |  |  | 	os.environ["FRONT_ADDRESS"] = socket.gethostbyname(os.environ.get("FRONT_ADDRESS", "front")) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-24 14:34:46 +02:00
										 |  |  | # Actual startup script | 
					
						
							| 
									
										
										
										
											2018-10-07 00:32:05 +03:00
										 |  |  | resolve() | 
					
						
							| 
									
										
										
										
											2018-01-31 22:24:54 +01:00
										 |  |  | os.environ["HOST_ANTISPAM"] = os.environ.get("HOST_ANTISPAM", "antispam:11332") | 
					
						
							|  |  |  | os.environ["HOST_LMTP"] = os.environ.get("HOST_LMTP", "imap:2525") | 
					
						
							| 
									
										
										
										
											2017-09-24 14:34:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | for postfix_file in glob.glob("/conf/*.cf"): | 
					
						
							|  |  |  |     convert(postfix_file, os.path.join("/etc/postfix", os.path.basename(postfix_file))) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if os.path.exists("/overrides/postfix.cf"): | 
					
						
							|  |  |  |     for line in open("/overrides/postfix.cf").read().strip().split("\n"): | 
					
						
							|  |  |  |         os.system('postconf -e "{}"'.format(line)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if os.path.exists("/overrides/postfix.master"): | 
					
						
							|  |  |  |     for line in open("/overrides/postfix.master").read().strip().split("\n"): | 
					
						
							|  |  |  |         os.system('postconf -Me "{}"'.format(line)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | for map_file in glob.glob("/overrides/*.map"): | 
					
						
							|  |  |  |     destination = os.path.join("/etc/postfix", os.path.basename(map_file)) | 
					
						
							|  |  |  |     shutil.copyfile(map_file, destination) | 
					
						
							|  |  |  |     os.system("postmap {}".format(destination)) | 
					
						
							|  |  |  |     os.remove(destination) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | convert("/conf/rsyslog.conf", "/etc/rsyslog.conf") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-26 21:57:21 +02:00
										 |  |  | # Run Podop and Postfix | 
					
						
							|  |  |  | multiprocessing.Process(target=start_podop).start() | 
					
						
							| 
									
										
										
										
											2017-09-24 14:34:46 +02:00
										 |  |  | if os.path.exists("/var/run/rsyslogd.pid"): | 
					
						
							|  |  |  |     os.remove("/var/run/rsyslogd.pid") | 
					
						
							|  |  |  | os.system("/usr/lib/postfix/post-install meta_directory=/etc/postfix create-missing") | 
					
						
							|  |  |  | os.system("/usr/lib/postfix/master &") | 
					
						
							|  |  |  | os.execv("/usr/sbin/rsyslogd", ["rsyslogd", "-n"]) |