#!/bin/bash if [ -r /etc/default/gitlab ]; then source /etc/default/gitlab fi GITLAB_USER=${GITLAB_USER:-%GITLABUSER%} BUNDLE_BIN=${BUNDLER_BIN:-/usr/bin/bundle} DOCROOT=${DOCROOT:-%DOCROOT%} LOGDIR=${LOGDIR:-%LOGDIR%} UNICORN_PIDFILE=${UNICORN_PIDFILE:-%PIDDIR%/unicorn.pid} RAILS_ENV=${RAILS_ENV:-'%RAILS_ENV%'} ME="$(whoami)" wait_pid() { local pid=${1} while test -d "/proc/${pid}"; do echo -n '.' sleep 1 done } as_user() { if [ "${ME}" != "${GITLAB_USER}" ]; then exec su -l ${GITLAB_USER} -c "${@}" & else exec bash -i -c "${@}" & fi } glunicorn_start() { CMD="${BUNDLE_BIN} exec \"unicorn_rails -c ${DOCROOT%/}/config/unicorn.rb -E production\"" echo "Starting GitLab Unicorn: ${CMD}" CMD="cd ${DOCROOT} && ${CMD} 1>>${LOGDIR%/}/unicorn.stdout.log 2>>${LOGDIR%/}/unicorn.stderr.log" as_user "${CMD}" } glunicorn_stop() { echo -n "Shutting down GitLab Unicorn..." if [ -r ${UNICORN_PIDFILE} ]; then PID=$(cat ${UNICORN_PIDFILE}) MSG=$(kill -QUIT ${PID} 2>&1) if [ ${?} -eq 0 ]; then wait_pid ${PID} echo "done" rm -f ${UNICORN_PIDFILE} &> /dev/null else echo "failed" echo "=== Error message ===" 1>&2 echo "${MSG}" 1>&2 rm -f ${UNICORN_PIDFILE} &> /dev/null fi else echo "failed" echo "PID file ${UNICORN_PIDFILE} is missing" 1>&2 exit 1 fi } glunicorn_restart() { glunicorn_stop glunicorn_start } glunicorn_reload() { echo "Reloading GitLab Unicorn..." if [ -r ${UNICORN_PIDFILE} ]; then kill -USR2 $(cat ${UNICORN_PIDFILE}) else echo "PID file ${UNICORN_PIDFILE} is missing" 1>&2 exit 1 fi } case "${1}" in 'start' ) glunicorn_start ;; 'stop' ) glunicorn_stop ;; 'restart' ) glunicorn_restart ;; 'reload' ) glunicorn_reload ;; *) echo "Usage: ${0} {start|stop|restart}" exit 1 ;; esac exit 0