57 lines
2.5 KiB
Python
57 lines
2.5 KiB
Python
import argparse
|
|
import signal
|
|
import logging
|
|
|
|
from nginx_site_discovery.watch import start_watcher
|
|
from nginx_site_discovery.server import start_server
|
|
|
|
|
|
class InterruptHandler():
|
|
def __init__(self, threads):
|
|
self.threads = threads
|
|
|
|
def __call__(self, signum, frame):
|
|
logging.info('Received signal %s, stopping background threads', signal.Signals(signum).name)
|
|
for t in self.threads:
|
|
t.stop()
|
|
|
|
|
|
def main():
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
parser = argparse.ArgumentParser(description='Watch Nginx configuration to create files for service discovery')
|
|
parser.add_argument('-l', '--labels', nargs='*', default=[], help='Optional, extra labels to add to the jobs in the format key=value')
|
|
parser.add_argument('-s', '--listen', nargs='?', const='127.0.0.1:4567', default=None, help='Optional, start a web server to listen for discovery files, if no listening address is provided the default is %(const)s')
|
|
parser.add_argument('-o', '--output-file', default=None, help='Optional, output the discovery file to the provided path')
|
|
parser.add_argument('-d', '--output-dir', default='.', help='Only valid with --listen, the directory in wich the files are dumped, default to current working directory')
|
|
parser.add_argument('-u', '--server-url', default=None, help='Optional, post the discovery file to the provided url, should be https://<server>/<service name>/<key>.')
|
|
parser.add_argument('-w', '--watched-dir', default='/etc/nginx/sites-available', help='Path to the Nginx configuration file directory, default to %(default)s')
|
|
parser.add_argument('-n', '--no-watch', action='store_true', default=False, help='Disable watching, useful for the server only mode')
|
|
parser.add_argument('-x', '--extension', default='', help='Configuration file extension, default to empty string')
|
|
|
|
args = parser.parse_args()
|
|
config = vars(args)
|
|
str_labels = config['labels']
|
|
labels = {}
|
|
for label in str_labels:
|
|
key, value = label.split('=')
|
|
labels[key] = value
|
|
config['labels'] = labels
|
|
|
|
threads = []
|
|
if not args.no_watch:
|
|
threads.append(start_watcher(config))
|
|
|
|
if args.listen:
|
|
ip, _sep, port = args.listen.rpartition(':')
|
|
threads.append(start_server(ip, int(port), args.output_dir))
|
|
|
|
for thread in threads:
|
|
thread.start()
|
|
|
|
signal.signal(signal.SIGINT, InterruptHandler(threads))
|
|
signal.signal(signal.SIGTERM, InterruptHandler(threads))
|
|
|
|
for thread in threads:
|
|
thread.join()
|