From 4b9201e6f51e5356e9a8b67d86361db6634e346d Mon Sep 17 00:00:00 2001 From: Andrea Dell'Amico Date: Wed, 16 Nov 2016 11:39:35 +0100 Subject: [PATCH] library/roles/smartgears/smartgears_egi_image/templates/smartgears-setup.sh.j2: Modifications to the smartgears setup script so it can now handle different java heap sizes based on the available memory. The script now handles the smartgears 2.0 token used to get the scopes tokens. --- .../templates/smartgears-setup.sh.j2 | 249 ++++++++++++------ 1 file changed, 171 insertions(+), 78 deletions(-) diff --git a/smartgears/smartgears_egi_image/templates/smartgears-setup.sh.j2 b/smartgears/smartgears_egi_image/templates/smartgears-setup.sh.j2 index 4f89d4a..8374c14 100755 --- a/smartgears/smartgears_egi_image/templates/smartgears-setup.sh.j2 +++ b/smartgears/smartgears_egi_image/templates/smartgears-setup.sh.j2 @@ -1,11 +1,16 @@ #!/bin/bash SM_DIR={{ smartgears_install_path }} -SM_CONF_F=${SM_DIR}/container.xml +SM_CONF_F={{ smartgears_user_home }}/.containerxml/1-container.xml +SM_CONF_TOKENS_F={{ smartgears_user_home }}/.containerxml/2-container.xml +SM_CONF_TAIL_F={{ smartgears_user_home }}/.containerxml/3-container.xml +SM_CONF_SCOPES_F=/usr/local/etc/scopes.list +SM_CONF_DEST_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 }} +TOMCAT_DEFAULT=/etc/default/${SVC} LOG_FILE=/var/log/smartgears-node-setup.log HOSTS_FILE=/etc/hosts FQDN_HOST= @@ -13,94 +18,182 @@ PUB_FQDN_HOST= PUBLIC_IP= MAIN_IP= RETVAL=0 +MEM=$( free -m | egrep ^Mem | awk '{ print $2 }' ) +JAVA_MAX_HEAP=$( expr $MEM - 2048 ) + +# Init some variables that should be passed +if [ -z "${SMARTGEARS_SCOPES}" ] ; then + SMARTGEARS_SCOPES= +fi +if [ -z "${HOST}" ] ; then + HOST= +fi {% 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 +function setup_hostname() { + echo "-- setup_hostname" >> $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 if available" >> $LOG_FILE + HOST=$( /bin/hostname ) + echo "Setting the plain hostname: $HOST" >> $LOG_FILE + else + HOST=$FQDN_HOST + fi 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 +function find_public_ip() { + echo "-- find_public_ip" >> $LOG_FILE + # 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 +} + +function modify_hosts_file() { + echo "-- modify_hosts_file" >> $LOG_FILE + # Set the hosts file with the new data + grep -v $HOST $HOSTS_FILE > $HOSTS_FILE.tmp + if [ "${FQDN_HOST}" == "${HOST}" ] ; then + FQDN_HOST=${HOST}.localhost + fi + 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 +} + +function smartgears_config_parameters_check() { + echo "-- smartgears_config_parameters_check" >> $LOG_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, setting a default" >> $LOG_FILE + export COUNTRY="IT" + 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 +} + +function populate_head_containerxml() { + echo "-- populate_head_containerxml" >> $LOG_FILE + # 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 + 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 +function set_scopes_list() { + echo "-- set_scopes_list" >> $LOG_FILE + if [ -z "${SMARTGEARS_SCOPES}" ] ; then + SMARTGEARS_SCOPES="" + echo "No scopes were defined" >> $LOG_FILE + else + echo "The scopes list is $SMARTGEARS_SCOPES" >> $LOG_FILE + fi + echo "SCOPES_LIST='${SMARTGEARS_SCOPES}'" > $SM_CONF_SCOPES_F +} -/etc/init.d/${SVC} stop +function get_smartgears_scope_tokens() { + # The SMARTGEARS_TOKEN variable need to be set outside the contextualization script + echo "-- get_smartgears_scope_tokens" >> $LOG_FILE + if [ -z "${SMARTGEARS_TOKEN}" ] ; then + echo "No infrastructure token was passed, aborting" >> $LOG_FILE + echo "" > $SM_CONF_TOKENS_F + else + /usr/local/bin/get-scopes $( echo ${SMARTGEARS_TOKEN}) >> $LOG_FILE + fi +} + +function merge_container_xml() { + echo "-- merge_container_xml: compose the file" >> $LOG_FILE + cat $SM_CONF_F $SM_CONF_TOKENS_F $SM_CONF_TAIL_F > $SM_CONF_DEST_F + chown gcube:gcube $SM_CONF_DEST_F + chmod 640 $SM_CONF_DEST_F +} + +function get_gcube_keys() { + echo "-- get_gcube_keys" >> $LOG_FILE + # 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 +} + +function set_java_heap() { + echo "-- set_java_heap" >> $LOG_FILE + if [ $JAVA_MAX_HEAP -lt 1024 ] ; then + JAVA_MAX_HEAP=1024 + fi + grep -v ^JAVA_HEAP $TOMCAT_DEFAULT > $TOMCAT_DEFAULT.tmp + echo 'JAVA_HEAP="-Xms${JAVA_MAX_HEAP}m -Xmx${JAVA_MAX_HEAP}m"' >> $TOMCAT_DEFAULT.tmp + mv $TOMCAT_DEFAULT.tmp $TOMCAT_DEFAULT +} + + +############################## +# +# Main +# + +/etc/init.d/${SVC} stop >> $LOG_FILE 2>&1 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 + +setup_hostname +find_public_ip +modify_hosts_file +smartgears_config_parameters_check +set_scopes_list +populate_head_containerxml +get_smartgears_scope_tokens +merge_container_xml +get_gcube_keys +set_java_heap + +/usr/sbin/update-rc.d ${SVC} enable >> $LOG_FILE 2>&1 +/etc/init.d/${SVC} start >> $LOG_FILE 2>&1 exit 0 - -