|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# NetPulse Kubernetes Secrets Setup Script |
| 4 | + |
| 5 | +set -e |
| 6 | + |
| 7 | +COLOR_RED='\033[0;31m' |
| 8 | +COLOR_GREEN='\033[0;32m' |
| 9 | +COLOR_YELLOW='\033[1;33m' |
| 10 | +COLOR_BLUE='\033[0;34m' |
| 11 | +COLOR_NC='\033[0m' # No Color |
| 12 | + |
| 13 | +print_status() { |
| 14 | + echo -e "${COLOR_BLUE}[INFO]${COLOR_NC} $1" |
| 15 | +} |
| 16 | + |
| 17 | +print_success() { |
| 18 | + echo -e "${COLOR_GREEN}[SUCCESS]${COLOR_NC} $1" |
| 19 | +} |
| 20 | + |
| 21 | +print_warning() { |
| 22 | + echo -e "${COLOR_YELLOW}[WARNING]${COLOR_NC} $1" |
| 23 | +} |
| 24 | + |
| 25 | +print_error() { |
| 26 | + echo -e "${COLOR_RED}[ERROR]${COLOR_NC} $1" |
| 27 | +} |
| 28 | + |
| 29 | +generate_secure_values() { |
| 30 | + print_status "Generating secure values for Kubernetes secrets..." |
| 31 | + |
| 32 | + local redis_password=$(openssl rand -base64 32 | tr -d "=+/" | cut -c1-25) |
| 33 | + local api_key="np_$(openssl rand -hex 32)" |
| 34 | + |
| 35 | + # Create deployment secrets file (not in git) |
| 36 | + cat > k8s/00-secrets-deploy.yaml << SECRETSEOF |
| 37 | +# 00-secrets/netpulse-secrets.yaml |
| 38 | +# Generated on $(date) - DO NOT commit to version control |
| 39 | +apiVersion: v1 |
| 40 | +kind: Secret |
| 41 | +metadata: |
| 42 | + name: netpulse-secrets |
| 43 | + namespace: netpulse |
| 44 | +type: Opaque |
| 45 | +stringData: |
| 46 | + # Generated secure passwords - DO NOT commit to version control |
| 47 | + password: "$redis_password" |
| 48 | + api-key: "$api_key" |
| 49 | +SECRETSEOF |
| 50 | + |
| 51 | + print_success "Secure secrets generated in k8s/00-secrets-deploy.yaml!" |
| 52 | + print_warning "Your API Key: $api_key" |
| 53 | + print_warning "Your Redis Password: $redis_password" |
| 54 | + print_warning "Please save these credentials securely!" |
| 55 | + print_status "Use: kubectl apply -f k8s/00-secrets-deploy.yaml" |
| 56 | +} |
| 57 | + |
| 58 | +apply_secrets() { |
| 59 | + print_status "Applying secrets to Kubernetes cluster..." |
| 60 | + |
| 61 | + # Check for deploy file first, then fall back to original |
| 62 | + local secrets_file="" |
| 63 | + if [ -f "k8s/00-secrets-deploy.yaml" ]; then |
| 64 | + secrets_file="k8s/00-secrets-deploy.yaml" |
| 65 | + print_status "Using generated secrets file: $secrets_file" |
| 66 | + elif [ -f "k8s/00-secrets.yaml" ]; then |
| 67 | + secrets_file="k8s/00-secrets.yaml" |
| 68 | + print_warning "Using original secrets file: $secrets_file" |
| 69 | + print_warning "Make sure you have updated the placeholder passwords!" |
| 70 | + else |
| 71 | + print_error "No secrets file found. Run 'generate' first or ensure k8s/00-secrets.yaml exists." |
| 72 | + return 1 |
| 73 | + fi |
| 74 | + |
| 75 | + kubectl apply -f "$secrets_file" |
| 76 | + |
| 77 | + print_success "Secrets applied to Kubernetes cluster!" |
| 78 | + |
| 79 | + # Verify secrets were created |
| 80 | + if kubectl get secret netpulse-secrets >/dev/null 2>&1; then |
| 81 | + print_success "Secret 'netpulse-secrets' created successfully" |
| 82 | + else |
| 83 | + print_error "Failed to create secret 'netpulse-secrets'" |
| 84 | + return 1 |
| 85 | + fi |
| 86 | +} |
| 87 | + |
| 88 | +check_prerequisites() { |
| 89 | + print_status "Checking prerequisites..." |
| 90 | + |
| 91 | + # Check if kubectl is available |
| 92 | + if ! command -v kubectl >/dev/null 2>&1; then |
| 93 | + print_error "kubectl is required but not installed" |
| 94 | + return 1 |
| 95 | + fi |
| 96 | + |
| 97 | + # Check if we can connect to cluster |
| 98 | + if ! kubectl cluster-info >/dev/null 2>&1; then |
| 99 | + print_error "Cannot connect to Kubernetes cluster" |
| 100 | + return 1 |
| 101 | + fi |
| 102 | + |
| 103 | + print_success "Prerequisites check passed" |
| 104 | +} |
| 105 | + |
| 106 | +cleanup() { |
| 107 | + print_status "Cleaning up deployment files..." |
| 108 | + |
| 109 | + if [ -f "k8s/00-secrets-deploy.yaml" ]; then |
| 110 | + rm k8s/00-secrets-deploy.yaml |
| 111 | + print_success "Deployment secrets file removed" |
| 112 | + fi |
| 113 | +} |
| 114 | + |
| 115 | +main() { |
| 116 | + echo "NetPulse Kubernetes Secrets Setup" |
| 117 | + echo "==================================" |
| 118 | + |
| 119 | + case "${1:-help}" in |
| 120 | + "generate") |
| 121 | + check_prerequisites |
| 122 | + generate_secure_values |
| 123 | + print_success "Secrets generation complete!" |
| 124 | + print_status "Next step: kubectl apply -f k8s/00-secrets-deploy.yaml" |
| 125 | + ;; |
| 126 | + "apply") |
| 127 | + check_prerequisites |
| 128 | + apply_secrets |
| 129 | + print_success "Secrets applied to cluster!" |
| 130 | + print_status "Next step: kubectl apply -f k8s/01-redis.yaml" |
| 131 | + ;; |
| 132 | + "auto") |
| 133 | + check_prerequisites |
| 134 | + generate_secure_values |
| 135 | + apply_secrets |
| 136 | + print_success "Complete setup finished!" |
| 137 | + print_status "Next step: kubectl apply -f k8s/01-redis.yaml" |
| 138 | + print_warning "Deployment file k8s/00-secrets-deploy.yaml kept for reference" |
| 139 | + print_status "Run '$0 cleanup' to remove deployment files when done" |
| 140 | + ;; |
| 141 | + "cleanup") |
| 142 | + cleanup |
| 143 | + ;; |
| 144 | + "help"|*) |
| 145 | + echo "Usage: $0 {generate|apply|auto|cleanup}" |
| 146 | + echo "" |
| 147 | + echo "Commands:" |
| 148 | + echo " generate - Generate secure secrets file (k8s/00-secrets-deploy.yaml)" |
| 149 | + echo " apply - Apply secrets to Kubernetes cluster (auto-detect file)" |
| 150 | + echo " auto - Generate and apply secrets in one step (recommended)" |
| 151 | + echo " cleanup - Remove deployment files" |
| 152 | + echo "" |
| 153 | + echo "Deployment Options:" |
| 154 | + echo " Option 1 (Auto): $0 auto" |
| 155 | + echo " Option 2 (Manual): Edit k8s/00-secrets.yaml, then kubectl apply -f k8s/00-secrets.yaml" |
| 156 | + echo "" |
| 157 | + echo "File Management:" |
| 158 | + echo " k8s/00-secrets-deploy.yaml - Deployment file (auto-generated, not in git)" |
| 159 | + echo " $0 cleanup - Remove deployment files when done" |
| 160 | + echo "" |
| 161 | + echo "Examples:" |
| 162 | + echo " $0 auto # Generate and apply secrets automatically" |
| 163 | + echo " $0 generate && $0 apply # Generate first, then apply" |
| 164 | + echo " kubectl apply -f k8s/00-secrets.yaml # Use manually edited file" |
| 165 | + echo " $0 cleanup # Clean up temporary files" |
| 166 | + ;; |
| 167 | + esac |
| 168 | +} |
| 169 | + |
| 170 | +main "$@" |
0 commit comments