-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild-config.py
executable file
·105 lines (90 loc) · 3.29 KB
/
build-config.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#! /usr/bin/env python
"""Build NGINX config for DCOS automatic load balancing."""
__author__ = 'David Parrish <[email protected]>'
from jinja2 import Template
import json
import requests
import subprocess
import sys
import time
import socket
TEMPLATE="""
server {
listen 80 default_server;
server_name _; # This is just an invalid value which will never trigger on a real hostname.
error_log /proc/self/fd/2;
access_log /proc/self/fd/1;
return 503;
}
{% for hostname, vhost in vhosts.items() %}
upstream {{ hostname }} {
{% for host in vhost.backends %}
server {{ host }};
{% endfor %}
}
server {
listen 80;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
server_name {{ hostname }};
proxy_buffering off;
error_log /proc/self/fd/2;
access_log /proc/self/fd/1;
location / {
proxy_pass http://{{ hostname }};
include /etc/nginx/proxy_params;
# HTTP 1.1 support
proxy_http_version 1.1;
proxy_set_header Connection "upgrade";
proxy_set_header Upgrade $http_upgrade;
}
}
{% endfor %}
"""
def main(argv):
try:
old_config = None
while True:
params = {
'vhosts': {},
}
s = requests.Session()
apps = json.loads(s.get('http://master.mesos:8080/v2/apps').text)
for app in apps['apps']:
try:
vhost = app['labels']['VIRTUAL_HOST']
except KeyError:
continue
tasks = json.loads(s.get('http://master.mesos:8080/v2/apps%s/tasks' % app['id'],
headers={'Accept': 'application/json'}).text)
backends = []
for task in tasks['tasks']:
try:
ip = socket.gethostbyname(task['host'])
except socket.gaierror:
print "Can't look up host %s" % task['host']
continue
backends.append('%s:%s' % (ip, task['ports'][0]))
if backends:
params['vhosts'][vhost] = {
'backends': backends,
}
template = Template(TEMPLATE)
new_config = template.render(params)
if new_config != old_config:
with file('/etc/nginx/sites-available/default', 'w') as fh:
fh.write(new_config)
test = subprocess.Popen(['/usr/sbin/nginx', '-t'], stderr=subprocess.PIPE)
output = test.communicate()
if test.returncode != 0:
if old_config:
print 'Error generating new NGINX configuration, not reloading'
return
else:
raise RuntimeError('Error generating NGINX configuration')
subprocess.call(['/usr/sbin/nginx', '-s', 'reload'])
old_config = new_config
time.sleep(10)
except KeyboardInterrupt:
return 1
if __name__ == '__main__':
sys.exit(main(sys.argv))