Skip to content

Instantly share code, notes, and snippets.

@jlsherrill
Created July 11, 2014 18:35
Show Gist options
  • Save jlsherrill/b4488a60823208132f80 to your computer and use it in GitHub Desktop.
Save jlsherrill/b4488a60823208132f80 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Copyright 2013 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public
# License as published by the Free Software Foundation; either version
# 2 of the License (GPLv2) or (at your option) any later version.
# There is NO WARRANTY for this software, express or implied,
# including the implied warranties of MERCHANTABILITY,
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
# have received a copy of GPLv2 along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
SERVICE=$1
COMMAND=$2
PLUGINS_DIR=${PLUGINS_DIR:-"$(dirname $(readlink -f $BASH_SOURCE))/../plugins"}
# number of attemts to check the service
WAIT_MAX=${WAIT_MAX:-30}
# detault delay between attempts
SLEEP=${SLEEP:-1}
# Plugins DSL
# ===========
# specify a condition indicating that this plugin is enabled for the service
confine () {
eval "$@"
ENABLE_PLUGIN=$?
}
# specify that the exit code of the service command is not relevant
# for the status of the service
ignore-retval () {
if [ $ENABLE_PLUGIN -eq 0 ]; then
RETVAL=0
fi
}
# Waiting Helpers
# ===============
wait-for-url () {
echo "Wait for URL"
# we use wget first because it's able to retry from connrefused situation
/usr/bin/wget --timeout=1 --tries=$WAIT_MAX --retry-connrefused -qO-\
--no-check-certificate $1 > /dev/null
# then we double check with curl that the service is really
# available. wget is not able to retry from 'Unable to establish
# SSL connection.' error, which happens for example with tomcat6
echo /usr/bin/curl -ks --retry $WAIT_MAX --retry-delay 1 $1
/usr/bin/curl -ks --retry $WAIT_MAX --retry-delay 1 $1
if ! [ $? = '0' ]; then
echo "Wait for URL FAILED"
RETVAL=5
fi
echo "Wait for URL SUCCESS"
}
wait-for-port () {
echo "Wait for port"
success=0
for i in $(seq 1 $WAIT_MAX); do
echo "netstat -ln | grep -q ':$1\s'"
if netstat -ln | grep -q ":$1\s"; then
echo "Port found"
return # SUCCESS
else
echo "Sleeping"
service-wait-sleep
fi
done
echo "Wait for port failed"
RETVAL=6
}
wait-for-command () {
for i in $(seq 1 $WAIT_MAX); do
if "$@" &> /dev/null; then
return # SUCCESS
else
service-wait-sleep
fi
done
RETVAL=7
}
service-wait-sleep () {
sleep $SLEEP
}
# Execution
# =========
/sbin/service "$SERVICE" "$COMMAND"
SERVICE_RETVAL=$?
echo SERVICE_RETVAL: $SERVICE_RETVAL
reset-defaults () {
RETVAL=$SERVICE_RETVAL
ENABLE_PLUGIN=1
}
evaluate-plugins() {
for plugin in $(ls $PLUGINS_DIR); do
evaluate-plugin $PLUGINS_DIR/$plugin
done
}
evaluate-plugin () {
reset-defaults
source $1
if [ $RETVAL -eq 0 -a $ENABLE_PLUGIN -eq 0 ]; then
service-wait
echo "Post service-wait $RETVAL"
if ! [ $RETVAL -eq 0 ]; then
exit $RETVAL
fi
fi
}
case "$COMMAND" in
start|restart)
evaluate-plugins;;
*)
RETVAL=$SERVICE_RETVAL
esac
echo "Final exit $RETVAL"
exit $RETVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment