feat: CI/CD pipeline setup complete - Ansible playbooks updated, secrets configured, workflow ready
This commit is contained in:
160
.deployment-archive-20251030-111806/scripts/lib/ansible.sh
Executable file
160
.deployment-archive-20251030-111806/scripts/lib/ansible.sh
Executable file
@@ -0,0 +1,160 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Ansible Integration Library
|
||||
# Provides helpers for Ansible operations
|
||||
#
|
||||
|
||||
# Source common library
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
# shellcheck source=./common.sh
|
||||
source "${SCRIPT_DIR}/common.sh"
|
||||
|
||||
# Default Ansible paths
|
||||
readonly ANSIBLE_DIR="${ANSIBLE_DIR:-${SCRIPT_DIR}/../../ansible}"
|
||||
readonly ANSIBLE_INVENTORY="${ANSIBLE_INVENTORY:-${ANSIBLE_DIR}/inventory/production.yml}"
|
||||
readonly ANSIBLE_PLAYBOOK_DIR="${ANSIBLE_PLAYBOOK_DIR:-${ANSIBLE_DIR}/playbooks}"
|
||||
|
||||
# Check Ansible installation
|
||||
check_ansible() {
|
||||
log_step "Checking Ansible installation..."
|
||||
|
||||
require_command "ansible" "sudo apt install ansible" || return 1
|
||||
require_command "ansible-playbook" || return 1
|
||||
|
||||
local version
|
||||
version=$(ansible --version | head -1)
|
||||
log_success "Ansible installed: $version"
|
||||
}
|
||||
|
||||
# Test Ansible connectivity
|
||||
test_ansible_connectivity() {
|
||||
local inventory="${1:-$ANSIBLE_INVENTORY}"
|
||||
|
||||
log_step "Testing Ansible connectivity..."
|
||||
|
||||
if ! ansible all -i "$inventory" -m ping &> /dev/null; then
|
||||
log_error "Cannot connect to production server"
|
||||
log_info "Check:"
|
||||
log_info " - SSH key: ~/.ssh/production"
|
||||
log_info " - Network connectivity"
|
||||
log_info " - Server availability"
|
||||
return 1
|
||||
fi
|
||||
|
||||
log_success "Connection successful"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Run Ansible playbook
|
||||
run_ansible_playbook() {
|
||||
local playbook="$1"
|
||||
shift
|
||||
local extra_args=("$@")
|
||||
|
||||
log_step "Running Ansible playbook: $(basename "$playbook")"
|
||||
|
||||
# Build command
|
||||
local cmd="ansible-playbook -i ${ANSIBLE_INVENTORY} ${playbook}"
|
||||
|
||||
# Add extra args
|
||||
if [[ ${#extra_args[@]} -gt 0 ]]; then
|
||||
cmd="${cmd} ${extra_args[*]}"
|
||||
fi
|
||||
|
||||
log_debug "Command: $cmd"
|
||||
|
||||
# Execute with proper error handling
|
||||
if eval "$cmd"; then
|
||||
log_success "Playbook completed successfully"
|
||||
return 0
|
||||
else
|
||||
local exit_code=$?
|
||||
log_error "Playbook failed with exit code $exit_code"
|
||||
return $exit_code
|
||||
fi
|
||||
}
|
||||
|
||||
# Run deployment playbook
|
||||
run_deployment() {
|
||||
local git_repo_url="${1:-}"
|
||||
local playbook="${ANSIBLE_PLAYBOOK_DIR}/deploy.yml"
|
||||
|
||||
if [[ ! -f "$playbook" ]]; then
|
||||
log_error "Deployment playbook not found: $playbook"
|
||||
return 1
|
||||
fi
|
||||
|
||||
log_step "Starting deployment..."
|
||||
|
||||
local extra_args=()
|
||||
if [[ -n "$git_repo_url" ]]; then
|
||||
extra_args+=("-e" "git_repo_url=${git_repo_url}")
|
||||
log_info "Git repository: $git_repo_url"
|
||||
else
|
||||
log_info "Using existing code on server"
|
||||
fi
|
||||
|
||||
run_ansible_playbook "$playbook" "${extra_args[@]}"
|
||||
}
|
||||
|
||||
# Get Ansible facts
|
||||
get_ansible_facts() {
|
||||
local inventory="${1:-$ANSIBLE_INVENTORY}"
|
||||
local host="${2:-production_server}"
|
||||
|
||||
ansible "$host" -i "$inventory" -m setup
|
||||
}
|
||||
|
||||
# Ansible dry-run
|
||||
ansible_dry_run() {
|
||||
local playbook="$1"
|
||||
shift
|
||||
local extra_args=("$@")
|
||||
|
||||
log_step "Running dry-run (check mode)..."
|
||||
|
||||
extra_args+=("--check" "--diff")
|
||||
|
||||
run_ansible_playbook "$playbook" "${extra_args[@]}"
|
||||
}
|
||||
|
||||
# List Ansible hosts
|
||||
list_ansible_hosts() {
|
||||
local inventory="${1:-$ANSIBLE_INVENTORY}"
|
||||
|
||||
log_step "Listing Ansible hosts..."
|
||||
|
||||
ansible-inventory -i "$inventory" --list
|
||||
}
|
||||
|
||||
# Check playbook syntax
|
||||
check_playbook_syntax() {
|
||||
local playbook="$1"
|
||||
|
||||
log_step "Checking playbook syntax..."
|
||||
|
||||
if ansible-playbook --syntax-check "$playbook" &> /dev/null; then
|
||||
log_success "Syntax check passed"
|
||||
return 0
|
||||
else
|
||||
log_error "Syntax check failed"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Execute Ansible ad-hoc command
|
||||
ansible_adhoc() {
|
||||
local host="$1"
|
||||
local module="$2"
|
||||
shift 2
|
||||
local args=("$@")
|
||||
|
||||
log_step "Running ad-hoc command on $host..."
|
||||
|
||||
ansible "$host" -i "$ANSIBLE_INVENTORY" -m "$module" -a "${args[*]}"
|
||||
}
|
||||
|
||||
# Export functions
|
||||
export -f check_ansible test_ansible_connectivity run_ansible_playbook
|
||||
export -f run_deployment get_ansible_facts ansible_dry_run
|
||||
export -f list_ansible_hosts check_playbook_syntax ansible_adhoc
|
||||
215
.deployment-archive-20251030-111806/scripts/lib/common.sh
Executable file
215
.deployment-archive-20251030-111806/scripts/lib/common.sh
Executable file
@@ -0,0 +1,215 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Common Library Functions for Deployment Scripts
|
||||
# Provides unified logging, error handling, and utilities
|
||||
#
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# Colors for output
|
||||
readonly RED='\033[0;31m'
|
||||
readonly GREEN='\033[0;32m'
|
||||
readonly YELLOW='\033[1;33m'
|
||||
readonly BLUE='\033[0;34m'
|
||||
readonly CYAN='\033[0;36m'
|
||||
readonly MAGENTA='\033[0;35m'
|
||||
readonly NC='\033[0m' # No Color
|
||||
|
||||
# Logging functions
|
||||
log_info() {
|
||||
echo -e "${BLUE}ℹ️ ${1}${NC}"
|
||||
}
|
||||
|
||||
log_success() {
|
||||
echo -e "${GREEN}✅ ${1}${NC}"
|
||||
}
|
||||
|
||||
log_warning() {
|
||||
echo -e "${YELLOW}⚠️ ${1}${NC}"
|
||||
}
|
||||
|
||||
log_error() {
|
||||
echo -e "${RED}❌ ${1}${NC}"
|
||||
}
|
||||
|
||||
log_debug() {
|
||||
if [[ "${DEBUG:-0}" == "1" ]]; then
|
||||
echo -e "${CYAN}🔍 ${1}${NC}"
|
||||
fi
|
||||
}
|
||||
|
||||
log_step() {
|
||||
echo -e "${MAGENTA}▶️ ${1}${NC}"
|
||||
}
|
||||
|
||||
# Error handling
|
||||
die() {
|
||||
log_error "$1"
|
||||
exit "${2:-1}"
|
||||
}
|
||||
|
||||
# Check if command exists
|
||||
command_exists() {
|
||||
command -v "$1" &> /dev/null
|
||||
}
|
||||
|
||||
# Validate prerequisites
|
||||
require_command() {
|
||||
local cmd="$1"
|
||||
local install_hint="${2:-}"
|
||||
|
||||
if ! command_exists "$cmd"; then
|
||||
log_error "Required command not found: $cmd"
|
||||
[[ -n "$install_hint" ]] && log_info "Install with: $install_hint"
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
# Run command with retry logic
|
||||
run_with_retry() {
|
||||
local max_attempts="${1}"
|
||||
local delay="${2}"
|
||||
shift 2
|
||||
local cmd=("$@")
|
||||
local attempt=1
|
||||
|
||||
while [[ $attempt -le $max_attempts ]]; do
|
||||
if "${cmd[@]}"; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
if [[ $attempt -lt $max_attempts ]]; then
|
||||
log_warning "Command failed (attempt $attempt/$max_attempts). Retrying in ${delay}s..."
|
||||
sleep "$delay"
|
||||
fi
|
||||
|
||||
((attempt++))
|
||||
done
|
||||
|
||||
log_error "Command failed after $max_attempts attempts"
|
||||
return 1
|
||||
}
|
||||
|
||||
# Execute command and capture output
|
||||
execute() {
|
||||
local cmd="$1"
|
||||
log_debug "Executing: $cmd"
|
||||
eval "$cmd"
|
||||
}
|
||||
|
||||
# Spinner for long-running operations
|
||||
spinner() {
|
||||
local pid=$1
|
||||
local delay=0.1
|
||||
local spinstr='⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏'
|
||||
|
||||
while ps -p "$pid" > /dev/null 2>&1; do
|
||||
local temp=${spinstr#?}
|
||||
printf " [%c] " "$spinstr"
|
||||
local spinstr=$temp${spinstr%"$temp"}
|
||||
sleep $delay
|
||||
printf "\b\b\b\b\b\b"
|
||||
done
|
||||
printf " \b\b\b\b"
|
||||
}
|
||||
|
||||
# Progress bar
|
||||
progress_bar() {
|
||||
local current=$1
|
||||
local total=$2
|
||||
local width=50
|
||||
local percentage=$((current * 100 / total))
|
||||
local completed=$((width * current / total))
|
||||
local remaining=$((width - completed))
|
||||
|
||||
printf "\r["
|
||||
printf "%${completed}s" | tr ' ' '█'
|
||||
printf "%${remaining}s" | tr ' ' '░'
|
||||
printf "] %3d%%" "$percentage"
|
||||
|
||||
if [[ $current -eq $total ]]; then
|
||||
echo ""
|
||||
fi
|
||||
}
|
||||
|
||||
# Confirm action
|
||||
confirm() {
|
||||
local prompt="${1:-Are you sure?}"
|
||||
local default="${2:-n}"
|
||||
|
||||
if [[ "$default" == "y" ]]; then
|
||||
prompt="$prompt [Y/n] "
|
||||
else
|
||||
prompt="$prompt [y/N] "
|
||||
fi
|
||||
|
||||
read -rp "$prompt" response
|
||||
response=${response:-$default}
|
||||
|
||||
[[ "$response" =~ ^[Yy]$ ]]
|
||||
}
|
||||
|
||||
# Parse YAML-like config
|
||||
parse_config() {
|
||||
local config_file="$1"
|
||||
local key="$2"
|
||||
|
||||
if [[ ! -f "$config_file" ]]; then
|
||||
log_error "Config file not found: $config_file"
|
||||
return 1
|
||||
fi
|
||||
|
||||
grep "^${key}:" "$config_file" | sed "s/^${key}:[[:space:]]*//" | tr -d '"'
|
||||
}
|
||||
|
||||
# Timestamp functions
|
||||
timestamp() {
|
||||
date '+%Y-%m-%d %H:%M:%S'
|
||||
}
|
||||
|
||||
timestamp_file() {
|
||||
date '+%Y%m%d_%H%M%S'
|
||||
}
|
||||
|
||||
# Duration calculation
|
||||
duration() {
|
||||
local start=$1
|
||||
local end=${2:-$(date +%s)}
|
||||
local elapsed=$((end - start))
|
||||
|
||||
local hours=$((elapsed / 3600))
|
||||
local minutes=$(((elapsed % 3600) / 60))
|
||||
local seconds=$((elapsed % 60))
|
||||
|
||||
if [[ $hours -gt 0 ]]; then
|
||||
printf "%dh %dm %ds" "$hours" "$minutes" "$seconds"
|
||||
elif [[ $minutes -gt 0 ]]; then
|
||||
printf "%dm %ds" "$minutes" "$seconds"
|
||||
else
|
||||
printf "%ds" "$seconds"
|
||||
fi
|
||||
}
|
||||
|
||||
# Cleanup handler
|
||||
cleanup_handlers=()
|
||||
|
||||
register_cleanup() {
|
||||
cleanup_handlers+=("$1")
|
||||
}
|
||||
|
||||
cleanup() {
|
||||
log_info "Running cleanup handlers..."
|
||||
for handler in "${cleanup_handlers[@]}"; do
|
||||
eval "$handler" || log_warning "Cleanup handler failed: $handler"
|
||||
done
|
||||
}
|
||||
|
||||
trap cleanup EXIT
|
||||
|
||||
# Export functions for use in other scripts
|
||||
export -f log_info log_success log_warning log_error log_debug log_step
|
||||
export -f die command_exists require_command run_with_retry execute
|
||||
export -f spinner progress_bar confirm parse_config
|
||||
export -f timestamp timestamp_file duration
|
||||
export -f register_cleanup cleanup
|
||||
Reference in New Issue
Block a user