#!/bin/bash
set -euo pipefail
# Validation script for deployment scripts
# Usage: validate_deployment_scripts.sh
SCRIPT_DIR="$(dirname "$0")"
VALIDATION_FAILED=0
echo "π Validating CodeGraph deployment scripts"
echo "π Script directory: $SCRIPT_DIR"
echo ""
# Function to validate script exists and is executable
validate_script() {
local script_name=$1
local script_path="${SCRIPT_DIR}/${script_name}"
echo "π Validating $script_name..."
# Check if file exists
if [[ ! -f "$script_path" ]]; then
echo "β Script not found: $script_path"
VALIDATION_FAILED=1
return 1
fi
# Check if file is executable
if [[ ! -x "$script_path" ]]; then
echo "β οΈ Script is not executable: $script_path"
echo " Run: chmod +x $script_path"
VALIDATION_FAILED=1
return 1
fi
# Basic syntax validation
if ! bash -n "$script_path"; then
echo "β Syntax error in script: $script_path"
VALIDATION_FAILED=1
return 1
fi
echo "β
$script_name is valid and executable"
return 0
}
# Function to validate script usage/help output
validate_script_usage() {
local script_name=$1
local script_path="${SCRIPT_DIR}/${script_name}"
echo "π Testing $script_name usage information..."
# Test that script shows usage when run with no arguments
if timeout 10s bash "$script_path" >/dev/null 2>&1; then
echo "β οΈ Script $script_name does not show usage when run without arguments"
# This is a warning, not a failure
else
echo "β
$script_name properly handles missing arguments"
fi
return 0
}
# Function to validate required dependencies
validate_dependencies() {
echo "π§ Validating required dependencies..."
local deps_ok=true
# Check for kubectl
if ! command -v kubectl >/dev/null 2>&1; then
echo "β kubectl is not installed or not in PATH"
deps_ok=false
else
echo "β
kubectl is available"
fi
# Check for curl
if ! command -v curl >/dev/null 2>&1; then
echo "β curl is not installed or not in PATH"
deps_ok=false
else
echo "β
curl is available"
fi
# Check for jq (optional but recommended)
if ! command -v jq >/dev/null 2>&1; then
echo "β οΈ jq is not installed - some features will be limited"
else
echo "β
jq is available"
fi
if [[ "$deps_ok" == "false" ]]; then
echo "β Missing required dependencies"
VALIDATION_FAILED=1
return 1
fi
echo "β
All required dependencies are available"
return 0
}
# Function to validate Kubernetes manifests
validate_k8s_manifests() {
echo "π Validating Kubernetes manifests..."
local manifests_dir="${SCRIPT_DIR}/../deploy/k8s"
if [[ ! -d "$manifests_dir" ]]; then
echo "β Kubernetes manifests directory not found: $manifests_dir"
VALIDATION_FAILED=1
return 1
fi
# Check for required manifest files
local required_manifests=("deployment.yaml" "service.yaml")
for manifest in "${required_manifests[@]}"; do
local manifest_path="${manifests_dir}/${manifest}"
if [[ ! -f "$manifest_path" ]]; then
echo "β Required manifest not found: $manifest_path"
VALIDATION_FAILED=1
continue
fi
# Validate basic YAML structure and required fields
local has_api_version=$(grep -c "^apiVersion:" "$manifest_path" || echo "0")
local has_kind=$(grep -c "^kind:" "$manifest_path" || echo "0")
local has_metadata=$(grep -c "^metadata:" "$manifest_path" || echo "0")
local has_spec=$(grep -c "^spec:" "$manifest_path" || echo "0")
if [[ $has_api_version -eq 1 && $has_kind -eq 1 && $has_metadata -eq 1 && $has_spec -eq 1 ]]; then
echo "β
Valid manifest: $manifest"
else
echo "β Manifest missing required fields: $manifest"
echo " apiVersion: $has_api_version, kind: $has_kind, metadata: $has_metadata, spec: $has_spec"
VALIDATION_FAILED=1
fi
done
return 0
}
# Function to validate script integrations
validate_script_integrations() {
echo "π Validating script integrations..."
# Check that deploy_k8s.sh references health_check.sh
if grep -q "health_check.sh" "${SCRIPT_DIR}/deploy_k8s.sh"; then
echo "β
deploy_k8s.sh integrates with health_check.sh"
else
echo "β οΈ deploy_k8s.sh does not reference health_check.sh"
fi
# Check that blue_green_deploy.sh references health_check.sh
if grep -q "health_check.sh" "${SCRIPT_DIR}/blue_green_deploy.sh"; then
echo "β
blue_green_deploy.sh integrates with health_check.sh"
else
echo "β οΈ blue_green_deploy.sh does not reference health_check.sh"
fi
# Check that switch_traffic.sh references health_check.sh
if grep -q "health_check.sh" "${SCRIPT_DIR}/switch_traffic.sh"; then
echo "β
switch_traffic.sh integrates with health_check.sh"
else
echo "β οΈ switch_traffic.sh does not reference health_check.sh"
fi
return 0
}
# Function to validate environment variable handling
validate_environment_variables() {
echo "π Validating environment variable handling..."
local env_vars_ok=true
# Check that scripts handle KUBECONFIG_CONTENT
for script in "deploy_k8s.sh" "blue_green_deploy.sh" "switch_traffic.sh"; do
if grep -q "KUBECONFIG_CONTENT" "${SCRIPT_DIR}/${script}"; then
echo "β
$script handles KUBECONFIG_CONTENT"
else
echo "β $script does not handle KUBECONFIG_CONTENT"
env_vars_ok=false
fi
done
# Check timeout variable handling
for script in "deploy_k8s.sh" "blue_green_deploy.sh" "health_check.sh"; do
if grep -q "TIMEOUT" "${SCRIPT_DIR}/${script}"; then
echo "β
$script supports timeout configuration"
else
echo "β οΈ $script does not support timeout configuration"
fi
done
if [[ "$env_vars_ok" == "false" ]]; then
VALIDATION_FAILED=1
return 1
fi
return 0
}
# Function to validate error handling
validate_error_handling() {
echo "β οΈ Validating error handling patterns..."
# Check for proper error handling (set -euo pipefail)
for script in "deploy_k8s.sh" "blue_green_deploy.sh" "health_check.sh" "switch_traffic.sh"; do
if grep -q "set -euo pipefail" "${SCRIPT_DIR}/${script}"; then
echo "β
$script uses proper error handling"
else
echo "β οΈ $script may not use proper error handling"
fi
done
return 0
}
# Function to generate validation report
generate_report() {
echo ""
echo "π Validation Report Summary"
echo "=========================="
# Count scripts
local script_count=0
for script in "deploy_k8s.sh" "blue_green_deploy.sh" "health_check.sh" "switch_traffic.sh"; do
if [[ -f "${SCRIPT_DIR}/${script}" ]]; then
((script_count++))
fi
done
echo "π Scripts found: $script_count/4"
echo "π§ Dependencies: $(command -v kubectl >/dev/null && echo "kubectl β
" || echo "kubectl β") $(command -v curl >/dev/null && echo "curl β
" || echo "curl β")"
echo "π Manifests: $(find "${SCRIPT_DIR}/../deploy/k8s" -name "*.yaml" 2>/dev/null | wc -l || echo "0") files"
if [[ $VALIDATION_FAILED -eq 0 ]]; then
echo ""
echo "π All validations passed!"
echo "β
Deployment scripts are ready for use"
return 0
else
echo ""
echo "β Some validations failed"
echo "β οΈ Please fix the issues above before using the deployment scripts"
return 1
fi
}
# Main validation execution
main() {
echo "π Starting deployment script validation..."
echo ""
# Validate each script
local scripts=("deploy_k8s.sh" "blue_green_deploy.sh" "health_check.sh" "switch_traffic.sh")
for script in "${scripts[@]}"; do
validate_script "$script"
validate_script_usage "$script"
echo ""
done
# Validate dependencies
validate_dependencies
echo ""
# Validate Kubernetes manifests
validate_k8s_manifests
echo ""
# Validate script integrations
validate_script_integrations
echo ""
# Validate environment variables
validate_environment_variables
echo ""
# Validate error handling
validate_error_handling
echo ""
# Generate final report
generate_report
}
# Run main function
main