-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdnsmasq_updater.py
executable file
·1075 lines (874 loc) · 40.2 KB
/
dnsmasq_updater.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env python3
# pylint: disable=too-many-lines
"""
Docker Dnsmasq Updater.
Use the Docker socket to update remote dnsmasq server with container hostnames
"""
import os
import sys
import signal
import logging
import argparse
import configparser
import json
import subprocess
import socket
import ipaddress
import tempfile
import time
import re
from threading import Timer
from types import SimpleNamespace
from collections import defaultdict
from typing import Dict
from python_hosts import Hosts, HostsEntry # type: ignore[import-untyped]
import python_hosts.exception # type: ignore[import-untyped]
from paramiko.client import SSHClient, AutoAddPolicy
from paramiko import RSAKey, DSSKey
from paramiko.ssh_exception import \
SSHException, AuthenticationException, PasswordRequiredException, BadHostKeyException
import docker # type: ignore[import-untyped]
from bottlejwt import JwtPlugin # type: ignore[import-untyped]
from bottle import Bottle, request, response # type: ignore[import-untyped, import-not-found]
from cryptography.hazmat.primitives.kdf.scrypt import Scrypt
import cryptography.exceptions
# config file and list of paths, in the order to try
CONFIG_FILE = 'dnsmasq_updater.conf'
CONFIG_PATHS = [os.path.dirname(os.path.realpath(__file__)), '/etc/', '/conf/']
DEFAULT_LOG_LEVEL = logging.INFO
BLOCK_START = '### docker dnsmasq updater start ###'
BLOCK_END = '### docker dnsmasq updater end ###'
class Formatter(logging.Formatter):
"""Format logger output."""
def formatTime(self, record, datefmt=None):
"""Use system timezone and add milliseconds."""
datefmt = f'%Y-%m-%d %H:%M:%S.{round(record.msecs):03d} ' + time.strftime('%z')
return time.strftime(datefmt, self.converter(record.created))
STDOUT_HANDLER = logging.StreamHandler(sys.stdout)
STDOUT_HANDLER.setFormatter(
Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
def signal_handler(sig, _frame):
"""Handle SIGINT cleanly."""
print('\nCaught signal:', sig, '\n')
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
loggers: Dict[str, str] = {}
def get_logger(class_name, log_level):
"""Get logger objects for individual classes."""
name = os.path.splitext(os.path.basename(__file__))[0]
if log_level == logging.DEBUG:
name = '.'.join([name, class_name])
if loggers.get(name):
return loggers.get(name)
logger = logging.getLogger(name)
if not logger.handlers:
logger.addHandler(STDOUT_HANDLER)
logger.setLevel(log_level)
loggers[name] = logger
return logger
def signal_ready(ready_fd, logger):
"""Signal we're ready."""
if ready_fd:
logger.info('Initialization done. Signalling readiness.')
logger.debug('Readiness signal writing to file descriptor %s.', ready_fd)
try:
os.write(ready_fd, '\n'.encode())
except OSError:
logger.warning('Could not signal file descriptor \'%s\'.', ready_fd)
else:
logger.info('Initialization done.')
class ResettableTimer():
"""
A resettable timer class.
A timer class so we can delay writes to the external device
to allow for multiple Docker events in a short space of time
without hammering the device.
"""
def __init__(self, delay, function):
"""Initialize timing."""
self._running = False
self._delay = delay
self._function = function
self._timer = Timer(self._delay, self._function)
def __set(self):
self._timer = Timer(self._delay, self._function)
def start(self):
"""If not running, start timer."""
if not self._running:
self.__set()
self._timer.daemon = True
self._timer.start()
self._running = True
def cancel(self):
"""If running, cancel timer."""
if self._running:
self._timer.cancel()
self._running = False
def reset(self):
"""Reset timer."""
self.cancel()
self.start()
class LocalHandler():
"""Handle writing of a local hosts file."""
def __init__(self, temp_file, **kwargs):
"""Initialize timing."""
self.params = SimpleNamespace(**kwargs)
self.temp_file = temp_file
self.logger = get_logger(self.__class__.__name__, self.params.log_level)
self.delayed_put = ResettableTimer(self.params.delay, self.put_hostfile)
def queue_put(self):
"""Delayed writing of the hosts file, allowing for multiple proximate events."""
self.logger.info('Queued hosts file update.')
self.delayed_put.reset()
def put_hostfile(self):
"""Copy the temporary hosts file over the top of the real file."""
self.logger.info('Writing hosts file: %s', self.params.file)
try:
with open(self.temp_file.name, 'r', encoding='utf-8') as temp_file:
hosts = temp_file.read()
with open(self.params.file, 'w', encoding='utf-8') as hosts_file:
hosts_file.write(str(BLOCK_START + '\n' + hosts + BLOCK_END + '\n'))
except FileNotFoundError as err:
self.logger.error('Error writing hosts file: %s', err)
self.exec_restart_command()
def exec_restart_command(self):
"""Execute command to restart dnsmasq on the local device."""
restart_cmd = self.params.restart_cmd.strip('\'"')
try:
subprocess.run(restart_cmd.split(), check=True)
except subprocess.CalledProcessError:
self.logger.error(
'CalledProcessError: Failed to execute restart command: %s', restart_cmd)
class RemoteHandler():
"""Handle getting/putting/cleaning of local and remote hosts files."""
def __init__(self, temp_file, **kwargs):
"""Initialize SSH client, temp file and timing."""
self.params = SimpleNamespace(**kwargs)
self.logger = get_logger(self.__class__.__name__, self.params.log_level)
self.temp_file = temp_file
self.ssh = SSHClient()
self.ssh.set_missing_host_key_policy(AutoAddPolicy())
self.delayed_put = ResettableTimer(self.params.delay, self.put_hostfile)
self.key = False
self.get_server_ip()
if self.params.key != '':
self.logger.debug('self.params.key: %s', self.params.key)
self.verify_key()
def get_server_ip(self):
"""
Check for a valid dnsmasq server IP to use.
We can't use a hostname for the server because we end up trying to do a
DNS lookup immediately after instructing dnsmasq to restart.
"""
try:
ipaddress.ip_address(self.params.server)
self.params.server_ip = self.params.server
except ValueError:
try:
self.params.server_ip = socket.getaddrinfo(self.params.server, None)[0][4][0]
except (ValueError, socket.gaierror):
self.logger.error('Server (%s) cannot be found.', self.params.server)
sys.exit(1)
def verify_key(self):
"""Verify and open key file or error on failure."""
self.check_key('RSA')
if not self.key:
self.check_key('DSA')
if not self.key:
self.logger.error('No usable RSA or DSA key found. Halting.')
sys.exit(1)
def check_key(self, algorithm):
"""Set self.key if self.params.key is valid for the algorithm."""
if algorithm == 'RSA':
algo_class = RSAKey
elif algorithm == 'DSA':
algo_class = DSSKey
else:
raise ValueError('check_key() works with \'RSA\' or \'DSA\' only.')
self.logger.debug('Testing if key is %s.', algorithm)
try:
key = algo_class.from_private_key_file(self.params.key)
except PasswordRequiredException:
if self.params.password != '':
self.logger.debug('Decrypting %s key.', algorithm)
try:
key = algo_class.from_private_key_file(
self.params.key, password=self.params.password)
except SSHException:
self.logger.error('Password for key is not valid.')
else:
self.logger.info('Found valid encrypted %s key.', algorithm)
self.key = key
else:
self.logger.error('Encrypted %s key, requires password.', algorithm)
except SSHException:
self.key = False
else:
self.logger.info('Found valid %s key.', algorithm)
self.key = key
def open_ssh(self):
"""Check if an SSH connection is open, open a new connection if not."""
try:
transport = self.ssh.get_transport()
transport.send_ignore()
except (EOFError, AttributeError):
self.logger.debug('Opening SSH connection.')
pass_params = {}
pass_params['username'] = self.params.login
if self.key:
pass_params['pkey'] = self.key
else:
pass_params['password'] = self.params.password
try:
self.ssh.connect(self.params.server_ip, **pass_params)
except AuthenticationException:
self.logger.error('Could not authenticate with remote device.')
sys.exit(1)
except BadHostKeyException:
self.logger.error('Host key does not match expected key.')
sys.exit(1)
def close_ssh(self):
"""Close the SSH connection."""
if self.ssh:
self.logger.debug('Closing SSH connection.')
self.ssh.close()
def queue_put(self):
"""
Delayed putting of the local hosts file on the remote device.
The delay allows for any additional changes in the immediate future,
such as expected when a container is restarting, for example.
"""
self.logger.info('Queued remote hosts file update.')
self.delayed_put.reset()
def put_hostfile(self):
"""Put the local hosts file on the remote device."""
self.open_ssh()
self.logger.info('Writing remote hosts file: %s', self.params.file)
with open(self.temp_file.name, 'r', encoding="utf-8") as temp_file:
hosts_block = BLOCK_START + '\n' + temp_file.read() + BLOCK_END
try:
exec_return = self.ssh.exec_command(
'echo -e "' + hosts_block + '" >' + self.params.file)[1]
if exec_return.channel.recv_exit_status():
self.logger.error('Could not write hosts file.')
else:
self.exec_restart_command()
except EOFError:
self.logger.error('Could not write hosts file.')
self.close_ssh()
def exec_restart_command(self):
"""Execute command to update dnsmasq on remote device."""
self.open_ssh()
restart_cmd = self.params.restart_cmd.strip('\'"')
try:
exec_return = self.ssh.exec_command(restart_cmd)[1]
except SSHException:
self.logger.error('SSHException: Failed to execute remote command: %s', restart_cmd)
if exec_return.channel.recv_exit_status() > 0:
self.logger.error('Could not execute remote command: %s', restart_cmd)
else:
self.logger.info('Executed remote command: %s', restart_cmd)
class HostsHandler():
"""Handle the Hosts object and the individual HostEntry objects."""
def __init__(self, output_handler, **kwargs):
"""Initialize file handler and timing."""
self.params = SimpleNamespace(**kwargs)
self.logger = get_logger(self.__class__.__name__, self.params.log_level)
self.output_handler = output_handler
self.temp_file = output_handler.temp_file
self.delayed_write = ResettableTimer(self.params.local_write_delay, self.write_hosts)
self.hosts = Hosts(path='/dev/null')
def parse_hostnames(self, hostnames, id_string):
"""
Return dictionary items containing IPs and a list of hostnames.
dict_items([('<IP_1>', ['<hostname1>', '<hostname2>', etc..]),
('<IP_2>', ['<hostname3>', '<hostname4>', etc..]), etc..])
"""
hostname_dict = defaultdict(set)
for hostname in hostnames:
host_ip = self.params.ip
host_list = set()
# extra-hosts will include the IP, separated by a colon
if ':' in hostname:
hostname, host_ip = hostname.split(':', 1)
# strip the top level demain, if included
try:
hostname = hostname[0:hostname.index('.' + self.params.domain)]
except ValueError:
pass
if not self.hosts.exists(comment=id_string):
host_list.update([hostname, hostname + '.' + self.params.domain])
if self.params.prepend_www and not re.search('^www', hostname):
host_list.update(['www.' + hostname + '.' + self.params.domain])
hostname_dict[host_ip].update(host_list)
else:
self.logger.debug('comment exists in Hosts: %s', id_string)
return dict([host_ip, sorted(hostnames)] for host_ip, hostnames in hostname_dict.items())
def add_hosts(self, short_id, hostnames, agent_id=None, do_write=True):
"""
Create host's HostsEntry, add it to Hosts object. Optionally write out.
Setting the comment to a unique string (like a contaienr's 'short_id')
makes it easy to delete the correct hosts (and only the correct hosts)
across multiple IPs. Including an identifier for the particular Agent
that added the host allows esay deletion for all that Agent's hosts if
the Agent goes down.
"""
id_string = short_id
if agent_id is not None:
id_string += '.' + agent_id
parsed_hostnames = self.parse_hostnames(hostnames, id_string)
parsed_items = parsed_hostnames.items()
if not parsed_items:
self.logger.debug('Added host(s): no hostnames to add: %s', short_id)
else:
try:
for host_ip, names in parsed_items:
self.logger.debug('Adding: %s: %s', host_ip, ', '.join(names))
try:
hostentry = HostsEntry(entry_type='ipv4', address=host_ip,
names=names, comment=id_string)
except python_hosts.exception.InvalidIPv4Address:
self.logger.error('Skipping invalid IP address: %s', host_ip)
else:
if self.params.mode == 'manager':
self.hosts.add([hostentry],
allow_address_duplication=True,
allow_name_duplication=True)
else:
self.hosts.add([hostentry], force=True,
allow_address_duplication=True)
if do_write:
self.queue_write()
self.logger.info('Added host(s): %s',
', '.join(sum(parsed_hostnames.values(), [])))
except ValueError as err:
self.logger.info('Host already exists, nothing to add.')
self.logger.debug(err)
return parsed_items
def del_hosts(self, id_string):
"""Delete hosts with a comment matching id_string."""
hostnames = sum([entry.names for entry in self.hosts.entries
if id_string in entry.comment], [])
if not hostnames:
self.logger.debug(
'Deleting host(s): no hostnames to delete: %s', id_string)
else:
self.logger.info('Deleting host(s): %s', ', '.join(hostnames))
self.hosts.entries = list(
set(self.hosts.entries) - {entry for entry in self.hosts.entries
if id_string in entry.comment}
)
self.queue_write()
def queue_write(self):
"""
Delayed writing of the local and remote hosts files.
The delay allows for any additional changes in the immediate future,
such as expected when a container is restarting, for example.
"""
self.delayed_write.reset()
def write_hosts(self):
"""Write local hosts file, send it to the output handler."""
if self.params.log_level == logging.DEBUG:
self.logger.debug('Writing local hosts temp file: %s', self.temp_file.name)
for entry in self.hosts.entries:
print(' ', entry)
self.hosts.write(path=self.temp_file.name)
self.temp_file.seek(0)
self.output_handler.queue_put()
class APIServerHandler(Bottle):
"""Run the API server."""
def __init__(self, hosts_handler, **kwargs):
"""Initislize the API and configure routes."""
super().__init__()
self.params = SimpleNamespace(**kwargs)
self.logger = get_logger(self.__class__.__name__, self.params.log_level)
self.hosts_handler = hosts_handler
if self.params.ready_fd == '':
self.ready_fd = False
else:
self.ready_fd = int(self.params.ready_fd)
self.install(JwtPlugin(self.validation, self.params.api_key, algorithm="HS512"))
self.permissions = {"user": 0, "service": 1, "admin": 2}
self.route('/auth', callback=self.auth, method='POST')
self.route('/status', callback=self.status)
self.route('/add', callback=self.add_hosts, method='POST', auth='user')
self.route('/del/<short_id>', callback=self.del_hosts, method='DELETE', auth='user')
self.instance_id = hash(time.time())
signal_ready(self.ready_fd, self.logger)
def validation(self, auth, user):
"""Validate request."""
return self.permissions[auth["type"]] >= self.permissions[user]
def auth(self):
"""
Authenticate a node.
request: {'clientId': <client_id>, 'clientSecret': <password>}
response: {'access_token': <token>, 'type': 'bearer'}
"""
client_id = request.headers.get('clientId')
client_secret = request.headers.get('clientSecret')
try:
kdf = Scrypt(salt=str.encode(client_id), length=32, n=2**14, r=8, p=1)
except TypeError as err:
self.logger.error('Invalid auth request: %s', err)
response.status = 401
return "Unauthorized."
try:
kdf.verify(str.encode(self.params.api_key), bytes.fromhex(client_secret))
except cryptography.exceptions.InvalidKey as err:
self.logger.warning('Invalid key from client %s: %s', client_id, err)
response.status = 401
return "Unauthorized."
user = {"client_id": client_id, "client_secret": client_secret, "type": "user"}
if not user:
raise self.HTTPError(403, "Invalid user or password")
user["exp"] = time.time() + 86400
return {"access_token": JwtPlugin.encode(user), "type": "bearer"}
def status(self):
"""
Return the instance ID.
This is a general up/ready indicator, as well as providing a unique ID
so the clients can tell if the API has restarted (and re-initialize the
hosts data accordingly).
"""
# self.logger.debug('Status check: %s', request.remote_addr)
response.add_header('DMU-API-ID', self.instance_id)
return str(self.instance_id)
def add_hosts(self):
"""Add new hosts."""
self.logger.debug('add_hosts: %s', request.json)
self.hosts_handler.add_hosts(request.json['short_id'],
request.json['hostnames'],
request.json.get('from', None))
response.add_header('DMU-API-ID', self.instance_id)
return str(self.instance_id)
def del_hosts(self, short_id):
"""Delete hosts."""
self.logger.debug('del_hosts: %s', short_id)
self.hosts_handler.del_hosts(short_id)
response.status = 204
response.add_header('DMU-API-ID', self.instance_id)
return str(self.instance_id)
def start_monitor(self):
"""
Run the API.
Clear sys.argv before calling run(), else args get sent to the backend.
"""
self.logger.info('Starting API..')
sys.argv = sys.argv[:1]
if self.params.api_backend is None:
self.run(host=self.params.api_address, port=self.params.api_port,
debug=self.params.debug)
else:
self.run(host=self.params.api_address, server=self.params.api_backend,
port=self.params.api_port, debug=self.params.debug)
class DockerHandler():
"""Handle connecting to the Docker socket and the data it produces."""
client = None
def __init__(self, hosts_handler, **kwargs):
"""Initialize variables, do nothing until start_monitor()."""
self.params = SimpleNamespace(**kwargs)
self.logger = get_logger(self.__class__.__name__, self.params.log_level)
self.hosts_handler = hosts_handler
self.scan_success = False
self.event_verbs = {'start': 'starting',
'stop': 'stopping',
'connect': 'connecting to',
'disconnect': 'disconnecting from'}
if self.params.ready_fd == '':
self.ready_fd = False
else:
self.ready_fd = int(self.params.ready_fd)
def get_client(self):
"""Create the Docker client object."""
try:
self.client = docker.DockerClient(base_url=self.params.docker_socket)
except docker.errors.DockerException as err:
self.logger.error('Could not open Docker socket at %s. Exiting.',
self.params.docker_socket)
self.logger.debug('Error: %s', err)
sys.exit(1)
self.logger.info('Connected to Docker socket.')
swarm_status = self.client.info()['Swarm']['LocalNodeState']
match swarm_status:
case 'inactive':
self.logger.info('Docker standalone detected.')
case 'active':
self.logger.info('Docker Swarm mode detected.')
if self.params.mode != 'manager':
# pylint: disable=line-too-long
self.logger.error('Can only run in a Swarm as a manager, run with `--manager` argument')
self.logger.error('Use `dnsmasq_updater_agent.py` for monitoring Swarm devices.')
# pylint: enable=line-too-long
self.logger.error('Exiting.')
sys.exit(2)
case _:
self.logger.error('Swarm detection failed: %s', swarm_status)
sys.exit(1)
def get_hostnames(self, container):
"""
Return a list of hostnames for a container or service.
Include any IP address override in the form '<hostname>:<address>'
"""
hostnames = [container.attrs['Config']['Hostname']]
labels = container.labels
try:
hostnames.append(labels['dnsmasq.updater.host'])
except KeyError:
pass
if self.params.labels_from is not None:
traefik_pattern = re.compile(r'Host\(`([^`]*)`\)')
for key, value in labels.items():
if 'traefik' in self.params.labels_from and key.startswith('traefik.http.routers.'):
for match in traefik_pattern.finditer(value):
hostnames.append(match.group(1))
ip = self.get_hostip(container)
if ip is not None:
hostnames = [x + ':' + ip for x in hostnames]
try:
extra_hosts = container.attrs['HostConfig']['ExtraHosts']
except KeyError:
pass
else:
if extra_hosts:
hostnames = hostnames + extra_hosts
return hostnames
def get_hostip(self, container):
"""Get any IP address set with a label."""
try:
return container.labels['dnsmasq.updater.ip']
except KeyError:
return None
def scan_runnning_containers(self):
"""Scan running containers, find any with dnsmasq.updater.enable."""
self.logger.info('Started scanning running containers.')
try:
containers = self.client.containers.list(
filters={"label": "dnsmasq.updater.enable", "status": "running"})
except docker.errors.APIError as err:
self.logger.warning('Could not scan running containers: %s', err)
return
for container in containers:
hostnames = self.get_hostnames(container)
if hostnames is None:
continue
self.logger.info('Found %s: %s', container.name, ', '.join(hostnames))
if self.hosts_handler.add_hosts(container.short_id, hostnames, do_write=False):
self.scan_success = True
self.logger.info('Finished scanning running containers.')
def scan_network_containers(self):
"""Scan all containers on a specified network."""
self.logger.info('Started scanning containers on \'%s\' network.', self.params.network)
try:
network = self.client.networks.get(self.params.network)
except docker.errors.NotFound:
self.logger.error(
'Cannot scan network: network \'%s\' does not exist.', self.params.network)
return
for container in network.attrs['Containers']:
try:
container_object = self.client.containers.get(container)
except docker.errors.NotFound:
continue
hostnames = self.get_hostnames(container_object)
self.logger.info('Found %s: %s', container_object.name, ', '.join(hostnames))
if self.hosts_handler.add_hosts(container_object.short_id, hostnames):
self.scan_success = True
self.logger.info('Finished scanning containers on \'%s\' network.', self.params.network)
def handle_container_event(self, event):
"""Handle a container event."""
if 'dnsmasq.updater.enable' not in event['Actor']['Attributes']:
return
container = self.client.containers.get(event['Actor']['ID'])
short_id = container.short_id
name = container.name
self.logger.info('Detected %s %s.', name, self.event_verbs[event['status']])
if event['status'] == 'stop':
self.hosts_handler.del_hosts(short_id)
elif event['status'] == 'start':
hostnames = self.get_hostnames(container)
if hostnames is not None:
self.hosts_handler.add_hosts(short_id, hostnames)
def handle_network_event(self, event):
"""Handle a network event."""
try:
container = self.client.containers.get(event['Actor']['Attributes']['container'])
except docker.errors.NotFound:
self.logger.warning(
'Container %s not found.', event['Actor']['Attributes']['container'])
container = None
if container is not None:
short_id = container.short_id
self.logger.info('Detected %s %s \'%s\' network.', container.name,
self.event_verbs[event['Action']],
event['Actor']['Attributes']['name'])
if event['Action'] == 'disconnect':
self.hosts_handler.del_hosts(short_id)
elif event['Action'] == 'connect':
self.hosts_handler.add_hosts(short_id, self.get_hostnames(container))
def handle_events(self, event):
"""Monitor the docker socket for relevant container and network events."""
if (event['Type'] == 'container') and (event['status'] in {'start', 'stop'}):
self.handle_container_event(event)
elif (event['Type'] == 'network') \
and (self.params.network in event['Actor']['Attributes']['name']) \
and (event['Action'] in {'connect', 'disconnect'}):
self.handle_network_event(event)
def start_monitor(self):
"""
Connect to Docker socket.
Process existing containers then monitor events.
"""
self.get_client()
self.scan_runnning_containers()
if self.params.network:
self.scan_network_containers()
if self.scan_success:
self.hosts_handler.queue_write()
events = self.client.events(decode=True)
signal_ready(self.ready_fd, self.logger)
while True:
for event in events:
self.handle_events(event)
class ConfigHandler():
"""Read config files and parse commandline arguments."""
log_level = DEFAULT_LOG_LEVEL
def __init__(self):
"""Initialize default config, parse config file and command line args."""
self.defaults = {
'config_file': CONFIG_FILE,
'domain': 'docker',
'labels_from': None,
'prepend_www': False,
'docker_socket': 'unix://var/run/docker.sock',
'network': '',
'server': '',
'port': '22',
'login': '',
'password': '',
'key': '',
'file': '',
'restart_cmd': '',
'mode': 'standalone',
'location': 'remote',
'api_address': '0.0.0.0',
'api_port': '8080',
'api_key': '',
'api_backend': None,
'log_level': self.log_level,
'delay': 10,
'local_write_delay': 3,
'ready_fd': ''
}
self.args = []
self.config_parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=__doc__, add_help=False)
self.parse_initial_config()
self.parse_config_file()
self.parse_command_line()
self.check_args()
def parse_initial_config(self):
"""Just enough argparse to specify a config file and a debug flag."""
self.config_parser.add_argument(
'-c', '--config_file', action='store', metavar='FILE',
help='external configuration file')
self.config_parser.add_argument(
'--debug', action='store_true', help='turn on debug messaging')
self.args = self.config_parser.parse_known_args()[0]
if self.args.debug:
self.log_level = logging.DEBUG
self.defaults['log_level'] = logging.DEBUG
self.logger = get_logger(self.__class__.__name__, self.log_level)
self.logger.debug('Initial args: %s', json.dumps(vars(self.args), indent=4))
def parse_config_file(self):
"""Find and read external configuration files, if they exist."""
self.logger.debug('self.args.config_file: %s', self.args.config_file)
# find external configuration if none is specified
if self.args.config_file is None:
for config_path in CONFIG_PATHS:
config_file = os.path.join(config_path, CONFIG_FILE)
self.logger.debug('Looking for config file: %s', config_file)
if os.path.isfile(config_file):
self.logger.info('Found config file: %s', config_file)
self.args.config_file = config_file
break
if self.args.config_file is None:
self.logger.info('No config file found.')
# read external configuration if specified and found
if self.args.config_file is not None:
if os.path.isfile(self.args.config_file):
config = configparser.ConfigParser()
config.read(self.args.config_file)
self.defaults.update(dict(config.items("general")))
self.defaults.update(dict(config.items("dns")))
self.defaults.update(dict(config.items("hosts")))
self.defaults.update(dict(config.items("docker")))
self.defaults['prepend_www'] = config['dns'].getboolean('prepend_www')
self.defaults.update(dict(config.items("api")))
self.logger.debug('Args from config file: %s', json.dumps(self.defaults, indent=4))
else:
self.logger.error('Config file (%s) does not exist.',
self.args.config_file)
@staticmethod
def parse_commas(this_string):
"""Convert a comma separated string into a list variable."""
if this_string:
return this_string.split(',')
return None
def parse_command_line(self):
"""
Parse command line arguments.
Overwrite the default config and anything found in a config file.
"""
parser = argparse.ArgumentParser(
description='Docker Dnsmasq Updater', parents=[self.config_parser])
parser.set_defaults(**self.defaults)
parser.add_argument(
'--local_write_delay', action='store', type=int, help=argparse.SUPPRESS)
parser.add_argument(
'--ready_fd', action='store', metavar='INT',
help='set to an integer to enable signalling readiness by writing '
'a new line to that integer file descriptor')
mode_group = parser.add_argument_group(title='Mode')
mode = mode_group.add_mutually_exclusive_group()
mode.add_argument(
'--standalone', action='store_const', dest='mode', const='standalone',
help='running on a standalone Docker host (default)')
mode.add_argument(
'--manager', action='store_const', dest='mode', const='manager',
help='bring up the API and run as the manager for multiple Docker nodes')
docker_group = parser.add_argument_group(title='Docker')
docker_group.add_argument(
'-D', '--docker_socket', action='store', metavar='SOCKET',
help='path to the docker socket (default: \'%(default)s\')')
docker_group.add_argument(
'-n', '--network', action='store', metavar='NETWORK',
help='Docker network to monitor')
dns_group = parser.add_argument_group(title='DNS')
dns_group.add_argument(
'-i', '--ip', action='store', metavar='IP',
help='default IP for the DNS records')
dns_group.add_argument(
'-d', '--domain', action='store', metavar='DOMAIN',
help='domain/zone for the DNS record (default: \'%(default)s\')')
dns_group.add_argument(
'-L', '--labels_from', action='store', metavar='PROXIES', type=self.parse_commas,
help='add hostnames from labels set by other services (standalone only, default: \'%(default)s\')')
dns_group.add_argument(
'-w', '--prepend_www', action='store_true',
help='add \'www\' subdomains for all hostnames')
hosts_group = parser.add_argument_group(title='hosts file')
location_group = hosts_group.add_mutually_exclusive_group()
location_group.add_argument(
'--remote', action='store_const', dest='location', const='remote',
help='write to a remote hosts file, via SSH (default)')
location_group.add_argument(
'--local', action='store_const', dest='location', const='local',
help='write to a local hosts file')
hosts_group.add_argument(
'-f', '--file', action='store', metavar='FILE',
help='the hosts file (including path) to write')
hosts_group.add_argument(
'-r', '--restart_cmd', action='store', metavar='COMMAND',
help='the dnsmasq restart command to execute')
hosts_group.add_argument(
'-t', '--delay', action='store', metavar='SECONDS', type=int,
help='delay for writes to the hosts file (default: \'%(default)s\')')
remote_hosts_group = parser.add_argument_group(
title='Remote hosts file (needed by --remote)')
remote_hosts_group.add_argument(
'-s', '--server', action='store', metavar='SERVER',
help='dnsmasq server address')
remote_hosts_group.add_argument(
'-P', '--port', action='store', metavar='PORT',
help='port for SSH on the dnsmasq server (default: \'%(default)s\')')
remote_hosts_group.add_argument(
'-l', '--login', action='store', metavar='USERNAME',
help='login name for the dnsmasq server')
remote_hosts_group.add_argument(
'-k', '--key', action='store', metavar='FILE',
help='identity/key file for SSH to the dnsmasq server')
remote_hosts_group.add_argument(
'-p', '--password', action='store', metavar='PASSWORD',
help='password for the dnsmasq server OR for an encrypted SSH key')
api_group = parser.add_argument_group(
title='API server (needed by --manager)')
api_group.add_argument(
'--api_address', action='store', metavar='IP',
help='address for API to listen on (default: \'%(default)s\')')
api_group.add_argument(
'--api_port', action='store', metavar='PORT',
help='port for API to listen on (default: \'%(default)s\')')
api_group.add_argument(
'--api_key', action='store', metavar='KEY', help='API access key')
api_group.add_argument(
'--api_backend', action='store', metavar='STRING',
help='API backend (refer to Bottle module docs for details)')
self.args = parser.parse_args()
self.logger.debug('Parsed command line:\n%s',
json.dumps(vars(self.args), indent=4))
def check_args(self):
# pylint: disable=too-many-branches
"""Check we have all the information we need to run."""
if self.args.ip == '':
self.logger.error('No host IP specified.')
sys.exit(1)