This repository has been archived by the owner on Aug 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
entrypoint.py
executable file
·61 lines (44 loc) · 1.67 KB
/
entrypoint.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python3
import functools
import ipaddress
import jinja2
import os
from pathlib import Path
import subprocess
def write_access_conf(raw, conf_file='/etc/nginx/access.conf'):
addresses = list(extract_addresses(raw))
with open(conf_file, 'w') as f:
write = functools.partial(print, file=f)
write('# generated by Docker entrypoint script')
if addresses:
for addr in addresses:
write('allow {};'.format(addr.compressed))
write('deny all;')
def extract_addresses(raw):
for addr in raw.split():
if '-' in addr:
start, end = addr.split('-', 1)
yield from ipaddress.summarize_address_range(ipaddress.ip_address(start), ipaddress.ip_address(end))
else:
yield ipaddress.ip_network(addr)
def create_nginx_conf():
process_template(Path('/etc/nginx/nginx.conf.template'))
for template in Path('/etc/nginx/conf.d/').glob('*.conf.template'):
process_template(template)
def process_template(template_path):
with template_path.open() as f:
template = jinja2.Template(f.read())
# allow use of any env. variable in nginx config files
config = template.render(os.environ)
config_path = template_path.with_suffix('')
with config_path.open('w') as f:
f.write(config)
def main():
write_access_conf(os.environ.get('IP_WHITELIST', ''))
GLOBAL_DIRECTIVES='daemon off;'
subprocess.check_call(['/usr/local/bin/header_config.py'])
create_nginx_conf()
subprocess.check_call(['nginx', '-t', '-g', GLOBAL_DIRECTIVES])
os.execvp('nginx', ['nginx', '-g', GLOBAL_DIRECTIVES])
if __name__ == '__main__':
main()