summaryrefslogtreecommitdiff
path: root/host-control
blob: 7e1f571eaedc0eee61f9fc6526db7d9e16c830f7 (plain)
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
#!/usr/bin/python

import os
import sys
import getopt
import socket
import subprocess
import yaml

config_base = ".config/host-control"
config_path = os.path.join(config_base, "host-control.yaml")

class host_control():
	hosts = [ ]
	host = None
	verbose = False
	interactive = True

	usage = "Usage: host-control [options] [action]\n" \
		"\n" \
		"Options:\n" \
		"  -h [host]:			specify host\n" \
		"  -v:				tell more details\n" \
		"  -n:				non-interactive mode\n" \
		"\n" \
		"Actions:\n" \
		"  system-info			show kernel info\n" \
		"  system-shell			access system shell\n" \
		"  system-ping			ping host system\n" \
		"  system-top			show top running tasks\n" \
		"  system-update			update system\n" \
		"  system-restart		restart system\n" \
		"  system-initramfs-update	update system initramfs\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" \
		"  config-import [config]	import host config\n" \
		"  config-diff [config]		diff 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\n" \
		"  unit-docker-shell [unit]	access unit docker shell"

	# utils

	def print_message(self, message):
		if self.interactive:
			print(message)

	def print_command(self, command):
		if self.verbose:
			print("Running: " + " ".join(command))

	def command_call(self, command):
		self.print_command(command)
		subprocess.call(command, env = os.environ)

	# ssh

	def ssh_address(self, host):
		address = ""

		if "user" in host:
			address += host["user"]+"@"

		address += self.host_address(host)

		return address

	def ssh_call(self, host, command):
		address = self.ssh_address(host)
		command = [ "ssh", "-t", address ] + command

		self.command_call(command)

	# host

	def host_address(self, host):
		try:
			socket.getaddrinfo(host["name"], None)
			return host["name"]
		except OSError:
			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

	def host_call(self, host, command):
		if self.host_address(host) != self.hostname:
			self.ssh_call(host, command)
		else:
			self.command_call(command)

	# hook

	def hook_lookup(self, host, name):
		if not "hooks" in host:
			self.print_message("No hooks in host: " + self.host)
			return None

		for h in host["hooks"]:
			if not "target" in h:
				continue

			if h["target"] == name:
				return h

		self.print_message("No hook for " + name + " in host: " + self.host)
		return None

	def hook_call(self, host, hook):
		if "command" not in hook:
			return None

		command = [ hook["command"] ]
		self.host_call(host, command)

	# 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 None

		for c in host["config"]:
			if not "name" in c:
				continue
			elif not "files" in c:
				continue

			if c["name"] == name:
				return c

		self.print_message("No config for " + name + " in host: " + self.host)
		return None

	def config_update_call(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.command_call(command)

	def config_import_call(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", sink, source_path ]
		else:
			sink_address = self.ssh_address(host) + ":" + sink
			self.print_message("Copy " + source + " to " + sink_address)

			command = [ "scp", sink_address, source_path ]

		self.command_call(command)

	def config_diff_call(self, host, source, sink):
		source_path = os.path.join(self.config_path(), source)

		if self.host_address(host) != self.hostname:
			address = self.ssh_address(host)
			command = [ "ssh", address, "cat", sink ]

			self.print_command(command)
			cat = subprocess.Popen(command, stdout = subprocess.PIPE)

			command = [ "diff", "--color", "-u", source_path, "-" ]

			self.print_command(command)
			subprocess.call(command, stdin = cat.stdout)
		else:
			command = [ "diff", "--color", "-u", source_path, sink ]
			self.command_call(command)


	def config_owner_call(self, host, sink, owner):
		self.print_message("Ownership of " + sink + " to " + owner)

		command = [ "chown", owner, sink ]
		self.host_call(host, command)

	def config_mode_call(self, host, sink, mode):
		self.print_message("Mode of " + sink + " to " + mode)

		command = [ "chmod", mode, sink ]
		self.host_call(host, command)

	def config_update(self, host, name):
		config = self.config_lookup(host, name)
		if config is None:
			return 1

		self.print_message("Updating host " + self.host + " config: " + name)

		for f in config["files"]:
			source = f.split(":")[0]
			sink = f.split(":")[1]

			self.config_update_call(host, source, sink)

			if "owner" in config:
				self.config_owner_call(host, sink, config["owner"])

			if "mode" in config:
				self.config_mode_call(host, sink, str(config["mode"]))

		return 0

	def config_import(self, host, name):
		config = self.config_lookup(host, name)
		if config is None:
			return 1

		self.print_message("Import host " + self.host + " config: " + name)

		for f in config["files"]:
			source = f.split(":")[0]
			sink = f.split(":")[1]

			self.config_import_call(host, source, sink)

	def config_diff(self, host, name):
		config = self.config_lookup(host, name)
		if config is None:
			return 1

		self.print_message("Diff host " + self.host + " config: " + name)

		for f in config["files"]:
			source = f.split(":")[0]
			sink = f.split(":")[1]

			self.config_diff_call(host, source, sink)

	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 self.interactive:
				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_docker_container(self, unit):
		if "docker-container" in unit:
			return unit["docker-container"]
		else:
			return unit["name"]

	def unit_systemctl(self, host, name, option):
		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

		arguments = [ "systemctl", option, unit["systemd-unit"] ]
		self.host_call(host, arguments)

		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 "log-path" in unit:
			arguments = [ "tail", "-n", "50", "-f", unit["log-path"] ]
		elif "systemd-unit" in unit:
			arguments = [ "journalctl", "-n", "50", "-fu", unit["systemd-unit"] ]
		else:
			return 1

		self.host_call(host, arguments)

	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_docker_shell(self, host, name):
		unit = self.unit_lookup(host, name)
		if unit is None:
			self.print_message("No " + name + " unit in host: " + self.host)
			return 1

		container = self.unit_docker_container(unit)
		command = [ "-t", "docker", "exec", "-ti", container, "sh" ]

		self.host_call(host, command)

	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 self.interactive:
				print("- " + u["name"])
			else:
				print(u["name"])

	# system

	def system_info(self, host):
		command = [ "uname", "-a" ]

		self.host_call(host, command)

	def system_shell(self, host):
		if "shell" in host:
			command = [ host["shell"] ]
		else:
			command = [ "bash" ]

		self.host_call(host, command)

	def system_ping(self, host):
		address = self.host_address(host)
		command = [ "ping", address ]

		self.command_call(command)

	def system_top(self, host):
		if "top" in host:
			command = [ host["top"] ]
		else:
			command = [ "htop" ]

		self.host_call(host, command)

	def system_update(self, host):
		address = self.ssh_address(host)

		hook = self.hook_lookup(host, "system-update")
		if hook == None:
			return 1

		self.hook_call(host, hook)

	def system_restart(self, host):
		command = [ "reboot" ]

		self.host_call(host, command)

	def system_initramfs_update(self, host):
		address = self.ssh_address(host)

		hook = self.hook_lookup(host, "initramfs-update")
		if hook == None:
			return 1

		self.hook_call(host, hook)

	def system_daemon_status(self, host):
		command = [ "systemctl", "status" ]

		self.host_call(host, command)

	def system_daemon_log(self, host):
		command = [ "journalctl", "-n", "50", "-fb" ]

		self.host_call(host, command)

	def system_daemon_reload(self, host):
		command = [ "systemctl", "daemon-reload" ]

		self.host_call(host, command)

	def system_docker_status(self, host):
		command = [ "docker", "ps" ]

		self.host_call(host, command)

	# host-control

	def data_load(self):
		if "SUDO_USER" in os.environ:
			user = os.environ["SUDO_USER"]
		else:
			user = os.environ["USER"]

		data_path_user = os.path.expanduser("~" + user)
		data_path = os.path.join(data_path_user, config_path)

		s = open(data_path, "r")
		y = yaml.load(s, Loader = yaml.SafeLoader)
		s.close()

		self.hostname = socket.gethostname()
		self.config = y["config"]

		self.hosts = [ ]

		for host in y["hosts"]:
			if "path" in host:
				data_path = os.path.join(data_path_user, config_base)
				data_path = os.path.join(data_path, host["path"])
				s = open(data_path, "r")
				y = yaml.load(s, Loader = yaml.SafeLoader)
				s.close()

				self.hosts.append(y)
			else:
				self.hosts.append(host)

	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 == "-n":
				self.interactive = False

		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

		if self.verbose:
			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-top":
			return self.system_top(host)
		elif action == "system-update":
			return self.system_update(host)
		elif action == "system-restart":
			return self.system_restart(host)
		elif action == "system-initramfs-update":
			return self.system_initramfs_update(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])
		if action == "config-import":
			return self.config_import(host, arguments[1])
		if action == "config-diff":
			return self.config_diff(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])
		elif action == "unit-docker-shell":
			return self.unit_docker_shell(host, arguments[1])
		else:
			self.print_message("Unknown action: " + action)
			return 1

		return 0

if __name__ == "__main__":
	sys.exit(host_control().main())