@lethevimlet/sshift 1.0.12 → 1.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -302,6 +302,32 @@ function registerSystemEndpoints(app, io) {
|
|
|
302
302
|
const updateScriptPath = path.join(dataDir, '.sshift-update.sh');
|
|
303
303
|
const updateLogPath = path.join(dataDir, '.sshift-update.log');
|
|
304
304
|
|
|
305
|
+
// Detect if running under systemd (moved before script template so the
|
|
306
|
+
// isSystemdManaged and systemdUnit variables are available for interpolation)
|
|
307
|
+
let isSystemdManaged = false;
|
|
308
|
+
let systemdUnit = '';
|
|
309
|
+
try {
|
|
310
|
+
// Check if this process is managed by systemd via cgroup
|
|
311
|
+
const myCgroup = fs.readFileSync(`/proc/${process.pid}/cgroup`, 'utf8');
|
|
312
|
+
if (myCgroup.includes('sshift')) {
|
|
313
|
+
isSystemdManaged = true;
|
|
314
|
+
systemdUnit = 'sshift.service';
|
|
315
|
+
console.log('[UPDATE] Detected systemd-managed process, unit:', systemdUnit);
|
|
316
|
+
} else if (process.ppid === 1) {
|
|
317
|
+
// Parent is init (systemd), check for sshift service
|
|
318
|
+
try {
|
|
319
|
+
require('child_process').execSync('systemctl is-enabled sshift.service 2>/dev/null', { encoding: 'utf8' });
|
|
320
|
+
isSystemdManaged = true;
|
|
321
|
+
systemdUnit = 'sshift.service';
|
|
322
|
+
console.log('[UPDATE] Detected systemd service:', systemdUnit);
|
|
323
|
+
} catch (e) {
|
|
324
|
+
// Not a systemd service
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
} catch (e) {
|
|
328
|
+
// /proc not available (non-Linux), ignore
|
|
329
|
+
}
|
|
330
|
+
|
|
305
331
|
// Build PATH for the update script to ensure npm and node are findable
|
|
306
332
|
const pathDirs = [];
|
|
307
333
|
if (process.env.PATH) pathDirs.push(...process.env.PATH.split(':'));
|
|
@@ -388,14 +414,6 @@ echo "$(date): node path: $NODE_BIN" >> "$LOG"
|
|
|
388
414
|
echo "$(date): sshift fallback: $SSHIFT_FALLBACK" >> "$LOG"
|
|
389
415
|
echo "$(date): systemd_managed: $SYSTEMD_MANAGED" >> "$LOG"
|
|
390
416
|
|
|
391
|
-
if [ "$SYSTEMD_MANAGED" = "1" ]; then
|
|
392
|
-
echo "$(date): Detected systemd-managed service: $SYSTEMD_UNIT" >> "$LOG"
|
|
393
|
-
# Stop the service before updating to prevent systemd from restarting old version
|
|
394
|
-
echo "$(date): Stopping $SYSTEMD_UNIT..." >> "$LOG"
|
|
395
|
-
systemctl stop "$SYSTEMD_UNIT" >> "$LOG" 2>&1 || true
|
|
396
|
-
sleep 1
|
|
397
|
-
fi
|
|
398
|
-
|
|
399
417
|
# Find the current sshift binary before npm changes anything
|
|
400
418
|
CURRENT_SSHIFT="$(command -v sshift 2>/dev/null || which sshift 2>/dev/null || echo "$SSHIFT_FALLBACK")"
|
|
401
419
|
|
|
@@ -422,7 +440,7 @@ rm -f "$MARKER"
|
|
|
422
440
|
|
|
423
441
|
if [ "$SYSTEMD_MANAGED" = "1" ]; then
|
|
424
442
|
echo "$(date): Restarting via systemd" >> "$LOG"
|
|
425
|
-
systemctl
|
|
443
|
+
systemctl restart "$SYSTEMD_UNIT" >> "$LOG" 2>&1
|
|
426
444
|
else
|
|
427
445
|
# Find the sshift binary — prefer PATH lookup since npm just updated it
|
|
428
446
|
SSHIFT_BIN="$(command -v sshift 2>/dev/null || which sshift 2>/dev/null || echo "$SSHIFT_FALLBACK")"
|
|
@@ -444,32 +462,6 @@ fi
|
|
|
444
462
|
console.log('[UPDATE] npm path:', npmBinPath);
|
|
445
463
|
console.log('[UPDATE] sshift restart path:', sshiftBinPath);
|
|
446
464
|
console.log('[UPDATE] Log file:', updateLogPath);
|
|
447
|
-
|
|
448
|
-
// Detect if running under systemd
|
|
449
|
-
let isSystemdManaged = false;
|
|
450
|
-
let systemdUnit = '';
|
|
451
|
-
try {
|
|
452
|
-
const { execSync: execSync2 } = require('child_process');
|
|
453
|
-
// Check if this process is managed by systemd
|
|
454
|
-
const myCgroup = fs.readFileSync(`/proc/${process.pid}/cgroup`, 'utf8');
|
|
455
|
-
if (myCgroup.includes('sshift')) {
|
|
456
|
-
isSystemdManaged = true;
|
|
457
|
-
systemdUnit = 'sshift.service';
|
|
458
|
-
console.log('[UPDATE] Detected systemd-managed process, unit:', systemdUnit);
|
|
459
|
-
} else if (process.ppid === 1) {
|
|
460
|
-
// Parent is init (systemd), check for sshift service
|
|
461
|
-
try {
|
|
462
|
-
execSync2('systemctl is-enabled sshift.service 2>/dev/null', { encoding: 'utf8' });
|
|
463
|
-
isSystemdManaged = true;
|
|
464
|
-
systemdUnit = 'sshift.service';
|
|
465
|
-
console.log('[UPDATE] Detected systemd service:', systemdUnit);
|
|
466
|
-
} catch (e) {
|
|
467
|
-
// Not a systemd service
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
} catch (e) {
|
|
471
|
-
// /proc not available (non-Linux), ignore
|
|
472
|
-
}
|
|
473
465
|
|
|
474
466
|
let spawnFailed = false;
|
|
475
467
|
let spawnCommand, spawnArgs;
|
|
@@ -484,6 +476,23 @@ fi
|
|
|
484
476
|
const updateEnv = { ...process.env };
|
|
485
477
|
updateEnv.PATH = scriptPath;
|
|
486
478
|
|
|
479
|
+
// When running under systemd, spawn the update script in a transient scope
|
|
480
|
+
// so it survives `systemctl restart` of the sshift service. Without this,
|
|
481
|
+
// the script is in the same cgroup as sshift and gets killed when the
|
|
482
|
+
// service restarts.
|
|
483
|
+
if (isSystemdManaged && platform !== 'win32') {
|
|
484
|
+
try {
|
|
485
|
+
const systemdRunCheck = require('child_process').execSync('command -v systemd-run', { encoding: 'utf8' }).trim();
|
|
486
|
+
if (systemdRunCheck) {
|
|
487
|
+
console.log('[UPDATE] Using systemd-run to isolate update script from service');
|
|
488
|
+
spawnCommand = systemdRunCheck;
|
|
489
|
+
spawnArgs = ['--scope', '--unit=sshift-update', '/bin/sh', updateScriptPath];
|
|
490
|
+
}
|
|
491
|
+
} catch (e) {
|
|
492
|
+
// systemd-run not available, fall through to default spawn
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
487
496
|
const updateProcess = spawn(spawnCommand, spawnArgs, {
|
|
488
497
|
cwd: dataDir,
|
|
489
498
|
detached: true,
|
|
@@ -500,23 +509,16 @@ fi
|
|
|
500
509
|
updateProcess.unref();
|
|
501
510
|
|
|
502
511
|
// Give the update script enough time to start before exiting.
|
|
503
|
-
//
|
|
504
|
-
//
|
|
505
|
-
//
|
|
506
|
-
//
|
|
507
|
-
|
|
508
|
-
const exitDelay = isSystemdManaged ? 500 : 2000;
|
|
512
|
+
// Under systemd: we exit so the service can be restarted by `systemctl
|
|
513
|
+
// restart` in the script. The script runs in a separate systemd scope
|
|
514
|
+
// so it survives the service restart.
|
|
515
|
+
// Non-systemd: we exit so the script can exec the new binary.
|
|
516
|
+
const exitDelay = isSystemdManaged ? 1000 : 2000;
|
|
509
517
|
setTimeout(() => {
|
|
510
518
|
if (spawnFailed) {
|
|
511
519
|
console.error('[UPDATE] Update process failed to start. Keeping server running.');
|
|
512
520
|
return;
|
|
513
521
|
}
|
|
514
|
-
if (isSystemdManaged) {
|
|
515
|
-
// Under systemd, the update script will call `systemctl stop` to stop us.
|
|
516
|
-
// We don't need to exit ourselves. Just keep running until systemd stops us.
|
|
517
|
-
console.log('[UPDATE] Update script running under systemd. Service will be stopped by the script.');
|
|
518
|
-
return;
|
|
519
|
-
}
|
|
520
522
|
console.log('[UPDATE] Exiting for update...');
|
|
521
523
|
process.exit(0);
|
|
522
524
|
}, exitDelay);
|