Files
sailing-analytics/configuration/sailing
T

138 lines
5.7 KiB
Bash
Executable File

#!/bin/bash
#
# sailing Starts sailing services
#
# chkconfig: 2345 95 10
# description: Sailing contains all sailing services
#
### BEGIN INIT INFO
# Provides: sailing
# Required-Start: $local_fs $network $named $remote_fs
# Should-Start:
# Required-Stop:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: The sailing service
# Description: Start all sailing services required for this instance
### END INIT INFO
# 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
echo "Executing with $1" >>/var/log/sailing.out
start_tmux() {
su - sailing -c "/home/sailing/bin/tmuxConsole.sh unattended"
success
}
start_servers() {
su - sailing -c "cd $GIT_REPOSITORY && git fetch && git merge origin/master" 2>>/var/log/sailing.err >>/var/log/sailing.out
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 "## EVENT ${server_name}" >>$APACHE_CONFIG_DIR/001-events.conf
echo "Use Event-SSL ${server_name}.sapsailing.com \"$EVENT_ID\" 127.0.0.1 $SERVER_PORT" >>$APACHE_CONFIG_DIR/001-events.conf
# check if an secondary event shall be pasted to 001-events.conf
if [[ ${HTTP_SEC_EVENT} && ${HTTP_SEC_EVENT_ID} ]]; then
http_sec_event="`echo $HTTP_SEC_EVENT | tr [A-Z] [a-z]`"
echo "Appending macro invocation to $APACHE_CONFIG_DIR/001-events.conf to map ${http_sec_event}.sapsailing.com to event with ID ${HTTP_SEC_EVENT_ID} with server running on local port $SERVER_PORT..." >>/var/log/sailing.out
echo "## EVENT ${http_sec_event}" >>$APACHE_CONFIG_DIR/001-events.conf
echo "Use Event ${http_sec_event}.sapsailing.com \"$HTTP_SEC_EVENT_ID\" 127.0.0.1 $SERVER_PORT" >>$APACHE_CONFIG_DIR/001-events.conf
echo "Use Event-SSL ${http_sec_event}.sapsailing.com \"$HTTP_SEC_EVENT_ID\" 127.0.0.1 $SERVER_PORT" >>$APACHE_CONFIG_DIR/001-events.conf
fi
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" >> /var/log/sailing.out
su - sailing -c "cd $SERVERS_DIR/$conf && ./stop"
RETVAL=$?
[ $RETVAL -eq 0 ] && success || failure
stop_httpd
sync_logs
done
}
sync_logs() {
echo "Executing logrotate followed by a sync to ensure that logs are synchronized" >> /var/log/sailing.out
logrotate -f /etc/logrotate.conf
sync
}
# 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 "## SERVER STATUS" >>$APACHE_CONFIG_DIR/001-events.conf
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
}
stop_httpd() {
if [ -x /etc/init.d/httpd ]; then
service httpd stop
echo "Stopped httpd..." >>/var/log/sailing.out
fi
}
# See how we were called.
case "$1" in
start)
/usr/sbin/update-motd
start_servers
touch /var/lock/subsys/sailing
;;
stop)
stop_servers
rm -f /var/lock/subsys/sailing
;;
status)
status java
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|status|stop}"
RETVAL=3
esac
exit $RETVAL