diff --git a/onm/nm-bootstrap-script.sh b/onm/nm-bootstrap-script.sh index 63475d4..6bf2e6c 100644 --- a/onm/nm-bootstrap-script.sh +++ b/onm/nm-bootstrap-script.sh @@ -6,7 +6,7 @@ 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" +LOGFILE="/var/log/onm-bootstrap.$PID.$public_ip.log" # Set up the logging for the script sudo touch $LOGFILE @@ -25,6 +25,36 @@ log_print(){ echo "$level [$(date)]: $Message" >&3 } +# A function to check for the apt lock +Check_lock() { + i=0 + log_print INFO "onm-bootstrap($PID): Checking for apt lock" + while [ `ps aux | grep [l]ock_is_held | wc -l` != 0 ]; do + log_print INFO "onm-bootstrap($PID): Lock_is_held $i" + ps aux | grep [l]ock_is_held + sleep 10 + ((i=i+10)); + done + + log_print INFO "onm-bootstrap($PID): Exited the while loop, time spent: $i" + log_print INFO "onm-bootstrap($PID): ps aux | grep apt" + ps aux | grep apt + log_print INFO "onm-bootstrap($PID): Waiting for lock task ended properly." +} + +# Function to check for the wg command +check_wg_installed() { + log_print "onm-bootstrap($PID): Checking if WireGuard (wg) is installed..." + + # Using command -v to check for the wg command + if command -v wg >/dev/null 2>&1; then + log_print "onm-bootstrap($PID): WireGuard (wg) is installed." + log_print "onm-bootstrap($PID): Location: $(which wg)" + else + log_print "onm-bootstrap($PID): WireGuard (wg) is not installed." + fi +} + # "CREATE" or "DELETE" Overlay Node ACTION=$1 # Define Application Node Type ("MASTER","WORKER") @@ -42,10 +72,24 @@ 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, +# Start the Configuration +log_print INFO "onm-bootstrap ($PID): Configuration started!" +log_print INFO "onm-bootstrap ($PID): Logs are saved at: $LOGFILE" + +log_print INFO "onm-bootstrap($PID): Starting onm-bootstrap 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" +log_print INFO "onm-bootstrap($PID): Installing wireguard and resolvconf..." + +Check_lock +# Install WireGuard package +sudo DEBIAN_FRONTEND=noninteractive apt-get install -y wireguard +sudo DEBIAN_FRONTEND=noninteractive apt-get install -y resolvconf + +# Check if Wireguard is installed +check_wg_installed + # Get the isMaster variable from the environment variable if [ "$NODE_TYPE" == "MASTER" ]; then IS_MASTER="true"; @@ -55,15 +99,15 @@ fi # Check if string1 is equal to string2 if [ "$ACTION" == "CREATE" ]; then - log_print INFO "Creating Wireguard folder to home directory..." + log_print INFO "onm-bootstrap($PID): 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..." + log_print INFO "onm-bootstrap($PID): 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..." + log_print INFO "onm-bootstrap($PID): Moving wireguard.pub file to authorized_keys file..." cat /home/${logged_in_user}/wireguard/wireguard.pub >> /home/${logged_in_user}/.ssh/authorized_keys fi @@ -82,10 +126,15 @@ PAYLOAD=$(cat <