Shellmin::StateManager
State Management System
Provides persistent key-value storage for scripts with per-server, per-module isolation. State is stored as JSON files in ~/.config/shellmin/state/ with the format: {host_name}.{module_name}.json
Features
- Per-server isolation: Each host has its own state namespace
- Per-module isolation: Each module has its own state within a host
- JSON persistence: Human-readable JSON format for state files
- Thread-safe operations: File locking prevents race conditions
- Automatic cleanup: Old state files are cleaned up periodically
Usage in Scripts
# Set a state value
shellmin state set installed_version "1.2.3"
# Get a state value
version=$(shellmin state get installed_version)
# Check if state key exists
if shellmin state has installed_version; then
echo "Already installed version $version"
fi
# List all state keys
shellmin state list
# Remove a state key
shellmin state remove installed_version
State File Structure
{
"installed_version": "1.2.3",
"last_updated": "2024-01-15T10:30:00Z",
"configuration": {
"port": 8080,
"ssl": true
}
}
Constants
MAX_STATE_AGE = 30.days
Maximum age for state files (30 days)
MAX_VERSIONS = 10
Maximum number of versions to keep
Constructors
Instance methods
Get all state as a hash
Get a value from state
Check if a key exists in state
Load state for a specific host and module
Rollback to a previous version
Save state for a specific host and module
Set a value in state
Get state file path for a host and module