Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 63 additions & 3 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,54 @@ upstream_openshell_gateway_user_service_installed() {
|| [[ -f /lib/systemd/user/openshell-gateway.service ]]
}

openshell_user_manager_unavailable_reason() {
# Keep in sync with userManagerLooksUnavailable in
# src/lib/onboard/docker-driver-gateway-service.ts.
local reason="${1:-}"
grep -qiE 'Failed to connect to bus|No medium found|XDG_RUNTIME_DIR|System has not been booted|Host is down' \
<<<"$reason"
}

upstream_openshell_gateway_user_manager_unavailable() {
local systemctl_stderr
if systemctl_stderr="$(systemctl --user show openshell-gateway.service --property=ExecStart --value 2>&1 >/dev/null)"; then
return 1
fi
openshell_user_manager_unavailable_reason "$systemctl_stderr"
}

upstream_openshell_gateway_user_service_activation_link_installed() {
local unit_dir link data_home="${XDG_DATA_HOME:-}"
if [[ "$data_home" != /* ]]; then
data_home="${HOME}/.local/share"
fi
# Persistent directories the systemd user manager honors for *.wants/
# *.requires enablement links. The manager resolves its own config home from
# the login environment, not this shell, so ~/.config is always probed even
# when the installer runs with XDG_CONFIG_HOME pointing elsewhere.
local -a unit_dirs=(
"$(openshell_user_config_home)/systemd/user"
"${HOME}/.config/systemd/user"
"${data_home}/systemd/user"
/etc/xdg/systemd/user
/etc/systemd/user
/usr/local/lib/systemd/user
/usr/lib/systemd/user
/lib/systemd/user
)
for unit_dir in "${unit_dirs[@]}"; do
# Quoted head keeps glob metacharacters in the directory path literal;
# only the *.wants/*.requires tail globs. -L also counts dangling links.
for link in "${unit_dir}"/*.wants/openshell-gateway.service \
"${unit_dir}"/*.requires/openshell-gateway.service; do
if [[ -e "$link" || -L "$link" ]]; then
return 0
fi
done
done
return 1
}

resolve_openshell_gateway_bin_for_user_service() {
local service_name="${1:-}" exec_start gateway_bin
local -a gateway_bins=()
Expand Down Expand Up @@ -1448,9 +1496,21 @@ install_nemoclaw_openshell_gateway_user_service() {
if [[ -f "$service_path" ]] && ! is_nemoclaw_openshell_gateway_user_service "$service_path"; then
error "Refusing to replace non-NemoClaw OpenShell gateway user service: $service_path"
fi
require_compatible_upstream_openshell_gateway_service
info "OpenShell upstream gateway user service is staged; onboarding will select and start it."
return 0
# A packaged unit file alone does not prove the user service is
# inspectable: without a systemd user session the effective ExecStart
# cannot be read at all. Fall back to the NemoClaw-managed lifecycle only
# when the static unit also cannot activate later and compete for the
# gateway port; keep every other failure fail-closed.
if upstream_openshell_gateway_user_manager_unavailable; then
if upstream_openshell_gateway_user_service_activation_link_installed; then
error "The systemd user manager is unavailable, but an enabled OpenShell gateway user unit can activate later and compete for gateway port 8080. Start a systemd user session and disable the unit (systemctl --user disable openshell-gateway.service), or remove its enablement links, then rerun the installer."
fi
info "The systemd user manager is unavailable and the packaged OpenShell gateway user unit is not enabled; staging the NemoClaw-managed gateway service instead."
else
require_compatible_upstream_openshell_gateway_service
info "OpenShell upstream gateway user service is staged; onboarding will select and start it."
return 0
fi
fi

local gateway_bin
Expand Down
124 changes: 124 additions & 0 deletions test/install-openshell-gateway-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,130 @@ describe("install.sh OpenShell gateway service", () => {
expect(fs.existsSync(servicePath(home))).toBe(false);
});

it("stages the managed service past a static upstream unit when the user manager is unreachable (#8926)", () => {
const home = makeTempRoot();
const gatewayBin = userGatewayBin(home);
const systemctlBin = path.join(home, "systemctl-bin");
fs.mkdirSync(systemctlBin);
writeExecutable(
path.join(systemctlBin, "systemctl"),
"#!/usr/bin/env bash\nprintf 'Failed to connect to bus: No medium found\\n' >&2\nexit 1\n",
);

const result = runInstallHelper(
home,
[
"upstream_openshell_gateway_user_service_installed() { return 0; }",
"upstream_openshell_gateway_user_service_activation_link_installed() { return 1; }",
`resolve_openshell_gateway_bin_for_service() { printf '%s\\n' ${JSON.stringify(gatewayBin)}; }`,
"install_nemoclaw_openshell_gateway_user_service",
].join("\n"),
{ PATH: `${systemctlBin}:${path.dirname(process.execPath)}:${TEST_SYSTEM_PATH}` },
);

expect(result.status, result.stdout + result.stderr).toBe(0);
expect(result.stdout).toContain("systemd user manager is unavailable");
expect(result.stdout).not.toContain("upstream gateway user service is staged");
expect(result.stderr).not.toContain("Could not locate the gateway binary");
expect(fs.readFileSync(servicePath(home), "utf-8")).toContain(
"# NEMOCLAW_MANAGED_OPENSHELL_GATEWAY=1",
);
});

it("stops before onboarding when the user manager is unreachable but the upstream unit is enabled (#8926)", () => {
const home = makeTempRoot();
const gatewayBin = userGatewayBin(home);
const systemctlBin = path.join(home, "systemctl-bin");
const wantsDir = path.join(home, ".config", "systemd", "user", "default.target.wants");
fs.mkdirSync(systemctlBin);
fs.mkdirSync(wantsDir, { recursive: true });
fs.symlinkSync(
"/usr/lib/systemd/user/openshell-gateway.service",
path.join(wantsDir, "openshell-gateway.service"),
);
writeExecutable(
path.join(systemctlBin, "systemctl"),
"#!/usr/bin/env bash\nprintf 'Failed to connect to bus: No medium found\\n' >&2\nexit 1\n",
);

const result = runInstallHelper(
home,
[
"upstream_openshell_gateway_user_service_installed() { return 0; }",
`resolve_openshell_gateway_bin_for_service() { printf '%s\\n' ${JSON.stringify(gatewayBin)}; }`,
"install_nemoclaw_openshell_gateway_user_service",
].join("\n"),
{ PATH: `${systemctlBin}:${path.dirname(process.execPath)}:${TEST_SYSTEM_PATH}` },
);

expect(result.status).toBe(1);
expect(result.stderr).toContain("enabled OpenShell gateway user unit");
expect(result.stderr).toContain("Start a systemd user session");
expect(result.stdout).not.toContain("upstream gateway user service is staged");
expect(fs.existsSync(servicePath(home))).toBe(false);
});

it("detects an enabled upstream unit under a config home with glob metacharacters (#8926)", () => {
const home = makeTempRoot();
const configHome = path.join(home, "[cfg]");
const gatewayBin = userGatewayBin(home);
const systemctlBin = path.join(home, "systemctl-bin");
const wantsDir = path.join(configHome, "systemd", "user", "default.target.wants");
fs.mkdirSync(systemctlBin);
fs.mkdirSync(wantsDir, { recursive: true });
fs.symlinkSync(
"/usr/lib/systemd/user/openshell-gateway.service",
path.join(wantsDir, "openshell-gateway.service"),
);
writeExecutable(
path.join(systemctlBin, "systemctl"),
"#!/usr/bin/env bash\nprintf 'Failed to connect to bus: No medium found\\n' >&2\nexit 1\n",
);

const result = runInstallHelper(
home,
[
"upstream_openshell_gateway_user_service_installed() { return 0; }",
`resolve_openshell_gateway_bin_for_service() { printf '%s\\n' ${JSON.stringify(gatewayBin)}; }`,
"install_nemoclaw_openshell_gateway_user_service",
].join("\n"),
{
PATH: `${systemctlBin}:${path.dirname(process.execPath)}:${TEST_SYSTEM_PATH}`,
XDG_CONFIG_HOME: configHome,
},
);

expect(result.status).toBe(1);
expect(result.stderr).toContain("enabled OpenShell gateway user unit");
expect(fs.existsSync(servicePath(home, configHome))).toBe(false);
});

it("still fails closed when the upstream service query fails without a manager-unavailable diagnostic (#8926)", () => {
const home = makeTempRoot();
const gatewayBin = userGatewayBin(home);
const systemctlBin = path.join(home, "systemctl-bin");
fs.mkdirSync(systemctlBin);
writeExecutable(
path.join(systemctlBin, "systemctl"),
"#!/usr/bin/env bash\nprintf 'unexpected systemctl failure\\n' >&2\nexit 1\n",
);

const result = runInstallHelper(
home,
[
"upstream_openshell_gateway_user_service_installed() { return 0; }",
`resolve_openshell_gateway_bin_for_service() { printf '%s\\n' ${JSON.stringify(gatewayBin)}; }`,
"trusted_openshell_gateway_bin_for_service() { return 0; }",
"install_nemoclaw_openshell_gateway_user_service",
].join("\n"),
{ PATH: `${systemctlBin}:${path.dirname(process.execPath)}:${TEST_SYSTEM_PATH}` },
);

expect(result.status).toBe(1);
expect(result.stderr).toContain("Could not locate the gateway binary");
expect(fs.existsSync(servicePath(home))).toBe(false);
});

it("resolves the gateway from the effective upstream ExecStart (#8051)", () => {
const home = makeTempRoot();
const conventionalGatewayBin = path.join(home, "usr", "bin", "openshell-gateway");
Expand Down
Loading