From 4a74a71bb4ebb3619d9a1aa80a408636cfb44716 Mon Sep 17 00:00:00 2001 From: "justin.r.lee@gmail.com" Date: Tue, 5 Sep 2017 14:59:31 -0400 Subject: [PATCH 1/7] Support providing MLB templates via environment variables rather than template tgz files --- config.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/config.py b/config.py index 9f74abf3..f3af3859 100644 --- a/config.py +++ b/config.py @@ -770,17 +770,25 @@ def __init__(self, directory='templates'): self.__load_templates() def __load_templates(self): - '''Loads template files if they exist, othwerwise it sets defaults''' + '''Look in environment variables for templates. If not set in env, + load template files if they exist. Othwerwise it sets defaults''' for template in self.t: name = self.t[template].full_name - try: - filename = os.path.join(self.__template_directory, name) - with open(filename) as f: - logger.info('overriding %s from %s', name, filename) - self.t[template].value = f.read() - except IOError: - logger.debug("setting default value for %s", name) + if os.environ.get(name): + logger.info('overriding %s from environment variable', name) + env_template_val = os.environ.get(name) + # logger.info(env_template_val) + # Handle escaped endlines + self.t[template].value = env_template_val.replace("\\n","\n") + else: + try: + filename = os.path.join(self.__template_directory, name) + with open(filename) as f: + logger.info('overriding %s from %s', name, filename) + self.t[template].value = f.read() + except IOError: + logger.debug("setting default value for %s", name) def get_descriptions(self): descriptions = '''\ From 6d4d38001b6c9ed5fd704b349cda6141206fb539 Mon Sep 17 00:00:00 2001 From: Justin Lee Date: Fri, 8 Sep 2017 15:06:32 -0400 Subject: [PATCH 2/7] Fix style (whitespace) --- config.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config.py b/config.py index f3af3859..6079d1d0 100644 --- a/config.py +++ b/config.py @@ -770,7 +770,7 @@ def __init__(self, directory='templates'): self.__load_templates() def __load_templates(self): - '''Look in environment variables for templates. If not set in env, + '''Look in environment variables for templates. If not set in env, load template files if they exist. Othwerwise it sets defaults''' for template in self.t: @@ -780,7 +780,7 @@ def __load_templates(self): env_template_val = os.environ.get(name) # logger.info(env_template_val) # Handle escaped endlines - self.t[template].value = env_template_val.replace("\\n","\n") + self.t[template].value = env_template_val.replace("\\n", "\n") else: try: filename = os.path.join(self.__template_directory, name) From 649e3dc168e865d66cb08aa9b900348b8083b6a9 Mon Sep 17 00:00:00 2001 From: Justin Lee Date: Fri, 8 Sep 2017 16:57:50 -0400 Subject: [PATCH 3/7] Add documentation --- README.md | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index dcc3f3ab..b1374d63 100644 --- a/README.md +++ b/README.md @@ -197,14 +197,28 @@ of labels. ### Templates -Marathon-lb searches for configuration files in the `templates/` -directory. The `templates/` directory is located in a relative -path from where the script is run. Some templates can also be -[overridden _per app service port_](#overridable-templates). You may add your -own templates to the Docker image, or provide them at startup. - -See [the configuration doc for the full list](Longhelp.md#templates) -of templates. +Marathon-lb global templates (as listed in the [Longhelp](Longhelp.md#templates)) can be overwritten in two ways: +-By creating an environment variable in the marathon-lb container +-By placing configuration files in the `templates/` directory (relative to where the script is run from) + +For example, to replace `HAPROXY_HTTPS_FRONTEND_HEAD` with this content: + +``` +frontend new_frontend_label + bind *:443 ssl crt /etc/ssl/cert.pem + mode http +``` + +Then this environment variable could be added to the Marathon-LB configuration: +``` +"HAPROXY_HTTPS_FRONTEND_HEAD": "\\nfrontend new_frontend_label\\n bind *:443 ssl {sslCerts}\\n mode http" +``` + +Alternately, a file called`HAPROXY_HTTPS_FRONTEND_HEAD` could be placed in `templates/` directory through the use of an artifact URI. + +Additionally, some templates can also be [overridden _per app service port_](#overridable-templates). You may add your own templates to the Docker image, or provide them at startup. + +See [the configuration doc for the full list](Longhelp.md#templates) of templates. #### Overridable Templates From cf0b20448ee14fb757eee7b6bd1f3ec222519956 Mon Sep 17 00:00:00 2001 From: Justin Lee Date: Wed, 20 Sep 2017 17:42:51 -0500 Subject: [PATCH 4/7] Remove leftover debug line --- config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.py b/config.py index 6079d1d0..6a32c5d1 100644 --- a/config.py +++ b/config.py @@ -778,7 +778,7 @@ def __load_templates(self): if os.environ.get(name): logger.info('overriding %s from environment variable', name) env_template_val = os.environ.get(name) - # logger.info(env_template_val) + # Handle escaped endlines self.t[template].value = env_template_val.replace("\\n", "\n") else: From 1bb98eb6da37331d4618ef1bcaed8017f85a4b88 Mon Sep 17 00:00:00 2001 From: Justin Lee Date: Wed, 20 Sep 2017 18:32:19 -0500 Subject: [PATCH 5/7] Remove whitespace from blank line --- config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.py b/config.py index 6a32c5d1..5f291128 100644 --- a/config.py +++ b/config.py @@ -778,7 +778,7 @@ def __load_templates(self): if os.environ.get(name): logger.info('overriding %s from environment variable', name) env_template_val = os.environ.get(name) - + # Handle escaped endlines self.t[template].value = env_template_val.replace("\\n", "\n") else: From bbf89cdd79adea06796c25920572328a0b9c1d9a Mon Sep 17 00:00:00 2001 From: Justin Lee Date: Wed, 20 Sep 2017 21:18:28 -0500 Subject: [PATCH 6/7] Add unit test for environment variable HAPROXY templates --- tests/test_marathon_lb.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/test_marathon_lb.py b/tests/test_marathon_lb.py index 85d0a5d1..e88b7591 100644 --- a/tests/test_marathon_lb.py +++ b/tests/test_marathon_lb.py @@ -98,6 +98,38 @@ def test_config_no_apps(self): bind *:9091 mode http +frontend marathon_https_in + bind *:443 ssl crt /etc/ssl/cert.pem + mode http +''' + print("actual config:\n") + print(config) + self.assertMultiLineEqual(config, expected) + + def test_config_env_template(self): + apps = dict() + groups = ['external'] + bind_http_https = True + ssl_certs = "" + os.environ["HAPROXY_HTTP_FRONTEND_HEAD"] = ''' +frontend changed_frontend + bind *:80 + mode http + ''' + templater = marathon_lb.ConfigTemplater() + del os.environ["HAPROXY_HTTP_FRONTEND_HEAD"] + + config = marathon_lb.config(apps, groups, bind_http_https, + ssl_certs, templater) + expected = self.base_config + ''' +frontend changed_frontend + bind *:80 + mode http + +frontend marathon_http_appid_in + bind *:9091 + mode http + frontend marathon_https_in bind *:443 ssl crt /etc/ssl/cert.pem mode http From 2677f19642a8d81aa054ed97fdb8c79841d82afe Mon Sep 17 00:00:00 2001 From: Justin Lee Date: Thu, 21 Sep 2017 07:47:18 -0500 Subject: [PATCH 7/7] Remove extra whitespace in test --- tests/test_marathon_lb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_marathon_lb.py b/tests/test_marathon_lb.py index e88b7591..fc02c41e 100644 --- a/tests/test_marathon_lb.py +++ b/tests/test_marathon_lb.py @@ -115,7 +115,7 @@ def test_config_env_template(self): frontend changed_frontend bind *:80 mode http - ''' +''' templater = marathon_lb.ConfigTemplater() del os.environ["HAPROXY_HTTP_FRONTEND_HEAD"]