|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Define Prometheus Helm repository URL |
| 4 | +prometheus_repo="https://prometheus-community.github.io/helm-charts" |
| 5 | + |
| 6 | +# Function to check if namespace exists |
| 7 | +check_namespace() { |
| 8 | + local namespace="$1" |
| 9 | + if kubectl get namespace "$namespace" &> /dev/null; then |
| 10 | + return 0 |
| 11 | + else |
| 12 | + return 1 |
| 13 | + fi |
| 14 | +} |
| 15 | + |
| 16 | +# Function to create namespace |
| 17 | +create_namespace() { |
| 18 | + local namespace="$1" |
| 19 | + echo "Creating namespace $namespace..." |
| 20 | + kubectl create namespace "$namespace" |
| 21 | +} |
| 22 | + |
| 23 | +# Function to install Prometheus if not already installed |
| 24 | +install_prometheus() { |
| 25 | + local namespace="monitoring" |
| 26 | + if check_namespace "$namespace"; then |
| 27 | + if helm list -n "$namespace" | grep -q "prometheus"; then |
| 28 | + echo "Prometheus is already installed in namespace $namespace. You can upgrade." |
| 29 | + else |
| 30 | + echo "Installing Prometheus in namespace $namespace..." |
| 31 | + helm install prometheus prometheus-community/kube-prometheus-stack -n "$namespace" -f config/grafana.yml -f config/prometheus.yml -f config/alert-manager.yml |
| 32 | + echo "Prometheus installed successfully." |
| 33 | + fi |
| 34 | + else |
| 35 | + create_namespace "$namespace" |
| 36 | + echo "Namespace $namespace created." |
| 37 | + kubectl apply -f grafana-secret.yml |
| 38 | + echo "Secret for grafana configured." |
| 39 | + install_prometheus |
| 40 | + fi |
| 41 | +} |
| 42 | + |
| 43 | +# Function to upgrade Prometheus |
| 44 | +upgrade_prometheus() { |
| 45 | + local namespace="monitoring" |
| 46 | + kubectl apply -f grafana-secret.yml |
| 47 | + echo "Secret for grafana configured." |
| 48 | + echo "Upgrading Prometheus in namespace $namespace..." |
| 49 | + helm upgrade prometheus prometheus-community/kube-prometheus-stack -n "$namespace" -f config/grafana.yml -f config/prometheus.yml -f config/alert-manager.yml |
| 50 | + echo "Prometheus upgraded successfully." |
| 51 | +} |
| 52 | + |
| 53 | +# Function to uninstall Prometheus |
| 54 | +uninstall_prometheus() { |
| 55 | + local namespace="monitoring" |
| 56 | + echo "Are you sure you want to uninstall Prometheus from namespace $namespace? (yes/no)" |
| 57 | + read confirm |
| 58 | + case "$confirm" in |
| 59 | + yes) |
| 60 | + echo "Uninstalling Prometheus from namespace $namespace..." |
| 61 | + helm uninstall prometheus -n "$namespace" |
| 62 | + echo "Prometheus uninstalled successfully." |
| 63 | + ;; |
| 64 | + *) |
| 65 | + echo "Uninstallation cancelled." |
| 66 | + ;; |
| 67 | + esac |
| 68 | +} |
| 69 | + |
| 70 | +# Prompt user for action |
| 71 | +echo "Do you want to install, upgrade, or uninstall Prometheus? (install/upgrade/uninstall)" |
| 72 | +read action |
| 73 | + |
| 74 | +# Perform action based on user input |
| 75 | +case "$action" in |
| 76 | + install) |
| 77 | + # Update Helm Repositories |
| 78 | + echo "Updating Helm repositories..." |
| 79 | + helm repo update |
| 80 | + install_prometheus |
| 81 | + ;; |
| 82 | + upgrade) |
| 83 | + upgrade_prometheus |
| 84 | + ;; |
| 85 | + uninstall) |
| 86 | + uninstall_prometheus |
| 87 | + ;; |
| 88 | + *) |
| 89 | + echo "Invalid action. Please choose 'install', 'upgrade', or 'uninstall'." |
| 90 | + ;; |
| 91 | +esac |
0 commit comments