Skip to content

Instantly share code, notes, and snippets.

@bamedro
Last active January 12, 2018 15:51
Show Gist options
  • Save bamedro/3ab153e199af50e54fc223c27cd0fbef to your computer and use it in GitHub Desktop.
Save bamedro/3ab153e199af50e54fc223c27cd0fbef to your computer and use it in GitHub Desktop.
#!/bin/bash
set -x
function debug {
DEBUGCONTENT=`echo $1 | base64 -w 0`
DEBUGMESSAGE="<QueueMessage><MessageText>$DEBUGCONTENT</MessageText></QueueMessage>"
curl -X POST -d "$DEBUGMESSAGE" "https://$STORAGEACCOUNT.queue.core.windows.net/debug/messages?$SASKEY"
}
# Downloads and install ProActive Node as a Systemd service
mkdir -p /opt/proactive
cd /opt/proactive
if apt-get --help; then
apt-get update
if ! apt-get install -y wget curl jq; then
sleep 10
apt-get update
if ! apt-get install -y wget curl jq; then
>&2 echo "Fatal error: Unable to run apt-get"
halt
fi
fi
else
yum update
if ! yum -y install wget curl jq; then
sleep 10
yum update
if ! yum -y install wget curl jq; then
>&2 echo "Fatal error: Unable to run yum"
halt
fi
fi
fi
wget --no-clobber https://s3.amazonaws.com/ci-materials/Latest_jre/jre-8u131-linux-x64.tar.gz
tar zxf jre-8u131-linux-x64.tar.gz
ln -s /opt/proactive/jre1.8.0_131 /opt/proactive/java
JSONCONFIG=`base64 -d /var/lib/waagent/CustomData`
echo $JSONCONFIG
#HOST=`echo $JSONCONFIG | jq '.host' -r`
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`
debug "$HOSTNAME INFO: CustomData read properly (as I can write this message)"
if [ ! -z "$USERCUSTOMSCRIPTURL" ]; then
curl -X GET "$USERCUSTOMSCRIPTURL" > user_custom_script.sh
./user_custom_script.sh
if [ $? -ne 0 ]; then
debug "$HOSTNAME FATAL: User custom script exited with error: $?"
halt #sleep 9999 #exit -1
fi
fi
# PNP or PAMR can be infered from RM URL
wget https://gist.githubusercontent.com/bamedro/c6c348af7741a4e09b4239077f5b2d67/raw/node.properties
#PAMRHOST=`echo $HOST | cut -d: -f1`
#PROPERTIES="-Dproactive.net.nolocal=true -Dproactive.communication.protocol=pamr -Dproactive.communication.protocols.order=pamr -Dproactive.pamr.router.address=$PAMRHOST"
PROPERTIES="-Dproactive.net.nolocal=true -Dproactive.communication.protocol=pnp -Dproactive.pnp.heartbeat_factor=10 -Dlog4j.configuration=file:/opt/proactive/node.properties"
# Getting node.jar
#wget --timestamping --tries=10 --timeout=10 --waitretry=10 http://$HOST/rest/node.jar
curl -X GET "https://$STORAGEACCOUNT.blob.core.windows.net/nodefiles/node.jar?$SASKEY" > node.jar
# 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=240&$SASKEY")
JSONMSG=`echo $msg | grep -oP '<MessageText>\K[^<]+' | base64 -d`
msgId=`echo $msg | grep -oP '<MessageId>\K[^<]+'`
popReceipt=`echo $msg | grep -oP '<PopReceipt>\K[^<]+'`
NODEBASENAME=`echo $JSONMSG | jq '.nodebasename' -r`
NODEINSTANCES=`echo $JSONMSG | jq '.nodeinstances' -r`
if [ -z "$NODEBASENAME" ]; then
debug "$HOSTNAME FATAL: Unable to retrieve node configuration from 'nodeconfig' queue"
halt #sleep 9999 #exit -1
fi
NOW=`date`
debug "$HOSTNAME INFO: Start filling the Table on $NOW"
ENTITY="{'PartitionKey':'$HOSTNAME','RowKey':'$NODEBASENAME', 'NodesCount':'$NODEINSTANCES'}"
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"
halt #sleep 9999 #exit -1
fi
NOW=`date`
debug "$HOSTNAME INFO: Terminated to fill the Table on $NOW"
# Generate proactive-node service description
cat > /etc/systemd/system/proactive-node.service <<EOL
[Unit]
After=sshd.service
[Service]
ExecStart=/opt/proactive/java/bin/java -jar /opt/proactive/node.jar ${PROPERTIES} -v ${CREDVALUE} -w ${NODEINSTANCES} -r ${RMURL} -n ${NODEBASENAME} -s ${NODESOURCENAME}
User=activeeon
[Install]
WantedBy=default.target
EOL
chmod 664 /etc/systemd/system/proactive-node.service
# Install ProActive Node Service
systemctl daemon-reload
systemctl enable proactive-node.service
sysctl fs.inotify.max_user_watches=524288 # Support for large numnber of nodes
# Until here, if script fails, another host could reuse this nodeconfig message.
# Once the service is started, if something goes wrong, this node configuration will not be reusable from another host
curl -X DELETE "https://${STORAGEACCOUNT}.queue.core.windows.net/nodeconfig/messages/${msgId}?popreceipt=${popReceipt}&${SASKEY}"
if [ $? -ne 0 ]; then
debug "$HOSTNAME FATAL: Unable to delete nodeconfig from the queue"
halt #sleep 9999 #exit -1
fi
# Debug message posted on debug queue
IP=`hostname -i`
debug "$HOSTNAME INFO: Service is ready to start: $IP , $NODEBASENAME , $NODEINSTANCES"
# Let's start the service
systemctl start proactive-node.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment