Skip to content

Commit

Permalink
Created nt-discovery.sh file
Browse files Browse the repository at this point in the history
Ticket: ENT-12576
Signed-off-by: Victor Moene <[email protected]>
  • Loading branch information
victormlg committed Jan 13, 2025
1 parent 43d720e commit 9d13e08
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 23 deletions.
51 changes: 30 additions & 21 deletions cf_remote/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from cf_remote.utils import (
os_release,
column_print,
parse_envfile,
pretty,
user_error,
parse_systeminfo,
Expand Down Expand Up @@ -205,38 +206,46 @@ def get_info(host, *, users=None, connection=None):
data["agent_version"] = parse_version(ssh_cmd(connection, version_cmd))
else:
data["os"] = "unix"
data["uname"] = ssh_cmd(connection, "uname")
data["arch"] = ssh_cmd(connection, "uname -m")
data["os_release"] = os_release(ssh_cmd(connection, "cat /etc/os-release"))

scp("nt-discovery.sh", host, connection)
discovery = parse_envfile(ssh_sudo(connection, "bash nt-discovery.sh"))

data["uname"] = (
discovery.get("NTD_UNAME")
if discovery.get("NTD_UNAME")
else user_error(discovery["NTD_UNAME_ERROR"])
)
data["arch"] = (
discovery.get("NTD_ARCH")
if discovery.get("NTD_ARCH")
else user_error(discovery["NTD_ARCH_ERROR"])
)
data["os_release"] = (
os_release(discovery.get("NTD_OS_RELEASE"))
if discovery.get("NTD_OS_RELEASE")
else user_error(discovery["NTD_OS_RELEASE_ERROR"])
)

os_release_data = data.get("os_release")
redhat_release_data = None
if not os_release_data:
redhat_release_data = ssh_cmd(connection, "cat /etc/redhat-release")
redhat_release_data = (
discovery.get("NTD_REDHAT_RELEASE")
if discovery.get("NTD_REDHAT_RELEASE")
else user_error(discovery["NTD_REDHAT_RELEASE_ERROR"])
)
data["redhat_release"] = redhat_release_data

data["package_tags"] = get_package_tags(os_release_data, redhat_release_data)

data["agent_location"] = ssh_cmd(connection, "command -v cf-agent")
data["policy_server"] = ssh_cmd(
connection, "cat /var/cfengine/policy_server.dat"
)
if user != "root" and not data["policy_server"]:
# If we are not SSHing as root and we failed to read
# the policy_server.dat file try again using sudo:
data["policy_server"] = ssh_sudo(
connection, "cat /var/cfengine/policy_server.dat"
)

data["agent_location"] = discovery.get("NTD_CFAGENT_PATH")
data["policy_server"] = discovery.get("NTD_POLICY_SERVER")
agent = r"/var/cfengine/bin/cf-agent"
data["agent"] = agent
data["agent_version"] = parse_version(
ssh_cmd(connection, "{} --version".format(agent))
)
data["agent_version"] = parse_version(discovery.get("NTD_CFAGENT_VERSION"))

data["bin"] = {}
for bin in ["dpkg", "rpm", "yum", "apt", "pkg", "zypper"]:
path = ssh_cmd(connection, "command -v {}".format(bin))
for bin in ["DPKG", "RPM", "YUM", "APT", "PKG", "ZYPPER"]:
path = discovery.get("NTD_{}".format(bin))
if path:
data["bin"][bin] = path

Expand Down
15 changes: 13 additions & 2 deletions cf_remote/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ def os_release(inp):
log.debug("Cannot parse os-release file (empty)")
return None
d = OrderedDict()
for line in inp.splitlines():
for line in inp.split(r"\n"):
line = line.strip()
if "=" not in line:
continue
key, sep, value = line.partition("=")
assert "=" not in key
if len(value) > 1 and value[0] == value[-1] and value[0] in ["'", '"']:
value = value[1:-1]
d[key] = value
d[key] = value.strip(r"\"")
return d


Expand Down Expand Up @@ -222,3 +222,14 @@ def is_different_checksum(checksum, content):

digest = hashlib.sha256(content).digest().hex()
return checksum != digest


def parse_envfile(text):

data = OrderedDict()
lines = text.split("\n")
for line in lines:
key, _, val = line.partition("=")
data[key] = val.strip(r"\"")

return data
54 changes: 54 additions & 0 deletions nt-discovery.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash

run_command() {
# $1: command to run
# $2: variable name to store in / output
# $3: custom error message (optional)
result="$(bash -c "$1" 2>&1)"
status=$?
if [ "$status" -eq "0" ]; then
echo "NTD_$2=\"$result\"" | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\\n/g' | sed -e 's/\"/\\"/g'
else
echo "NTD_$2_CMD=\"$1\"" | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\\n/g' | sed -e 's/\"/\\"/g'
# custom output result
if [ "$#" -eq "3" ]; then
echo "NTD_$2_ERROR=\"$3\"" | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\\n/g' | sed -e 's/\"/\\"/g'
else
echo "NTD_$2_ERROR=\"$result\"" | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\\n/g' | sed -e 's/\"/\\"/g'
fi

fi
}

run_command "uname" "UNAME"
run_command "uname -m" "ARCH"
run_command "cat /etc/os-release" "OS_RELEASE"
run_command "cat /etc/redhat-release" "REDHAT_RELEASE"

# cf-agent

cfagent_path=$(command -v cf-agent)

if ! [ $? -eq "0" ]; then
cfagent_path=$(command -v /var/cfengine/bin/cf-agent)

if ! [ $? -eq "0" ]; then
cfagent_path="cf-agent"
fi
fi

run_command "command -v $cfagent_path" "CFAGENT_PATH"
run_command "$cfagent_path --version" "CFAGENT_VERSION"
run_command "cat /var/cfengine/policy_server.dat" "POLICY_SERVER"

# packages

run_command "echo $UID" "UID"
run_command "command -v dpkg" "DPKG"
run_command "command -v rpm" "RPM"
run_command "command -v yum" "YUM"
run_command "command -v apt" "APT"
run_command "command -v pkg" "PKG"
run_command "command -v zypper" "ZYPPER"


0 comments on commit 9d13e08

Please sign in to comment.