258 lines
6.5 KiB
Bash
Executable File
258 lines
6.5 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# MOTM App Helm Deployment Script
|
|
# Usage: ./deploy.sh [environment] [action]
|
|
# Environment: development, staging, production
|
|
# Action: install, upgrade, uninstall, template
|
|
|
|
set -e
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Default values
|
|
ENVIRONMENT=${1:-development}
|
|
ACTION=${2:-install}
|
|
RELEASE_NAME="motm-app"
|
|
CHART_PATH="./motm-app"
|
|
NAMESPACE="motm-app"
|
|
|
|
# Function to print colored output
|
|
print_status() {
|
|
echo -e "${BLUE}[INFO]${NC} $1"
|
|
}
|
|
|
|
print_success() {
|
|
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
|
}
|
|
|
|
print_warning() {
|
|
echo -e "${YELLOW}[WARNING]${NC} $1"
|
|
}
|
|
|
|
print_error() {
|
|
echo -e "${RED}[ERROR]${NC} $1"
|
|
}
|
|
|
|
# Function to check prerequisites
|
|
check_prerequisites() {
|
|
print_status "Checking prerequisites..."
|
|
|
|
# Check if helm is installed
|
|
if ! command -v helm &> /dev/null; then
|
|
print_error "Helm is not installed. Please install Helm 3.0+"
|
|
exit 1
|
|
fi
|
|
|
|
# Check if kubectl is installed
|
|
if ! command -v kubectl &> /dev/null; then
|
|
print_error "kubectl is not installed. Please install kubectl"
|
|
exit 1
|
|
fi
|
|
|
|
# Check if we can connect to Kubernetes cluster
|
|
if ! kubectl cluster-info &> /dev/null; then
|
|
print_error "Cannot connect to Kubernetes cluster"
|
|
exit 1
|
|
fi
|
|
|
|
print_success "Prerequisites check passed"
|
|
}
|
|
|
|
# Function to create namespace
|
|
create_namespace() {
|
|
print_status "Creating namespace: $NAMESPACE"
|
|
kubectl create namespace $NAMESPACE --dry-run=client -o yaml | kubectl apply -f -
|
|
print_success "Namespace created/verified"
|
|
}
|
|
|
|
# Function to validate values file
|
|
validate_values() {
|
|
local values_file="values-${ENVIRONMENT}.yaml"
|
|
|
|
if [[ ! -f "$values_file" ]]; then
|
|
print_error "Values file $values_file not found"
|
|
exit 1
|
|
fi
|
|
|
|
print_status "Validating values file: $values_file"
|
|
|
|
# Check for required values
|
|
if ! grep -q "repository:" "$values_file"; then
|
|
print_error "Image repository not specified in $values_file"
|
|
exit 1
|
|
fi
|
|
|
|
if ! grep -q "host:" "$values_file"; then
|
|
print_error "Database host not specified in $values_file"
|
|
exit 1
|
|
fi
|
|
|
|
print_success "Values file validation passed"
|
|
}
|
|
|
|
# Function to template the chart
|
|
template_chart() {
|
|
local values_file="values-${ENVIRONMENT}.yaml"
|
|
|
|
print_status "Generating Kubernetes manifests..."
|
|
helm template $RELEASE_NAME $CHART_PATH \
|
|
--namespace $NAMESPACE \
|
|
--values $values_file \
|
|
--output-dir ./generated-manifests
|
|
|
|
print_success "Manifests generated in ./generated-manifests/"
|
|
}
|
|
|
|
# Function to install the chart
|
|
install_chart() {
|
|
local values_file="values-${ENVIRONMENT}.yaml"
|
|
|
|
print_status "Installing MOTM App in $ENVIRONMENT environment..."
|
|
|
|
helm install $RELEASE_NAME $CHART_PATH \
|
|
--namespace $NAMESPACE \
|
|
--values $values_file \
|
|
--create-namespace \
|
|
--wait \
|
|
--timeout 10m
|
|
|
|
print_success "MOTM App installed successfully"
|
|
|
|
# Show status
|
|
kubectl get pods -n $NAMESPACE -l app.kubernetes.io/name=motm-app
|
|
}
|
|
|
|
# Function to upgrade the chart
|
|
upgrade_chart() {
|
|
local values_file="values-${ENVIRONMENT}.yaml"
|
|
|
|
print_status "Upgrading MOTM App in $ENVIRONMENT environment..."
|
|
|
|
helm upgrade $RELEASE_NAME $CHART_PATH \
|
|
--namespace $NAMESPACE \
|
|
--values $values_file \
|
|
--wait \
|
|
--timeout 10m
|
|
|
|
print_success "MOTM App upgraded successfully"
|
|
|
|
# Show status
|
|
kubectl get pods -n $NAMESPACE -l app.kubernetes.io/name=motm-app
|
|
}
|
|
|
|
# Function to uninstall the chart
|
|
uninstall_chart() {
|
|
print_warning "Uninstalling MOTM App from $ENVIRONMENT environment..."
|
|
|
|
read -p "Are you sure you want to uninstall? (y/N): " -n 1 -r
|
|
echo
|
|
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
|
helm uninstall $RELEASE_NAME --namespace $NAMESPACE
|
|
print_success "MOTM App uninstalled successfully"
|
|
else
|
|
print_status "Uninstall cancelled"
|
|
fi
|
|
}
|
|
|
|
# Function to show status
|
|
show_status() {
|
|
print_status "Showing MOTM App status..."
|
|
|
|
echo "=== Helm Release Status ==="
|
|
helm status $RELEASE_NAME --namespace $NAMESPACE
|
|
|
|
echo -e "\n=== Pods Status ==="
|
|
kubectl get pods -n $NAMESPACE -l app.kubernetes.io/name=motm-app
|
|
|
|
echo -e "\n=== Services ==="
|
|
kubectl get svc -n $NAMESPACE -l app.kubernetes.io/name=motm-app
|
|
|
|
echo -e "\n=== Ingress ==="
|
|
kubectl get ingress -n $NAMESPACE -l app.kubernetes.io/name=motm-app
|
|
}
|
|
|
|
# Function to show logs
|
|
show_logs() {
|
|
print_status "Showing MOTM App logs..."
|
|
kubectl logs -n $NAMESPACE -l app.kubernetes.io/name=motm-app --tail=100 -f
|
|
}
|
|
|
|
# Function to show help
|
|
show_help() {
|
|
echo "MOTM App Helm Deployment Script"
|
|
echo ""
|
|
echo "Usage: $0 [environment] [action]"
|
|
echo ""
|
|
echo "Environments:"
|
|
echo " development Deploy to development environment"
|
|
echo " staging Deploy to staging environment"
|
|
echo " production Deploy to production environment"
|
|
echo ""
|
|
echo "Actions:"
|
|
echo " install Install the application (default)"
|
|
echo " upgrade Upgrade the application"
|
|
echo " uninstall Uninstall the application"
|
|
echo " template Generate Kubernetes manifests"
|
|
echo " status Show application status"
|
|
echo " logs Show application logs"
|
|
echo " help Show this help message"
|
|
echo ""
|
|
echo "Examples:"
|
|
echo " $0 development install"
|
|
echo " $0 production upgrade"
|
|
echo " $0 staging template"
|
|
echo " $0 development status"
|
|
}
|
|
|
|
# Main execution
|
|
main() {
|
|
case $ACTION in
|
|
install)
|
|
check_prerequisites
|
|
create_namespace
|
|
validate_values
|
|
install_chart
|
|
;;
|
|
upgrade)
|
|
check_prerequisites
|
|
create_namespace
|
|
validate_values
|
|
upgrade_chart
|
|
;;
|
|
uninstall)
|
|
check_prerequisites
|
|
uninstall_chart
|
|
;;
|
|
template)
|
|
check_prerequisites
|
|
validate_values
|
|
template_chart
|
|
;;
|
|
status)
|
|
check_prerequisites
|
|
show_status
|
|
;;
|
|
logs)
|
|
check_prerequisites
|
|
show_logs
|
|
;;
|
|
help|--help|-h)
|
|
show_help
|
|
;;
|
|
*)
|
|
print_error "Unknown action: $ACTION"
|
|
show_help
|
|
exit 1
|
|
;;
|
|
esac
|
|
}
|
|
|
|
# Run main function
|
|
main "$@"
|
|
|