-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcname.py
executable file
·93 lines (65 loc) · 2.52 KB
/
cname.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/env python3
import sys
import os
import logging
import logging.handlers
import syslog
import re
import signal
import functools
from argparse import ArgumentParser, ArgumentTypeError
from time import sleep
from mpublisher import AvahiPublisher
log = logging.getLogger("docker-to-cname");
def positive_int_arg(value):
"""Helper type (for argparse) to validate and return positive integer argument."""
try:
ivalue = int(value)
except ValueError:
raise ArgumentTypeError("invalid int value: %s" % repr(value))
if ivalue <= 0:
raise ArgumentTypeError("value must be greater than zero")
return ivalue
def local_hostname_arg(hostname):
"""Helper type (for argparse) to validate and return a (normalized) local hostname argument."""
if not re.match(r"^[a-z0-9][a-z0-9_-]*(?:\.[a-z0-9][a-z0-9_-]*)*\.local$", hostname, re.I):
raise ArgumentTypeError("malformed CNAME: %s" % repr(hostname))
return hostname.lower()
def handle_signals(publisher, signum, frame):
"""Unpublish all mDNS records and exit cleanly."""
signame = next(v for v, k in signal.__dict__.items() if k == signum)
log.info("Exiting on %s...", signame)
publisher.__del__()
# Avahi needs time to forget us...
sleep(1)
os._exit(0)
def main():
pid = os.getpid()
f = open("cname.pid", "w")
f.write(str(pid))
f.close()
cnames = [line.rstrip('\n') for line in open("cnames")]
handler = logging.StreamHandler(sys.stderr)
format_string = "%(levelname)s: %(message)s"
handler.setFormatter(logging.Formatter(format_string))
logging.getLogger().addHandler(handler)
log.setLevel(logging.INFO)
publisher = None
while True:
if not publisher or not publisher.available():
publisher = AvahiPublisher(30)
signal.signal(signal.SIGTERM, functools.partial(handle_signals, publisher))
signal.signal(signal.SIGINT, functools.partial(handle_signals, publisher))
signal.signal(signal.SIGQUIT, functools.partial(handle_signals, publisher))
for cname in cnames:
status = publisher.publish_cname(cname, True)
if not status:
log.error("failed to publish '%s'", cname)
continue
if publisher.count() == len(cnames):
log.info("All CNAMEs published")
else:
log.warning("%d of %d published", publisher.count(), len(cnames))
sleep(1)
if __name__ == "__main__":
main()