From 8c7bb1bfef625305f9bac23b7aed70ff15753668 Mon Sep 17 00:00:00 2001 From: Paul Kocialkowski Date: Fri, 21 Apr 2023 23:13:44 +0200 Subject: host-control: Add config import and diff Signed-off-by: Paul Kocialkowski --- host-control | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/host-control b/host-control index 9540b6b..58f311d 100755 --- a/host-control +++ b/host-control @@ -34,6 +34,8 @@ class host_control(): " 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" \ @@ -145,7 +147,7 @@ class host_control(): self.print_message("No config for " + name + " in host: " + self.host) return None - def config_copy_call(self, host, source, sink): + def config_update_call(self, host, source, sink): source_path = os.path.join(self.config_path(), source) if self.host_address(host) == self.hostname: @@ -160,6 +162,40 @@ class host_control(): 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) @@ -184,7 +220,7 @@ class host_control(): source = f.split(":")[0] sink = f.split(":")[1] - self.config_copy_call(host, source, sink) + self.config_update_call(host, source, sink) if "owner" in config: self.config_owner_call(host, sink, config["owner"]) @@ -194,6 +230,34 @@ class host_control(): return 0 + def config_import(self, host, name): + config = self.config_lookup(host, name) + if config is None: + return 1 + + if not self.shell: + 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 + + if not self.shell: + 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) @@ -483,6 +547,10 @@ class host_control(): 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": -- cgit v1.2.3