From 7d4147617999505c5d5e8b81e7c1b07016c5faa5 Mon Sep 17 00:00:00 2001 From: Paul Kocialkowski Date: Sat, 28 Jan 2023 18:07:02 +0100 Subject: host-control: Add shell mode to support bash-completion Signed-off-by: Paul Kocialkowski --- host-control | 97 ++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 58 insertions(+), 39 deletions(-) diff --git a/host-control b/host-control index f16225b..3adfcee 100755 --- a/host-control +++ b/host-control @@ -10,14 +10,16 @@ import yaml data_path = os.path.expanduser("~") + "/.config/host-control.yaml" class host_control(): - verbose = False 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" \ @@ -41,7 +43,11 @@ class host_control(): # trace - def trace_command(self, command): + def print_message(self, message): + if not self.shell: + print(message) + + def print_command(self, command): if self.verbose: print("Running: " + " ".join(command)) @@ -85,7 +91,7 @@ class host_control(): def config_lookup(self, host, name): if not "config" in host: - print("No config in host: " + self.host) + self.print_message("No config in host: " + self.host) return 1 for c in host["config"]: @@ -103,26 +109,27 @@ class host_control(): source_path = os.path.join(self.config_path(), source) if self.host_address(host) == self.hostname: - print("Copy " + source + " to " + sink) + self.print_message("Copy " + source + " to " + sink) command = [ "cp", source_path, sink ] else: sink_address = self.ssh_address(host) + ":" + sink - print("Copy " + source + " to " + sink_address) + self.print_message("Copy " + source + " to " + sink_address) command = [ "scp", source_path, sink_address ] - self.trace_command(command) + self.print_command(command) subprocess.call(command) def config_update(self, host, name): config = self.config_lookup(host, name) if config is None: - print("No config for " + name + " in host: " + self.host) + self.print_message("No config for " + name + " in host: " + self.host) return 1 - print("Updating host " + self.host + " config: " + name) + if not self.shell: + self.print_message("Updating host " + self.host + " config: " + name) for f in config["files"]: source = f.split(":")[0] @@ -134,10 +141,10 @@ class host_control(): def config_list(self, host): if not "config" in host: - print("No config in host: " + self.host) + self.print_message("No config in host: " + self.host) return 1 - print("Host " + self.host + " config:") + self.print_message("Host " + self.host + " config:") for c in host["config"]: if not "name" in c: @@ -145,7 +152,10 @@ class host_control(): elif not "files" in c: continue - print("- " + c["name"]) + if not self.shell: + print("- " + c["name"]) + else: + print(c["name"]) return 0 @@ -153,7 +163,7 @@ class host_control(): def unit_lookup(self, host, name): if not "units" in host: - print("No units in host: " + self.host) + self.print_message("No units in host: " + self.host) return 1 for u in host["units"]: @@ -170,7 +180,7 @@ class host_control(): unit = self.unit_lookup(host, name) if unit is None: - print("No " + name + " unit in host: " + self.host) + self.print_message("No " + name + " unit in host: " + self.host) return 1 if not "systemd-unit" in unit: @@ -178,7 +188,7 @@ class host_control(): command = self.ssh_command(address, [ "SYSTEMD_COLORS=1", "systemctl", option, unit["systemd-unit"] ]) - self.trace_command(command) + self.print_command(command) subprocess.call(command) return 0 @@ -187,15 +197,15 @@ class host_control(): return self.unit_systemctl(host, name, "status") def unit_start(self, host, name): - print("Starting unit: " + name) + self.print_message("Starting unit: " + name) return self.unit_systemctl(host, name, "start") def unit_stop(self, host, name): - print("Stopping unit: " + name) + self.print_message("Stopping unit: " + name) return self.unit_systemctl(host, name, "stop") def unit_restart(self, host, name): - print("Restarting unit: " + name) + self.print_message("Restarting unit: " + name) return self.unit_systemctl(host, name, "restart") def unit_log(self, host, name): @@ -203,7 +213,7 @@ class host_control(): unit = self.unit_lookup(host, name) if unit is None: - print("No " + name + " unit in host: " + self.host) + self.print_message("No " + name + " unit in host: " + self.host) return 1 if not "systemd-unit" in unit: @@ -216,7 +226,7 @@ class host_control(): command = self.ssh_command(address, arguments) - self.trace_command(command) + self.print_command(command) subprocess.call(command) def unit_update(self, host, name): @@ -224,13 +234,13 @@ class host_control(): unit = self.unit_lookup(host, name) if unit is None: - print("No " + name + " unit in host: " + self.host) + self.print_message("No " + name + " unit in host: " + self.host) return 1 if not "config" in unit: return 1 - print("Updating unit: " + name) + self.print_message("Updating unit: " + name) if isinstance(unit["config"], list): for c in unit["config"]: @@ -252,16 +262,19 @@ class host_control(): def unit_list(self, host): if not "units" in host: - print("No units in host: " + self.host) + self.print_message("No units in host: " + self.host) return 1 - print("Host " + self.host + " units:") + self.print_message("Host " + self.host + " units:") for u in host["units"]: if not "name" in u: continue - print("- " + u["name"]) + if not self.shell: + print("- " + u["name"]) + else: + print(u["name"]) # system @@ -270,21 +283,21 @@ class host_control(): arguments = [ "uname", "-a" ] command = self.ssh_command(address, arguments) - self.trace_command(command) + self.print_command(command) subprocess.call(command) def system_shell(self, host): address = self.ssh_address(host) command = self.ssh_command(address, []) - self.trace_command(command) + self.print_command(command) subprocess.call(command) def system_ping(self, host): address = self.host_address(host) command = [ "ping", address ] - self.trace_command(command) + self.print_command(command) subprocess.call(command) def system_update(self, host): @@ -294,7 +307,7 @@ class host_control(): command = self.ssh_command(address, arguments) - self.trace_command(command) + self.print_command(command) subprocess.call(command) def system_restart(self, host): @@ -303,7 +316,7 @@ class host_control(): command = self.ssh_command(address, arguments) - self.trace_command(command) + self.print_command(command) subprocess.call(command) def system_daemon_status(self, host): @@ -311,7 +324,7 @@ class host_control(): arguments = [ "SYSTEMD_COLORS=1", "systemctl", "status" ] command = self.ssh_command(address, arguments) - self.trace_command(command) + self.print_command(command) subprocess.call(command) def system_daemon_log(self, host): @@ -319,7 +332,7 @@ class host_control(): arguments = [ "SYSTEMD_COLORS=1", "journalctl", "-n", "50", "-fb" ] command = self.ssh_command(address, arguments) - self.trace_command(command) + self.print_command(command) subprocess.call(command) def system_daemon_reload(self, host): @@ -328,7 +341,7 @@ class host_control(): command = self.ssh_command(address, arguments) - self.trace_command(command) + self.print_command(command) subprocess.call(command) def system_docker_status(self, host): @@ -337,7 +350,7 @@ class host_control(): command = self.ssh_command(address, arguments) - self.trace_command(command) + self.print_command(command) subprocess.call(command) # main @@ -355,16 +368,22 @@ class host_control(): action = None self.data_load() - options, arguments = getopt.getopt(sys.argv[1:], "h:v") + 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: - print(self.usage) + self.print_message(self.usage) return 1 if self.host is None: @@ -372,10 +391,10 @@ class host_control(): host = self.host_lookup(self.host) if host is None: - print("Unknown host: " + self.host) + self.print_message("Unknown host: " + self.host) return 1 - print("Target host: " + self.host) + self.print_message("Target host: " + self.host) action = arguments[0] @@ -403,7 +422,7 @@ class host_control(): return self.unit_list(host) if len(arguments) < 2: - print(self.usage) + self.print_message(self.usage) return 1 if action == "config-update": @@ -421,7 +440,7 @@ class host_control(): elif action == "unit-update": return self.unit_update(host, arguments[1]) else: - print("Unknown action: " + action) + self.print_message("Unknown action: " + action) return 1 return 0 -- cgit v1.2.3