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
|
#!/usr/bin/python
import os
import sys
import getopt
import socket
import subprocess
import yaml
data_path = os.path.expanduser("~") + "/.config/host-control.yaml"
class host_control():
host = None
verbose = False
shell = False
usage = "Usage: host-control [options] [action]\n" \
"\n" \
"Options:\n" \
" -h [host]: specify host\n" \
" -v: tell more details\n" \
" -s: produce minimal shell output\n" \
"\n" \
"Actions:\n" \
" system-info show kernel info\n" \
" system-shell access system shell\n" \
" system-ping ping host system\n" \
" system-update update system\n" \
" system-restart restart system\n" \
" system-daemon-status show systemd status\n" \
" system-daemon-log show systemd log\n" \
" system-daemon-reload reload systemd units\n" \
" system-docker-status show docker status\n" \
" config-list list host config items\n" \
" config-update [config] update host config\n" \
" unit-list list host units\n" \
" unit-status [unit] show unit status\n" \
" unit-start [unit] start unit\n" \
" unit-stop [unit] stop unit\n" \
" unit-restart [unit] restart unit\n" \
" unit-log [unit] show unit log\n" \
" unit-update [unit] update unit"
# trace
def print_message(self, message):
if not self.shell:
print(message)
def print_command(self, command):
if self.verbose:
print("Running: " + " ".join(command))
# ssh
def ssh_address(self, host):
address = ""
if "user" in host:
address += host["user"]+"@"
address += self.host_address(host)
return address
def ssh_command(self, address, arguments):
return [ "ssh", address ] + arguments
# host
def host_address(self, host):
if "address" in host:
return host["address"]
else:
return host["name"]
def host_lookup(self, name):
for h in self.hosts:
if not "name" in h:
continue
if h["name"] == name:
return h
return None
# config
def config_path(self):
return self.config["path"]
def config_lookup(self, host, name):
if not "config" in host:
self.print_message("No config in host: " + self.host)
return 1
for c in host["config"]:
if not "name" in c:
continue
elif not "files" in c:
continue
if c["name"] == name:
return c
return None
def config_copy(self, host, source, sink):
source_path = os.path.join(self.config_path(), source)
if self.host_address(host) == self.hostname:
self.print_message("Copy " + source + " to " + sink)
command = [ "cp", source_path, sink ]
else:
sink_address = self.ssh_address(host) + ":" + sink
self.print_message("Copy " + source + " to " + sink_address)
command = [ "scp", source_path, sink_address ]
self.print_command(command)
subprocess.call(command)
def config_update(self, host, name):
config = self.config_lookup(host, name)
if config is None:
self.print_message("No config for " + name + " in host: " + self.host)
return 1
if not self.shell:
self.print_message("Updating host " + self.host + " config: " + name)
for f in config["files"]:
source = f.split(":")[0]
sink = f.split(":")[1]
self.config_copy(host, source, sink)
return 0
def config_list(self, host):
if not "config" in host:
self.print_message("No config in host: " + self.host)
return 1
self.print_message("Host " + self.host + " config:")
for c in host["config"]:
if not "name" in c:
continue
elif not "files" in c:
continue
if not self.shell:
print("- " + c["name"])
else:
print(c["name"])
return 0
# unit
def unit_lookup(self, host, name):
if not "units" in host:
self.print_message("No units in host: " + self.host)
return 1
for u in host["units"]:
if not "name" in u:
continue
if u["name"] == name:
return u
return None
def unit_systemctl(self, host, name, option):
address = self.ssh_address(host)
unit = self.unit_lookup(host, name)
if unit is None:
self.print_message("No " + name + " unit in host: " + self.host)
return 1
if not "systemd-unit" in unit:
return 1
command = self.ssh_command(address, [ "SYSTEMD_COLORS=1", "systemctl", option, unit["systemd-unit"] ])
self.print_command(command)
subprocess.call(command)
return 0
def unit_status(self, host, name):
return self.unit_systemctl(host, name, "status")
def unit_start(self, host, name):
self.print_message("Starting unit: " + name)
return self.unit_systemctl(host, name, "start")
def unit_stop(self, host, name):
self.print_message("Stopping unit: " + name)
return self.unit_systemctl(host, name, "stop")
def unit_restart(self, host, name):
self.print_message("Restarting unit: " + name)
return self.unit_systemctl(host, name, "restart")
def unit_log(self, host, name):
address = self.ssh_address(host)
unit = self.unit_lookup(host, name)
if unit is None:
self.print_message("No " + name + " unit in host: " + self.host)
return 1
if not "systemd-unit" in unit:
return 1
if "log-path" in unit:
arguments = [ "tail", "-n", "50", "-f", unit["log-path"] ]
else:
arguments = [ "SYSTEMD_COLORS=1", "journalctl", "-n", "50", "-fu", unit["systemd-unit"] ]
command = self.ssh_command(address, arguments)
self.print_command(command)
subprocess.call(command)
def unit_update(self, host, name):
daemon_reload = False
unit = self.unit_lookup(host, name)
if unit is None:
self.print_message("No " + name + " unit in host: " + self.host)
return 1
if not "config" in unit:
return 1
self.print_message("Updating unit: " + name)
if isinstance(unit["config"], list):
for c in unit["config"]:
self.config_update(host, c)
if c.startswith("systemd"):
daemon_reload = True
else:
self.config_update(host, unit["config"])
if unit["config"].startswith("systemd"):
daemon_reload = True
if daemon_reload:
self.system_daemon_reload(host)
self.unit_restart(host, name)
return 0
def unit_list(self, host):
if not "units" in host:
self.print_message("No units in host: " + self.host)
return 1
self.print_message("Host " + self.host + " units:")
for u in host["units"]:
if not "name" in u:
continue
if not self.shell:
print("- " + u["name"])
else:
print(u["name"])
# system
def system_info(self, host):
address = self.ssh_address(host)
arguments = [ "uname", "-a" ]
command = self.ssh_command(address, arguments)
self.print_command(command)
subprocess.call(command)
def system_shell(self, host):
address = self.ssh_address(host)
command = self.ssh_command(address, [])
self.print_command(command)
subprocess.call(command)
def system_ping(self, host):
address = self.host_address(host)
command = [ "ping", address ]
self.print_command(command)
subprocess.call(command)
def system_update(self, host):
address = self.ssh_address(host)
# TODO: Move update command in config.
arguments = [ "apt", "update", "&&", "apt", "upgrade" ]
command = self.ssh_command(address, arguments)
self.print_command(command)
subprocess.call(command)
def system_restart(self, host):
address = self.ssh_address(host)
arguments = [ "reboot" ]
command = self.ssh_command(address, arguments)
self.print_command(command)
subprocess.call(command)
def system_daemon_status(self, host):
address = self.ssh_address(host)
arguments = [ "SYSTEMD_COLORS=1", "systemctl", "status" ]
command = self.ssh_command(address, arguments)
self.print_command(command)
subprocess.call(command)
def system_daemon_log(self, host):
address = self.ssh_address(host)
arguments = [ "SYSTEMD_COLORS=1", "journalctl", "-n", "50", "-fb" ]
command = self.ssh_command(address, arguments)
self.print_command(command)
subprocess.call(command)
def system_daemon_reload(self, host):
address = self.ssh_address(host)
arguments = [ "SYSTEMD_COLORS=1", "systemctl", "daemon-reload" ]
command = self.ssh_command(address, arguments)
self.print_command(command)
subprocess.call(command)
def system_docker_status(self, host):
address = self.ssh_address(host)
arguments = [ "docker", "ps" ]
command = self.ssh_command(address, arguments)
self.print_command(command)
subprocess.call(command)
# main
def data_load(self):
s = open(data_path, "r")
y = yaml.load(s, Loader = yaml.SafeLoader)
s.close()
self.hostname = socket.gethostname()
self.config = y["config"]
self.hosts = y["hosts"]
def main(self):
action = None
self.data_load()
try:
options, arguments = getopt.getopt(sys.argv[1:], "h:vs")
except getopt.GetoptError:
self.print_message(self.usage)
return 1
for key, value in options:
if key == "-h":
self.host = value
elif key == "-v":
self.verbose = True
elif key == "-s":
self.shell = True
if len(arguments) < 1:
self.print_message(self.usage)
return 1
if self.host is None:
self.host = self.hostname
host = self.host_lookup(self.host)
if host is None:
self.print_message("Unknown host: " + self.host)
return 1
self.print_message("Target host: " + self.host)
action = arguments[0]
if action == "system-info":
return self.system_info(host)
elif action == "system-shell":
return self.system_shell(host)
elif action == "system-ping":
return self.system_ping(host)
elif action == "system-update":
return self.system_update(host)
elif action == "system-restart":
return self.system_restart(host)
elif action == "system-daemon-status":
return self.system_daemon_status(host)
elif action == "system-daemon-log":
return self.system_daemon_log(host)
elif action == "system-daemon-reload":
return self.system_daemon_reload(host)
elif action == "system-docker-status":
return self.system_docker_status(host)
elif action == "config-list":
return self.config_list(host)
elif action == "unit-list":
return self.unit_list(host)
if len(arguments) < 2:
self.print_message(self.usage)
return 1
if action == "config-update":
return self.config_update(host, arguments[1])
elif action == "unit-status":
return self.unit_status(host, arguments[1])
elif action == "unit-start":
return self.unit_start(host, arguments[1])
elif action == "unit-stop":
return self.unit_stop(host, arguments[1])
elif action == "unit-restart":
return self.unit_restart(host, arguments[1])
elif action == "unit-log":
return self.unit_log(host, arguments[1])
elif action == "unit-update":
return self.unit_update(host, arguments[1])
else:
self.print_message("Unknown action: " + action)
return 1
return 0
if __name__ == "__main__":
sys.exit(host_control().main())
|