|
9 | 9 | from docker import Client |
10 | 10 | from docker.utils import kwargs_from_env |
11 | 11 |
|
| 12 | +# Templates for nsupdate |
12 | 13 | zone_update_template = """server {0} |
13 | 14 | zone {1}. |
14 | 15 | update delete {2}.{3} |
15 | 16 | update add {2}.{3} 60 A {4} |
16 | 17 | """ |
| 18 | + |
17 | 19 | zone_update_add_alias_template = """update delete {0}.{1} |
18 | 20 | update add {0}.{1} 600 CNAME {2}.{1}. |
19 | 21 | """ |
20 | 22 |
|
| 23 | + |
| 24 | +def register_container(container_id): |
| 25 | + detail = c.inspect_container(container_id) |
| 26 | + container_hostname = detail["Config"]["Hostname"] |
| 27 | + container_name = detail["Name"].split('/', 1)[1] |
| 28 | + container_ip = detail["NetworkSettings"]["IPAddress"] |
| 29 | + logging.info("Updating %s to ip (%s|%s) -> %s", container_id, container_hostname, container_name, container_ip) |
| 30 | + if not args.dry_run: |
| 31 | + nsupdate = Popen(['nsupdate', '-k', args.key], stdin=PIPE) |
| 32 | + nsupdate.stdin.write( |
| 33 | + bytes(zone_update_template.format(args.server, args.zone, container_hostname, args.domain, container_ip), |
| 34 | + "UTF-8")) |
| 35 | + if container_name != container_hostname: |
| 36 | + nsupdate.stdin.write( |
| 37 | + bytes(zone_update_add_alias_template.format(container_name, args.domain, container_hostname), "UTF-8")) |
| 38 | + nsupdate.stdin.write(bytes("send\n", "UTF-8")) |
| 39 | + nsupdate.stdin.close() |
| 40 | + |
| 41 | + |
| 42 | + |
21 | 43 | parser = argparse.ArgumentParser() |
22 | 44 |
|
23 | 45 | parser.add_argument("--key", required=True, help="Path to the dynamic dns key") |
24 | 46 | parser.add_argument("--server", help="IP/Hostname of the server to update", default="127.0.0.1") |
25 | 47 | parser.add_argument("--domain", help="The domain to be updated", required=True) |
26 | 48 | parser.add_argument("--zone", help="The zone to be updated (default to the domain)") |
| 49 | + |
| 50 | +parser.add_argument("--dry-run", help="Run in dry run mode without doing any update", default=False, action="store_true") |
| 51 | +parser.add_argument("--catchup", help="Register the running containers on startup", default=False, action="store_true") |
| 52 | + |
27 | 53 | parser.add_argument("--log-level", help="Log level to display", default="INFO") |
28 | 54 | parser.add_argument("--log-file", help="Where to put the logs", default="/var/log/docker-ddns.log") |
29 | 55 |
|
|
40 | 66 |
|
41 | 67 | c = Client(**(kwargs_from_env())) |
42 | 68 |
|
| 69 | +if args.catchup: |
| 70 | + logging.info("Registering existing containers") |
| 71 | + containers = c.containers() |
| 72 | + for container in containers: |
| 73 | + register_container(container["Id"]) |
| 74 | + |
43 | 75 | # Too bad docker-py does not currently support docker events |
44 | 76 | p = Popen(['docker', 'events'], stdout=PIPE) |
45 | 77 |
|
| 78 | + |
46 | 79 | while True: |
47 | 80 | line = p.stdout.readline() |
48 | 81 | if line != '': |
|
55 | 88 | logging.debug("Got event %s for container %s", event, container_id) |
56 | 89 |
|
57 | 90 | if event == "start": |
58 | | - detail = c.inspect_container(container_id) |
59 | | - container_hostname = detail["Config"]["Hostname"] |
60 | | - container_name = detail["Name"].split('/',1)[1] |
61 | | - container_ip = detail["NetworkSettings"]["IPAddress"] |
62 | | - |
63 | | - logging.info("Updating %s to ip (%s|%s) -> %s", container_id, container_hostname, container_name, container_ip) |
64 | | - nsupdate = Popen(['nsupdate', '-k', args.key], stdin=PIPE) |
65 | | - nsupdate.stdin.write(bytes(zone_update_template.format(args.server, args.zone, container_hostname, args.domain, container_ip), "UTF-8")) |
66 | | - if container_name != container_hostname: |
67 | | - nsupdate.stdin.write(bytes(zone_update_add_alias_template.format(container_name, args.domain, container_hostname), "UTF-8")) |
68 | | - nsupdate.stdin.write(bytes("send\n", "UTF-8")) |
69 | | - nsupdate.stdin.close() |
| 91 | + register_container(container_id) |
70 | 92 | elif event == "destroy": |
71 | 93 | logging.info("Destroying %s", container_id) |
72 | 94 | else: |
|
0 commit comments