/
gabasoft
/
winpulse
Обзор
Документация
Войти
/
gabasoft
/
winpulse
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
scripts/deploy.sh
304 строки
8 KB
Pavel Kashchenko
change filenames
07 июл 2025, 22:02
07 июл 2025, 22:02
dfae9aa
Код
Авторство
О чём код?
#!/bin/bash # WinPulse Deployment Script # This script deploys the application to different environments set -e # Exit on any error # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Function to print colored output print_status() { echo -e "${GREEN}[INFO]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } print_header() { echo -e "${BLUE}[DEPLOY]${NC} $1" } # Default values ENVIRONMENT="staging" NAMESPACE="winpulse-staging" REGISTRY="cr.yandex.net/winpulse" VERSION=$(git rev-parse --short HEAD) # Parse command line arguments while [[ $# -gt 0 ]]; do case $1 in --environment|-e) ENVIRONMENT="$2" shift 2 ;; --namespace|-n) NAMESPACE="$2" shift 2 ;; --registry|-r) REGISTRY="$2" shift 2 ;; --version|-v) VERSION="$2" shift 2 ;; --help|-h) echo "Usage: $0 [OPTIONS]" echo "Options:" echo " --environment, -e Deployment environment (staging|production)" echo " --namespace, -n Kubernetes namespace" echo " --registry, -r Container registry URL" echo " --version, -v Version tag for images" echo " --help, -h Show this help message" exit 0 ;; *) print_error "Unknown option: $1" exit 1 ;; esac done # Validate environment if [[ "$ENVIRONMENT" != "staging" && "$ENVIRONMENT" != "production" ]]; then print_error "Invalid environment: $ENVIRONMENT. Must be 'staging' or 'production'" exit 1 fi # Set environment-specific values if [[ "$ENVIRONMENT" == "production" ]]; then NAMESPACE="winpulse-production" print_warning "Deploying to PRODUCTION environment!" read -p "Are you sure you want to continue? (y/N): " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then print_status "Deployment cancelled" exit 0 fi fi print_header "Starting deployment to $ENVIRONMENT environment" print_status "Namespace: $NAMESPACE" print_status "Registry: $REGISTRY" print_status "Version: $VERSION" # Check prerequisites check_prerequisites() { print_status "Checking prerequisites..." if ! command -v kubectl &> /dev/null; then print_error "kubectl is not installed" exit 1 fi if ! command -v helm &> /dev/null; then print_error "helm is not installed" exit 1 fi if ! command -v yc &> /dev/null; then print_error "Yandex Cloud CLI (yc) is not installed" exit 1 fi # Check if we're connected to the cluster if ! kubectl cluster-info &> /dev/null; then print_error "Not connected to Kubernetes cluster" exit 1 fi print_status "All prerequisites are satisfied" } # Create namespace if it doesn't exist create_namespace() { print_status "Creating namespace $NAMESPACE if it doesn't exist..." kubectl create namespace $NAMESPACE --dry-run=client -o yaml | kubectl apply -f - } # Deploy infrastructure deploy_infrastructure() { print_status "Deploying infrastructure..." cd infrastructure/yandex-cloud # Initialize Terraform if needed if [ ! -d ".terraform" ]; then terraform init fi # Plan and apply Terraform terraform plan -var="environment=$ENVIRONMENT" -out=tfplan terraform apply tfplan cd ../.. print_status "Infrastructure deployment completed" } # Deploy monitoring stack deploy_monitoring() { print_status "Deploying monitoring stack..." # Add Helm repositories helm repo add prometheus-community https://prometheus-community.github.io/helm-charts helm repo add grafana https://grafana.github.io/helm-charts helm repo update # Deploy Prometheus helm upgrade --install prometheus prometheus-community/prometheus \ --namespace monitoring \ --create-namespace \ --values infrastructure/monitoring/prometheus-values.yaml # Deploy Grafana helm upgrade --install grafana grafana/grafana \ --namespace monitoring \ --values infrastructure/monitoring/grafana-values.yaml # Deploy Loki for logging helm upgrade --install loki grafana/loki \ --namespace monitoring \ --values infrastructure/monitoring/loki-values.yaml print_status "Monitoring stack deployment completed" } # Deploy services deploy_services() { print_status "Deploying microservices..." services=("auth" "data" "llm" "recommendation" "notification" "analytics" "support") for service in "${services[@]}"; do print_status "Deploying $service service..." # Update Helm values with current version sed -i "s/IMAGE_TAG/$VERSION/g" infrastructure/kubernetes/$service/values.yaml # Deploy using Helm helm upgrade --install $service infrastructure/kubernetes/$service \ --namespace $NAMESPACE \ --set image.tag=$VERSION \ --set image.repository=$REGISTRY/$service-service \ --wait \ --timeout=10m print_status "$service service deployed successfully" done print_status "All services deployed successfully" } # Deploy frontend deploy_frontend() { print_status "Deploying frontend..." # Build and push frontend image cd frontend docker build -t $REGISTRY/winpulse-frontend:$VERSION . docker push $REGISTRY/winpulse-frontend:$VERSION cd .. # Deploy frontend using Helm helm upgrade --install frontend infrastructure/kubernetes/frontend \ --namespace $NAMESPACE \ --set image.tag=$VERSION \ --set image.repository=$REGISTRY/winpulse-frontend \ --wait \ --timeout=10m print_status "Frontend deployed successfully" } # Run health checks health_checks() { print_status "Running health checks..." # Wait for all pods to be ready kubectl wait --for=condition=ready pod -l app.kubernetes.io/instance=frontend -n $NAMESPACE --timeout=300s services=("auth" "data" "llm" "recommendation" "notification" "analytics" "support") for service in "${services[@]}"; do kubectl wait --for=condition=ready pod -l app.kubernetes.io/instance=$service -n $NAMESPACE --timeout=300s done # Check service endpoints print_status "Checking service endpoints..." for service in "${services[@]}"; do if kubectl get svc $service-service -n $NAMESPACE &> /dev/null; then print_status "$service service is accessible" else print_warning "$service service endpoint not found" fi done print_status "Health checks completed" } # Rollback function rollback() { print_error "Deployment failed, initiating rollback..." # Get the previous revision PREVIOUS_REVISION=$(helm history frontend -n $NAMESPACE --output json | jq -r '.[-2].revision') if [ "$PREVIOUS_REVISION" != "null" ]; then print_status "Rolling back to revision $PREVIOUS_REVISION" services=("auth" "data" "llm" "recommendation" "notification" "analytics" "support" "frontend") for service in "${services[@]}"; do helm rollback $service $PREVIOUS_REVISION -n $NAMESPACE || print_warning "Failed to rollback $service" done print_status "Rollback completed" else print_error "No previous revision found for rollback" fi } # Main deployment process main() { print_header "Starting WinPulse deployment to $ENVIRONMENT" # Set up error handling for rollback trap rollback ERR # Execute deployment steps check_prerequisites create_namespace deploy_infrastructure deploy_monitoring deploy_services deploy_frontend health_checks # Remove error trap on success trap - ERR print_header "🎉 Deployment to $ENVIRONMENT completed successfully!" # Show service URLs print_status "Service URLs:" kubectl get svc -n $NAMESPACE -o wide # Show ingress URLs if available if kubectl get ingress -n $NAMESPACE &> /dev/null; then print_status "Ingress URLs:" kubectl get ingress -n $NAMESPACE fi } # Execute main function main