Skip to content

Latest commit

Β 

History

History
259 lines (192 loc) Β· 6.22 KB

File metadata and controls

259 lines (192 loc) Β· 6.22 KB

CLI Modules

SwarmCLI modules reference for developers.

Architecture

bin/
β”œβ”€β”€ swarm.sh              # Entry point
└── lib/
    β”œβ”€β”€ core.sh           # Core: logging, retry
    β”œβ”€β”€ args.sh            # Argument parsing
    β”œβ”€β”€ router.sh          # Command routing
    β”œβ”€β”€ profiles.sh        # Profile operations
    β”œβ”€β”€ yaml.sh            # YAML parser
    β”œβ”€β”€ signals.sh         # Graceful shutdown
    β”œβ”€β”€ git.sh             # Git operations
    β”œβ”€β”€ docker.sh          # Docker build/prune
    β”œβ”€β”€ deploy.sh          # Deploy, rollback
    β”œβ”€β”€ locks.sh           # Locks
    β”œβ”€β”€ secrets.sh         # Secrets
    β”œβ”€β”€ commands.sh        # CLI commands
    β”œβ”€β”€ validation.sh      # Validation
    β”œβ”€β”€ generator.sh       # Stack generation
    β”œβ”€β”€ plugins.sh         # Plugins
    β”œβ”€β”€ registry.sh       # Service registry
    β”œβ”€β”€ wizard.sh          # Interactive wizard
    β”œβ”€β”€ dependencies.sh    # Dependency check
    β”œβ”€β”€ templates.sh       # Bash wrapper for Jinja2 templating
    └── templates.py      # Jinja2 templating (Python)

Modules

core.sh

Size: ~260 lines

Functions:

  • log β€” logging
  • fail β€” error with exit
  • with_spinner β€” animated spinner
  • retry_with_backoff β€” retry with exponential backoff
  • safe_load_env β€” safe .env loading

profiles.sh

Size: ~400 lines

Functions:

  • load_profile β€” load profile
  • list_profiles β€” list profiles
  • profile_exists β€” check existence
  • get_profile_config β€” get value from config.yaml
  • save_default_profile β€” save default profile
  • load_default_profile β€” load default profile
  • cmd_use β€” profile management command

args.sh

Size: ~120 lines

Functions:

  • parse_global_args β€” parse global flags
  • resolve_and_load_profile β€” resolve and load profile

router.sh

Size: ~260 lines

Functions:

  • resolve_command_alias β€” command aliases (dβ†’deploy, bβ†’build)
  • show_help β€” display help
  • select_stack_interactive β€” interactive stack selection
  • route_command β€” main command router

yaml.sh

Size: ~750 lines

Description: Built-in Bash YAML parser (requires Bash 4.0+). jq required for JSON output.

Functions:

  • yaml_get β€” get value
  • yaml_get_keys β€” get keys
  • get_services_list β€” service list
  • get_service_field β€” service field
  • iter_variables_yaml β€” iterate over variables.yaml

signals.sh

Size: ~320 lines

Description: Graceful shutdown and signal handling module. Solves: zombie processes after Ctrl+C, stuck builds, incomplete cleanup.

Configuration:

GRACEFUL_SHUTDOWN_TIMEOUT=10  # Wait before SIGKILL (sec)
SIGNAL_DEBUG=1                # Signal handling debug

Functions:

  • init_signal_handlers β€” initialize INT/TERM handlers
  • register_child_pid β€” register child process for termination
  • unregister_child_pid β€” unregister
  • register_cleanup_handler β€” register cleanup function
  • set_operation_context β€” set operation context (for logs)
  • run_tracked β€” run command with tracking
  • run_with_graceful_timeout β€” run with timeout and graceful shutdown
  • run_docker_build_tracked β€” docker build with timeout

Shutdown order:

  1. SIGTERM to all child processes
  2. Wait GRACEFUL_SHUTDOWN_TIMEOUT seconds
  3. SIGKILL to remaining processes
  4. Run cleanup handlers (LIFO)
  5. Exit with appropriate code

git.sh

Size: ~150 lines

Functions:

  • sync_repo_for_service β€” clone/update repo
  • get_service_commit_sha β€” get SHA
  • get_service_current_branch β€” current branch

docker.sh

Size: ~200 lines

Functions:

  • build_for_service β€” build image
  • smart_prune_stack_images β€” clean old images
  • get_service_replicas_status β€” replica status

deploy.sh

Size: ~770 lines

Functions:

  • save_deploy_checkpoint β€” save checkpoint
  • save_deploy_history β€” save history
  • rollback_deploy β€” rollback
  • run_hook β€” run hook
  • export_service_tags β€” export TAG_*
  • generate_compose_with_resources β€” generate compose with resources
  • wait_for_services_ready β€” wait for readiness
  • validate_deploy_prerequisites β€” validation

locks.sh

Size: ~80 lines

Functions:

  • acquire_deploy_lock β€” acquire lock
  • release_deploy_lock β€” release
  • list_active_locks β€” list active
  • force_release_deploy_lock β€” force release

secrets.sh

Size: ~150 lines

Functions:

  • secrets_sync β€” sync secrets
  • check_required_secrets β€” check required
  • secret_exists β€” check existence
  • cmd_secret_create β€” create secret

commands.sh

Size: ~400 lines

Functions:

  • cmd_deploy β€” deploy
  • cmd_build β€” build
  • cmd_rollback β€” rollback
  • cmd_repos_sync β€” repo sync
  • get_changed_stacks β€” changed stacks

validation.sh

Size: ~100 lines

Functions:

  • validate_services_yaml β€” validate services.yaml
  • validate_service_branches β€” validate service branches
  • validate_service_references β€” validate SERVICE_*

Module Dependencies

graph TD
    swarm.sh --> core.sh
    swarm.sh --> args.sh
    swarm.sh --> router.sh
    swarm.sh --> profiles.sh
    swarm.sh --> yaml.sh
    swarm.sh --> signals.sh
    swarm.sh --> commands.sh
    
    args.sh --> profiles.sh
    router.sh --> commands.sh
    
    profiles.sh --> yaml.sh
    commands.sh --> deploy.sh
    commands.sh --> git.sh
    commands.sh --> docker.sh
    
    deploy.sh --> yaml.sh
    deploy.sh --> locks.sh
    deploy.sh --> signals.sh
    deploy.sh --> templates.sh
    
    docker.sh --> core.sh
    docker.sh --> signals.sh
    
    git.sh --> core.sh
Loading

Adding a New Module

  1. Create file in bin/lib/:
#!/usr/bin/env bash
# my_module.sh - Description

my_function() {
  local arg="$1"
  # ...
}
  1. Include in swarm.sh:
. "$LIB_DIR/my_module.sh"

Conventions

Bash

  • Local variables: local var="value"
  • Functions: function_name() { ... }
  • Checks: [ -f "$file" ]
  • Errors: fail "message"

Naming

  • Functions: lower_snake_case
  • Variables: UPPER_SNAKE_CASE
  • Commands: cmd_<name>

See also