Merge branch 'development' into 'master'

Development

See merge request !5
This commit is contained in:
Joachim Lusiardi 2016-12-31 17:05:16 +01:00
commit 67267a6e75
1 changed files with 17 additions and 15 deletions

View File

@ -22,7 +22,7 @@ server {
add_header X-Forwarded-For $$remote_addr; add_header X-Forwarded-For $$remote_addr;
} }
proxy_set_header Host $$host; proxy_set_header Host $$host;
proxy_pass http://$ip:$port/; proxy_pass http://$ip:$port;
} }
} }
""" """
@ -41,26 +41,27 @@ server {
add_header X-Forwarded-For $$remote_addr; add_header X-Forwarded-For $$remote_addr;
} }
proxy_set_header Host $$host; proxy_set_header Host $$host;
proxy_pass http://$ip:$port/; proxy_pass http://$ip:$port;
} }
} }
""" """
def print_json(data):
"""Prints the given value in JSON to stdout. Use this for debugging only"""
print(json.dumps(data, sort_keys=True, indent=4))
def analyse_env_vars(inspect_data): def analyse_env_vars(inspect_data):
"""Extracts the environment variables from the given result of an 'inspect """Extracts the environment variables from the given result of an 'inspect
container' call.""" container' call."""
env_data = {} env_data = {}
counter = 0
if 'Env' not in inspect_data['Config'] or inspect_data['Config']['Env'] is None: if 'Env' not in inspect_data['Config'] or inspect_data['Config']['Env'] is None:
return env_data return env_data
for env_var in inspect_data['Config']['Env']: for env_var in inspect_data['Config']['Env']:
t = env_var.split("=") t = env_var.split("=")
env_data[t[0]] = t[1] key = t[0]
value = t[1]
if key == 'PROXY_DATA':
if key in env_data:
key = "{key}{postfix}".format(key=key, postfix=counter)
env_data[key] = value
return env_data return env_data
@ -68,7 +69,7 @@ def analyse_proxy_data(data):
"""Extracts the data for the proxy configuration (envrionment variable """Extracts the data for the proxy configuration (envrionment variable
'PROXY_DATA' and converts it to a dictionary.""" 'PROXY_DATA' and converts it to a dictionary."""
proxy_data = {} proxy_data = {}
for proxy_var in data['PROXY_DATA'].split(','): for proxy_var in data.split(','):
t = proxy_var.split(":",1) t = proxy_var.split(":",1)
proxy_data[t[0]] = t[1] proxy_data[t[0]] = t[1]
return proxy_data return proxy_data
@ -106,18 +107,19 @@ def handle_container(id):
written to the directory of temporary nginx files""" written to the directory of temporary nginx files"""
inspect_data = client.inspect_container(id) inspect_data = client.inspect_container(id)
env_vars = analyse_env_vars(inspect_data) env_vars = analyse_env_vars(inspect_data)
if 'PROXY_DATA' in env_vars: for env_key in env_vars:
if not check_proxy_data_format(env_vars['PROXY_DATA']): env_data = env_vars[env_key]
logging.info('cannot handle container with id "%s" named "%s": %s', id, extract_name(inspect_data), env_vars['PROXY_DATA']) if not check_proxy_data_format(env_data):
logging.info('cannot handle container with id "%s" named "%s": %s', id, extract_name(inspect_data), env_var)
return return
proxy_data = analyse_proxy_data(env_vars) proxy_data = analyse_proxy_data(env_data)
container_listen_ip = get_if_available(proxy_data, 'ip', '0.0.0.0') container_listen_ip = get_if_available(proxy_data, 'ip', '0.0.0.0')
if container_listen_ip != '0.0.0.0' and container_listen_ip not in listen_ips: if container_listen_ip != '0.0.0.0' and container_listen_ip not in listen_ips:
logging.info('container "%s"(%s) does not listen on %s.', extract_name(inspect_data), container_listen_ip, str(listen_ips)) logging.info('container "%s"(%s) does not listen on %s.', extract_name(inspect_data), container_listen_ip, str(listen_ips))
return return
logging.info('container "%s"(%s) is allowed to listen on %s.', extract_name(inspect_data), container_listen_ip, str(listen_ips)) logging.info('container "%s"(%s) is allowed to listen on %s.', extract_name(inspect_data), container_listen_ip, str(listen_ips))
substitutes = { substitutes = {
'containername': extract_name(inspect_data), 'containername': '{c} {d}'.format(c=extract_name(inspect_data), d=env_data),
'ip': extract_ip(inspect_data), 'ip': extract_ip(inspect_data),
'location': get_if_available(proxy_data, 'location', ''), 'location': get_if_available(proxy_data, 'location', ''),
'names': get_if_available(proxy_data, 'server_names', '').replace(';', ' '), 'names': get_if_available(proxy_data, 'server_names', '').replace(';', ' '),
@ -126,7 +128,7 @@ def handle_container(id):
'listen': '*:80' 'listen': '*:80'
} }
logging.info('writing to %sproxy_%s', target_path, id) logging.info('writing to %sproxy_%s', target_path, id)
with open(target_path + '/proxy_'+id, 'w') as file: with open(target_path + '/proxy_{id}_{code}'.format(id=id,code=env_key), 'w') as file:
if substitutes['location'] == '': if substitutes['location'] == '':
del substitutes['location'] del substitutes['location']
file.write(Template(non_location_template).substitute(substitutes)) file.write(Template(non_location_template).substitute(substitutes))