Automation Framework
The automation framework built around standardized shell scripting patterns, declarative service management, and integrated monitoring. Provides consistent operations across all nodes while maintaining flexibility for node-specific requirements.
Framework Architecture
Bootstrap System
The automation framework centers around a universal bootstrap system that provides standardized dependencies and environment setup for all scripts:
| |
Bootstrap Components:
- Environment Setup: Automatic repository root detection and navigation
- Library Loading: Standardized loading of all utility libraries
- Error Handling: Consistent error trapping and notification patterns
- Logging Framework: Unified logging with script identification and timestamps
Library Structure
The framework consists of modular libraries providing specialized functionality:
.scripts/
├── bootstrap.sh # Universal dependency sourcing
├── lib/
│ ├── common.sh # Core utilities and logging
│ ├── init.sh # Declarative service initialization
│ ├── services.sh # System service management
│ ├── templates.sh # Configuration template rendering
│ └── validation.sh # Input validation and system checks
└── ops/
├── check-package-updates # Security update monitoring
├── envsubst-crontab # Cron job template management
├── envsubst-directory # Bulk template rendering
├── envsubst-ufw # Firewall rule automation
├── lint-docker-compose # Docker Compose validation
├── setup-tiers # Data tier infrastructure setup
├── sync-remote # Git repository synchronization
└── sync-tiers # Data backup synchronizationCore Library Functions
Common Utilities (common.sh)
Provides fundamental utilities used across all automation scripts:
Logging Framework
| |
Features:
- Automatic script name detection for log prefixes
- Consistent timestamp formatting
- Error output to stderr
- Integration with monitoring systems
Repository Navigation
| |
Capabilities:
- Automatic git repository detection
- Consistent working directory establishment
- Error handling for non-git environments
Argument Validation
| |
Validation Features:
- Required argument checking with descriptive errors
- Usage hint integration
- Standardized exit codes for different error types
Template Rendering
| |
Template System:
- Environment variable substitution via
envsubst - Atomic file operations with temporary files
- Error handling and rollback on failures
Telegram Integration
| |
Notification Features:
- Automatic error trapping with notifications
- Rich message formatting with Markdown support
- Node identification for multi-host environments
- Timestamp inclusion for audit trails
Service Management (services.sh)
Provides standardized systemd service management functions:
Service Status Operations
| |
Service Control Operations
| |
Service Monitoring
| |
Validation Framework (validation.sh)
Comprehensive input and system validation:
File System Validation
| |
System Validation
| |
Network and Resource Validation
| |
Declarative Initialization (init.sh)
Provides a declarative approach to service initialization:
Service Initialization Pattern
| |
Initialization Steps:
- Package Installation: Automatic installation of required packages
- Environment Setup: Node and tier environment variable configuration
- Custom Setup: Execution of service-specific configuration functions
- Service Management: Automatic service start and enable
- Health Validation: Comprehensive health checks with failure handling
Base System Setup
| |
Base System Components:
- Debian/Ubuntu system validation
- Package list updates
- Essential package installation (
curl,wget,git,rsyslog) - System logging service configuration
Template Management (templates.sh)
Advanced template rendering capabilities:
Single File Rendering
| |
Batch Directory Rendering
| |
Variable-Specific Rendering
| |
Template Features:
- Atomic file operations with temporary files
- Environment variable validation
- Batch processing with directory preservation
- External variable file support
Operational Scripts
Infrastructure Setup
Tier Setup (setup-tiers)
| |
Operations Performed:
- Creates complete
@tiersymbolic link structure - Establishes project-level tier directories
- Sets up host-level
.host/@tierlinks - Creates shared directory hierarchy with cross-links
- Links
.envfiles to@tier1for configuration management
Interactive Safety Features:
- Confirmation prompts for existing symlink removal
- Validation of target directory existence
- Safe handling of non-symlink paths
- Comprehensive logging of all operations
Data Synchronization (sync-tiers)
| |
Synchronization Process:
- Validation: Confirms node directory and compose file existence
- Tier 1 Sync: Executes
rclone1service for critical data - Tier 2 Sync: Executes
rclone2service for application data - Error Handling: Automatic error trapping with Telegram notifications
- Success Reporting: Completion notifications with operation details
Configuration Management
Template Directory Processing (envsubst-directory)
| |
Processing Features:
- Recursive template file discovery
- Directory structure preservation
- Existing file backup to
/tmp - Environment variable substitution
- Atomic file operations
Cron Job Management (envsubst-crontab)
| |
Crontab Features:
- Environment variable substitution in cron templates
- Current crontab backup before installation
- Validation of rendered crontab before installation
- Automatic rollback on installation failures
- Preview of rendered crontab before installation
Firewall Rule Management (envsubst-ufw)
| |
Firewall Management:
- Docker service firewall rules via
ufw-docker - Host-level UFW rules for system protection
- Rule file processing with comment and blank line handling
- Idempotent rule application
- Error handling with rule validation
Development and Maintenance
Docker Compose Linting (lint-docker-compose)
| |
Linting Features:
- Automatic fixing of common Docker Compose issues
- Pre-commit hook integration with staged file processing
- Exclusion of data directories from linting (
data,state,vault) - Automatic re-staging of fixed files in hook mode
- Integration with development workflows
Package Update Monitoring (check-package-updates)
| |
Update Monitoring:
- System package list updates
- Available upgrade detection and reporting
- Telegram notifications with package details
- Security update prioritization
- Formatted update notifications with package counts
Repository Synchronization (sync-remote)
| |
Git Operations:
- Automatic staging of node directory changes
- Branch creation and management (
sync/<NODE>pattern) - Rebase integration with
origin/trunk - Conflict detection and handling
- Force-push with lease protection
- Bot identity configuration for automated commits
Error Handling and Monitoring
Universal Error Trapping
All scripts implement standardized error handling:
| |
Error Handling Features:
- Automatic error detection via
set -euo pipefail - Context-aware error messages with line numbers and commands
- Immediate Telegram notifications on script failures
- Graceful cleanup of temporary resources
- Integration with monitoring and alerting systems
Monitoring Integration
The framework integrates comprehensive monitoring:
Health Check Integration
- Service startup validation
- Database integrity checks
- Network connectivity verification
- Resource availability monitoring
Alert Classification
- Success: Operation completion notifications
- Info: Informational updates and status reports
- Warning: Non-critical issues requiring attention
- Error: Critical failures requiring immediate intervention
Logging and Audit Trail
Structured Logging
| |
Audit Features
- Consistent log formatting across all scripts
- Timestamp integration with timezone information
- Operation correlation through script name identification
- Integration with centralized logging systems
Framework Best Practices
Script Development Standards
- Universal Bootstrap: All scripts must source the bootstrap system
- Error Handling: Implement error trapping for all critical operations
- Argument Validation: Validate all required arguments before execution
- Logging Integration: Use standardized logging functions throughout
- Documentation: Include usage instructions and parameter descriptions
Security Considerations
- Privilege Validation: Verify required permissions before execution
- Input Sanitization: Validate and sanitize all external inputs
- Secret Handling: Use secure methods for credential management
- Audit Logging: Maintain comprehensive logs for security auditing
- Error Information: Avoid exposing sensitive data in error messages
Operational Reliability
- Idempotent Operations: Ensure scripts can be safely re-executed
- Atomic Changes: Use temporary files and atomic moves for critical updates
- Rollback Capability: Implement rollback mechanisms for configuration changes
- Health Validation: Verify system state after all operations
- Resource Cleanup: Ensure proper cleanup of temporary resources
This automation framework provides a robust foundation for system management while maintaining consistency, security, and operational reliability.