From 0a8a52f33446046bf638b96e9fcb37651fbe086e Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Tue, 25 Mar 2025 09:23:00 +0000 Subject: [PATCH] feat(cli): Add update command to CLI to update NUPST from repository and refresh the systemd service --- changelog.md | 7 ++++ ts/00_commitinfo_data.ts | 2 +- ts/cli.ts | 82 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 448d17a..821811e 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,12 @@ # Changelog +## 2025-03-25 - 1.9.0 - feat(cli) +Add update command to CLI to update NUPST from repository and refresh the systemd service + +- Integrate 'update' subcommand in CLI command parser +- Update documentation and help output to include new command +- Implement update process that fetches changes from git, runs install.sh/setup.sh, and refreshes systemd service if installed + ## 2025-03-25 - 1.8.2 - fix(cli) Refactor logs command to use child_process spawn for real-time log tailing diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 56c8b2c..de1dd36 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: '1.8.2', + version: '1.9.0', description: 'Node.js UPS Shutdown Tool for SNMP-enabled UPS devices' } diff --git a/ts/cli.ts b/ts/cli.ts index 6da118e..6bfdf61 100644 --- a/ts/cli.ts +++ b/ts/cli.ts @@ -90,6 +90,10 @@ export class NupstCli { case 'test': await this.test(debugMode); break; + + case 'update': + await this.update(); + break; case 'help': default: @@ -359,6 +363,7 @@ Usage: nupst status - Show status of the systemd service and UPS status nupst setup - Run the interactive setup to configure SNMP settings nupst test - Test the current configuration by connecting to the UPS + nupst update - Update NUPST from repository and refresh systemd service (requires root) nupst help - Show this help message Options: @@ -367,6 +372,83 @@ Options: `); } + /** + * Update NUPST from repository and refresh systemd service + */ + private async update(): Promise { + try { + // Check if running as root + this.checkRootAccess('This command must be run as root to update NUPST and refresh the systemd service.'); + + console.log('┌─ NUPST Update Process ──────────────────┐'); + console.log('│ Updating NUPST from repository...'); + + // Determine the installation directory (assuming it's either /opt/nupst or the current directory) + const { existsSync } = await import('fs'); + let installDir = '/opt/nupst'; + + if (!existsSync(installDir)) { + // If not installed in /opt/nupst, use the current directory + const { dirname } = await import('path'); + installDir = dirname(dirname(process.argv[1])); // Go up two levels from the executable + console.log(`│ Using local installation directory: ${installDir}`); + } + + try { + // 1. Update the repository + console.log('│ Pulling latest changes from git repository...'); + execSync(`cd ${installDir} && git fetch origin && git reset --hard origin/main`, { stdio: 'pipe' }); + + // 2. Run the install.sh script + console.log('│ Running install.sh to update NUPST...'); + execSync(`cd ${installDir} && bash ./install.sh`, { stdio: 'pipe' }); + + // 3. Run the setup.sh script + console.log('│ Running setup.sh to update dependencies...'); + execSync(`cd ${installDir} && bash ./setup.sh`, { stdio: 'pipe' }); + + // 4. Refresh the systemd service + console.log('│ Refreshing systemd service...'); + + // First check if service exists + const serviceExists = execSync('systemctl list-unit-files | grep nupst.service').toString().includes('nupst.service'); + + if (serviceExists) { + // Stop the service if it's running + const isRunning = execSync('systemctl is-active nupst.service || true').toString().trim() === 'active'; + if (isRunning) { + console.log('│ Stopping nupst service...'); + execSync('systemctl stop nupst.service'); + } + + // Reinstall the service + console.log('│ Reinstalling systemd service...'); + await this.nupst.getSystemd().install(); + + // Restart the service if it was running + if (isRunning) { + console.log('│ Restarting nupst service...'); + execSync('systemctl start nupst.service'); + } + } else { + console.log('│ Systemd service not installed, skipping service refresh.'); + console.log('│ Run "nupst enable" to install the service.'); + } + + console.log('│ Update completed successfully!'); + console.log('└──────────────────────────────────────────┘'); + } catch (error) { + console.error('│ Error during update process:'); + console.error(`│ ${error.message}`); + console.error('└──────────────────────────────────────────┘'); + process.exit(1); + } + } catch (error) { + console.error(`Update failed: ${error.message}`); + process.exit(1); + } + } + /** * Interactive setup for configuring SNMP settings */