7.7 KiB
7.7 KiB
NUPST Project Hints
Recent Refactoring (January 2026)
Phase 1 - Quick Wins
-
Prompt Utility (
ts/helpers/prompt.ts)- Extracted readline/prompt pattern from all CLI handlers
- Provides
createPrompt()andwithPrompt()helper functions - Used in:
ups-handler.ts,group-handler.ts,service-handler.ts,action-handler.ts,feature-handler.ts
-
Constants File (
ts/constants.ts)- Centralized all magic numbers (timeouts, intervals, thresholds)
- Contains:
TIMING,SNMP,THRESHOLDS,WEBHOOK,SCRIPT,SHUTDOWN,HTTP_SERVER,UI,NETWORK,UPSD,PAUSE,PROXMOX - Used in:
daemon.ts,snmp/manager.ts,actions/*.ts,upsd/client.ts
-
Logger Consistency
- Replaced all
console.log/console.errorinsnmp/manager.tswith properlogger.*calls - Debug output uses
logger.dim()for less intrusive output
- Replaced all
Phase 2 - Type Safety
-
Circular Dependency Fix (
ts/interfaces/nupst-accessor.ts)- Created
INupstAccessorinterface to break the circular dependency betweenNupstandNupstSnmp NupstSnmp.nupstproperty now uses the interface instead ofany
- Created
-
Webhook Payload Interface (
ts/actions/webhook-action.ts)- Added
IWebhookPayloadinterface for webhook action payloads - Exported from
ts/actions/index.ts
- Added
-
CLI Handler Type Safety
- Replaced
anytypes inups-handler.tsandgroup-handler.tswith proper interfaces - Uses:
IUpsConfig,INupstConfig,ISnmpConfig,IActionConfig,IThresholds,ISnmpUpsStatus
- Replaced
-
SNMP Manager Boundary Types (
ts/snmp/manager.ts)- Added local wrapper interfaces for the untyped
net-snmppackage surface used by NUPST - SNMP metric reads now coerce values explicitly instead of relying on
any-typed responses
- Added local wrapper interfaces for the untyped
Features Added (February 2026)
Network Loss Handling
TPowerStatusextended with'unreachable'stateIUpsStatushasconsecutiveFailuresandunreachableSincetracking- After
NETWORK.CONSECUTIVE_FAILURE_THRESHOLD(3) failures, UPS transitions tounreachable - Shutdown action explicitly won't fire on
unreachable(prevents false shutdowns) - Recovery is logged when UPS comes back from unreachable
UPSD/NIS Protocol Support
- New
ts/upsd/directory with TCP client for NUT (Network UPS Tools) servers ts/protocol/directory withProtocolResolverfor protocol-agnostic status queriesIUpsConfig.protocolfield:'snmp'(default) or'upsd'IUpsConfig.snmpis now optional (not needed for UPSD devices)- CLI supports protocol selection during
nupst ups add - Config version is now
4.3, including the4.2->4.3runtime unit migration
Pause/Resume Command
- File-based signaling via
/etc/nupst/pauseJSON file nupst pause [--duration 30m|2h|1d]creates pause filenupst resumedeletes pause filets/pause-state.tsowns pause snapshot parsing and transition detection for daemon polling- Daemon polls continue but actions are suppressed while paused
- Auto-resume after duration expires
- HTTP API includes pause state in response
Shutdown Orchestration
ts/shutdown-executor.tsowns command discovery and fallback execution for delayed and emergency shutdownsts/daemon.tsnow delegates OS shutdown execution instead of embedding command lookup logic inlinedefaultShutdownDelayin config provides the inherited delay for shutdown actions without an explicitshutdownDelayoverride
Config Watch Handling
ts/config-watch.tsowns file-watch event matching and config-reload transition analysists/daemon.tsnow delegates config/pause watch event classification and reload messaging decisions
UPS Status Tracking
ts/ups-status.tsowns the daemon UPS status shape and default status factoryts/daemon.tsnow reuses a shared initializer instead of duplicating the default UPS status object
UPS Monitoring Transitions
ts/ups-monitoring.tsowns pure UPS poll success/failure transition logic and threshold detectionts/daemon.tsnow orchestrates protocol calls and logging while delegating state transitions
Action Orchestration
ts/action-orchestration.tsowns action context construction and action execution decisionsts/daemon.tsnow delegates pause suppression, legacy shutdown fallback, and action context building
Shutdown Monitoring
ts/shutdown-monitoring.tsowns shutdown-loop row building and emergency candidate selectionts/daemon.tsnow keeps the shutdown loop orchestration while delegating row/emergency decisions
Proxmox VM Shutdown Action
- New action type
'proxmox'ints/actions/proxmox-action.ts - Uses Proxmox REST API with PVEAPIToken authentication
- Shuts down QEMU VMs and LXC containers before host shutdown
- Supports: exclude IDs, configurable timeout, force-stop, TLS skip for self-signed certs
- Should be placed BEFORE shutdown actions in the action chain
Architecture Notes
- SNMP Manager: Uses
INupstAccessorinterface (not directNupstreference) to avoid circular imports - Protocol Resolver: Routes to SNMP or UPSD based on
IUpsConfig.protocol - CLI Handlers: All use the
helpers.withPrompt()utility for interactive input - Constants: All timing values should be referenced from
ts/constants.ts - Actions: Use
IActionConfigfromts/actions/base-action.tsfor action configuration - Action orchestration: Use helpers from
ts/action-orchestration.tsfor action context and execution decisions - Config watch logic: Use helpers from
ts/config-watch.tsfor file event filtering and reload transitions - Pause state: Use
loadPauseSnapshot()andIPauseStatefromts/pause-state.ts - Shutdown execution: Use
ShutdownExecutorfor OS-level shutdown command lookup and fallbacks - Shutdown monitoring: Use helpers from
ts/shutdown-monitoring.tsfor emergency loop rows and candidate selection - UPS status state: Use
IUpsStatusandcreateInitialUpsStatus()fromts/ups-status.ts - UPS poll transitions: Use helpers from
ts/ups-monitoring.tsfor success/failure updates - Config version: Currently
4.3, migrations run automatically
File Organization
ts/
├── constants.ts # All timing/threshold constants
├── action-orchestration.ts # Action context and execution decisions
├── config-watch.ts # File watch filters and config reload transitions
├── shutdown-monitoring.ts # Shutdown loop rows and emergency selection
├── ups-monitoring.ts # Pure UPS poll transition and threshold helpers
├── pause-state.ts # Shared pause state types and transition detection
├── shutdown-executor.ts # Delayed/emergency shutdown command execution
├── ups-status.ts # Daemon UPS status shape and initializer
├── interfaces/
│ └── nupst-accessor.ts # Interface to break circular deps
├── helpers/
│ ├── prompt.ts # Readline utility
│ └── shortid.ts # ID generation
├── actions/
│ ├── base-action.ts # Base action class, IActionConfig, TPowerStatus
│ ├── webhook-action.ts # Includes IWebhookPayload
│ ├── proxmox-action.ts # Proxmox VM/LXC shutdown
│ └── ...
├── upsd/
│ ├── types.ts # IUpsdConfig
│ ├── client.ts # NupstUpsd TCP client
│ └── index.ts
├── protocol/
│ ├── types.ts # TProtocol = 'snmp' | 'upsd'
│ ├── resolver.ts # ProtocolResolver
│ └── index.ts
├── migrations/
│ ├── migration-runner.ts
│ └── migration-v4.2-to-v4.3.ts # Adds SNMP runtimeUnit defaults
└── cli/
└── ... # All handlers use helpers.withPrompt()