Last active
November 12, 2015 19:01
-
-
Save zircote/112de462bd85c843f8b7 to your computer and use it in GitHub Desktop.
Marathon Config writer for NGINX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from jinja2 import Template | |
import json | |
import httplib2 | |
import argparse | |
TEMPLATE = """ | |
user www-data; | |
worker_processes 4; | |
pid /run/nginx.pid; | |
daemon off; | |
events { | |
worker_connections 768; | |
} | |
http { | |
sendfile on; | |
tcp_nopush on; | |
tcp_nodelay on; | |
keepalive_timeout 65; | |
types_hash_max_size 2048; | |
include /etc/nginx/mime.types; | |
include /etc/nginx/proxy_params; | |
default_type application/octet-stream; | |
access_log /dev/stdout; | |
error_log /dev/stdout info; | |
gzip on; | |
gzip_disable "msie6"; | |
{%- for a in apps %} | |
{%- for p in a.ports %} | |
{%- set p_index = loop.index0 %} | |
upstream {{ a.id }}_{{ a.ports[p_index] }} { | |
ip_hash; | |
{%- for d in a.instances %} | |
server {{ d.host }}:{{ d.ports[p_index] }} max_fails=3 fail_timeout=10s; | |
{%- endfor %} | |
} | |
server { | |
listen {{ a.ports[p_index] }}; | |
server_name 0.0.0.0; | |
location / { | |
proxy_pass http://{{ a.id }}_{{ a.ports[p_index] }}; | |
} | |
} | |
{%- endfor %} | |
{%- endfor %} | |
} | |
""" | |
if __name__ == "__main__": | |
argparser = argparse.ArgumentParser(description='Marathon/HA-Proxy configuration generator', epilog=None) | |
argparser.add_argument('--marathon_host', '-H', type=str, help="message polling interval", default="http://127.0.0.1") | |
argparser.add_argument('--marathon_port', '-p', type=str, help="Marathong endpoint port", default="8080") | |
argparser.add_argument('--nginx_config', '-C', type=str, help="Path to the NGINX Config File", default=None) | |
opts = argparser.parse_args() | |
h = httplib2.Http() | |
(resp_headers, content) = h.request( | |
"%s:%s/v1/endpoints" % (opts.marathon_host, opts.marathon_port), "GET", | |
headers={'Accept': 'application/json'} | |
) | |
data = json.loads(content) | |
template = Template(TEMPLATE) | |
import sys | |
sys.stdout.write(template.render(apps=data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment