-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdps
More file actions
executable file
·126 lines (96 loc) · 3.26 KB
/
dps
File metadata and controls
executable file
·126 lines (96 loc) · 3.26 KB
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
#!/usr/bin/env python3
import csv
from io import StringIO
import re
import subprocess
import sys
FORMAT_ARGS = [
'--format',
'{{.Names}};{{.Status}};{{.Image}};{{.Ports}}'
]
RESET = '\033[0m'
RED = '\033[31m'
GREEN = '\033[32m'
YELLOW = '\033[33m'
CYAN = '\033[36m'
FUCHSIA = '\033[95m'
def _colourise(s, col):
return '%s%s%s' % (col, s, RESET)
class DPSRow(object):
def __init__(self, names, status, image, ports):
self.names = names
self.status = status
self.ports = ports
image_comps = image.split(':')
self.image_name = image_comps[0]
self.image_tag = image_comps[1] if len(image_comps) > 1 else 'latest'
@staticmethod
def _abbreviate_timings(status):
"""Shorten timings where possible for easier display"""
status = status.replace('Less than an', '<')
status = status.replace('Less than a', '<')
status = status.replace('About an', '~1')
status = status.replace('About a', '~1')
status = status.replace(' ago', '')
status = re.sub('([a-zA-Z]+)s', r'\1', status)
status = status.replace(' second', 's')
status = status.replace(' minute', 'm')
status = status.replace(' hour', 'h')
status = status.replace(' day', 'd')
status = status.replace(' week', 'w')
status = status.replace(' month', 'mo')
status = status.replace(' year', 'y')
return status
@staticmethod
def _colourise_status(status):
if 'Exited' in status:
status = 'Exited'
if 'Restarting' in status:
timing = DPSRow._abbreviate_timings(
re.sub(r'Restarting \(.+\) ', '', status)
)
status = f'♻️ {timing}'
if 'Up' not in status:
return _colourise(status, RED)
col = YELLOW if 'second' in status or 'minute' in status else GREEN
status = status.replace(' (healthy)', '')
status = DPSRow._abbreviate_timings(status)
return _colourise(status, col)
@staticmethod
def _abbreviate_image(name):
"""
Shorten long image names, such as by trimming length AWS ECR names
down to a short representation
12345789.dkr.ecr.{region}.amazon.aws.com -> ecr[123456789]
"""
if '/' not in name:
return name
comps = name.split('/')
repo = comps[0]
img = comps[1]
repo = re.sub(
r'(\d+)\.dkr\.ecr\.[^\.]+\.amazonaws\.com', r'ecr[\1]', repo
)
return '{}/{}'.format(repo, img)
def __str__(self):
return '%-50s %-16s %-70s %s' % (
_colourise(self.names, CYAN),
self._colourise_status(self.status),
'%s:%s' % (
_colourise(self._abbreviate_image(self.image_name), CYAN),
_colourise(self.image_tag, FUCHSIA)
),
self.ports
)
def main():
output = docker_ps(sys.argv[1:])
fp = StringIO(output)
reader = sorted(list(csv.reader(fp, delimiter=';')))
for row in reader:
print(DPSRow(*row))
def docker_ps(args=None):
args = args or []
res = subprocess.check_output(['docker', 'ps'] + FORMAT_ARGS + args)
return res.decode('utf-8')
if __name__ == '__main__':
main()