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
|
#!/usr/bin/env python3
###############################################################################
# Copyright (c) 2017 Koren Lev (Cisco Systems), Yaron Yogev (Cisco Systems) #
# and others #
# #
# All rights reserved. This program and the accompanying materials #
# are made available under the terms of the Apache License, Version 2.0 #
# which accompanies this distribution, and is available at #
# http://www.apache.org/licenses/LICENSE-2.0 #
###############################################################################
# Check that Docker images are up and running
import argparse
from subprocess import check_output
import sys
from utils.binary_converter import BinaryConverter
IMAGES_TO_SEARCH = ["ui", "sensu", "scan", "api", "ldap", "listen", "mongo"]
class DockerImageCheck:
STATUS_HEADER = "STATUS"
PORTS_HEADER = "PORTS"
HEADERS = [
"CONTAINER ID",
"IMAGE",
"COMMAND",
"CREATED",
"STATUS",
"PORTS",
"NAMES"
]
def __init__(self):
args = self.get_args()
self.docker_ps = ""
if args.inputfile:
try:
with open(args.inputfile, 'r') as input_file:
dummy_input = input_file.read()
self.docker_ps = dummy_input.splitlines()
except FileNotFoundError:
raise FileNotFoundError("failed to open input file: {}"
.format(args.inputfile))
exit(1)
else:
cmd = "sudo docker ps"
output = check_output(cmd, shell=True)
converter = BinaryConverter()
output = converter.binary2str(output)
self.docker_ps = output.splitlines()
headers_line = self.docker_ps[0]
self.header_location = {}
for h in self.HEADERS:
self.header_location[h] = headers_line.index(h)
@staticmethod
def get_args():
# try to read scan plan from command line parameters
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--inputfile', nargs='?', type=str,
default='',
help="read input from the specifed file \n" +
"(default: from stdin)")
return parser.parse_args()
def verify_image_is_up(self, image_name) -> bool:
matches = [line for line in self.docker_ps if line.endswith(image_name)]
if not matches:
print("missing docker image: {}".format(image_name))
return False
line = matches[0]
status = line[self.header_location[self.STATUS_HEADER]-1:]
status = status.split()[0] if ' ' in status else status
status = status.lower()
if status == "up":
return True
print("image {} is not up".format(image_name))
return False
if __name__ == '__main__':
image_checker = DockerImageCheck()
ret = True
for image in ["calipso-{}".format(i) for i in IMAGES_TO_SEARCH]:
if not image_checker.verify_image_is_up(image):
ret = False
sys.exit(0 if ret else 1)
|