-
Notifications
You must be signed in to change notification settings - Fork 3
/
start.py
183 lines (151 loc) · 4.78 KB
/
start.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
import subprocess
import itertools
import sys
from scripts import common
# Stacks:
ALL = (
'overlay', # order important!
'traefik',
'keycloak',
'bazarr',
'deluge',
'hydra',
'lidarr',
'organizr',
'pihole',
'plex',
'portainer',
'radarr',
'sabnzbd',
'sonarr',
'tdarr',
'qbittorrent',
'makemkv'
)
ENCODING = 'utf-8'
def list_from_stdout(reply, first_column_name):
iterator = iter(str.splitlines(reply.stdout))
for line in iterator:
if line.startswith(first_column_name):
break
else:
raise RuntimeError(f'Did not catch first column name {first_column_name}')
column_names = [x.lower() for x in line.split()]
answ = []
for line in iterator:
answ.append(dict(itertools.zip_longest(column_names,
line.split(),
fillvalue=None)))
return answ
def easyprint(rep):
sys.stdout.write(rep.stdout)
if rep.stderr:
sys.stdout.write(rep.stderr)
def clear_screen():
subprocess.run(['clear'])
def wait_for_user():
input('\nPress any key to continue\n')
def list_to_dict(stacklist):
return {entry['name']: entry for entry in stacklist}
def currently_running_stacks(print_to_stdout=True):
rep = subprocess.run(['docker', 'stack', 'ls'],
capture_output=True,
encoding=ENCODING)
if print_to_stdout:
print('Currently running stacks:')
easyprint(rep)
answ = list_from_stdout(rep, 'NAME')
return list_to_dict(answ)
def update_stacks(stacks):
for stack in stacks:
if stack == 'overlay':
continue
rep = subprocess.run(['docker-compose', '-f', f'{stack}.yml', 'pull'],
capture_output=True,
encoding=ENCODING)
easyprint(rep)
rep = subprocess.run(['docker', 'stack', 'deploy', '-c', f'{stack}.yml', stack],
capture_output=True,
encoding=ENCODING)
easyprint(rep)
wait_for_user()
def process_stack(relevant_stacks, action, commands):
clear_screen()
for i, stack in enumerate(relevant_stacks):
print(f'{i}) {stack}')
user = input(f'\nWhich stack do you want to {action}? (q to quit)\n')
try:
user = int(user)
except ValueError:
return
for i, stack in enumerate(relevant_stacks):
if i == user:
break
else:
return
for i, command in enumerate(commands):
rep = subprocess.run([x.format(stack=stack) for x in command],
capture_output=True,
encoding=ENCODING)
easyprint(rep)
wait_for_user()
return currently_running_stacks(print_to_stdout=False)
def start_stack(stacks):
stacks = process_stack([stack for stack in ALL if stack not in stacks],
'start',
[('docker', 'stack', 'deploy', '-c', '{stack}.yml', '{stack}')])
if stacks is None:
return
start_stack(stacks)
def stop_stack(stacks):
stacks = process_stack([stack for stack in ALL if stack in stacks],
'stop',
[('docker', 'stack', 'rm', '{stack}')])
if stacks is None:
return
stop_stack(stacks)
def update_stack(stacks):
stacks = process_stack([stack for stack in ALL if stack in stacks],
'update',
[('docker-compose', '-f', '{stack}.yml', 'pull'),
('docker', 'stack', 'deploy', '-c', '{stack}.yml', '{stack}')])
if stacks is None:
return
update_stack(stacks)
def check_service_health():
rep = subprocess.run(['docker', 'service', 'ls'],
capture_output=True,
encoding=ENCODING)
easyprint(rep)
wait_for_user()
def menu():
while True:
clear_screen()
stacks = currently_running_stacks()
print('\n')
print('What do you want to do?')
print('#######################')
print('0) Check service health')
print('1) Upgrade all stacks')
print('2) Start stack')
print('3) Remove stack')
print('4) Upgrade stack')
print('5) Exit')
user = input('\n')
if user == '0':
check_service_health()
elif user == '1':
update_stacks(stacks)
elif user == '2':
start_stack(stacks)
elif user == '3':
stop_stack(stacks)
elif user == '4':
update_stack(stacks)
elif user == '5':
break
def main():
common.sanity_checks()
menu()
if __name__ == '__main__':
main()