can handle more than one proxy data variable

This commit is contained in:
Joachim Lusiardi 2016-12-31 16:55:12 +01:00
parent ca543dcb5b
commit f4ddd542dc

View File

@ -22,7 +22,7 @@ server {
add_header X-Forwarded-For $$remote_addr;
}
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;
}
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):
"""Extracts the environment variables from the given result of an 'inspect
container' call."""
env_data = {}
counter = 0
if 'Env' not in inspect_data['Config'] or inspect_data['Config']['Env'] is None:
return env_data
for env_var in inspect_data['Config']['Env']:
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
@ -68,7 +69,7 @@ def analyse_proxy_data(data):
"""Extracts the data for the proxy configuration (envrionment variable
'PROXY_DATA' and converts it to a dictionary."""
proxy_data = {}
for proxy_var in data['PROXY_DATA'].split(','):
for proxy_var in data.split(','):
t = proxy_var.split(":",1)
proxy_data[t[0]] = t[1]
return proxy_data
@ -106,18 +107,19 @@ def handle_container(id):
written to the directory of temporary nginx files"""
inspect_data = client.inspect_container(id)
env_vars = analyse_env_vars(inspect_data)
if 'PROXY_DATA' in env_vars:
if not check_proxy_data_format(env_vars['PROXY_DATA']):
logging.info('cannot handle container with id "%s" named "%s": %s', id, extract_name(inspect_data), env_vars['PROXY_DATA'])
for env_key in env_vars:
env_data = env_vars[env_key]
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
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')
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))
return
logging.info('container "%s"(%s) is allowed to listen on %s.', extract_name(inspect_data), container_listen_ip, str(listen_ips))
substitutes = {
'containername': extract_name(inspect_data),
'containername': '{c} {d}'.format(c=extract_name(inspect_data), d=env_data),
'ip': extract_ip(inspect_data),
'location': get_if_available(proxy_data, 'location', ''),
'names': get_if_available(proxy_data, 'server_names', '').replace(';', ' '),
@ -126,7 +128,7 @@ def handle_container(id):
'listen': '*:80'
}
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'] == '':
del substitutes['location']
file.write(Template(non_location_template).substitute(substitutes))