From 3bbf57db6c2ddef2cd7a2145edd63a81412e0bda Mon Sep 17 00:00:00 2001 From: samattridge Date: Tue, 25 Jul 2017 10:54:10 +0100 Subject: [PATCH 1/5] Added RUNNING_MODE_OVERRIDE env var Allows you to override the running mode detection that's performed. --- haproxy/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/haproxy/config.py b/haproxy/config.py index 6986997..5bd5866 100644 --- a/haproxy/config.py +++ b/haproxy/config.py @@ -122,7 +122,7 @@ def parse_additional_backend_settings(envvars): RELOAD_TIMEOUT = os.getenv("RELOAD_TIMEOUT", "0") # global -RUNNING_MODE = None +RUNNING_MODE = os.getenv("RUNNING_MODE_OVERRIDE", None) # const CERT_DIR = "/certs/" From 4b5ad8357d60a77c08f6991d4e0647ea1225aca3 Mon Sep 17 00:00:00 2001 From: samattridge Date: Tue, 25 Jul 2017 11:03:09 +0100 Subject: [PATCH 2/5] Added ability to override the running mode with an env var --- haproxy/main.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/haproxy/main.py b/haproxy/main.py index 1377038..7b17f52 100644 --- a/haproxy/main.py +++ b/haproxy/main.py @@ -82,6 +82,15 @@ def is_process_running(p): def check_running_mode(container_uri, service_uri, api_auth): mode, msg = None, "" + + # If config.RUNNING_MODE has been set already then it was done using the RUNNING_MODE_OVERRIDE environment variable + if config.RUNNING_MODE and (config.RUNNING_MODE == RunningMode.LegacyMode or config.RUNNING_MODE == RunningMode.SwarmMode or config.RUNNING_MODE == RunningMode.ComposeMode or config.RUNNING_MODE == RunningMode.CloudMode): + msg = "Haproxy running mode has been overridden." + mode = config.RUNNING_MODE + + logger.info(msg) + return mode + if container_uri and service_uri and api_auth: if container_uri and service_uri: if api_auth: From ebec50f3d2e5e843adecd1a064e1ab7e3faf77dd Mon Sep 17 00:00:00 2001 From: Sam Attridge Date: Tue, 25 Jul 2017 17:03:52 +0100 Subject: [PATCH 3/5] Added SWARM_MASTER_ADDRESS env var to allow use on worker nodes in Swarm mode --- haproxy/config.py | 5 +++-- haproxy/eventhandler.py | 11 +++++++---- haproxy/haproxycfg.py | 8 +++++++- haproxy/helper/swarm_mode_link_helper.py | 0 haproxy/main.py | 8 -------- requirements.txt | 8 ++++---- 6 files changed, 21 insertions(+), 19 deletions(-) mode change 100644 => 100755 haproxy/config.py mode change 100644 => 100755 haproxy/eventhandler.py mode change 100644 => 100755 haproxy/haproxycfg.py mode change 100644 => 100755 haproxy/helper/swarm_mode_link_helper.py mode change 100644 => 100755 haproxy/main.py mode change 100644 => 100755 requirements.txt diff --git a/haproxy/config.py b/haproxy/config.py old mode 100644 new mode 100755 index 5bd5866..e2f2d0c --- a/haproxy/config.py +++ b/haproxy/config.py @@ -1,6 +1,7 @@ import logging import os import re +import enum logger = logging.getLogger("haproxy") @@ -8,7 +9,6 @@ class RunningMode(): LegacyMode, ComposeMode, SwarmMode, CloudMode = range(4) - def parse_extra_bind_settings(extra_bind_settings): bind_dict = {} if extra_bind_settings: @@ -120,9 +120,10 @@ def parse_additional_backend_settings(envvars): HAPROXY_USER = os.getenv("HAPROXY_USER", "haproxy") HAPROXY_GROUP = os.getenv("HAPROXY_GROUP", "haproxy") RELOAD_TIMEOUT = os.getenv("RELOAD_TIMEOUT", "0") +SWARM_MASTER_ADDRESS = os.getenv("SWARM_MASTER_ADDRESS") # global -RUNNING_MODE = os.getenv("RUNNING_MODE_OVERRIDE", None) +RUNNING_MODE = None # const CERT_DIR = "/certs/" diff --git a/haproxy/eventhandler.py b/haproxy/eventhandler.py old mode 100644 new mode 100755 index 946d23c..17a6af2 --- a/haproxy/eventhandler.py +++ b/haproxy/eventhandler.py @@ -103,12 +103,15 @@ def polling_service_status_swarm_mode(): time.sleep(config.SWARM_MODE_POLLING_INTERVAL) try: try: - docker = docker_client() + swarmMaster = docker_client() except: - docker = docker_client(os.environ) + swarmMaster = docker_client(os.environ) + + if config.SWARM_MASTER_ADDRESS: + swarmMaster = docker_client(os.environ, host=config.SWARM_MASTER_ADDRESS) - services = docker.services() - tasks = docker.tasks(filters={"desired-state": "running"}) + services = swarmMaster.services() + tasks = swarmMaster.tasks(filters={"desired-state": "running"}) _, linked_tasks = SwarmModeLinkHelper.get_task_links(tasks, services, Haproxy.cls_service_id, Haproxy.cls_nets) if cmp(Haproxy.cls_linked_tasks, linked_tasks) != 0: diff --git a/haproxy/haproxycfg.py b/haproxy/haproxycfg.py old mode 100644 new mode 100755 index efc3af8..985d1cd --- a/haproxy/haproxycfg.py +++ b/haproxy/haproxycfg.py @@ -108,10 +108,16 @@ def _init_swarm_mode_links(): try: try: docker = docker_client() + swarmMaster = docker_client() except: docker = docker_client(os.environ) + swarmMaster = docker_client(os.environ) + + if config.SWARM_MASTER_ADDRESS: + swarmMaster = docker_client(os.environ, host=config.SWARM_MASTER_ADDRESS) docker.ping() + swarmMaster.ping() except Exception as e: logger.info("Docker API error, regressing to legacy links mode: %s" % e) @@ -119,7 +125,7 @@ def _init_swarm_mode_links(): haproxy_container_id = os.environ.get("HOSTNAME", "") Haproxy.cls_service_id, Haproxy.cls_nets = SwarmModeLinkHelper.get_swarm_mode_haproxy_id_nets(docker, haproxy_container_id) - links, Haproxy.cls_linked_tasks = SwarmModeLinkHelper.get_swarm_mode_links(docker, Haproxy.cls_service_id, + links, Haproxy.cls_linked_tasks = SwarmModeLinkHelper.get_swarm_mode_links(swarmMaster, Haproxy.cls_service_id, Haproxy.cls_nets) logger.info("Linked service: %s", ", ".join(SwarmModeLinkHelper.get_service_links_str(links))) logger.info("Linked container: %s", ", ".join(SwarmModeLinkHelper.get_container_links_str(links))) diff --git a/haproxy/helper/swarm_mode_link_helper.py b/haproxy/helper/swarm_mode_link_helper.py old mode 100644 new mode 100755 diff --git a/haproxy/main.py b/haproxy/main.py old mode 100644 new mode 100755 index 7b17f52..30124e2 --- a/haproxy/main.py +++ b/haproxy/main.py @@ -83,14 +83,6 @@ def is_process_running(p): def check_running_mode(container_uri, service_uri, api_auth): mode, msg = None, "" - # If config.RUNNING_MODE has been set already then it was done using the RUNNING_MODE_OVERRIDE environment variable - if config.RUNNING_MODE and (config.RUNNING_MODE == RunningMode.LegacyMode or config.RUNNING_MODE == RunningMode.SwarmMode or config.RUNNING_MODE == RunningMode.ComposeMode or config.RUNNING_MODE == RunningMode.CloudMode): - msg = "Haproxy running mode has been overridden." - mode = config.RUNNING_MODE - - logger.info(msg) - return mode - if container_uri and service_uri and api_auth: if container_uri and service_uri: if api_auth: diff --git a/requirements.txt b/requirements.txt old mode 100644 new mode 100755 index de2d3e0..fa01381 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ PyYAML==3.11 cached-property==1.2.0 -docker-py==1.10.3 -dockerpty==0.4.1 +docker-py>=1.10.3 +dockerpty>=0.4.1 docopt==0.6.1 enum34==1.0.4 jsonschema==2.5.1 @@ -10,6 +10,6 @@ future==0.15.0 requests==2.7.0 six==1.9.0 websocket-client==0.37.0 -docker-compose==1.6.0 -python-dockercloud==1.0.10 +docker-compose>=1.6.0 +python-dockercloud>=1.0.10 gevent==1.1.1 From 50e2cebe7b72ad2f401776d9f1cd5f10517d5433 Mon Sep 17 00:00:00 2001 From: samattridge Date: Fri, 10 Nov 2017 11:01:56 +0000 Subject: [PATCH 4/5] Updated README.md re: SWARM_MASTER_ADDRESS --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 115bb93..ed00188 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ Docker 1.12 supports SwarmMode natively. `dockercloud/haproxy` will auto config - You **HAVE TO** set the environment variable `SERVICE_PORTS=, ` in your application service, which are the ports you would like to expose. - For `dockercloud/haproxy` service: If you mount `/var/run/docker.sock`, it can only be run on swarm manager nodes. - If you want the haproxy service to run on worker nodes, you need to setup DOCKER_HOST envvar that points to the manager address. + If you want the haproxy service to run on worker nodes, you need to setup SWARM_MASTER_ADDRESS envvar that points to the manager address. * If your application services need to access other services(database, for example), you can attach your application services to two different network, one is for database and the other one for the proxy * This feature is still experimental, please let us know if you find any bugs or have any suggestions. @@ -231,6 +231,7 @@ Settings in this part is immutable, you have to redeploy HAProxy service to make |SSL_BIND_OPTIONS|no-sslv3|explicitly set which SSL bind options will be used for the SSL server. This sets the HAProxy `ssl-default-bind-options` configuration setting. The default will allow only TLSv1.0+ to be used on the SSL server.| |STATS_AUTH|stats:stats|username and password required to access the Haproxy stats.| |STATS_PORT|1936|port for the HAProxy stats section. If this port is published, stats can be accessed at `http://:/` +|SWARM_MASTER_ADDRESS|If set, allows HAProxy to run on worker nodes. Use this in conjunction with mounting `/var/run/docker.sock` to make this work. |TIMEOUT|connect 5000, client 50000, server 50000|comma-separated list of HAProxy `timeout` entries to the `default` section.| |NBPROC|1|sets the `nbproc` entry to the `global` section. By default, only one process is created, which is the recommended mode of operation.| |HAPROXY_USER|haproxy|sets the user of the UNIX sockets to the designated system user name| From 1050d0ad7c54fc09c6f9ad63bcedea6879a49206 Mon Sep 17 00:00:00 2001 From: samattridge Date: Fri, 10 Nov 2017 11:02:57 +0000 Subject: [PATCH 5/5] Update README.md Fixed table cell position of SWARM_MASTER_ADDRESS. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ed00188..d4bcf60 100644 --- a/README.md +++ b/README.md @@ -231,7 +231,7 @@ Settings in this part is immutable, you have to redeploy HAProxy service to make |SSL_BIND_OPTIONS|no-sslv3|explicitly set which SSL bind options will be used for the SSL server. This sets the HAProxy `ssl-default-bind-options` configuration setting. The default will allow only TLSv1.0+ to be used on the SSL server.| |STATS_AUTH|stats:stats|username and password required to access the Haproxy stats.| |STATS_PORT|1936|port for the HAProxy stats section. If this port is published, stats can be accessed at `http://:/` -|SWARM_MASTER_ADDRESS|If set, allows HAProxy to run on worker nodes. Use this in conjunction with mounting `/var/run/docker.sock` to make this work. +|SWARM_MASTER_ADDRESS| |If set, allows HAProxy to run on worker nodes. Use this in conjunction with mounting `/var/run/docker.sock` to make this work. |TIMEOUT|connect 5000, client 50000, server 50000|comma-separated list of HAProxy `timeout` entries to the `default` section.| |NBPROC|1|sets the `nbproc` entry to the `global` section. By default, only one process is created, which is the recommended mode of operation.| |HAPROXY_USER|haproxy|sets the user of the UNIX sockets to the designated system user name|