From ae8219acf7b3591d07c87318dccf3bb62ec609ca Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Tue, 25 Mar 2025 13:26:27 +0000 Subject: [PATCH] feat(cli): Automatically restart running NUPST service after configuration changes in interactive setup --- changelog.md | 6 ++++++ ts/00_commitinfo_data.ts | 2 +- ts/cli.ts | 41 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index fb41c35..cc5f456 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,11 @@ # Changelog +## 2025-03-25 - 2.5.0 - feat(cli) +Automatically restart running NUPST service after configuration changes in interactive setup + +- Added restartServiceIfRunning() to check and restart the service if it's active. +- Invoked the restart function post-setup to apply configuration changes immediately. + ## 2025-03-25 - 2.4.8 - fix(installer) Improve Git dependency handling and repository cloning in install.sh diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 4be00eb..2b36426 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@serve.zone/nupst', - version: '2.4.8', + version: '2.5.0', description: 'Node.js UPS Shutdown Tool for SNMP-enabled UPS devices' } diff --git a/ts/cli.ts b/ts/cli.ts index 11633ca..9d051d3 100644 --- a/ts/cli.ts +++ b/ts/cli.ts @@ -533,6 +533,9 @@ Options: // Test the connection if requested await this.optionallyTestConnection(config, prompt); + // Check if service is running and restart it if needed + await this.restartServiceIfRunning(); + console.log('\nSetup complete!'); await this.optionallyEnableService(prompt); } @@ -833,6 +836,44 @@ Options: } } + /** + * Check if the systemd service is running and restart it if it is + * This is useful after configuration changes + */ + private async restartServiceIfRunning(): Promise { + try { + // Check if the service is active + const isActive = execSync('systemctl is-active nupst.service || true').toString().trim() === 'active'; + + if (isActive) { + // Service is running, restart it + console.log('┌─ Service Update ─────────────────────────┐'); + console.log('│ Configuration has changed.'); + console.log('│ Restarting NUPST service to apply changes...'); + + try { + if (process.getuid && process.getuid() === 0) { + // We have root access, restart directly + execSync('systemctl restart nupst.service'); + console.log('│ Service restarted successfully.'); + } else { + // No root access, show instructions + console.log('│ Please restart the service with:'); + console.log('│ sudo systemctl restart nupst.service'); + } + } catch (error) { + console.log(`│ Error restarting service: ${error.message}`); + console.log('│ You may need to restart the service manually:'); + console.log('│ sudo systemctl restart nupst.service'); + } + + console.log('└──────────────────────────────────────────┘'); + } + } catch (error) { + // Ignore errors checking service status + } + } + /** * Optionally enable and start systemd service * @param prompt Function to prompt for user input