now i need to write the init script

This commit is contained in:
Manuel Friedli 2014-09-24 15:37:27 +02:00
parent 43cc153d42
commit 632461a087
4 changed files with 213 additions and 64 deletions

View file

@ -0,0 +1,139 @@
#! /bin/bash
### BEGIN INIT INFO
# Provides: gitlab-ci-runner
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: GitLab CI Runner init.d script
# Description: Enables automatic start of runners at boot time in the background.
### END INIT INFO
APP_ROOT="/home/gitlab_ci_runner/gitlab-ci-runner"
APP_USER="gitlab_ci_runner"
PID_PATH="$APP_ROOT/tmp/pids"
PROCESS_NAME="ruby ./bin/runner"
RUNNERS_PID="$PID_PATH/runners.pid"
RUNNERS_NUM=1 # number of runners to spawn
START_RUNNER="nohup bundle exec ./bin/runner"
NAME="gitlab-ci-runner"
DESC="GitLab CI runner"
RUNNERS_REGISTERED=0
RUNNERS_RUNNING=0
INIT_LOG="/var/log/gitlab_ci_runner.log"
check_pid() {
# Number of registered runners in PID file
[ -f $RUNNERS_PID ] && RUNNERS_REGISTERED=`cat $RUNNERS_PID | wc -l`
# Number of active runner processes
RUNNERS_RUNNING=`ps -ef | grep "$PROCESS_NAME" | grep -v grep | wc -l`
echo "Number of registered runners in PID file=$RUNNERS_REGISTERED"
echo "Number of running runners=$RUNNERS_RUNNING"
}
execute() {
sudo -u $APP_USER -H bash -l -c "$1"
}
start() {
cd $APP_ROOT
check_pid
if [ "$RUNNERS_REGISTERED" -ne 0 -o "$RUNNERS_RUNNING" -ne 0 ]; then
# Program is running, Exit with error code.
echo "Error! $DESC(s) ($NAME) appear to be running already! Try stopping them first. Exiting."
exit 1
else
if [ `whoami` = root ]; then
[ ! -f $PID_PATH ] && execute "mkdir -p $PID_PATH"
[ -f $RUNNERS_PID ] && execute "rm -f $RUNNERS_PID"
# Spawn runners
for (( i=1; i<=$RUNNERS_NUM; i++ ))
do
# Check log file
if [ ! -f $INIT_LOG ]; then
touch $INIT_LOG
chown $APP_USER $INIT_LOG
fi
echo "Starting runner #$i"
execute "$START_RUNNER >> $INIT_LOG 2>&1 & echo \$! >> $RUNNERS_PID"
done
echo "SUCCESS: Started $RUNNERS_NUM $DESC(s)."
fi
fi
}
stop() {
check_pid
# Exit if there are no runners
if [ $RUNNERS_REGISTERED -eq 0 -a $RUNNERS_RUNNING -eq 0 ]; then
echo "No runners have been found. Exiting."
fi
# Runners found. Check if there are any ghost runners.
KILL_GHOSTS=0;
if [ $RUNNERS_REGISTERED -ne $RUNNERS_RUNNING ]; then
echo "WARNING: Numbers of registered runners don't match number of running runners. Will try to stop them all"
echo "Registered runners=$RUNNERS_REGISTERED"
echo "Running runners=$RUNNERS_RUNNING"
KILL_GHOSTS=1;
fi
echo -n "Trying to stop registered runners..."
if [ $RUNNERS_REGISTERED -gt 0 ]; then
execute "cat $RUNNERS_PID | xargs kill -USR2"
rm -f $RUNNERS_PID
echo "OK"
else
echo "FAILED!"
echo "Couldn't stop registered runners as there is no record of such in $RUNNERS_PID file".
fi
if [ $KILL_GHOSTS -eq 1 ]; then
echo -ne "Trying to kill ghost runners..."
ps -C "$PROCESS_NAME" -o "%p" h | xargs kill -USR2
[ $? -eq 0 ] && echo "OK"
else
echo "No ghost runners have been found.This is good."
fi
}
status() {
echo "Here is what we have at the moment:"
check_pid
}
## Check to see if we are running as root first.
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root"
exit 1
fi
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: sudo service gitlab_ci_runner {start|stop|restart|status}" >&2
exit 1
;;
esac
exit 0

View file

@ -0,0 +1,67 @@
#!/sbin/runscript
name="GitLab CI"
description="GitLab CI on Unicorns"
: ${gitlab_ci_user:=@USER@}
: ${gitlab_ci_base:="@GITLAB_CI_BASE@"}
: ${rails_env:=production}
: ${server_pidfile:="@RUN_DIR@/unicorn.pid"}
: ${sidekiq_pidfile:="@RUN_DIR@/sidekiq.pid"}
: ${sidekiq_logfile:="@LOGS_DIR@/sidekiq.log"}
: ${sidekiq_queues:="@QUEUES@"}
server_command="/usr/bin/bundle"
server_command_args="exec unicorn_rails -c ${gitlab_ci_base}/config/unicorn.rb -E ${rails_env} -D"
sidekiq_command="/usr/bin/bundle"
sidekiq_command_args="exec sidekiq -q ${sidekiq_queues} -P ${sidekiq_pidfile} -L ${sidekiq_logfile}"
depend() {
provide gitlab-ci
need redis
use net
}
start() {
ebegin "Starting ${name} - Unicorn servers"
checkpath -d -o ${gitlab_ci_user} -m755 "$(dirname "${server_pidfile}")"
checkpath -d -o ${gitlab_ci_user} -m755 "$(dirname "${sidekiq_pidfile}")"
start-stop-daemon --start \
--chdir "${gitlab_ci_base}" \
--user=${gitlab_ci_user} \
--pidfile="${server_pidfile}" \
--env RAILS_ENV=${rails_env} \
--exec ${server_command} -- ${server_command_args}
eend $?
ebegin "Starting ${name} - Sidekiq"
start-stop-daemon --start \
--background --quiet \
--chdir "${gitlab_ci_base}" \
--user=${gitlab_ci_user} \
--pidfile="${sidekiq_pidfile}" \
--env RAILS_ENV=${rails_env} \
--exec ${sidekiq_command} -- ${sidekiq_command_args}
eend $?
}
stop() {
ebegin "Stopping ${name} - Sidekiq"
start-stop-daemon --stop \
--pidfile=${sidekiq_pidfile} \
--exec ${sidekiq_command}
eend $?
ebegin "Stopping ${name} - Unicorn servers"
start-stop-daemon --stop \
--signal QUIT \
--pidfile=${server_pidfile} \
--exec ${server_command}
eend $?
}