From 9ccb16d88ddb950be4fd67e58fb64aff86a6fedf Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Sun, 22 Jan 2017 10:00:43 +0100 Subject: [PATCH] fixed bug regarding the cleanup of old proxy configs --- nginx_proxy.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/nginx_proxy.py b/nginx_proxy.py index e816a6e..9f8aef2 100755 --- a/nginx_proxy.py +++ b/nginx_proxy.py @@ -131,9 +131,13 @@ def handle_container(id): 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)) + logging.info(Template(non_location_template).safe_substitute(substitutes)) + file.write(Template(non_location_template).safe_substitute(substitutes)) else: - file.write(Template(location_template).substitute(substitutes)) + # make sure we have a name for the redirect + substitutes['name'] = substitutes['names'].split(' ')[0] + logging.info(Template(location_template).safe_substitute(substitutes)) + file.write(Template(location_template).safe_substitute(substitutes)) def reload_nginx_configuration(): @@ -221,9 +225,14 @@ if __name__ == '__main__': ip = '' if ip == '': - logging.info('removing %sproxy_%s', target_path, container_id) - if os.path.exists(target_path + 'proxy_' + container_id): - os.remove(target_path + 'proxy_' + container_id) + logging.info('removing data for container %s', container_id) + # since a container can expose multiple ports per PROXY_DATA it + # will generate multiple files. All names contain the container id. + # This will be used to delete all relevant files for a container + for filename in os.listdir(target_path): + if container_id in filename: + logging.info('removing file %s%s', target_path, container_id) + os.remove(target_path + filename) else: handle_container(container_id)