-
Notifications
You must be signed in to change notification settings - Fork 2
/
kastenwesen.py
executable file
·1327 lines (1140 loc) · 53.8 KB
/
kastenwesen.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
# -*- coding: utf-8 -*-
#
# kastenwesen: a python tool for managing multiple docker containers
#
# Copyright (C) 2016 kastenwesen contributors [see git log]
# https://github.com/fau-fablab/kastenwesen
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
kastenwesen: a python tool for managing multiple docker containers
Usage:
kastenwesen [help]
kastenwesen status [<container>...] [--cron]
kastenwesen (start|stop|restart) [--ignore-dependencies] [<container>...]
kastenwesen rebuild [--no-cache] [--missing] [--ignore-dependencies] [<container>...]
kastenwesen check-for-updates [--auto-upgrade] [<container>...]
kastenwesen shell [--new-instance] <container>
kastenwesen log [-f] <container>
kastenwesen cleanup [--simulate] [--min-age=<days>]
Options:
-v enable verbose log output
Actions explained:
status: show status
start: inverse of stop.
Due to the way how docker links work,
some additional containers will automatically be restarted to fix links.
If this is not possible, use --ignore-dependencies to suppress errors and bring up the container in a partly-working state.
stop: stop a container or stop all containers.
Also stops dependent containers
(e.g. web application is stopped if you stop its database container)
--ignore-dependencies: Don't stop dependent containers, but rather leave them in a partly-working state.
restart: stop and start again
rebuild: rebuild and restart.
Takes care of dependencies.
--no-cache: Force rebuild of all layers.
--missing: skip images that are already built
check-for-updates: Check if there are updates for this image
--auto-upgrade: Also trigger a rebuild to apply these updates
(auto-upgrade can be disabled by setting disable_auto_upgrade=True in kastenwesen_config)
shell: exec a shell inside the running container,
or inside a separate instance of this image if using --new-instance
cleanup: carefully remove old containers and images that are no longer used
If the containers argument is not given, the command refers to all containers in the config.
"""
import datetime
import json
import logging
import os
import socket
import subprocess
import sys
import time
from collections import namedtuple
from copy import copy
from packaging.version import Version
import concurrent.futures
import dateutil.parser
import docker
import requests
import termcolor
from docopt import docopt
from pidfilemanager import AlreadyRunning, PidFileManager
# switch off strange python requests warnings and log output
requests.packages.urllib3.disable_warnings()
REQUESTS_LOG = logging.getLogger("requests")
REQUESTS_LOG.setLevel(logging.WARNING)
# time to wait between starting containers and checking the status
DEFAULT_STARTUP_GRACETIME = 2
# default TCP timeout for tests
TCP_TIMEOUT = 5
HTTP_TIMEOUT = 10
# parallelization of status tests: how many containers are checked in parallel
NUM_PARALLEL_TESTS = 99
SELINUX_STATUS = None
NAMESPACE = '' # Namespace for containers and images. '' or '$namespace/'
# status files
STATUS_FILES_DIR = '/var/lib/kastenwesen/'
RUNNING_CONTAINER_NAME_FILE = STATUS_FILES_DIR + '%(name)s.running_container_name'
RUNNING_CONTAINER_ID_FILE = STATUS_FILES_DIR + '%(name)s.running_container_id'
class ContainerStatus(object):
OKAY = "OKAY"
FAILED = "FAILED"
STARTING = "STARTING"
MISSING = 'MISSING'
class ImageNotFound(Exception):
"""Exception if an image could not be found on the local machine."""
def __init__(self, container):
self.container = container
super(ImageNotFound, self).__init__(
'There is no image on the local machine for container {}. '
'Maybe you have to build it first?'.format(self.container.name)
)
def exec_verbose(cmd, return_output=False):
"""
Run a command, and print infos about that to the terminal and log.
:param bool return_output: return output as string, don't print it to the terminal.
"""
print(os.getcwd() + "$ " + colored(cmd, attrs=['bold']), flush=True)
if return_output:
return subprocess.check_output(cmd, shell=True).decode('utf8')
else:
subprocess.check_call(cmd, shell=True)
def cprint(text, file=None, **options):
"""
Print colored text for output on interactive terminals.
Automatically disabled if the output is not a TTY.
See ``termcolor.cprint`` for documentation on the parameters.
"""
if file is None:
file = sys.stdout
if sys.stdout.isatty() and sys.stderr.isatty():
termcolor.cprint(text, file=file, **options)
else:
print(text, file=file)
def colored(text, **options):
"""
Color the text for output on interactive terminals.
Automatically disabled if the output is not a TTY.
See ``termcolor.colored`` for documentation on the parameters.
"""
if sys.stdout.isatty() and sys.stderr.isatty():
return termcolor.colored(text, **options)
else:
return text
def print_success(text):
"""Print positive information and success messages."""
cprint(text, attrs=['bold'], color='green')
def print_notice(text):
"""Print information which is between good and bad, e.g. "container is still starting..."."""
cprint(text, attrs=['bold'], color='yellow')
def print_warning(text):
"""Print negative information, errors, "container stopped", ... """
cprint(text, attrs=['bold'], color='red', file=sys.stderr)
def print_fatal(text):
"""Print fatal errors and immediately exit."""
cprint(text, attrs=['bold'], color='red', file=sys.stderr)
sys.exit(1)
def print_bold(text):
"""Print neutral but important information."""
cprint(text, attrs=['bold'])
def get_selinux_status():
""":return: (disabled|permissive|enforcing)"""
global SELINUX_STATUS
if SELINUX_STATUS:
return SELINUX_STATUS
else:
try:
return subprocess.check_output(
'getenforce 2>/dev/null || echo "disabled"',
shell=True).decode('utf8').strip().lower()
except subprocess.CalledProcessError as err:
print_warning("Error while running 'getenforce' to get current SELinux status")
logging.error(err)
return 'disabled'
def docker_version_geq(version):
"""Return True, if the version of docker is at least `version`."""
return Version(DOCKER_API_CLIENT.version()['Version']) >= Version(version)
class AbstractTest(object):
def __call__(self, container_instance):
"""Run the test. May print error messages if something is not ok.
:param container_instance: instance of the current container
:rtype: bool
:return: True if test successful, False otherwise.
"""
return False
class HTTPTest(AbstractTest):
def __init__(self, url, verify_ssl_cert=True, timeout=HTTP_TIMEOUT):
self.timeout = timeout
self.url = url
self.verify_ssl_cert = verify_ssl_cert
def __call__(self, container_instance):
try:
t = requests.get(self.url, verify=self.verify_ssl_cert, timeout=self.timeout)
t.raise_for_status()
except IOError as e:
logging.error("Test failed for HTTP %s: %s", self.url, e)
return False
return True
class TCPPortTest(AbstractTest):
def __init__(self, port, host=None, expect_data=True, timeout=TCP_TIMEOUT):
self.timeout = timeout
self.port = port
self.host = host or 'localhost'
self.expect_data = expect_data
def __call__(self, container_instance):
try:
sock = socket.create_connection((self.host, self.port), timeout=self.timeout)
except IOError:
logging.error("Connection failed for TCP host %s port %s", self.host, self.port)
return False
try:
sock.settimeout(1)
# send something
sock.send(b'hello\n')
# try to get a reply
data = sock.recv(1)
if not data:
raise IOError("no response?")
except IOError:
logging.error(
"No response from TCP host %s port %s - server dead "
"or this protocol doesn't answer to a simple 'hello' packet.",
self.host, self.port
)
return False
return True
class DockerShellTest(AbstractTest):
def __init__(self, shell_cmd, timeout=HTTP_TIMEOUT):
"""
Test which runs a shell command with ``docker exec`` and tests for return value equal to zero.
Only supported for docker containers.
:param str shell_cmd:
shell command for testing, e.g.
``hello | grep -q world``
Will be interpreted by ``bash`` on the container.
"""
assert isinstance(shell_cmd, str)
self.shell_cmd = shell_cmd
self.timeout = timeout
def __call__(self, container_instance):
"""
Run the test. See AbstractTest.run().
:type container_instance: DockerContainer
:return: status
"""
assert isinstance(container_instance, DockerContainer)
if not container_instance.is_running():
return False
cmd = ["docker", "exec", container_instance.running_container_name(),
'bash', '-c', self.shell_cmd]
try:
subprocess.check_call(
cmd,
timeout=self.timeout,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
except subprocess.CalledProcessError as err:
logging.warning(
"Test with shell command '%s' failed with returncode %s",
self.shell_cmd, err.returncode,
)
return False
except subprocess.TimeoutExpired as err:
logging.warning(
"Test with shell command '%s' timed out after %d seconds",
self.shell_cmd, err.timeout,
)
return False
return True
class AbstractContainer(object):
def __init__(self, name, sleep_before_test=0.5, only_build=False, startup_gracetime=None):
self.name = NAMESPACE + name
self.tests = []
self.links = []
self.sleep_before_test = sleep_before_test
if startup_gracetime is None:
startup_gracetime = DEFAULT_STARTUP_GRACETIME
self.startup_gracetime = startup_gracetime
self.only_build = only_build
def add_test(self, test):
assert isinstance(test, AbstractTest), "given test must be a AbstractTest subclass"
self.tests.append(test)
def stop(self):
pass
def start(self):
pass
def rebuild(self, ignore_cache=False):
pass
def is_running(self):
return False
@property
def is_built(self):
"""Return True if this container is already built or does not need to be built."""
return True
def time_running(self):
"""
Time in seconds since last start/restart, or `None` if unsupported or temporarily not available.
Test failures will be ignored if this runtime is shorter than a startup gracetime.
:rtype: None | float
"""
return None
def test(self, sleep_before=True):
"""Return True if all tests succeeded."""
success = True
for test in self.tests:
success = test(self) and success
# check that the container is running
if sleep_before:
time.sleep(self.sleep_before_test)
return success
def get_status(self, sleep_before=True):
"""Return a tuple: (okay: ContainerStatus, msg: str)."""
if not self.is_built:
return (ContainerStatus.MISSING, 'image is missing on the local system')
elif self.only_build and not self.tests:
# no tests for build-only container -> always return OK
return (ContainerStatus.OKAY, '(only build)')
elif self.test(sleep_before):
if self.is_running() or self.only_build:
running = "running, " if self.is_running() else ""
return (ContainerStatus.OKAY, '{message_run}{tests_ok}/{tests_ok} tests ok'.format(message_run=running, tests_ok=len(self.tests)))
else:
if self.tests:
return (ContainerStatus.FAILED, 'stopped, but tests succeeded. Check your tests!')
else:
return (ContainerStatus.FAILED, 'stopped')
else: # tests failed
if self.only_build:
return (ContainerStatus.FAILED, 'tests failed')
if self.is_running():
if self.time_running() < self.startup_gracetime:
return (ContainerStatus.STARTING, 'starting up... Tests not yet OK')
else:
return (ContainerStatus.FAILED, 'running, but tests failed')
else:
return (ContainerStatus.FAILED, 'stopped')
def needs_package_updates(self):
"""
Run a check for package updates.
:return: ``True`` if any packages could be updated
:rtype: bool
"""
return False
class CustomBuildscriptTask(AbstractContainer):
def __init__(self, name, build_command):
"""
Run a custom build script for a build-only container.
The environment variable IGNORE_CACHE is set to 0/1 depending on the use of --no-cache in 'kastenwesen rebuild'.
"""
AbstractContainer.__init__(self, name, only_build=True)
self.build_command = build_command
def rebuild(self, ignore_cache=False):
# TODO handle ignore_cache
exec_verbose("IGNORE_CACHE={} ".format(int(ignore_cache)) + self.build_command)
class MonitoringTask(AbstractContainer):
def __init__(self, name):
"""
Pseudo-'container' that only runs tests, nothing else.
Can be used for monitoring external services from kastenwesen status.
"""
AbstractContainer.__init__(self, name, only_build=True)
class DockerDatetime(object):
def __init__(self, value):
"""
Convert a datetime representation from the docker API into suitable python objects.
Most functions take a ``default`` argument to control what is returned
if the Docker API returns the pseudo-date ``0001-01-01T00:00:00Z``.
:type value: int | str
"""
if isinstance(value, int):
self.date = datetime.datetime.utcfromtimestamp(value)
elif value == "0001-01-01T00:00:00Z":
self.date = None
else:
date = dateutil.parser.parse(value)
# the returned timestamp is always UTC
date = date.replace(tzinfo=None)
self.date = date
# We cannot subclass datetime because it cannot contain the special value ``None``,
# but we try to be as transparent and similar as possible.
def __bool__(self):
"""Evaluate as logical ``False`` if the Docker API returns the pseudo-date ``0001-01-01T00:00:00Z``."""
return bool(self.date)
def __str__(self):
return str(self.date)
# a few helpful functions
def to_datetime(self, default=None):
return self.date or default
def timedelta_to_now(self, default=None):
"""Difference between the datetime and the system time.
Positive if the date is in the past."""
if self.date is None:
return default
else:
return datetime.datetime.utcnow() - self.date
def seconds_to_now(self, default=float('inf')):
"""Seconds between the datetime and the system time.
Positive if the date is in the past."""
delta = self.timedelta_to_now()
if delta is None:
return default
else:
return delta.total_seconds()
class DockerContainer(AbstractContainer):
def __init__(self, name, path, docker_options="", sleep_before_test=0.5, only_build=False, alias_tags=None, startup_gracetime=None):
"""
:param docker_options: commandline options to 'docker run'
"""
AbstractContainer.__init__(self, name, sleep_before_test,
only_build, startup_gracetime)
self.image_name = self.name + ':latest'
self.path = path
self.docker_options = docker_options
self.links = []
self.alias_tags = alias_tags or []
def add_link(self, link_to_container):
"""Add a link to the given container.
The link alias will be the container name given in the config, so you can directly reach the container under its name."""
assert isinstance(link_to_container, DockerContainer)
self.links.append(link_to_container)
def add_volume(self, host_path, container_path, readonly=False):
assert os.path.exists(host_path), "volume path {p} doesn't exist".format(p=host_path)
vol = [host_path, container_path]
options = []
if readonly:
options.append('ro')
if get_selinux_status() == 'enforcing':
options.append('Z')
if options:
vol.append(','.join(options))
self.docker_options += " -v {0} ".format(':'.join(vol))
def add_port(self, host_port, container_port, host_addr=None, test=True, udp=False):
"""
Forward incoming connections on host_addr:host_post to container_port inside the container.
:param boolean test:
test for an open TCP server on the port, raise error if nothing is listening there.
Parameter is ignored for UDP.
:param host_addr: host IP (or name) to listen on, or ``None`` to listen on all interfaces
:type host_addr: str | None
:param boolean udp: use UDP instead of TCP.
"""
if host_addr:
self.docker_options += " -p {host_addr}:{host_port}:{container_port}".format(host_port=host_port, container_port=container_port, host_addr=host_addr)
else:
self.docker_options += " -p {host_port}:{container_port}".format(host_port=host_port, container_port=container_port)
if udp:
self.docker_options += "/udp"
if test:
self.add_test(TCPPortTest(port=host_port, host=host_addr))
def __str__(self):
return self.name
def container_base_name(self):
"""Return the image name without namespace."""
return self.name[len(NAMESPACE):]
def rebuild(self, ignore_cache=False):
"""Rebuild the container image."""
# self.is_running() is called for the check against manually started containers from this image.
# after building, the old images will be nameless and this check is no longer possible
self.is_running()
print_bold("rebuilding image " + self.image_name)
nocache = "--no-cache" if ignore_cache else ""
exec_verbose("docker build {nocache} -t {imagename} {path}".format(nocache=nocache, imagename=self.image_name, path=self.path))
# docker version < 1.10 needs '-f' argument to 'docker tag'
# so that it works the way we expect it (overwrite tag if it exists)
force_tag_argument = '' if docker_version_geq('1.10') else '-f'
for tag in self.alias_tags:
exec_verbose(
"docker tag {force} {imagename} {tag}".format(
imagename=self.image_name, tag=tag,
force=force_tag_argument,
)
)
def running_container_id(self):
"""Return id of last known container instance, or False otherwise"""
# the running id file is written by `docker run --cidfile <file>` in .start()
try:
return open(
RUNNING_CONTAINER_ID_FILE % {'name': self.container_base_name()}, 'r'
).read()
except IOError:
return False
def running_container_name(self):
""" return name of last known container instance, or False otherwise"""
try:
return open(
RUNNING_CONTAINER_NAME_FILE % {'name': self.container_base_name()}, 'r'
).read()
except IOError:
return False
def _set_running_container_name(self, new_id):
previous_id = self.running_container_name()
base_name = self.container_base_name()
logging.debug("previous '%s' container name was: %s", base_name, previous_id)
logging.debug("new '%s' container name is now: %s", base_name, new_id)
open(RUNNING_CONTAINER_NAME_FILE % {'name': base_name}, 'w').write(new_id)
def _get_docker_options(self):
"""Get all docker additional options like --link or custom options."""
docker_options = ""
for linked_container in self.links:
if not linked_container.is_running():
# linked container isn't running. This will only happen if startup of one container fails, or if --missing-dependencies is used.
# FIXME: There is a race condition (time-of-check vs time-of-use) between .is_running() and the docker command execution-
# If the container dies inbetween, the docker command will fail.
print_warning(
"linked container {} is not running - container {} "
"will be missing this link until it is restarted!"
.format(linked_container.name, self.name)
)
continue
docker_options += "--link={name}:{alias} ".format(name=linked_container.running_container_name(), alias=linked_container.name)
docker_options += self.docker_options
return docker_options
def stop(self):
"""Stop the container."""
running_id = self.running_container_name()
print_bold("Stopping {name} container {container}".format(name=self.name, container=running_id))
if running_id and self.is_running():
exec_verbose("docker stop {id}".format(id=running_id))
else:
logging.info("no known instance running")
def start(self):
"""Start the container."""
if not self.is_built:
raise ImageNotFound(container=self)
if self.is_running():
raise Exception('container is already running')
base_name = self.container_base_name()
container_id_file = RUNNING_CONTAINER_ID_FILE % {'name': base_name}
# move container id file out of the way if it exists - otherwise docker complains at startup
try:
os.rename(container_id_file, container_id_file + "_previous")
except OSError:
pass
# names cannot be reused :( so we need to generate a new one each time
new_name = base_name + datetime.datetime.now().strftime("-%Y-%m-%d_%H_%M_%S")
cmd = "docker run -d" \
" --dns-search=." \
" --memory=2g --cidfile={container_id_file}" \
" --name={new_name} {docker_options}" \
" {image_name} ".format(
container_id_file=container_id_file,
new_name=new_name,
docker_options=self._get_docker_options(),
image_name=self.image_name,
)
print_bold("Starting container {}".format(new_name))
exec_verbose(cmd)
self._set_running_container_name(new_name)
def logs(self, follow=False):
MAX_LINES = 1000
if not follow:
out = DOCKER_API_CLIENT.logs(container=self.running_container_name(), stream=False, tail=MAX_LINES)
lines = sum([1 for char in out if char == '\n'])
if lines > MAX_LINES - 3:
print_warning("Output is truncated, printing only the last {} lines".format(MAX_LINES))
print(out.decode('utf8'))
else:
try:
for l in DOCKER_API_CLIENT.logs(container=self.running_container_name(), stream=True, timestamps=True, stdout=True, stderr=True, tail=MAX_LINES):
print(l.decode('utf8'), end='')
except KeyboardInterrupt:
sys.exit(0)
def check_for_unmanaged_containers(self):
""" Warn if any containers not managed by kastenwesen are running from the same image."""
config_container_ids = [
container.running_container_id() for container in CONFIG_CONTAINERS
if isinstance(container, DockerContainer)
]
conflicting_containers = [
container for container in DOCKER_API_CLIENT.containers()
if container['Image'] == self.image_name
and container['Id'] not in config_container_ids
and not 'de.fau.fablab.kastenwesen.temporary' in container['Labels']
]
logging.debug("Conflicting containers: %s", str(conflicting_containers))
if conflicting_containers:
container_list = '\n'.join((
'- Container %s: Image %s' % (c['Id'][:12], c['Image'])
for c in conflicting_containers
))
raise Exception(
"The following containers are not managed by kastenwesen.py, are currently running from kastenwesen images. "
"I am assuming this is not what you want. "
"Please stop it yourself and restart it via kastenwesen. "
"See the output of 'docker ps' for more info.\n" + container_list
)
def is_running(self):
"""Return True if this container is running."""
self.check_for_unmanaged_containers()
if not self.running_container_id():
return False
try:
status = DOCKER_API_CLIENT.inspect_container(self.running_container_id())
return status['State']['Running']
except (docker.errors.NotFound, docker.errors.NullResource):
return False
@property
def is_built(self):
"""Return True if an image for this container exists locally."""
return any(
any(
tag == self.name + (':latest' if ':' not in self.name else '')
for tag in image['RepoTags']
)
for image in DOCKER_API_CLIENT.images(name=self.name)
)
def time_running(self):
"""
Time in seconds since last start/restart, or `None` if unsupported or temporarily not available.
Test failures will be ignored if this runtime is shorter than a startup gracetime.
:rtype: None | float
"""
if not self.running_container_id():
return None
try:
status = DOCKER_API_CLIENT.inspect_container(self.running_container_id())
return DockerDatetime(status['State']['StartedAt']).seconds_to_now()
except docker.errors.NotFound:
return None
def needs_package_updates(self):
"""
Run a check for package updates
:return: ``True`` if any packages could be updated
:rtype: bool
"""
if not self.is_built:
raise ImageNotFound(container=self)
kastenwesen_path = os.path.dirname(os.path.realpath(__file__))
if self.is_running():
exec_verbose(
"docker cp {kastenwesen_path}/helper/ {container}:/usr/local/".format(
container=self.running_container_name(),
kastenwesen_path=kastenwesen_path,
)
)
cmd = "docker exec --user=root {container}" \
" /usr/local/helper/python-wrapper.sh" \
" /usr/local/helper/check_for_updates.py".format(
container=self.running_container_name(),
)
else:
base_name = self.container_base_name()
new_name = base_name + '-check-for-updates' + datetime.datetime.now().strftime("-%Y-%m-%d_%H_%M_%S")
# run check_for_updates.py in a new container instance.
# the temporary label is set so that check_for_unmanaged_containers()
# does not complain about this "unmanaged" instance
cmd = "docker run --rm" \
" --dns-search=." \
" --label de.fau.fablab.kastenwesen.temporary=True" \
" --user=root" \
" -v {kastenwesen_path}/helper/:/usr/local/kastenwesen_tmp/:ro{vol_opts}" \
" --name={new_name} {docker_options}" \
" {image_name}" \
" /usr/local/kastenwesen_tmp/python-wrapper.sh" \
" /usr/local/kastenwesen_tmp/check_for_updates.py".format(
new_name=new_name,
docker_options=self._get_docker_options(),
vol_opts=',Z' if get_selinux_status() == 'enforcing' else '',
kastenwesen_path=kastenwesen_path,
image_name=self.image_name,
)
updates = exec_verbose(cmd, return_output=True)
if updates:
print_warning("Container {} has outdated packages: {}".format(self.name, updates))
return True
else:
return False
def interactive_shell(self, new_instance=False):
"""
start a shell inside the running instance using ``docker exec``
:param bool new_instance:
start the shell in a separate container instance
using ``docker run``,
do not start it in the already running container.
"""
if new_instance:
# docker run ... to launch new instance
if not self.is_built:
raise ImageNotFound(container=self)
print("Starting a new container instance with an interactive shell:")
base_name = self.container_base_name()
new_name = base_name + '-tmp' + datetime.datetime.now().strftime("-%Y-%m-%d_%H_%M_%S")
# the temporary label is set so that check_for_unmanaged_containers()
# does not complain about this "unmanaged" instance
cmd = "docker run --rm -it" \
" --dns-search=." \
" --label de.fau.fablab.kastenwesen.temporary=True" \
" --name={new_name} {docker_options}" \
" {image_name} bash".format(
new_name=new_name,
docker_options=self._get_docker_options(),
image_name=self.image_name,
)
else:
# docker exec ... in running instance
print("Starting a shell inside the running instance.")
if not self.is_running():
print_fatal("Container {} is not running. Use --new-instance to start a new container instance especially for the shell.".format(self.name))
cmd = "docker exec -it {container} bash".format(container=self.running_container_name())
exec_verbose(cmd)
def rebuild_many(containers, ignore_cache=False, only_missing=False, ignore_dependencies=False):
""" rebuild given containers
:param list[AbstractContainer] containers: containers to rebuild
:param bool ignore_cache: use ``--no-cache`` in docker build to ensure that external dependencies are fresh
:param bool only_missing: rebuild only containers if there is no image on the local system
:param bool ignore_dependencies: do not stop/start dependent containers
:return list[AbstractContainer]: all containers that were affected by the rebuild. Also contains additional dependent containers that had to be restarted.
"""
for container in containers:
if only_missing and container.is_built:
logging.info("Skipping %s, because it is already built", container.name)
continue
container.rebuild(ignore_cache)
return restart_many(containers, ignore_dependencies=ignore_dependencies)
def ordered_by_dependency(containers, add_dependencies=False, add_reverse_dependencies=False):
""" Sort and possibly enlarge the list of containers so that it can be used for starting/stopping a group of containers without breaking any links.
The list will be given in an order in which they can be started. Reverse it for stopping.
:param bool add_dependencies: Add any containers that the given ones depend on. (useful for starting)
:param bool add_reverse_dependencies: Add any containers that depend on the given ones. (useful for stopping)
"""
containers = list(containers)
if add_reverse_dependencies:
reverse_dependencies = set(containers)
something_changed = True
while something_changed:
# loop through all links, looking for something that can be directly or indirectly broken by stopping one of the given containers
something_changed = False
for container in config_containers:
for link in container.links:
if link in containers or link in reverse_dependencies:
# stopping the given list will break this container
if container in reverse_dependencies:
# already added, skip this one
continue
else:
something_changed = True
logging.debug("Adding reverse dependency %s to the given list of containers", link)
reverse_dependencies.add(container)
containers += list(reverse_dependencies)
ordered_containers = []
something_changed = True
while something_changed:
something_changed = False
for container in copy(containers):
if container in ordered_containers:
# already added, skip this one
continue
links_satisfied = True
for link in container.links:
if link not in containers:
# this container links to a container not given in the list
if add_dependencies:
logging.debug("Adding dependency %s to the given list of containers", link)
containers.append(link)
something_changed = True
else:
# this dependency cannot be satisfied, ignore.
continue
if link not in ordered_containers:
links_satisfied = False
if links_satisfied:
ordered_containers.append(container)
something_changed = True
return ordered_containers
def restart_many(requested_containers, ignore_dependencies=False):
"""
Restart given containers, and if necessary also their dependencies and reverse dependencies.
:param list[AbstractContainer] requested_containers: containers to restart
:param bool ignore_dependencies: do not stop/start dependent containers
:return list[AbstractContainer]: all containers that were affected. Also contains additional dependent containers that had to be (re)started.
"""
# also restart the containers that will be broken by this:
stop_containers = stop_many(requested_containers, message_restart=True, ignore_dependencies=ignore_dependencies)
start_containers = ordered_by_dependency(stop_containers, add_dependencies=True)
added_dep_containers = [container for container in start_containers if container not in stop_containers]
if added_dep_containers:
print_bold(
"Also starting necessary dependencies, if not yet running: {}".format(
", ".join([str(i) for i in added_dep_containers])
)
)
for container in start_containers:
if container.only_build:
# container is only a meta container, not really started
continue
if container in stop_containers or not container.is_running():
try:
container.start()
except ImageNotFound as exc:
if ignore_dependencies:
print_warning("Ignoring missing dependency:")
print_warning(str(exc))
else:
print_fatal(str(exc) + " Use --ignore-dependencies to skip this error.")
return start_containers
def stop_many(requested_containers, message_restart=False, ignore_dependencies=False):
"""
Stop the given containers and all that that depend on them (i.e. are linked to them)
:param requested_containers: List of containers
:type requested_containers: list[AbstractContainer]
:param bool message_restart:
Will the containers be restarted later?
This only affects the log output, not the actions taken
:param bool ignore_dependencies: do not stop/start dependent containers
:rtype: list[AbstractContainer]
:return: list of all containers that were stopped
(includes the ones stopped because of dependencies)
"""
stop_containers = list(reversed(ordered_by_dependency(requested_containers, add_reverse_dependencies=not ignore_dependencies)))
added_dep_containers = [
container for container in stop_containers
if container not in requested_containers and container.is_running()
]
if added_dep_containers:
print_bold(
"Also {verb} containers affected by this action: {containers}"
.format(
verb="restarting" if message_restart else "stopping",
containers=", ".join([str(i) for i in added_dep_containers])
)
)
for container in stop_containers:
container.stop()
return stop_containers
def need_package_updates(containers):
"""Return all of the given containers that need package updates."""
try:
return [container for container in containers if container.needs_package_updates()]
except ImageNotFound as exc:
print_fatal(str(exc))
def cleanup_containers(min_age_days=0, simulate=False):
# TODO how to make sure this doesn't delete data-containers for use with --volumes-from?
# -> only delete containers known to this script? that would require logging all previous IDs
# get all non-running containers
containers = DOCKER_API_CLIENT.containers(trunc=False, all=True)
config_container_ids = [
c.running_container_id() for c in CONFIG_CONTAINERS
if isinstance(c, DockerContainer)
]
removed_containers = []
for container in containers:
state = DOCKER_API_CLIENT.inspect_container(container['Id'])['State']
if state['Running']:
continue
date_finished = DOCKER_API_CLIENT.inspect_container(container['Id'])['State']['FinishedAt']
date_finished = DockerDatetime(date_finished)
if date_finished:
assert date_finished.to_datetime() > datetime.datetime(2002, 1, 1)
if date_finished.timedelta_to_now() < datetime.timedelta(days=1) * min_age_days:
# too young
continue
date_created = DockerDatetime(container['Created'])
assert date_created.to_datetime() <= date_finished.to_datetime(), \
"Container creation time is after the time it finished: " \
"container='{}', parsed creation time={} -- state='{}' " \
"parsed finishing time={}" \
.format(container,
date_created,
DOCKER_API_CLIENT.inspect_container(container['Id'])['State'],