Browse Source

Initial version

pull/4/head
Chris Smith 8 years ago
commit
d3134060e7
4 changed files with 84 additions and 0 deletions
  1. 11
    0
      Dockerfile
  2. 27
    0
      README.md
  3. 36
    0
      generate.py
  4. 10
    0
      nginx.tpl

+ 11
- 0
Dockerfile View File

@@ -0,0 +1,11 @@
1
+FROM csmith/service-reporter-lib:latest 
2
+MAINTAINER Chris Smith <chris87@gmail.com> 
3
+
4
+RUN \
5
+  pip install \
6
+    jinja2
7
+
8
+COPY *.py *.tpl /
9
+
10
+VOLUME ["/nginx-config"]
11
+ENTRYPOINT ["python", "/generate.py"]

+ 27
- 0
README.md View File

@@ -0,0 +1,27 @@
1
+# Automatic Nginx proxy config generator 
2
+
3
+This uses my [docker-service-reporter](https://github.com/csmith/docker-service-reporter/)
4
+container to generate an nginx config file defining virtual hosts that proxy
5
+to docker containers with appropriate labels.
6
+
7
+## How? 
8
+
9
+The `service-reporter` container populates `etcd` with details about
10
+known containers.
11
+
12
+This container monitors `etcd` for a label specifying vhosts and proxy ports,
13
+and puts them into a template file for nginx to use. 
14
+
15
+## Usage
16
+
17
+TODO: Finish this!
18
+
19
+Then run this container. It takes the same arguments as `service-reporter`:
20
+
21
+```
22
+  --etcd-host (default: etcd) hostname where ectd is running
23
+  --etcd-port (default: 2379) port to connect to ectd on
24
+  --etcd-prefix (default: /docker) prefix to read keys from
25
+  --name (default: unknown) name of the host running docker
26
+```
27
+

+ 36
- 0
generate.py View File

@@ -0,0 +1,36 @@
1
+#!/usr/bin/env python3
2
+
3
+from collections import defaultdict
4
+import argparse
5
+import etcdlib
6
+import jinja2
7
+import os
8
+
9
+parser = argparse.ArgumentParser()
10
+parser.add_argument('--name', help='Name of the docker host to request certificates for', default='unknown')
11
+parser.add_argument('--etcd-port', type=int, help='Port to connect to etcd on', default=2379)
12
+parser.add_argument('--etcd-host', help='Host to connect to etcd on', default='etcd')
13
+parser.add_argument('--etcd-prefix', help='Prefix to use when retrieving keys from etcd', default='/docker')
14
+args = parser.parse_args()
15
+
16
+jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader('/'))
17
+template = jinja_env.get_template('nginx.tpl')
18
+fetcher = etcdlib.Connection(args.etcd_host, args.etcd_port, args.etcd_prefix)
19
+
20
+while True:
21
+  services = []
22
+  domains = fetcher.get_label('com.chameth.vhost')
23
+  for container, values in fetcher.get_label('com.chameth.proxy').items():
24
+    networks = fetcher.get_networks(container)
25
+    services.append({
26
+      'protocol': 'http', # TODO: Support HTTPS
27
+      'vhost': domains[container], # TODO: Handle SANs
28
+      'host': next(iter(networks.values())), # TODO: Pick a bridge sensibly?
29
+      'port': values      
30
+    })
31
+
32
+  print(template.render(services=services)) # TODO: Actually write it out
33
+  print('Done writing config.', flush=True)
34
+
35
+  fetcher.wait_for_update()
36
+

+ 10
- 0
nginx.tpl View File

@@ -0,0 +1,10 @@
1
+{% for service in services %}
2
+server {
3
+    server_name {{ service.vhost }};
4
+    listen [::]:443 ssl http2;
5
+
6
+    location / {
7
+        proxy_pass {{ service.protocol }}://{{ service.host }}:{{ service.port }};
8
+    }
9
+}
10
+{% endfor %}

Loading…
Cancel
Save