Sunday, 2 March 2014

Generic multi instance startup script

Currently working on creating a generic multi instance start up script for TC server  (tomcat).
Features should include:
1- startall
2- stopall
3- start a set of TCs serving same part of the business
4- stop a set of TCs serving same part of the business
5- start 1 instance
6- stop 1 instance
7- print status of all TC instances

Once done will post the script here.
The script used a config file, will post it also :)

#set -x

if [ -z ${1} ]
then
        echo "missing property file"
        exit 1
fi
####
## need to initalize the APPS Associative array here.
typeset -A APPS
source ${1}

###
# Display the apps :)
#echo ${!APPS[@]}



####
## Setting action menu
actions="start stop status stopAll startAllDefault start_selective stop_selective"
if [ "$2" != "" ]; then
  action=$2
else
  echo "Please select an action:"
  select action in ${actions}; do
    case ${REPLY} in
      1|2|3|4|5|6|7) echo "Selection: ${action}"; break;;
      *) echo "Invalid Selection. Exiting."; exit 1;;
    esac
  done
fi
 
#set -x

if [ -z ${1} ]
then
        echo "missing property file"
        exit 1
fi
####
## need to initalize the APPS Associative array here.
typeset -A APPS
source ${1}

###
# Display the apps :)
#echo ${!APPS[@]}



####
## Setting action menu
actions="start stop status stopAll startAllDefault start_selective stop_selective"
if [ "$2" != "" ]; then
  action=$2
else
  echo "Please select an action:"
  select action in ${actions}; do
    case ${REPLY} in
      1|2|3|4|5|6|7) echo "Selection: ${action}"; break;;
      *) echo "Invalid Selection. Exiting."; exit 1;;
    esac
  done
fi
 
start|stop|status)
                for instance in `echo ${APP_SERVER_RUN_DEFAULT}|tr "," " "`
                do
                        HOST=`echo ${instance} |cut -d"_" -f1`
                        PORT=`echo ${instance} |cut -d"_" -f2`
                        echo ""
                        echo "Working on ${HOST}"
                        read -n1 -p "Do you want to preform the action: ${action} on ${instance} ?" reply
                        if [ "${reply}" = "y" ]
                        then
                                echo ""
                                ssh -q ${TARGET_USER_NAME}@${HOST} ${TARGET_TCS_INST_HOME}/${instance}/bin/tcruntime-ctl.sh ${action}
                        else
                                echo ""
                                echo "no action on ${instance}"
                        fi
                done
                echo "All Done"
        ;;

        start_selective)


                select APPkey in ${!APPS[@]}
                do
                        echo "${APPkey}: ${APPS[${APPkey}]}";
                        action=start
                        for instance in `echo ${APP_SERVER_RUN_DEFAULT}|tr "," " "`
                        do
                                HOST=`echo ${instance} |cut -d"_" -f1`
                                PORT=`echo ${instance} |cut -d"_" -f2`
                                FOUND=`echo ${APPS[${APPkey}]} |grep ${PORT} |wc -l`
                                #echo $FOUND
                                if [ ${FOUND} = "0" ]
                                then
                                        echo "skipping"
                                else
                                        echo ""
                                        echo "Working on ${HOST}"
read -n1 -p "Do you want to preform the action: ${action} on ${instance} ?" reply
                                        if [ "${reply}" = "y" ]
                                        then
                                                echo ""
                                                ssh -q ${TARGET_USER_NAME}@${HOST} ${TARGET_TCS_INST_HOME}/${instance}/bin/tcruntime-ctl.sh ${action}
                                        else
                                                echo ""
                                                echo "no action on ${instance}"
                                        fi
                                fi
                        done

                        #break the select loop
                        break
                done
        ;;

        stop_selective)

                select APPkey in ${!APPS[@]}
                do
                        echo "${APPkey}: ${APPS[${APPkey}]}";
                        action=stop
                        for instance in `echo ${APP_SERVER_RUN_DEFAULT}|tr "," " "`
                        do
                                HOST=`echo ${instance} |cut -d"_" -f1`
                                PORT=`echo ${instance} |cut -d"_" -f2`
                                FOUND=`echo ${APPS[${APPkey}]} |grep ${PORT} |wc -l`
                                #echo $FOUND
                                if [ ${FOUND} = "0" ]
                                then
                                        echo "skipping"
                                else
                                        echo ""
                                        echo "Working on ${HOST}"
                                        read -n1 -p "Do you want to preform the action: ${action} on ${instance} ?" reply
                                        if [ "${reply}" = "y" ]
                                        then
echo ""
                                                ssh -q ${TARGET_USER_NAME}@${HOST} ${TARGET_TCS_INST_HOME}/${instance}/bin/tcruntime-ctl.sh ${action}
                                                ssh -q ${TARGET_USER_NAME}@${HOST} rm -rf ${TARGET_TCS_INST_HOME}/${instance}/work/*
                                                ssh -q ${TARGET_USER_NAME}@${HOST} rm -rf ${TARGET_TCS_INST_HOME}/${instance}/temp/*
                                                ssh -q ${TARGET_USER_NAME}@${HOST} "find ${TARGET_TCS_INST_HOME}/${instance}/webapps/* -type d|grep -v ROOT|xargs rm -rf"
                                        else
                                                echo ""
                                                echo "no action on ${instance}"
                                        fi
                                fi
                        done

                        #break the select loop
                        break
                done

        ;;


esac
 

1 comment:

  1. Seems like i didn't upload the config file and it is lost from my Centos VM :(
    that's unfortunate.

    ReplyDelete