class

Shellmin::StateManager

Inherits Reference < Object

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

STATE_DIR = Path.new(ENV["HOME"], ".config", "shellmin", "state")

State directory path

Constructors

new

Initialize state manager

Source

Instance methods

all(host_name : String, module_name : String) : Hash(String, JSON::Any)

Get all state as a hash

Source
cleanup_old_state_files

Clean up old state files

Source
clear(host_name : String, module_name : String) : Bool

Clear all state for a host and module

Source
get(host_name : String, module_name : String, key : String) : JSON::Any | Nil

Get a value from state

Source
has_key?(host_name : String, module_name : String, key : String) : Bool

Check if a key exists in state

Source
keys(host_name : String, module_name : String) : Array(String)

List all keys in state

Source
load_state(host_name : String, module_name : String) : Hash(String, JSON::Any)

Load state for a specific host and module

Source
remove(host_name : String, module_name : String, key : String) : Bool

Remove a key from state

Source
rollback(host_name : String, module_name : String, version_index : Int32 = 0) : Bool

Rollback to a previous version

Source
save_state(host_name : String, module_name : String, state : Hash(String, JSON::Any)) : Bool

Save state for a specific host and module

Source
set(host_name : String, module_name : String, key : String, value : JSON::Any) : Bool

Set a value in state

Source
state_file_path(host_name : String, module_name : String, format : Format = Format::JSON) : String

Get state file path for a host and module

Source
stats

Get state statistics

Source
versions(host_name : String, module_name : String) : Array(NamedTuple(timestamp: String, data: Hash(String, JSON::Any)))

Get list of versions

Source

Nested types