From fba603c9a6cb35ec5f64473c21a7bbdf9da73f60 Mon Sep 17 00:00:00 2001
From: pb <pierre.bayle@irt-saintexupery.com>
Date: Thu, 9 Jan 2025 15:31:37 +0100
Subject: [PATCH] adding a script to launch the OC stack, see comments at the
 beginning

---
 stack.sh | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 166 insertions(+)
 create mode 100755 stack.sh

diff --git a/stack.sh b/stack.sh
new file mode 100755
index 0000000..30dd68a
--- /dev/null
+++ b/stack.sh
@@ -0,0 +1,166 @@
+#!/bin/bash
+
+# This script will help you running the different services needed
+# in the open cloud stack :
+#  - checks if directories containing the core services are present 
+#  - allows you to build or not each service image
+#  - launch the containerized service
+
+#  TODO :
+#  - Provide a list of all directories wich contain a dockerfile and choose which to build
+#   - Parallelize building
+#   - Flag building errors without stoping the other and flooding the stdout
+#  - Provide a list of directories with a docker-compose and choose which to launch
+
+# Define the required directories
+REQUIRED_DIRECTORIES=("oc-auth" "oc-catalog" "oc-schedulerd")
+oc_root=""
+
+check_directory() {
+    for dir in "${REQUIRED_DIRECTORIES[@]}"; do
+        if [ ! -d "$dir" ]; then
+            return 1 # Return failure if any required directory is missing
+        fi
+    done
+    return 0 
+}
+
+check_path() {
+    local path="$1"
+
+    if [ -e "$PWD/$path" ]; then
+         cd "$PWD/$path"
+        echo "$PWD"  
+    elif [ -e "$path" ]; then
+        echo "$path"  
+    else
+    
+        echo "$path does not exist"
+        return 1  # Return a non-zero exit status to indicate failure
+    fi
+}
+
+
+create_oc_root_env() {
+   if [ -z ${OC_ROOT} ]; then
+      echo "export OC_ROOT='$1'" >> ~/.bashrc
+      echo "OC_ROOT has been added to your ~/.bashrc file for next executions."
+      echo "Please run 'source ~/.bashrc' or restart your terminal to apply the changes."
+   fi
+}
+
+# Main script
+echo "Verifying the script is being run in the correct directory..."
+sleep 1.1
+echo 
+
+if [ ! -z ${OC_ROOT} ]; then
+   echo "la variable env existe : $OC_ROOT"
+   cd $OC_ROOT
+   oc_root=${OC_ROOT}
+fi
+
+if ! check_directory; then
+    echo "The current directory ($(pwd)) does not contain all required directories:"
+    for dir in "${REQUIRED_DIRECTORIES[@]}"; do
+      echo " - $dir"
+    done
+    echo
+
+    echo "Please ensure the script is run from the correct root directory."
+    read -p "Would you like to specify the path to the correct directory? (y/n): " choice
+
+    if [[ "$choice" =~ ^[Yy]$ ]]; then
+        read -p "Enter the relative or absolute path to the correct directory: " target_path
+        target_path=$(eval echo "$target_path")
+        target_path=$(check_path "$target_path")
+         echo
+         echo "updated path : $target_path"
+         echo 
+
+         cd "$target_path" || { echo "Failed to change directory. Exiting."; exit 1; }
+         if check_directory; then
+               oc_root="$(pwd)"
+               echo "Directory verified successfully. All required directories are present."
+               echo
+               create_oc_root_env "$oc_root"
+               sleep 2.5
+         else
+               echo "The specified directory does not contain all required directories. Exiting."
+               exit 1
+         fi
+      
+    else
+        echo "Please rerun the script from the correct directory. Exiting."
+        exit 1
+    fi
+else
+   echo "Directory verification passed. All required directories are present."
+   create_oc_root_env "$(pwd)"
+fi
+
+arr=("oc-catalog" "oc-datacenter" "oc-peer" "oc-scheduler" "oc-shared" "oc-workflow" "oc-workspace" "oc-auth")
+oc_directories=($(find . -maxdepth 1 -type d -name "oc-*" -exec basename {} \;))
+
+# Check for directories in 'arr' that are missing from 'oc_directories'
+missing_directories=()
+for dir in "${arr[@]}"; do
+    if [[ ! " ${oc_directories[@]} " =~ " $dir " ]]; then
+        missing_directories+=("$dir")
+    fi
+done
+
+if [ ${#missing_directories[@]} -gt 0 ]; then
+    echo "Warning: The following directories are missing and won't be built:"
+    for missing in "${missing_directories[@]}"; do
+        echo "- $missing"
+    done
+
+    read -p "Do you want to proceede with deploy, without these missing components ?[Y/n]: " choice
+      if [[ ! "$choice" =~ ^[Yy]$ ]]; then
+         echo "Exiting the Open Cloud deployment process"
+         exit 0
+      fi
+fi
+
+
+# Continue with the rest of your script
+echo "Executing the main script..."
+
+docker create networks catalog | true
+ 
+docker kill mongo | true
+docker rm mongo | true
+ 
+docker kill nats | true
+docker rm nats | true
+ 
+docker kill loki | true
+docker rm loki | true
+ 
+docker kill graphana | true
+docker rm graphana | true
+ 
+cd ./oc-auth/ldap-hydra && docker compose up -d
+cd ../keto && docker compose up -d
+cd ../../oc-catalog && docker compose -f docker-compose.base.yml up -d
+cd ../oc-schedulerd && docker compose -f docker-compose.tools.yml up -d
+ 
+
+for i in "${oc_directories[@]}"
+do
+   cd ../$i
+   if [ -e "$PWD/Dockerfile" ];then 
+      read -p "Do you want to build the image for $i [y/n] : " build
+      if [[ "$build" =~ ^[Yy]$ ]];then
+         docker build . -t $i
+      fi 
+
+      if [ -e "$PWD/docker-compose.yml" ];then
+         docker compose up -d
+      fi
+   fi
+
+done
+ 
+cd ../oc-schedulerd && go build . && ./oc-schedulerd