first attempt at an init script

This commit is contained in:
Manuel Friedli 2014-09-24 18:00:16 +02:00
parent 632461a087
commit 3b07f1e019
4 changed files with 183 additions and 193 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

@ -1,139 +1,58 @@
#! /bin/bash
#!/sbin/runscript
### 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
name="GitLab CI Runner"
description="GitLab CI Runner, starts runners in the background"
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"
: ${gitlab_ci_runner_user:=@USER@}
: ${gitlab_ci_runner_base:="@GITLAB_CI_RUNNER_BASE@"}
: ${gitlab_ci_runner_pidfile_base:="@RUN_DIR@/gitlab-ci-runner.pid"}
: ${gitlab_ci_runner_logfile:="@LOGS_DIR@/gitlab-ci-runner.log"}
: ${gitlab_ci_runner_num:=1} # number of runners to spawn
check_pid() {
# Number of registered runners in PID file
[ -f $RUNNERS_PID ] && RUNNERS_REGISTERED=`cat $RUNNERS_PID | wc -l`
bundle_command="/usr/bin/bundle"
bundle_command_args="exec ./bin/runner"
# 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"
depend() {
provide gitlab-ci-runner
}
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"
ebegin "Starting ${name}"
# 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
for (( i=1; i<=${gitlab_ci_runner_num}; i++ )) ; do
einfo "Starting runner ${i} of ${gitlab_ci_runner_num} ..."
local pidfile="${gitlab_ci_runner_pidfile_base}${i}"
checkpath -d -o ${gitlab_ci_runner_user} -m755 "$(dirname "${pidfile}")"
start-stop-daemon --start \
--chdir "${gitlab_ci_runner_base}" \
--user=${gitlab_ci_runner_user} \
--make-pidfile \
--pidfile="${pidfile}" \
--background \
--startas /bin/bash -- -c "exec ${bundle_command} ${bundle_command_args} >> ${gitlab_ci_runner_logfile} 2>&1"
local success=$?
if [ ${success}!=0 ] ; then
eend ${success}
fi
done
fi
eend 0
}
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
ebegin "Stopping ${name}"
# 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
for (( i=1; i<=${gitlab_ci_runner_num}; i++ )) ; do
einfo "Stopping runner ${i} of ${gitlab_ci_runner_num} ..."
local pidfile="${gitlab_ci_runner_pidfile_base}${i}"
start-stop-daemon --stop \
--pidfile="${pidfile}"
# DO WE NEED --exec? or --startas???
local $success=$?
if [ ${success}!=0 ] ; then
eend ${success}
fi
done
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
eend 0
}
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

@ -1,67 +0,0 @@
#!/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 $?
}

View file

@ -127,10 +127,9 @@ all_ruby_install() {
cp "${FILESDIR}/${rcscript}" "${T}" || die
sed -i \
-e "s|@USER@|${MY_USER}|" \
-e "s|@GITLAB_CI_BASE@|${dest}|" \
-e "s|@GITLAB_CI_RUNNER_BASE@|${dest}|" \
-e "s|@LOGS_DIR@|${logs}|" \
-e "s|@RUN_DIR@|${runs}|" \
-e "s|@QUEUES@|${SIDEKIQ_QUEUES}|" \
"${T}/${rcscript}" \
|| die "failed to filter ${rcscript}"