From ef3f872dd3e635e038a9096f99c7e93c17b03e2f Mon Sep 17 00:00:00 2001
From: Nico Poegel <nico_benjamin.poegel@student.reutlingen-university.de>
Date: Tue, 13 Jun 2023 00:41:19 +0200
Subject: [PATCH] changed the install script to work for root users and other
 users.

---
 README.md          |  2 +-
 install_msa.sh     | 22 +++++++++----
 install_msa_wsl.sh | 79 ----------------------------------------------
 3 files changed, 16 insertions(+), 87 deletions(-)
 delete mode 100644 install_msa_wsl.sh

diff --git a/README.md b/README.md
index 2bc379d..7efc27d 100644
--- a/README.md
+++ b/README.md
@@ -28,7 +28,7 @@ git clone https://gitlab.reutlingen-university.de/poegel/microservice-architectu
 ### Installation with the script
 There are a few ways to install the architecture on a new server. The first one is the easiest, by simply running the [install_msa.sh](install_msa.sh) script. On Linux you can do that with the following command:
 ```
-bash install_msa.sh
+sudo bash install_msa.sh
 ```
 This script installs all the tools necessary to run the architecture on your Ubuntu server. After that, the base microservice architecture is ready for use and you can start implementing your own applications.
 
diff --git a/install_msa.sh b/install_msa.sh
index 9501ce7..d91d34d 100644
--- a/install_msa.sh
+++ b/install_msa.sh
@@ -1,13 +1,14 @@
 #Save directory of the script in variable
 SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
-USER_HOME=$(getent passwd $SUDO_USER | cut -d: -f6)
+USER_HOME=$(getent passwd ${SUDO_USER:-$USER} | cut -d: -f6)
+ORIGINAL_USER=${SUDO_USER:-$USER}
 
 #Download and install K3s, then create a single node cluster
 curl -sfL https://get.k3s.io | sh -
 
 #Wait for availability of Traefik-CRDs in the cluster
-counter=1
-retries=10
+counter=0
+retries=15
 sleeptime=10
 while [ $counter -le $retries ]
 do
@@ -20,17 +21,24 @@ do
          echo CRDs are not yet available, please install helm-chart manually
          exit 1
       fi
-      echo CRDs not yet ready, retrying in $sleeptime seconds
+      echo CRDs not yet ready, retrying in $sleeptime seconds. \(Try $counter/$retries\)
       sleep $sleeptime
    fi
 done
 
+#Edit K3s service environment variable file to change access permission to k3s.yaml if not root
+if [ $ORIGINAL_USER != 'root' ]; then
+   echo User not root, changing access permission for k3s.yaml
+   sudo echo K3S_KUBECONFIG_MODE=\"644\" >> /etc/systemd/system/k3s.service.env
+   sudo systemctl restart k3s
+fi
+
 #Download Linkerd and install onto cluster
 linkerd version
 if [ $? -eq 0 ]; then
    echo Linkerd already installed
 else
-   curl --proto '=https' --tlsv1.2 -sSfL https://run.linkerd.io/install | sh
+   curl --proto '=https' --tlsv1.2 -sSfL https://run.linkerd.io/install | sudo -u $ORIGINAL_USER sh
 fi
 
 export PATH=$PATH:$USER_HOME/.linkerd2/bin
@@ -52,7 +60,7 @@ else
 fi
 
 #Wait for availability of Linkerd-CRDs in the cluster
-counter=1
+counter=0
 retries=10
 sleeptime=10
 while [ $counter -le $retries ]
@@ -66,7 +74,7 @@ do
          echo CRDs are not yet available, please install helm-chart manually
          exit 1
       fi
-      echo CRDs not yet ready, retrying in $sleeptime seconds
+      echo CRDs not yet ready, retrying in $sleeptime seconds. \(Try $counter/$retries\)
       sleep $sleeptime
    fi
 done
diff --git a/install_msa_wsl.sh b/install_msa_wsl.sh
deleted file mode 100644
index 6eb7fde..0000000
--- a/install_msa_wsl.sh
+++ /dev/null
@@ -1,79 +0,0 @@
-#Save directory of the script in variable
-SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
-USER_HOME=$(getent passwd $SUDO_USER | cut -d: -f6)
-
-#Download and install K3s, then create a single node cluster
-curl -sfL https://get.k3s.io | sh -
-
-#Wait for availability of Traefik-CRDs in the cluster
-counter=1
-retries=10
-sleeptime=10
-while [ $counter -le $retries ]
-do
-   ((counter++))
-   kubectl get ingressroute
-   if [ $? -eq 0 ]; then
-      break
-   else
-      if [ $counter -eq $retries ]; then
-         echo CRDs are not yet available, please install helm-chart manually
-         exit 1
-      fi
-      echo CRDs not yet ready, retrying in $sleeptime seconds
-      sleep $sleeptime
-   fi
-done
-
-#Edit K3s service environment variable file to change access permissions to k3s.yaml
-sudo echo K3S_KUBECONFIG_MODE=\"644\" >> /etc/systemd/system/k3s.service.env
-sudo systemctl restart k3s
-
-#Download Linkerd and install onto cluster
-linkerd version
-if [ $? -eq 0 ]; then
-   echo Linkerd already installed
-else
-   curl --proto '=https' --tlsv1.2 -sSfL https://run.linkerd.io/install | sh
-fi
-
-export PATH=$PATH:$USER_HOME/.linkerd2/bin
-export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
-
-linkerd install --crds | kubectl apply -f -
-linkerd install | kubectl apply -f -
-
-#Install Helm if not present
-helm version
-if [ $? -eq 0 ]; then
-   echo Helm already installed
-else
-   curl https://baltocdn.com/helm/signing.asc | gpg --dearmor | sudo tee /usr/share/keyrings/helm.gpg > /dev/null
-   sudo apt-get install apt-transport-https --yes
-   echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/helm.gpg] https://baltocdn.com/helm/stable/debian/ all main" | sudo tee /etc/apt/sources.list.d/helm-stable-debian.list
-   sudo apt-get update
-   sudo apt-get install helm
-fi
-
-#Wait for availability of Linkerd-CRDs in the cluster
-counter=1
-retries=10
-sleeptime=10
-while [ $counter -le $retries ]
-do
-   ((counter++))
-   kubectl get serviceprofiles
-   if [ $? -eq 0 ]; then
-      break
-   else
-      if [ $counter -eq $retries ]; then
-         echo CRDs are not yet available, please install helm-chart manually
-         exit 1
-      fi
-      echo CRDs not yet ready, retrying in $sleeptime seconds
-      sleep $sleeptime
-   fi
-done
-
-#Install Helm-Chart
-helm install helm-msa $SCRIPT_DIR/helm/helm-msa
-- 
GitLab