From 7fb0888f42d2967551fd8788a9a1833ef5944b39 Mon Sep 17 00:00:00 2001 From: Craig Comstock Date: Thu, 13 Feb 2025 12:57:06 -0600 Subject: [PATCH] Fixed nt-discovery.sh usage when not in cf-remote repository directory The nt-discovery.sh file was assumed to be in PWD which would almost never be the case except during development. This change required some further refactoring to change put() on UNIX hosts to copy to /tmp since otherwise we don't really know where PWD is reliably. Ticket: ENT-12668 --- MANIFEST.in | 1 + nt-discovery.sh => cf_remote/nt-discovery.sh | 0 cf_remote/remote.py | 29 ++++++++++++++------ cf_remote/ssh.py | 16 +++++------ 4 files changed, 29 insertions(+), 17 deletions(-) create mode 100644 MANIFEST.in rename nt-discovery.sh => cf_remote/nt-discovery.sh (100%) diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..f48a209 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +include cf_remote/nt-discovery.sh diff --git a/nt-discovery.sh b/cf_remote/nt-discovery.sh similarity index 100% rename from nt-discovery.sh rename to cf_remote/nt-discovery.sh diff --git a/cf_remote/remote.py b/cf_remote/remote.py index 7ec8096..13fa7d5 100644 --- a/cf_remote/remote.py +++ b/cf_remote/remote.py @@ -1,7 +1,8 @@ #!/usr/bin/env python3 import sys import re -from os.path import basename +from os.path import basename, dirname, join +from os import stat from collections import OrderedDict from cf_remote.utils import ( @@ -208,8 +209,17 @@ def get_info(host, *, users=None, connection=None): else: data["os"] = "unix" - scp("nt-discovery.sh", host, connection, hide=True) - discovery = parse_envfile(ssh_sudo(connection, "bash nt-discovery.sh")) + cf_remote_dir = dirname(__file__) + script_path = join(cf_remote_dir, "nt-discovery.sh") + stat(script_path) + result = scp( + script_path, + host, + connection, + hide=True, + rename="/tmp/nt-discovery.sh", + ) + discovery = parse_envfile(ssh_sudo(connection, "bash /tmp/nt-discovery.sh")) if discovery is None: programmer_error("Couldn't parse NT discovery file") @@ -255,11 +265,11 @@ def get_info(host, *, users=None, connection=None): @auto_connect def install_package(host, pkg, data, *, connection=None): - print("Installing: '{}' on '{}'".format(pkg, host)) output = None + unix_pkg_path = join("/tmp", pkg) if ".deb" in pkg: - output = ssh_sudo(connection, 'dpkg -i "{}"'.format(pkg), True) + output = ssh_sudo(connection, 'dpkg -i "{}"'.format(unix_pkg_path), True) elif ".msi" in pkg: # Windows is crazy, be careful if you decide to change this; # This needs to work in both powershell and cmd, and in @@ -269,7 +279,9 @@ def install_package(host, pkg, data, *, connection=None): output = ssh_cmd(connection, powershell(r".\{} ; sleep 10".format(pkg)), True) elif ".rpm" in pkg: if "yum" in data["bin"]: - output = ssh_sudo(connection, "yum -y install {}".format(pkg), True) + output = ssh_sudo( + connection, "yum -y install {}".format(unix_pkg_path), True + ) elif "zypper" in data["bin"]: # suse case allow_unsigned = ( "" @@ -278,7 +290,9 @@ def install_package(host, pkg, data, *, connection=None): else "--allow-unsigned-rpm" ) output = ssh_sudo( - connection, "zypper install -y {} {}".format(allow_unsigned, pkg), True + connection, + "zypper install -y {} {}".format(allow_unsigned, unix_pkg_path), + True, ) else: log.error( @@ -478,7 +492,6 @@ def install_host( remote_download=False, trust_keys=None ): - data = get_info(host, connection=connection) if show_info: print_info(data) diff --git a/cf_remote/ssh.py b/cf_remote/ssh.py index f7e6722..ce81ca4 100644 --- a/cf_remote/ssh.py +++ b/cf_remote/ssh.py @@ -34,9 +34,8 @@ def run(self, command, hide=False): result.retcode = result.returncode return result - def put(self, src, hide=False): + def put(self, src, dst, hide=False): src = os.path.abspath(src) - dst = os.path.basename(src) if src != dst: if not hide: print("Local copy: '%s' -> '%s'" % (src, dst)) @@ -92,8 +91,7 @@ def run(self, command, hide=False): results = aramid.execute([ahost], command, echo=(not hide)) return results[ahost][0] - def put(self, src, hide=False): - dst = os.path.basename(src) + def put(self, src, dst, hide=False): ahost = aramid.Host(self.ssh_host, self.ssh_user) results = aramid.put([ahost], src, dst, echo=(not hide)) return results[ahost][0].retcode @@ -171,13 +169,13 @@ def scp(file, remote, connection=None, rename=None, hide=False): else: print_function = log.debug if hide else print print_function("Copying: '%s' to '%s'" % (file, remote)) - connection.put(file, hide=hide) + dst = os.path.join("/tmp", os.path.basename(file)) + connection.put(file, dst, hide=hide) if rename: - file = os.path.basename(file) - if file == rename: + if rename == dst: return 0 - print_function("Renaming '%s' -> '%s' on '%s'" % (file, rename, remote)) - ssh_cmd(connection, "mv %s %s" % (file, rename), hide=hide) + print_function("Renaming '%s' -> '%s' on '%s'" % (rename, dst, remote)) + ssh_cmd(connection, "mv %s %s" % (rename, dst), hide=hide) return 0