ansible-roles/smartgears/smartgears_egi_image/templates/smartgears-setup.sh.j2

107 lines
3.2 KiB
Plaintext
Raw Normal View History

#!/bin/bash
SM_DIR={{ smartgears_install_path }}
SM_CONF_F=${SM_DIR}/container.xml
SM_LIB_DIR=${SM_DIR}/lib
SM_STATE_DIR=${SM_DIR}/state/
GET_KEYS_SCRIPT=/usr/local/bin/get-gcube-keys
SVC=tomcat-instance-{{ smartgears_http_port }}
LOG_FILE=/var/log/smartgears-node-setup.log
HOSTS_FILE=/etc/hosts
FQDN_HOST=
PUB_FQDN_HOST=
PUBLIC_IP=
MAIN_IP=
RETVAL=0
{% raw %}
> $LOG_FILE
if [ -z "${HOST}" ] ; then
echo "No hostname was passed from the outside. Trying to find it out by ourselves" >> $LOG_FILE
FQDN_HOST=$( /bin/hostname -f )
if [ -z "${FQDN_HOST}" ] ; then
echo "The machine has no FQDN. Revert to plain hostname" >> $LOG_FILE
HOST=$( /bin/hostname )
echo "Setting the plain hostname: $HOST" >> $LOG_FILE
else
HOST=$FQDN_HOST
fi
fi
# We don't know if we have been assigned a private or public IP address
PUBLIC_IP=$( wget http://ipecho.net/plain -qO - )
MAIN_IP=$( ip addr show scope global dev eth0 | grep inet | awk '{ print $2 }' | cut -d "/" -f 1 )
echo "Our main IP is ${MAIN_IP}" >> $LOG_FILE
if [ -z $PUBLIC_IP ] ; then
# We have some kind of problem
echo "We cannot obtain our public IP from outside. Using our main IP address as public IP" >> $LOG_FILE
PUBLIC_IP=$MAIN_IP
else
echo "Our public IP is ${PUBLIC_IP}" >> $LOG_FILE
fi
# Try to know our public FQDN
TMP_FQDN_HOST=$( host $PUBLIC_IP )
RET_FQDN=$?
if [ $RET_FQDN -eq 0 ] ; then
PUB_FQDN_HOST=$( host $PUBLIC_IP | awk '{print $5}' | sed -e 's/\.$//g' )
fi
# Set the hosts file with the new data
grep -v $HOST $HOSTS_FILE > $HOSTS_FILE.tmp
echo "${PUBLIC_IP} ${FQDN_HOST} ${PUB_FQDN_HOST} ${HOST}" >> $HOSTS_FILE.tmp
mv $HOSTS_FILE.tmp $HOSTS_FILE
chmod 644 $HOSTS_FILE
chown root:root $HOSTS_FILE
if [ -z "${INFRA_NAME}" ] ; then
echo "The infrastructure name is void" >> $LOG_FILE
RETVAL=1
fi
if [ -z "${SCOPE}" ] ; then
echo "The scope is void" >> $LOG_FILE
RETVAL=1
fi
if [ -z "${COUNTRY}" ] ; then
echo "The country code is void" >> $LOG_FILE
RETVAL=1
elif [ ${#COUNTRY} -ne 2 ] ; then
echo "The country code is wrong. It must be two characters long" >> $LOG_FILE
RETVAL=1
fi
if [ $RETVAL -eq 1 ] ; then
echo "Exiting because of errors. The SmartExecutor will not start" >> $LOG_FILE
exit 1
fi
# We use the public FQDN if there's one. Otherwise we use the public IP
if [ ! -z ${PUB_FQDN_HOST} ] ; then
sed -i -e "s#@SMARTGEARS_HOSTNAME@#${PUB_FQDN_HOST}#g" $SM_CONF_F
else
sed -i -e "s#@SMARTGEARS_HOSTNAME@#${PUBLIC_IP}#g" $SM_CONF_F
fi
sed -i -e "s#@SMARTGEARS_INFRASTRUCTURE_NAME@#${INFRA_NAME}#g" $SM_CONF_F
sed -i -e "s#@SMARTGEARS_VO_NAME@#${SCOPE}#g" $SM_CONF_F
sed -i -e "s#@SMARTGEARS_COUNTRY@#${COUNTRY}#g" $SM_CONF_F
sed -i -e "s#@SMARTGEARS_LOCATION@#${LOCATION}#g" $SM_CONF_F
{% endraw %}
# Get and install the gcube security keys
if [ -x $GET_KEYS_SCRIPT ] ; then
echo "Getting the infrastructure keys" >> $LOG_FILE
$GET_KEYS_SCRIPT ${INFRA_NAME} >> $LOG_FILE
rm -f $GET_KEYS_SCRIPT
echo "Done" >> $LOG_FILE
else
echo "Not getting the infrastructure keys, the script is not available." >> $LOG_FILE
fi
/etc/init.d/${SVC} stop
rm -fr ${SM_STATE_DIR}/*
rm -f ${SM_DIR}/ghn.log
sleep 20
/usr/sbin/update-rc.d ${SVC} enable
/etc/init.d/${SVC} start
exit 0