From 5a6c8229c67e2452e8d61ad536997d80f35b9b05 Mon Sep 17 00:00:00 2001 From: meliurwen Date: Thu, 19 Nov 2020 16:33:54 +0100 Subject: [PATCH] Added sudo detection and compatibility in scripts --- README.md | 4 ++++ custom/generate_ssh_cluster.sh | 25 +++++++++++++++++-------- custom/install_kubectl.sh | 31 ++++++++++++++++++++++--------- 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 0003e18..f344c03 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,10 @@ Minimal requirements: Once the unattended installation finished successfully we should prepare the environment inside the VM. +> **user:** `virtualuser` +> +> **password:** `test` + Launch this script: ```sh diff --git a/custom/generate_ssh_cluster.sh b/custom/generate_ssh_cluster.sh index 949dde1..9a63bf1 100755 --- a/custom/generate_ssh_cluster.sh +++ b/custom/generate_ssh_cluster.sh @@ -1,12 +1,21 @@ #!/bin/sh -if [ "$(id -u)" -ne 0 ]; then - echo "This script should be run as root. Aborting..." > /dev/stderr - exit 1 -fi +# This if statement evaulates if the script is launched using sudo AND not +# a `sudo su` logged user +if [ -n "$SUDO_USER" ] && [ "$SUDO_COMMAND" != "/usr/bin/su" ]; then + + echo "Creating SSH key..." + su -c 'ssh-keygen -t rsa -b 4096 -q -N "" -f ~/.ssh/id_rsa' "$SUDO_USER" + + echo "Adding SSH key to authorized..." + su -c 'cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys' "$SUDO_USER" -echo "Creating SSH key..." -ssh-keygen -t rsa -b 4096 -q -N "" -f ~/.ssh/id_rsa +else -echo "Adding SSH key to authorized..." -cat ~/.ssh/rsa.pub >> ~/.ssh/authorized_keys + echo "Creating SSH key..." + ssh-keygen -t rsa -b 4096 -q -N "" -f ~/.ssh/id_rsa + + echo "Adding SSH key to authorized..." + cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys + +fi diff --git a/custom/install_kubectl.sh b/custom/install_kubectl.sh index a29f7a1..a9e741b 100755 --- a/custom/install_kubectl.sh +++ b/custom/install_kubectl.sh @@ -53,12 +53,25 @@ kubeadm config images pull echo "Initializing kubernetes.." kubeadm init -echo "Adding kubernetes config to this user home folder (useful for non-root users)..." -mkdir -p "$HOME/.kube" -cp -i /etc/kubernetes/admin.conf "$HOME/.kube/config" -chown "$(id -u)":"$(id -g)" "$HOME/.kube/config" - -# This last one thanks to: -# https://stackoverflow.com/questions/53814150/how-to-setup-a-2-node-kubernetes-cluster-in-custom-environment - -echo "export KUBECONFIG=/etc/kubernetes/admin.conf" | tee -a ~/.bashrc +# This if statement evaulates if the script is launched using sudo AND not +# a `sudo su` logged user +if [ -n "$SUDO_USER" ] && [ "$SUDO_COMMAND" != "/usr/bin/su" ]; then + SUDO_HOME=$(su -c "echo \$HOME" "$SUDO_USER") + echo "Adding kubernetes config to this user home folder (useful for non-root users)..." + su -c "mkdir -p $SUDO_HOME/.kube" "$SUDO_USER" + cp -i /etc/kubernetes/admin.conf "$SUDO_HOME/.kube/config" + chown "$SUDO_UID":"$SUDO_GID" "$SUDO_HOME/.kube/config" + # This last one thanks to: + # https://stackoverflow.com/questions/53814150/how-to-setup-a-2-node-kubernetes-cluster-in-custom-environment + + su -c "echo \"export KUBECONFIG=/etc/kubernetes/admin.conf\" | tee -a $SUDO_HOME/.bashrc" "$SUDO_USER" +else + echo "Adding kubernetes config to this user home folder (useful for non-root users)..." + mkdir -p "$HOME/.kube" + cp -i /etc/kubernetes/admin.conf "$HOME/.kube/config" + chown "$(id -u)":"$(id -g)" "$HOME/.kube/config" + # This last one thanks to: + # https://stackoverflow.com/questions/53814150/how-to-setup-a-2-node-kubernetes-cluster-in-custom-environment + + echo "export KUBECONFIG=/etc/kubernetes/admin.conf" | tee -a ~/.bashrc +fi