Files
sailing-analytics/configuration/sailing
T

98 lines
3.8 KiB
Bash
Executable File

#!/bin/bash
#
# sailing Starts sailing services
#
# chkconfig: 2345 95 10
# description: Sailing contains all sailing services
# Source function library.
. /etc/init.d/functions
RETVAL=0
JAVA_START_INSTANCES="server"
SERVERS_DIR=/home/sailing/servers
GIT_REPOSITORY=/home/sailing/code
APACHE_CONFIG_DIR=/etc/httpd/conf.d
rm /var/log/sailing.out
rm /var/log/sailing.err
start_tmux() {
su - sailing -c "/home/sailing/bin/tmuxConsole.sh unattended"
success
}
start_servers() {
NUMBER_OF_INSTANCES=`echo "$JAVA_START_INSTANCES" | wc -w`
MEMORY_PER_INSTANCE_IN_MB=`cat /proc/meminfo | grep MemTotal | awk '{printf("%i\n", ($2 / 1024 * 0.75 - 1500) / '$NUMBER_OF_INSTANCES' );}'`
echo "Using ${MEMORY_PER_INSTANCE_IN_MB}MB as default heap size per instance."
for conf in $JAVA_START_INSTANCES; do
echo "Checking for amazon update..." >>/var/log/sailing.out
echo "MEMORY=\"${MEMORY_PER_INSTANCE_IN_MB}m\"" >>$SERVERS_DIR/$conf/env.sh
su - sailing -c "cd $SERVERS_DIR/$conf && $GIT_REPOSITORY/java/target/refreshInstance.sh auto-install" 2>>/var/log/sailing.err >>/var/log/sailing.out
(
# execute env.sh in a subshell which now has the same set of variables that the server will see
source $SERVERS_DIR/$conf/env.sh
echo "Starting Java server \"$conf\"" >>/var/log/sailing.out
su - sailing -c "cd $SERVERS_DIR/$conf && ./start" 2>>/var/log/sailing.err >>/var/log/sailing.out
server_name="`echo $SERVER_NAME | tr [A-Z] [a-z]`"
echo "Appending macro invocation to $APACHE_CONFIG_DIR/001-events.conf to map internal IP $INSTANCE_INTERNAL_IP4 to plain server running $SERVER_PORT..." >>/var/log/sailing.out
echo "Use Plain-SSL ${INSTANCE_INTERNAL_IP4} 127.0.0.1 $SERVER_PORT" >>$APACHE_CONFIG_DIR/001-events.conf
echo "Appending macro invocation to $APACHE_CONFIG_DIR/001-events.conf to map ${server_name}.sapsailing.com to event with ID $EVENT_ID with server running on local port $SERVER_PORT..." >>/var/log/sailing.out
echo "Use Event-SSL ${server_name}.sapsailing.com \"$EVENT_ID\" 127.0.0.1 $SERVER_PORT" >>$APACHE_CONFIG_DIR/001-events.conf
HTTP_LOGROTATE=/etc/logrotate.d/httpd
echo "Patching $HTTP_LOGROTATE so that old logs go to /var/log/old/$SERVER_NAME/$INSTANCE_IP4" >>/var/log/sailing.out
sed -i -e "s/\/var\/log\/old\/\([^/]*\)\/\([^/ ]*\)/\/var\/log\/old\/$SERVER_NAME\/$INSTANCE_IP4/" $HTTP_LOGROTATE
# terminating the subshell will forget all the variable assignments brought in by env.sh, clearing for the next server instance
)
RETVAL=$?
[ $RETVAL -eq 0 ] && success || failure
done
launch_httpd
}
stop_servers() {
for conf in $JAVA_START_INSTANCES; do
echo "Stopping Java server $conf"
su - sailing -c "cd $SERVERS_DIR/$conf && ./stop"
RETVAL=$?
[ $RETVAL -eq 0 ] && success || failure
done
}
# launch_httpd(server_name, port, event_id)
launch_httpd() {
echo "Will try to launch httpd so this replica can work with an ELB easily." >>/var/log/sailing.out
if [ -x /etc/init.d/httpd ]; then
FIRST_SERVER=`echo $JAVA_START_INSTANCES | awk '{print $1;}'`
source $SERVERS_DIR/$FIRST_SERVER/env.sh
# Append Apache macro invocation for /internal-server-status based on mod_status and INSTANCE_DNS to $APACHE_CONFIG_DIR/001-events.conf
echo "Appending macro usage for $INSTANCE_DNS/internal-server-status URL for mod_status based Apache monitoring to $APACHE_CONFIG_DIR/001-events.conf" >>/var/log/sailing.out
echo "Use Status $INSTANCE_DNS internal-server-status" >>$APACHE_CONFIG_DIR/001-events.conf
echo "Launching httpd..." >>/var/log/sailing.out
service httpd start
else
echo Can't launch httpd; start script doesn't seem to be installed at /etc/init.d/httpd
fi
}
# See how we were called.
case "$1" in
start)
start_servers
;;
stop)
stop_servers
;;
status)
status java
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|status|stop}"
RETVAL=3
esac
exit $RETVAL