4.6 KiB
4.6 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
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 bumped to
4.2with migration from4.1
Pause/Resume Command
- File-based signaling via
/etc/nupst/pauseJSON file nupst pause [--duration 30m|2h|1d]creates pause filenupst resumedeletes pause file- Daemon polls continue but actions are suppressed while paused
- Auto-resume after duration expires
- HTTP API includes pause state in response
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 - Config version: Currently
4.2, migrations run automatically
File Organization
ts/
├── constants.ts # All timing/threshold constants
├── 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.1-to-v4.2.ts # Adds protocol field
└── cli/
└── ... # All handlers use helpers.withPrompt()