mirror of
https://github.com/ryankazokas/turbovault-app.git
synced 2026-04-16 22:12:53 +00:00
219 lines
5.8 KiB
Bash
Executable File
219 lines
5.8 KiB
Bash
Executable File
#!/bin/bash
|
||
|
||
# TurboVault Kubernetes Deployment Script
|
||
|
||
set -e
|
||
|
||
echo "🚀 TurboVault Kubernetes Deployment"
|
||
echo "===================================="
|
||
echo ""
|
||
|
||
# Configuration
|
||
NAMESPACE="turbovault"
|
||
IMAGE_NAME="turbovault"
|
||
REGISTRY="" # e.g., ghcr.io/username or docker.io/username
|
||
|
||
# Color codes
|
||
RED='\033[0;31m'
|
||
GREEN='\033[0;32m'
|
||
YELLOW='\033[1;33m'
|
||
NC='\033[0m' # No Color
|
||
|
||
# Check prerequisites
|
||
echo "📋 Checking prerequisites..."
|
||
|
||
if ! command -v kubectl &> /dev/null; then
|
||
echo -e "${RED}❌ kubectl not found. Please install kubectl.${NC}"
|
||
exit 1
|
||
fi
|
||
|
||
if ! command -v docker &> /dev/null; then
|
||
echo -e "${RED}❌ docker not found. Please install Docker.${NC}"
|
||
exit 1
|
||
fi
|
||
|
||
echo -e "${GREEN}✅ Prerequisites check passed${NC}"
|
||
echo ""
|
||
|
||
# Get version/tag
|
||
read -p "Enter version tag (default: latest): " VERSION
|
||
VERSION=${VERSION:-latest}
|
||
|
||
# Get registry
|
||
echo ""
|
||
echo "Enter your container registry details:"
|
||
echo "Examples:"
|
||
echo " - ghcr.io/username"
|
||
echo " - docker.io/username"
|
||
echo " - registry.example.com/myapp"
|
||
read -p "Registry path: " REGISTRY_INPUT
|
||
if [ -z "$REGISTRY_INPUT" ]; then
|
||
echo -e "${RED}❌ Registry path is required${NC}"
|
||
exit 1
|
||
fi
|
||
|
||
IMAGE_TAG="${REGISTRY_INPUT}/${IMAGE_NAME}:${VERSION}"
|
||
|
||
echo ""
|
||
echo "📝 Configuration:"
|
||
echo " Namespace: $NAMESPACE"
|
||
echo " Image: $IMAGE_TAG"
|
||
echo ""
|
||
|
||
# Build and push image
|
||
read -p "Build and push Docker image? (y/n) " -n 1 -r
|
||
echo ""
|
||
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
||
echo ""
|
||
echo "🏗️ Building Docker image..."
|
||
docker build -t "$IMAGE_TAG" .
|
||
|
||
echo ""
|
||
echo "📤 Pushing to registry..."
|
||
docker push "$IMAGE_TAG"
|
||
|
||
echo -e "${GREEN}✅ Image built and pushed${NC}"
|
||
fi
|
||
|
||
# Update deployment with new image
|
||
echo ""
|
||
echo "📝 Updating deployment.yaml with new image..."
|
||
sed -i.bak "s|image:.*turbovault.*|image: $IMAGE_TAG|g" k8s/deployment.yaml
|
||
sed -i.bak "s|image:.*turbovault.*|image: $IMAGE_TAG|g" k8s/migrate-job.yaml
|
||
rm k8s/*.bak
|
||
echo -e "${GREEN}✅ Deployment files updated${NC}"
|
||
|
||
# Check if secrets exist
|
||
if [ ! -f k8s/secrets.yaml ]; then
|
||
echo ""
|
||
echo -e "${YELLOW}⚠️ secrets.yaml not found!${NC}"
|
||
echo " Please create k8s/secrets.yaml from k8s/secrets.yaml.example"
|
||
echo " and add your actual secrets."
|
||
echo ""
|
||
exit 1
|
||
fi
|
||
|
||
# Check if using private registry
|
||
echo ""
|
||
read -p "Is this a private registry that requires authentication? (y/n) " -n 1 -r
|
||
echo ""
|
||
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
||
echo "🔐 Checking for registry secret..."
|
||
if ! kubectl get secret registry-secret -n $NAMESPACE 2>/dev/null; then
|
||
echo -e "${YELLOW}⚠️ Registry secret not found!${NC}"
|
||
echo ""
|
||
echo "You need to create a secret for pulling images from your private registry."
|
||
echo ""
|
||
read -p "Create the secret now? (y/n) " -n 1 -r
|
||
echo ""
|
||
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
||
echo ""
|
||
read -p "Registry server (e.g., registry.example.com): " REGISTRY_SERVER
|
||
read -p "Username: " REGISTRY_USER
|
||
read -sp "Password/Token: " REGISTRY_PASS
|
||
echo ""
|
||
read -p "Email: " REGISTRY_EMAIL
|
||
|
||
kubectl create secret docker-registry registry-secret \
|
||
--docker-server="$REGISTRY_SERVER" \
|
||
--docker-username="$REGISTRY_USER" \
|
||
--docker-password="$REGISTRY_PASS" \
|
||
--docker-email="$REGISTRY_EMAIL" \
|
||
--namespace=$NAMESPACE
|
||
|
||
if [ $? -eq 0 ]; then
|
||
echo -e "${GREEN}✅ Registry secret created${NC}"
|
||
else
|
||
echo -e "${RED}❌ Failed to create secret${NC}"
|
||
exit 1
|
||
fi
|
||
else
|
||
echo "Please create the secret manually before deploying."
|
||
echo ""
|
||
echo "kubectl create secret docker-registry registry-secret \\"
|
||
echo " --docker-server=your-registry.com \\"
|
||
echo " --docker-username=your-username \\"
|
||
echo " --docker-password=your-token \\"
|
||
echo " --docker-email=your-email \\"
|
||
echo " --namespace=$NAMESPACE"
|
||
exit 1
|
||
fi
|
||
else
|
||
echo -e "${GREEN}✅ Registry secret exists${NC}"
|
||
fi
|
||
else
|
||
echo "ℹ️ Using public registry (no authentication needed)"
|
||
fi
|
||
|
||
# Deploy
|
||
echo ""
|
||
read -p "Deploy to Kubernetes? (y/n) " -n 1 -r
|
||
echo ""
|
||
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
||
echo "Deployment cancelled."
|
||
exit 0
|
||
fi
|
||
|
||
echo ""
|
||
echo "🚀 Deploying to Kubernetes..."
|
||
echo ""
|
||
|
||
# Create namespace
|
||
echo "Creating namespace..."
|
||
kubectl apply -f k8s/namespace.yaml
|
||
|
||
# Apply ConfigMap
|
||
echo "Applying ConfigMap..."
|
||
kubectl apply -f k8s/configmap.yaml
|
||
|
||
# Apply Secrets
|
||
echo "Applying Secrets..."
|
||
kubectl apply -f k8s/secrets.yaml
|
||
|
||
# Run migrations
|
||
echo ""
|
||
echo "🗄️ Running database migrations..."
|
||
kubectl delete job turbovault-migrate -n $NAMESPACE 2>/dev/null || true
|
||
kubectl apply -f k8s/migrate-job.yaml
|
||
|
||
echo "Waiting for migrations to complete..."
|
||
if kubectl wait --for=condition=complete --timeout=300s job/turbovault-migrate -n $NAMESPACE 2>/dev/null; then
|
||
echo -e "${GREEN}✅ Migrations completed${NC}"
|
||
else
|
||
echo -e "${RED}❌ Migration failed or timed out${NC}"
|
||
echo "Check logs with: kubectl logs job/turbovault-migrate -n $NAMESPACE"
|
||
exit 1
|
||
fi
|
||
|
||
# Deploy application
|
||
echo ""
|
||
echo "📦 Deploying application..."
|
||
kubectl apply -f k8s/deployment.yaml
|
||
|
||
# Create service
|
||
echo "Creating service..."
|
||
kubectl apply -f k8s/service.yaml
|
||
|
||
# Create ingress
|
||
echo "Creating ingress..."
|
||
kubectl apply -f k8s/ingress.yaml
|
||
|
||
echo ""
|
||
echo -e "${GREEN}✅ Deployment complete!${NC}"
|
||
echo ""
|
||
echo "📊 Checking status..."
|
||
kubectl get pods -n $NAMESPACE
|
||
echo ""
|
||
kubectl get svc -n $NAMESPACE
|
||
echo ""
|
||
kubectl get ingress -n $NAMESPACE
|
||
|
||
echo ""
|
||
echo "🔍 Useful commands:"
|
||
echo " View logs: kubectl logs -f -l app=turbovault -n $NAMESPACE"
|
||
echo " Check pods: kubectl get pods -n $NAMESPACE"
|
||
echo " Port forward: kubectl port-forward svc/turbovault-service 3000:80 -n $NAMESPACE"
|
||
echo " Shell access: kubectl exec -it deployment/turbovault -n $NAMESPACE -- /bin/bash"
|
||
echo ""
|
||
echo "🎉 Done!"
|