From 1094e4043d7b26eb1265c0d41e6c992208600d1c Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Tue, 17 May 2016 07:45:05 +0200 Subject: [PATCH] reformat to confirm PEPs --- nginx_proxy.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/nginx_proxy.py b/nginx_proxy.py index 5c95ca3..d64d127 100755 --- a/nginx_proxy.py +++ b/nginx_proxy.py @@ -8,8 +8,8 @@ import signal import os import logging -target_path="/tmp/nginx/" -pid_file="/var/run/nginx.pid" +target_path = "/tmp/nginx/" +pid_file = "/var/run/nginx.pid" non_location_template = """# proxy for container '$containername' server { listen $listen; @@ -25,7 +25,7 @@ server { } """ -location_template="""# proxy for container '$containername' +location_template = """# proxy for container '$containername' server { listen $listen; server_name $names; @@ -43,21 +43,24 @@ server { } """ + 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 = {} - if not 'Env' 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 for env_var in inspect_data['Config']['Env']: t = env_var.split("=") env_data[t[0]] = t[1] return env_data + def analyse_proxy_data(data): """Extracts the data for the proxy configuration (envrionment variable 'PROXY_DATA' and converts it to a dictionary.""" @@ -67,20 +70,24 @@ def analyse_proxy_data(data): proxy_data[t[0]] = t[1] return proxy_data + def extract_ip(inspect_data): """extracts the container's ip from the given inspect data""" return inspect_data['NetworkSettings']['IPAddress'] + def extract_name(inspect_data): """extracts the container's name from the given inspect data""" return inspect_data['Name'] -def get_if_available(dict, key, defValue): - if key in dict: - return dict[key] + +def get_if_available(dictionary, key, defValue): + if key in dictionary: + return dictionary[key] else: return defValue + def handle_container(id): """This function take a container's id and collects all data required to create a proper proxy configuration. The configuration is then @@ -111,15 +118,18 @@ def handle_container(id): else: file.write(Template(location_template).substitute(substitutes)) + def reload_nginx_configuration(): logging.info('HUPing nginx') os.kill(pid, signal.SIGHUP) + def get_pid(): """This function reads the process id from the given file.""" with open(pid_file, 'r') as file: return int(file.read()) + def get_docker_id(): """This function extracts the container's id from /proc/self/cgroup.""" id = '' @@ -138,6 +148,7 @@ def get_docker_id(): logging.error('could not determine container\'s id!') return id + def get_listen_ips(): inspect_data = client.inspect_container(get_docker_id()) mappings = inspect_data['NetworkSettings']['Ports']['80/tcp'] @@ -150,6 +161,7 @@ def get_listen_ips(): ips.append(data['HostIp']) return ips + def setup_logging(): logging.basicConfig(format='%(asctime)s [%(levelname)s]: %(message)s', level=logging.INFO)