Merge branch 'master' of code.nerd2nerd.org:n0ob/docker_nginx_auto_proxy

This commit is contained in:
Joachim Lusiardi 2015-01-07 20:45:59 +01:00
commit 824b501863
1 changed files with 16 additions and 6 deletions

View File

@ -1,6 +1,7 @@
#!/usr/bin/python3.4 #!/usr/bin/python3.4
import os import os
import sys
import http.client import http.client
import json import json
import signal import signal
@ -9,7 +10,7 @@ from string import Template
target_path="/tmp/nginx" target_path="/tmp/nginx"
pid_file="/run/nginx.pid" pid_file="/run/nginx.pid"
non_location_template = """server { non_location_template = """server {
listen 80; listen $listen;
server_name $name; server_name $name;
location / { location / {
proxy_set_header X-Real-IP $$remote_addr; proxy_set_header X-Real-IP $$remote_addr;
@ -21,7 +22,7 @@ non_location_template = """server {
""" """
location_template="""server { location_template="""server {
listen 80; listen $listen;
server_name $name; server_name $name;
location / { location / {
return 301 $$scheme://$name/$location; return 301 $$scheme://$name/$location;
@ -69,7 +70,7 @@ def analyse_env_vars(data):
def analyse_proxy_data(data): def analyse_proxy_data(data):
proxy_data = {} proxy_data = {}
for proxy_var in data.split(','): for proxy_var in data.split(','):
t = proxy_var.split(":") t = proxy_var.split(":",1)
proxy_data[t[0]] = t[1] proxy_data[t[0]] = t[1]
return proxy_data return proxy_data
@ -101,14 +102,18 @@ def handle_event(event):
if 'location' in proxy_data: if 'location' in proxy_data:
location = proxy_data['location'] location = proxy_data['location']
listen ='*:80'
if 'ip' in proxy_data:
listen = proxy_data['ip']+':80'
print('writing /tmp/nginx/proxy_'+container_id) print('writing /tmp/nginx/proxy_'+container_id)
with open('/tmp/nginx/proxy_'+container_id, 'w') as file: with open('/tmp/nginx/proxy_'+container_id, 'w') as file:
if location == '': if location == '':
s = Template(non_location_template) s = Template(non_location_template)
file.write(s.substitute(name=server_name,ip=ip,port=port)) file.write(s.substitute(name=server_name,ip=ip,port=port,listen=listen))
else: else:
s = Template(location_template) s = Template(location_template)
file.write(s.substitute(name=server_name,ip=ip,port=port,location=location)) file.write(s.substitute(name=server_name,ip=ip,port=port,listen=listen,location=location))
print('HUPing nginx') print('HUPing nginx')
os.kill(pid, signal.SIGHUP) os.kill(pid, signal.SIGHUP)
@ -118,7 +123,12 @@ if not os.path.exists(target_path):
pid = get_pid() pid = get_pid()
conn = http.client.HTTPConnection("localhost:2375") conn = http.client.HTTPConnection("localhost:2375")
try:
conn.request("GET", "/events") conn.request("GET", "/events")
except ConnectionRefusedError:
print('Docker does not expose events on its REST-API. Perhaps it is not running?')
sys.exit(-1)
response = conn.getresponse() response = conn.getresponse()
events = "" events = ""