feat(cli): Automatically restart running NUPST service after configuration changes in interactive setup
This commit is contained in:
		| @@ -1,5 +1,11 @@ | |||||||
| # Changelog | # 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) | ## 2025-03-25 - 2.4.8 - fix(installer) | ||||||
| Improve Git dependency handling and repository cloning in install.sh | Improve Git dependency handling and repository cloning in install.sh | ||||||
|  |  | ||||||
|   | |||||||
| @@ -3,6 +3,6 @@ | |||||||
|  */ |  */ | ||||||
| export const commitinfo = { | export const commitinfo = { | ||||||
|   name: '@serve.zone/nupst', |   name: '@serve.zone/nupst', | ||||||
|   version: '2.4.8', |   version: '2.5.0', | ||||||
|   description: 'Node.js UPS Shutdown Tool for SNMP-enabled UPS devices' |   description: 'Node.js UPS Shutdown Tool for SNMP-enabled UPS devices' | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										41
									
								
								ts/cli.ts
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								ts/cli.ts
									
									
									
									
									
								
							| @@ -533,6 +533,9 @@ Options: | |||||||
|     // Test the connection if requested |     // Test the connection if requested | ||||||
|     await this.optionallyTestConnection(config, prompt); |     await this.optionallyTestConnection(config, prompt); | ||||||
|      |      | ||||||
|  |     // Check if service is running and restart it if needed | ||||||
|  |     await this.restartServiceIfRunning(); | ||||||
|  |      | ||||||
|     console.log('\nSetup complete!'); |     console.log('\nSetup complete!'); | ||||||
|     await this.optionallyEnableService(prompt); |     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 |    * Optionally enable and start systemd service | ||||||
|    * @param prompt Function to prompt for user input |    * @param prompt Function to prompt for user input | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user