Skip to content

Commit cba1151

Browse files
committed
fix(setup): use sudo -u unconditionally and resolve pm2 via mise exec
- asUser and the deploy-dir chown built \`\$SUDO -u ...\` which reduces to a broken \`-u ...\` command when SSH is already root (EUID=0 sets SUDO=""). sudo is transparent under root, so unconditional \`sudo -u\` works for both entry paths without leaving \`-u\` naked. - \`pm2 startup\` targeted \`~/.local/share/mise/shims/pm2\` which fails with "not a valid shim" because mise only shims tools it manages itself, not npm-installed globals like pm2. Resolve pm2's real path via \`mise exec node@X -- which pm2\` under the deploy user, then invoke that binary with an env PATH pointing at the same directory so pm2's spawn of node resolves.
1 parent c184fd8 commit cba1151

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

src/cli/commands/setup.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,14 @@ async function bootstrapDeployUser(
7474
/**
7575
* Wrap a command so it runs as `user` when set, otherwise as the current SSH user.
7676
* `bash -lc` gives the target user a login shell so $HOME/$PATH resolve to theirs.
77+
* Uses `sudo -u` unconditionally — sudo is transparent (no password) when the SSH
78+
* user is already root, and the conditional `$SUDO` fallback breaks on `sudo -u`
79+
* because it leaves `-u` as the leading token when SUDO="".
7780
*/
7881
function asUser(user: string | null, cmd: string): string {
7982
if (!user) return cmd;
8083
const escaped = cmd.replace(/'/g, "'\\''");
81-
return `SUDO=""; [ "$EUID" -ne 0 ] && SUDO="sudo"; $SUDO -u "${user}" bash -lc '${escaped}'`;
84+
return `sudo -u "${user}" bash -lc '${escaped}'`;
8285
}
8386

8487
function buildTasks(executor: RemoteExecutor, config: ShipnodeConfig, ownerUser: string | null) {
@@ -151,12 +154,15 @@ function buildTasks(executor: RemoteExecutor, config: ShipnodeConfig, ownerUser:
151154
// pm2 startup writes /etc/systemd/system/pm2-<user>.service — needs root
152155
// to write the unit + enable it, but the unit targets the deploy user.
153156
// pm2 save writes ~/.pm2/dump.pm2 for that user and must run as them.
157+
//
158+
// npm-installed globals like pm2 don't get valid mise shims (mise only
159+
// shims tools it manages itself), so we resolve pm2's real path via
160+
// `mise exec node@X -- which pm2` and pass that to `env PATH=... pm2`.
154161
title: `Configure systemd startup (${ownerUser ?? '$USER'})`,
155162
task: () => executor.execOrThrow(
156163
(ownerUser
157-
? `SUDO=""; [ "$EUID" -ne 0 ] && SUDO="sudo"; ` +
158-
`PM2_BIN="${targetHome}/.local/share/mise/shims/pm2"; ` +
159-
`$SUDO env PATH="${targetHome}/.local/share/mise/shims:$PATH" "$PM2_BIN" startup systemd -u "${ownerUser}" --hp "${targetHome}" || true`
164+
? `PM2_BIN=$(sudo -u "${ownerUser}" bash -lc '${mise}; mise exec "node@${nodeVersion}" -- which pm2'); ` +
165+
`[ -n "$PM2_BIN" ] && env PATH="$(dirname "$PM2_BIN"):$PATH" "$PM2_BIN" startup systemd -u "${ownerUser}" --hp "${targetHome}" || true`
160166
: `${mise}; mise exec "node@${nodeVersion}" -- pm2 startup systemd -u $USER --hp $HOME || true`) +
161167
` && ` +
162168
asUser(
@@ -228,8 +234,10 @@ function buildTasks(executor: RemoteExecutor, config: ShipnodeConfig, ownerUser:
228234
task: () => executor.execOrThrow(
229235
'SUDO=""; [ "$EUID" -ne 0 ] && SUDO="sudo"; ' +
230236
`$SUDO mkdir -p "${config.remotePath}" && ` +
231-
(ownerUser ? `$SUDO chown "${ownerUser}:${ownerUser}" "${config.remotePath}" && ` : '') +
232-
`$SUDO -u "${ownerUser ?? '$USER'}" mkdir -p "${config.remotePath}/releases" "${config.remotePath}/shared" "${config.remotePath}/.shipnode"`,
237+
(ownerUser
238+
? `$SUDO chown "${ownerUser}:${ownerUser}" "${config.remotePath}" && ` +
239+
`sudo -u "${ownerUser}" mkdir -p "${config.remotePath}/releases" "${config.remotePath}/shared" "${config.remotePath}/.shipnode"`
240+
: `mkdir -p "${config.remotePath}/releases" "${config.remotePath}/shared" "${config.remotePath}/.shipnode"`),
233241
),
234242
},
235243
{

0 commit comments

Comments
 (0)