github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/scripts/gitlab/runners/start_runner.sh (about)

     1  #!/bin/bash
     2  
     3  # This script starts or restarts minikube with a specific amount of resources, 
     4  # registers a gitlab runner if necessary, then opens a minikube tunnel 
     5  
     6  # Check if the RUNNER_TOKEN was provided
     7  RUNNER_TOKEN=$1
     8  if [ -z "$RUNNER_TOKEN" ]; then
     9    echo "No runner token provided. No new runner will be registered."
    10  fi
    11  
    12  # Install required packages and make sure the gitlab runner system service is running
    13  sudo ./setup.sh
    14  
    15  GITLAB_HOST="https://gitlab-master.nvidia.com/"
    16  
    17  MINIKUBE_NODES=${MINIKUBE_NODES:-3}
    18  # Requirements for each minikube node
    19  MAX_CPU=16
    20  MAX_RAM=32768
    21  MIN_CPU=2
    22  MIN_RAM=4096
    23  # Reserved for host system
    24  HOST_MEM=2000
    25  HOST_CPU=2
    26  
    27  NUM_CPU=$(nproc --all)
    28  TOTAL_MEM=$(free -m | awk '/^Mem:/{print $2}')
    29  # Reserve 2 cores for the host system, but do not exceed the max number of cores
    30  MINIKUBE_CPU=$(((NUM_CPU-HOST_CPU) / MINIKUBE_NODES))
    31  if [ "$MINIKUBE_CPU" -gt "$MAX_CPU" ]; then
    32    MINIKUBE_CPU=$MAX_CPU
    33  elif [ "$MINIKUBE_CPU" -lt $MIN_CPU ]; then
    34    MINIKUBE_CPU=$MIN_CPU
    35  fi
    36  
    37  # Reserve 2000MB for the host system, but do not exceed the max amount of memory
    38  MINIKUBE_MEMORY=$(((TOTAL_MEM - HOST_MEM) / MINIKUBE_NODES))
    39  if [ "$MINIKUBE_MEMORY" -gt "$MAX_RAM" ]; then
    40    MINIKUBE_MEMORY=$MAX_RAM
    41  elif [ "$MINIKUBE_MEMORY" -lt "$MIN_RAM" ]; then
    42    MINIKUBE_MEMORY=$MIN_RAM
    43  fi
    44  # Must be named this for minikube to pick it up, so we have a consistent config dir
    45  export MINIKUBE_HOME=/var/local/minikube/.minikube
    46  
    47  cleanup_minikube() {
    48    # Find all running minikube tunnel processes and get their PIDs
    49    PIDS=$(ps aux | grep '[m]inikube tunnel' | awk '{print $2}')
    50  
    51    # Check if any PIDs were found
    52    if [ -z "$PIDS" ]; then
    53        echo "No minikube tunnel process found."
    54    else
    55        # Kill the processes
    56        for PID in $PIDS; do
    57            echo "Terminating minikube tunnel process with PID: $PID"
    58            sudo kill -9 $PID
    59            if [ $? -eq 0 ]; then
    60                echo "Process $PID terminated."
    61            else
    62                echo "Failed to terminate process $PID."
    63            fi
    64        done
    65    fi
    66    rm minikube_tunnel.log
    67    minikube stop
    68    minikube delete
    69  }
    70  
    71  # (Re)Start minikube
    72  cleanup_minikube
    73  minikube start --cpus=$MINIKUBE_CPU --memory=$MINIKUBE_MEMORY --nodes=$MINIKUBE_NODES
    74  
    75  # Required for hostPath mounts on multi-node clusters https://minikube.sigs.k8s.io/docs/tutorials/multi_node/
    76  minikube addons enable volumesnapshots
    77  minikube addons enable csi-hostpath-driver
    78  # Cannot build with "minikube docker-env" for multi-node, so use registry
    79  minikube addons enable registry
    80  
    81  # Apply RBAC to allow the default service account admin privileges
    82  kubectl apply -f minikube_rbac.yaml
    83  
    84  # Apply modified coredns config to allow for faster DNS updates
    85  # Useful for tests where we create and destroy new services rapidly
    86  kubectl replace -f coredns_config.yaml
    87  
    88  # Register the runner in short-lived config container if a runner token is provided
    89  if [ -n "$RUNNER_TOKEN" ]; then
    90  echo "Running gitlab-runner register to create config with new token"
    91    sudo gitlab-runner register \
    92    --non-interactive \
    93    --url "$GITLAB_HOST" \
    94    --token "$RUNNER_TOKEN" \
    95    --name test-runner \
    96    --executor kubernetes \
    97    --kubernetes-host "$(minikube ip):8443" \
    98    --kubernetes-image ubuntu:22.04 \
    99    --kubernetes-namespace default \
   100    --kubernetes-cert-file "$MINIKUBE_HOME/profiles/minikube/apiserver.crt" \
   101    --kubernetes-key-file "$MINIKUBE_HOME/profiles/minikube/apiserver.key" \
   102    --kubernetes-ca-file "$MINIKUBE_HOME/ca.crt"
   103  fi
   104  
   105  # Open a minikube tunnel indefinitely
   106  nohup minikube tunnel > minikube_tunnel.log 2>&1 &