Skip to content

Commit

Permalink
Fixed nt-discovery.sh usage when not in cf-remote repository directory
Browse files Browse the repository at this point in the history
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
  • Loading branch information
craigcomstock committed Feb 14, 2025
1 parent 3cf65ef commit 7fb0888
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include cf_remote/nt-discovery.sh
File renamed without changes.
29 changes: 21 additions & 8 deletions cf_remote/remote.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand All @@ -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 = (
""
Expand All @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 7 additions & 9 deletions cf_remote/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down

0 comments on commit 7fb0888

Please sign in to comment.