Skip to content

Commit 7ac2cae

Browse files
committed
Fixed handling of role and agent fields in data
Signed-off-by: Ole Herman Schumacher Elgesem <ole.elgesem@northern.tech>
1 parent 7e23fde commit 7ac2cae

3 files changed

Lines changed: 26 additions & 9 deletions

File tree

cf_remote/commands.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,10 +1056,10 @@ def agent(hosts, bootstrap=None):
10561056
for host in hosts:
10571057
data = get_info(host)
10581058

1059-
if not data["agent_location"]:
1059+
if not data["agent"]:
10601060
raise CFRExitError("CFEngine not installed on {}".format(host))
10611061

1062-
command = "{}".format(data["agent_location"])
1062+
command = "{}".format(data["agent"])
10631063
if bootstrap:
10641064
command += "--bootstrap {}".format(bootstrap[0])
10651065

cf_remote/remote.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,16 +216,30 @@ def get_info(host, *, users=None, connection=None):
216216
data["ssh_user"] = user
217217

218218
systeminfo = ssh_cmd(connection, "systeminfo")
219+
assert systeminfo is None or (type(systeminfo) is str and len(systeminfo) > 0)
220+
# Note: ssh_cmd is supposed to return None on failure however, it seems
221+
# like there are some cases where it returns output on failure
222+
# because the exit code was 0
223+
# TODO: systeminfo has a lot of output normally, looking for
224+
# "command not found" in this output is not very robust.
219225
if systeminfo and "command not found" not in systeminfo:
220226
data["os"] = "windows"
221227
data["systeminfo"] = parse_systeminfo(systeminfo)
222228
data["package_tags"] = ["x86_64", "msi"]
223229
data["arch"] = "x86_64"
224230
agent = r"& 'C:\Program Files\Cfengine\bin\cf-agent.exe'"
225-
data["agent"] = agent
226231
version_cmd = powershell("{} -V".format(agent))
227-
data["agent_version"] = parse_version(ssh_cmd(connection, version_cmd))
232+
version_output = ssh_cmd(connection, version_cmd)
233+
if version_output:
234+
data["agent"] = agent
235+
else:
236+
data["agent"] = None
237+
data["agent_version"] = parse_version(version_output)
228238
data["role"] = "client"
239+
if data["agent_version"]:
240+
data["role"] = "client"
241+
else:
242+
data["role"] = None
229243
else:
230244
data["os"] = "unix"
231245

@@ -270,12 +284,15 @@ def get_info(host, *, users=None, connection=None):
270284

271285
data["package_tags"] = get_package_tags(os_release_data, redhat_release_data)
272286
data["cfengine_id"] = discovery.get("NTD_CFENGINE_ID")
273-
data["agent_location"] = discovery.get("NTD_CFAGENT_PATH")
287+
data["agent"] = discovery.get("NTD_CFAGENT_PATH")
274288
data["policy_server"] = discovery.get("NTD_POLICY_SERVER")
275-
agent = r"/var/cfengine/bin/cf-agent"
276-
data["agent"] = agent
277289
data["agent_version"] = parse_version(discovery.get("NTD_CFAGENT_VERSION"))
278-
data["role"] = "hub" if discovery.get("NTD_CFHUB") else "client"
290+
if discovery.get("NTD_CFHUB"):
291+
data["role"] = "hub"
292+
elif discovery.get("NTD_CFAGENT_PATH"):
293+
data["role"] = "client"
294+
else:
295+
data["role"] = None
279296

280297
data["bin"] = {}
281298
for bin in [

cf_remote/ssh.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def scp(file, remote, connection=None, rename=None, hide=False):
243243
return 0
244244

245245

246-
def ssh_cmd(connection, cmd, errors=False, needs_pty=True):
246+
def ssh_cmd(connection, cmd, errors=False, needs_pty=True) -> str | None:
247247
assert connection
248248

249249
if needs_pty:

0 commit comments

Comments
 (0)