feat(cli): Automatically restart running NUPST service after configuration changes in interactive setup

This commit is contained in:
Philipp Kunz 2025-03-25 13:26:27 +00:00
parent 4ad383884c
commit ae8219acf7
3 changed files with 48 additions and 1 deletions

View File

@ -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

View File

@ -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'
}

View File

@ -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<void> {
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