From 26a4f91f8729d192309d5fbc887e1f99fc5bc254 Mon Sep 17 00:00:00 2001 From: Paul Kocialkowski Date: Fri, 21 Apr 2023 22:40:52 +0200 Subject: host-control: Refactor ssh and commands Signed-off-by: Paul Kocialkowski --- host-control | 163 +++++++++++++++++++---------------------------------------- 1 file changed, 52 insertions(+), 111 deletions(-) diff --git a/host-control b/host-control index a7c8716..9540b6b 100755 --- a/host-control +++ b/host-control @@ -43,7 +43,7 @@ class host_control(): " unit-update [unit] update unit\n" \ " unit-docker-shell [unit] access unit docker shell" - # trace + # utils def print_message(self, message): if not self.shell: @@ -53,6 +53,10 @@ class host_control(): 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): @@ -65,8 +69,11 @@ class host_control(): return address - def ssh_command(self, address, arguments): - return [ "ssh", address ] + arguments + def ssh_call(self, host, command): + address = self.ssh_address(host) + command = [ "ssh", address, "SYSTEMD_COLORS=1" ] + command + + self.command_call(command) # host @@ -86,6 +93,12 @@ class host_control(): 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): @@ -103,11 +116,12 @@ class host_control(): self.print_message("No hook for " + name + " in host: " + self.host) return None - def hook_command(self, hook): + def hook_call(self, host, hook): if "command" not in hook: return None - return [ hook["command"] ] + command = [ hook["command"] ] + self.host_call(host, command) # config @@ -131,7 +145,7 @@ class host_control(): self.print_message("No config for " + name + " in host: " + self.host) return None - def config_copy_command(self, host, source, sink): + def config_copy_call(self, host, source, sink): source_path = os.path.join(self.config_path(), source) if self.host_address(host) == self.hostname: @@ -144,33 +158,19 @@ class host_control(): command = [ "scp", source_path, sink_address ] - return command + self.command_call(command) - def config_owner_command(self, host, sink, owner): + def config_owner_call(self, host, sink, owner): self.print_message("Ownership of " + sink + " to " + owner) - command_base = [ "chown", owner, sink ] - - if self.host_address(host) == self.hostname: - command = command_base - else: - address = self.ssh_address(host) - command = self.ssh_command(address, command_base) + command = [ "chown", owner, sink ] + self.host_call(host, command) - return command - - def config_mode_command(self, host, sink, mode): + def config_mode_call(self, host, sink, mode): self.print_message("Mode of " + sink + " to " + mode) - command_base = [ "chmod", mode, sink ] - - if self.host_address(host) == self.hostname: - command = command_base - else: - address = self.ssh_address(host) - command = self.ssh_command(address, command_base) - - return command + command = [ "chmod", mode, sink ] + self.host_call(host, command) def config_update(self, host, name): config = self.config_lookup(host, name) @@ -184,22 +184,13 @@ class host_control(): source = f.split(":")[0] sink = f.split(":")[1] - command = self.config_copy_command(host, source, sink) - - self.print_command(command) - subprocess.call(command) + self.config_copy_call(host, source, sink) if "owner" in config: - command = self.config_owner_command(host, sink, config["owner"]) - - self.print_command(command) - subprocess.call(command) + self.config_owner_call(host, sink, config["owner"]) if "mode" in config: - command = self.config_mode_command(host, sink, str(config["mode"])) - - self.print_command(command) - subprocess.call(command) + self.config_mode_call(host, sink, str(config["mode"])) return 0 @@ -246,8 +237,6 @@ class host_control(): return unit["name"] 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) @@ -256,10 +245,8 @@ class host_control(): 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) + arguments = [ "systemctl", option, unit["systemd-unit"] ] + self.host_call(host, arguments) return 0 @@ -292,12 +279,9 @@ class host_control(): 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"] ] + arguments = [ "journalctl", "-n", "50", "-fu", unit["systemd-unit"] ] - command = self.ssh_command(address, arguments) - - self.print_command(command) - subprocess.call(command) + self.host_call(host, arguments) def unit_update(self, host, name): daemon_reload = False @@ -337,11 +321,9 @@ class host_control(): return 1 container = self.unit_docker_container(unit) - address = self.ssh_address(host) - command = self.ssh_command(address, [ "-t", "docker", "exec", "-ti", container, "sh" ]) + command = [ "-t", "docker", "exec", "-ti", container, "sh" ] - self.print_command(command) - subprocess.call(command) + self.host_call(host, command) def unit_list(self, host): if not "units" in host: @@ -362,26 +344,19 @@ class host_control(): # system def system_info(self, host): - address = self.ssh_address(host) - arguments = [ "uname", "-a" ] - command = self.ssh_command(address, arguments) + command = [ "uname", "-a" ] - self.print_command(command) - subprocess.call(command) + self.host_call(host, command) def system_shell(self, host): - address = self.ssh_address(host) - command = self.ssh_command(address, []) - - self.print_command(command) - subprocess.call(command) + command = [ "sh" ] + self.host_call(host, command) def system_ping(self, host): address = self.host_address(host) command = [ "ping", address ] - self.print_command(command) - subprocess.call(command) + self.command_call(command) def system_update(self, host): address = self.ssh_address(host) @@ -390,23 +365,12 @@ class host_control(): if hook == None: return 1 - arguments = self.hook_command(hook) - if arguments == None: - return 1 - - command = self.ssh_command(address, arguments) - - self.print_command(command) - subprocess.call(command) + self.hook_call(host, hook) def system_restart(self, host): - address = self.ssh_address(host) - arguments = [ "reboot" ] + command = [ "reboot" ] - command = self.ssh_command(address, arguments) - - self.print_command(command) - subprocess.call(command) + self.host_call(host, command) def system_initramfs_update(self, host): address = self.ssh_address(host) @@ -415,48 +379,25 @@ class host_control(): if hook == None: return 1 - arguments = self.hook_command(hook) - if arguments == None: - return 1 - - command = self.ssh_command(address, arguments) - - self.print_command(command) - subprocess.call(command) + self.hook_call(host, hook) 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) + command = [ "systemctl", "status" ] + self.host_call(host, 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) + command = [ "journalctl", "-n", "50", "-fb" ] + self.host_call(host, 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) + arguments = [ "systemctl", "daemon-reload" ] - self.print_command(command) - subprocess.call(command) + self.host_call(host, 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) + self.host_call(host, command) # main -- cgit v1.2.3