#!/bin/bash # This script is used as internalCustomStartupScript to setup the VM of the Azure Scale Set. # This setup is for CentOS 6 VM only, and involves: # - setting up ProActive Node service with PNP # - starting the ProActive Node service # Be sure the variable below is OK, depending on your context # The URL from where to retrieve the JRE tarball JRE_TARBALL_URL="https://s3.amazonaws.com/ci-materials/Latest_jre/jre-8u382b05-linux-x64.tar.gz" set -x set -e yum install -y epel-release sed -i 's/https/http/g' /etc/yum.repos.d/epel.repo # update curl cat > /etc/yum.repos.d/city-fan.repo <\K.*?(?=)' /var/lib/waagent/ovf-env.xml | base64 -d) else JSONCONFIG="" echo "Custom data not found in the system, aborting" exit 1 fi echo $JSONCONFIG export RMURL=`echo $JSONCONFIG | jq '.rmurl' -r` export CREDVALUE=`echo $JSONCONFIG | jq '.credentials' -r` export NODESOURCENAME=`echo $JSONCONFIG | jq '.nodesourcename' -r` export STORAGEACCOUNT=`echo $JSONCONFIG | jq '.storageaccount' -r` export SASKEY=`echo $JSONCONFIG | jq '.saskey' -r` export USERCUSTOMSCRIPTURL=`echo $JSONCONFIG | jq '.usercustomscripturl' -r` export EXTERNALSTORAGEACCOUNT=`echo $JSONCONFIG | jq '.externalstorageaccount' -r` export JVMPARAMETERS=`echo $JSONCONFIG | jq '.jvmparameters' -r` export NODEPRECOMMAND=`echo $JSONCONFIG | jq '.nodeprecommand' -r | sed -e 's/\\\\n/\n/g'` ############################### # Bash function used by the VM to send debug messages to the dedicated Azure Queue ############################### function debug { DEBUGCONTENT=`echo $1 | base64 -w 0` DEBUGMESSAGE="$DEBUGCONTENT" curl -X POST -d "$DEBUGMESSAGE" "https://$STORAGEACCOUNT.queue.core.windows.net/debug/messages?$SASKEY" } # debug debug "$HOSTNAME INFO: CustomData read properly (as I can write this message)" # Override custom script if specified if [ ! -z "$USERCUSTOMSCRIPTURL" ]; then curl -X GET "$USERCUSTOMSCRIPTURL" > user_custom_script.sh chmod +x user_custom_script.sh ./user_custom_script.sh if [ $? -ne 0 ]; then debug "$HOSTNAME FATAL: User custom script exited with error: $?" fi fi # Getting node.jar curl -X GET "https://$STORAGEACCOUNT.blob.core.windows.net/nodefiles/node.jar?$SASKEY" > node.jar # Change ownership to /tmp/node chown -R activeeon:activeeon /tmp/node # debug NOW=`date` debug "$HOSTNAME INFO: Start filling the Table on $NOW" start_time=$(date +%s) # Max time to try de delete msg from queue timeout_minutes=30 isValidMessage="false" while [[ "$isValidMessage" == "false" ]]; do # Read Azure Queue to get node name Deletion is performed on NS Side when nodes are properly registered msg=$(curl "https://$STORAGEACCOUNT.queue.core.windows.net/nodeconfig/messages?visibilitytimeout=300&$SASKEY") JSONMSG=`echo $msg | grep -oP '\K[^<]+' | base64 -d` msgId=`echo $msg | grep -oP '\K[^<]+'` popReceipt=`echo $msg | grep -oP '\K[^<]+'` # Encode popReceipt query param popReceipt=$(echo $popReceipt | jq -R -r @uri) dequeueCount=`echo $msg | grep -oP '\K[^<]+'` NODEBASENAME=`echo $JSONMSG | jq '.nodebasename' -r` NODEINSTANCES=`echo $JSONMSG | jq '.nodeinstances' -r` if [ -z "$NODEBASENAME" ]; then echo "$HOSTNAME ERROR: Unable to retrieve node configuration from 'nodeconfig' queue message $msg" # Check if 30 minutes have passed current_time=$(date +%s) elapsed_time=$((current_time - start_time)) if [ "$elapsed_time" -ge $((timeout_minutes * 60)) ]; then echo "Timeout of $timeout_minutes minutes reached for azure queue deletion." exit 5 fi echo "Retrying..." sleep 10 continue #halt #sleep 9999 #exit -1 fi # Try to delete message from queue URL="https://${STORAGEACCOUNT}.queue.core.windows.net/nodeconfig/messages/${msgId}?popreceipt=${popReceipt}&${SASKEY}" delete_response=$(curl -w "%{http_code}" -X DELETE $URL) echo "$delete_response" http_code=$(tail -n1 <<< "$delete_response") if [[ $http_code -ne 204 ]]; then echo "$HOSTNAME WARN: Could not delete Azure message from queue" fi if [[ "$dequeueCount" == "1" ]]; then isValidMessage="true" fi done # Retrieve instance metadata INSTANCE_NAME=$(curl -H Metadata:true "http://169.254.169.254/metadata/instance/compute?api-version=2017-12-01" | jq .name | sed 's/"//g') INSTANCE_ID=$(echo -n "$INSTANCE_NAME" | rev | cut -d'_' -f1 | rev) # Update Azure Table ENTITY="{'PartitionKey':'$HOSTNAME','RowKey':'$NODEBASENAME', 'NodesCount':'$NODEINSTANCES', 'InstanceId': '$INSTANCE_ID'}" curl -H "Content-Type: application/json" -d "$ENTITY" -X POST "https://$STORAGEACCOUNT.table.core.windows.net/nodesperhost?$SASKEY" if [ $? -ne 0 ]; then debug "$HOSTNAME FATAL: Unable to register the host into 'nodesperhost' table" exit 3 fi NOW=`date` debug "$HOSTNAME INFO: Terminated to fill the Table on $NOW" # prepare py4j yum -y install python-pip pip install py4j pip install numpy # Generate the ExecStartPre commands if they were provided by the user #EXECSTARTPRE='' #if [ ! -z "$NODEPRECOMMAND" ]; then # while read -r line; do # EXECSTARTPRE=$EXECSTARTPRE`echo "ExecStartPre=$line"`$'\n' # done <<< "$NODEPRECOMMAND" #fi # Generate proactive-node systemd service unit file cat > /etc/rc.d/init.d/proactive-node <> /tmp/node/node.log 2>&1 & CURRPID=\$(ps aux|grep java|grep -v "grep"|awk '{ print \$2 }') echo \$CURRPID > \$PIDFILE return 0 } stop() { echo "Shutting down test... " kill -9 \$(cat \$PIDFILE) return 0 } case "\$1" in start) start ;; stop) stop ;; status) ;; restart) stop start ;; reload) ;; *) echo "Usage: proactive-node {start|stop|status|reload|restart}" exit 1 ;; esac exit \$? EOL chmod +x /etc/rc.d/init.d/proactive-node # Debug message posted on debug queue IP=`hostname -i` debug "$HOSTNAME INFO: Service is ready to start: $IP , $NODEBASENAME , $NODEINSTANCES" ############################### ############################### # Let's start the ProActive node service ############################### service proactive-node start