From ac17835f250379312b979d07cf407374ab2d2bba Mon Sep 17 00:00:00 2001 From: jmarchel Date: Fri, 19 Apr 2024 11:02:48 +0200 Subject: [PATCH] Add nm bootstrap script Change-Id: If9421c2ecd8b0608bea2265b1b7f8a731c672f6c --- onm/nm-bootstrap-script.sh | 96 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 onm/nm-bootstrap-script.sh diff --git a/onm/nm-bootstrap-script.sh b/onm/nm-bootstrap-script.sh new file mode 100644 index 0000000..63475d4 --- /dev/null +++ b/onm/nm-bootstrap-script.sh @@ -0,0 +1,96 @@ +#!/bin/bash + +# Get the public IP +public_ip=${5:-$(curl -s http://httpbin.org/ip | grep -oP '(?<="origin": ")[^"]*')} + +# Set up the script variables +STARTTIME=$(date +%s) +PID=$(echo $$) +LOGFILE="/var/log/nm-bootstrap-script.$PID.$public_ip.log" + +# Set up the logging for the script +sudo touch $LOGFILE +sudo chown $USER:$USER $LOGFILE + +# All the output of this shell script is redirected to the LOGFILE +exec 3>&1 4>&2 +trap 'exec 2>&4 1>&3' 0 1 2 3 +exec 1>$LOGFILE 2>&1 + +# A function to print a message to the stdout as well as as the LOGFILE +log_print(){ + level=$1 + Message=$2 + echo "$level [$(date)]: $Message" + echo "$level [$(date)]: $Message" >&3 +} + +# "CREATE" or "DELETE" Overlay Node +ACTION=$1 +# Define Application Node Type ("MASTER","WORKER") +NODE_TYPE=$2 +# Application UUID +APPLICATION_UUID=$3 +# Overlay Network Manager Public IP +ONM_IP=$4 +# SSH Port +SSH_PORT=${6:-22} + +# Get the Application UUID from the environment variable +application_uuid=$APPLICATION_UUID + +# Get the currently logged in user (assuming single user login) +logged_in_user=$(whoami) + +log_print INFO "Starting nm-bootstrap-script with the following parameters: ACTION=$ACTION, NODE_TYPE=$NODE_TYPE, + APPLICATION_UUID=$APPLICATION_UUID, ONM_IP=$ONM_IP, PUBLIC_IP=$public_ip, + LOGGED_IN_USER=$logged_in_user, SSH_PORT=$SSH_PORT" + +# Get the isMaster variable from the environment variable +if [ "$NODE_TYPE" == "MASTER" ]; then + IS_MASTER="true"; +elif [ "$NODE_TYPE" == "WORKER" ]; then + IS_MASTER="false" +fi + +# Check if string1 is equal to string2 +if [ "$ACTION" == "CREATE" ]; then + log_print INFO "Creating Wireguard folder to home directory..." + # Create Wireguard Folder to accept the wireguard scripts + mkdir -p /home/${logged_in_user}/wireguard + + log_print INFO "Creating OpenSSH Public/Private Key Pair..." + # Create OpenSSH Public/Private Key files + ssh-keygen -C wireguard-pub -t rsa -b 4096 -f /home/${logged_in_user}/wireguard/wireguard -N "" + + log_print INFO "Moving wireguard.pub file to authorized_keys file..." + cat /home/${logged_in_user}/wireguard/wireguard.pub >> /home/${logged_in_user}/.ssh/authorized_keys +fi + +PRIVATE_KEY_FILE=$(cat /home/${logged_in_user}/wireguard/wireguard | base64 | tr '\n' ' ') + +PAYLOAD=$(cat <