Last active
December 14, 2015 13:29
-
-
Save ianmariano/5093993 to your computer and use it in GitHub Desktop.
/etc/init.d script for chef-server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
### BEGIN INIT INFO | |
# Provides: chef-server | |
# Required-Start: $local_fs $remote_fs $network | |
# Required-Stop: $local_fs $remote_fs $network | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: Control the Chef Server | |
# Description: Control the Chef Server | |
### END INIT INFO | |
LSB_LIBRARY=/lib/lsb/init-functions | |
log_daemon_msg () { | |
echo $@ | |
} | |
log_end_msg () { | |
return $1 | |
} | |
if test -r $LSB_LIBRARY; then | |
. $LSB_LIBRARY | |
fi | |
# Service Names | |
CHEF_EXPANDER="chef-expander" | |
CHEF_SOLR="chef-solr" | |
CHEF_SERVER="chef-server" | |
# Where to put files | |
VAR_DIR=/var/chef # this is default | |
LOG_DIR=/var/log/chef | |
LOCK_DIR=/var/lock/chef | |
PID_DIR=/var/run/chef | |
SRV_DIR=/srv/chef | |
dir_ensure() { | |
if [ ! -d "$1" ]; then | |
mkdir -p $1 | |
chown -R chef:chef $1 | |
chmod -R g+w $1 | |
fi | |
} | |
service_is_running() { | |
ps_output=`ps -p $1|grep $1` | |
if [[ -z "$ps_output" ]]; then | |
return 1 | |
else | |
return 0 | |
fi | |
} | |
chef_service_start() { | |
LOCK_FILE=$LOCK_DIR/$1.lock | |
LOG_FILE=$LOG_DIR/$1.log | |
PID_FILE=$PID_DIR/$1.pid | |
if [ -f $LOCK_FILE ]; then | |
log_end_msg 0 | |
return 0 | |
fi | |
log_daemon_msg "Starting $1.." | |
touch $LOCK_FILE | |
$1 $2 > $LOG_FILE 2>&1 & | |
PID=$! | |
RUNNING=0 | |
for i in {1..30}; do | |
sleep 1 | |
fgrep "$3" $LOG_FILE 2>&1 >/dev/null | |
if [ $? -eq 0 ]; then | |
RUNNING=1 | |
break | |
fi | |
done | |
if [ ${RUNNING} == "0" ]; then | |
rm -f $LOCK_FILE | |
log_daemon_msg "$1 was not started, see $LOG_FILE for more information" | |
log_end_msg 1 | |
return 1 | |
fi | |
echo $PID > $PID_FILE | |
log_end_msg 0 | |
return 0 | |
} | |
chef_service_status() { | |
LOCK_FILE=$LOCK_DIR/$1.lock | |
if [ -f $LOCK_FILE ]; then | |
log_daemon_msg "$1 is running." | |
else | |
log_daemon_msg "$1 is stopped." | |
fi | |
} | |
chef_service_stop() { | |
LOCK_FILE=$LOCK_DIR/$1.lock | |
PID_FILE=$PID_DIR/$1.pid | |
log_daemon_msg "Stopping $1.." | |
if [ -f $LOCK_FILE ]; then | |
PID=`cat $PID_FILE` | |
if service_is_running $PID; then | |
kill $PID | |
fi | |
fi | |
rm -f $PID_FILE | |
rm -f $LOCK_FILE | |
log_end_msg 0 | |
} | |
start_services() { | |
if ! pgrep couchdb > /dev/null 2>&1; then | |
service couchdb start | |
fi | |
if ! pgrep rabbitmq-server > /dev/null 2>&1; then | |
service rabbitmq-server start | |
fi | |
chef_service_start $CHEF_EXPANDER "-n1" "Starting cluster worker 1" | |
chef_service_start $CHEF_SOLR "" "Started SocketConnector" | |
chef_service_start $CHEF_SERVER "-N -u chef -G chef" "Successfully bound to port" | |
} | |
status_services() { | |
chef_service_status $CHEF_SERVER | |
chef_service_status $CHEF_SOLR | |
chef_service_status $CHEF_EXPANDER | |
} | |
stop_services() { | |
chef_service_stop $CHEF_SERVER | |
chef_service_stop $CHEF_SOLR | |
chef_service_stop $CHEF_EXPANDER | |
} | |
dir_ensure $VAR_DIR | |
dir_ensure $LOG_DIR | |
dir_ensure $LOCK_DIR | |
dir_ensure $PID_DIR | |
dir_ensure $SRV_DIR | |
case "$1" in | |
start) | |
start_services | |
;; | |
stop) | |
stop_services | |
;; | |
restart) | |
$0 stop | |
$0 start | |
;; | |
status) | |
status_services | |
;; | |
*) | |
echo "Usage $0 (start|stop|restart|status)" | |
exit 1 | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment