Skip to content

Instantly share code, notes, and snippets.

@n-miyo
Forked from denvazh/gitlab
Last active May 28, 2017 07:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save n-miyo/5957623 to your computer and use it in GitHub Desktop.
Save n-miyo/5957623 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# Written by Denis Vazhenin <denis.vazhenin@me.com>
#
# This script was ported from Debian/Ubuntu version of start script for Gitlab:
# https://raw.github.com/gitlabhq/gitlabhq/master/lib/support/init.d/gitlab
#
# PROVIDE: gitlab
# REQUIRE: NETWORKING SERVERS DAEMON LOGIN
# KEYWORD: shutdown
#
# Requirements:
#
# Before using this script, please install gitlab using guidelines from here:
# https://github.com/gitlabhq/gitlabhq/blob/master/doc/install/installation.md
#
# This file should be placed to: /usr/local/etc/rc.d/gitlab
#
# Add the following lines to /etc/rc.conf to enable gitlab:
#
# Required:
# gitlab_enable="YES"
#
# Optional:
# gitlab_dir
# gitlab_user
# gitlab_env
. /etc/rc.subr
name="gitlab"
rcvar=gitlab_enable
extra_commands="status restart"
load_rc_config $name
###############################################################################
# Set default values (overrides should be placed in /etc/rc.conf )
###############################################################################
: ${gitlab_enable:=NO}
: ${gitlab_dir:=/home/services/git/gitlab}
: ${gitlab_user:=git}
: ${gitlab_group:=git}
: ${gitlab_env:=production}
required_dirs="${gitlib_dir}"
_grep=`command -v grep 2>&1 >/dev/null && command -v grep`
_pgrep=`command -v pgrep 2>&1 >/dev/null && command -v pgrep`
_sh=`command -v sh 2>&1 >/dev/null && command -v sh`
_ps=`command -v ps 2>&1 >/dev/null && command -v ps`
_wc=`command -v wc 2>&1 >/dev/null && command -v wc`
_printf=`command -v printf 2>&1 >/dev/null && command -v printf`
_mkdir=`command -v mkdir 2>&1 >/dev/null && command -v mkdir`
_rm=`command -v rm 2>&1 >/dev/null && command -v rm`
_bundle=/usr/local/bin/bundle
_stop_sidekiq="RAILS_ENV=${gitlab_env} \${_bundle} exec rake sidekiq:stop"
_start_sidekiq="RAILS_ENV=${gitlab_env} \${_bundle} exec rake sidekiq:start"
DAEMON_OPTS="-c ${gitlab_dir}/config/unicorn.rb -E ${gitlab_env}"
PID_PATH="${gitlab_dir}/tmp/pids"
SOCKET_PATH="${gitlab_dir}/tmp/sockets"
WEB_SERVER_PID="${PID_PATH}/unicorn.pid"
SIDEKIQ_PID="${PID_PATH}/sidekiq.pid"
STOP_SIDEKIQ=$(eval "echo ${_stop_sidekiq}")
START_SIDEKIQ=$(eval "echo ${_start_sidekiq}")
start_cmd="${name}_start"
stop_cmd="${name}_stop"
restart_cmd="${name}_restart"
status_cmd="${name}_status"
###############################################################################
# Helper functions
###############################################################################
__pid=0
__spid=0
__status=0
__skiqstat=0
gitlab_findbundler(){
__path='/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin'
if [ ! -f $_bundle ]; then
# clearing any custom path
__oldpath=$PATH
unset PATH
export PATH=$__path
_bundle=`command -v bundle 2>&1 >/dev/null && command -v bundle`
unset PATH
export PATH=$__oldpath
if [ x"${_bundle}" = x ]; then
$_printf 'Unable to find %s command' 'bundle'
exit 1
fi
STOP_SIDEKIQ=$(eval "echo ${_stop_sidekiq}")
START_SIDEKIQ=$(eval "echo ${_start_sidekiq}")
fi
}
gitlab_execute(){
su ${gitlab_user} -c "$1"
}
gitlab_check_if_running(){
# checking if bundle gem is installed before doing anything
gitlab_findbundler
# check if process is already running
if [ -f $WEB_SERVER_PID ]; then
__pid=`${_pgrep} -F $WEB_SERVER_PID`
if [ x"${__pid}" != x ]; then
__status=`${_ps} aux | ${_grep} ${__pid} | ${_grep} -v "grep" | ${_wc} -l`
else
__pid=0
fi
fi
# check if sidekiq process is already running
if [ -f $SIDEKIQ_PID ]; then
__spid=`${_pgrep} -F $SIDEKIQ_PID`
if [ x"${__spid}" != x ]; then
__skiqstat=`${_ps} aux | ${_grep} ${__spid} | ${_grep} -v "grep" | ${_wc} -l`
else
__spid=0
fi
fi
}
gitlab_check_if_root(){
if [ `id -u` -ne 0 ]; then
${_printf} 'Must be a root user (current is %s)\n' `whoami`
exit 1
fi
}
###############################################################################
# Public functions
###############################################################################
gitlab_prestart_check(){
gitlab_check_if_running
# if process is already running, exit with code 1
if [ ${__pid} -ne 0 ] && [ ${__status} -ne 0 ]; then
${_printf} '%s is already running...pid is %s\n' ${name} ${__pid}
exit 1
fi
gitlab_check_if_root
}
gitlab_prestop_check(){
gitlab_check_if_running
# if process is not running, exit with code 1
if [ ${__pid} -eq 0 ] && [ ${__status} -eq 0 ]; then
${_printf} '%s not started\n' ${name}
exit 1
fi
gitlab_check_if_root
}
gitlab_start(){
gitlab_prestart_check
cd ${gitlab_dir}
gitlab_execute "rm -f ${SOCKET_PATH}/gitlab.socket"
gitlab_execute "RAILS_ENV=${gitlab_env} ${_bundle} exec unicorn_rails $DAEMON_OPTS > /dev/null 2>&1 &"
gitlab_execute "${_mkdir} -p $PID_PATH && $START_SIDEKIQ > /dev/null 2>&1 &"
${_printf} '%s started\n' ${name}
}
gitlab_stop(){
gitlab_prestop_check
cd ${gitlab_dir}
kill -QUIT `${_pgrep} -F ${WEB_SERVER_PID}`
gitlab_execute "${_mkdir} -p $PID_PATH && $STOP_SIDEKIQ > /dev/null 2>&1 &"
${_rm} "$WEB_SERVER_PID" >> /dev/null
${_printf} '%s stopped\n' ${name}
}
gitlab_restart(){
gitlab_check_if_root
cd ${gitlab_dir}
${_printf} 'Restarting %s service...\n' ${name}
gitlab_check_if_running
# check if gitlab unicorn service is running, if not then don't kill it
if [ ${__pid} -ne 0 ] && [ ${__status} -ne 0 ]; then
# kill -USR2 `${_pgrep} -F $WEB_SERVER_PID`
kill -QUIT `${_pgrep} -F ${WEB_SERVER_PID}`
${_rm} "$WEB_SERVER_PID" >> /dev/null
fi
# check if background task manager sidekiq is running, if not then don't kill it
if [ ${__spid} -ne 0 ] && [ ${__skiqstat} -ne 0 ]; then
gitlab_execute "${_mkdir} -p $PID_PATH && $STOP_SIDEKIQ > /dev/null 2>&1 &"
fi
# start services normally
gitlab_execute "rm -f ${SOCKET_PATH}/gitlab.socket"
gitlab_execute "RAILS_ENV=${gitlab_env} ${_bundle} exec unicorn_rails $DAEMON_OPTS > /dev/null 2>&1 &"
gitlab_execute "${_mkdir} -p $PID_PATH && $START_SIDEKIQ > /dev/null 2>&1 &"
${_printf} 'Service %s restarted.\n' ${name}
}
gitlab_status(){
cd ${gitlab_dir}
gitlab_check_if_running
# checking if gitlab unicorn service is running
if [ ${__pid} -ne 0 ] && [ ${__status} -ne 0 ]; then
${_printf} '%s service unicorn is running with pid %s\n' ${name} ${__pid}
else
${_printf} '%s service unicorn is not running\n' ${name}
fi
# checking if gitlab sudekiq service is running
if [ ${__spid} -ne 0 ] && [ ${__skiqstat} -ne 0 ]; then
${_printf} '%s service sidekiq is running with pid %s\n' ${name} ${__spid}
else
${_printf} '%s service sidekiq is not running\n' ${name}
fi
}
PATH="${PATH}:/usr/local/bin"
run_rc_command "$1"
@n-miyo
Copy link
Author

n-miyo commented Aug 27, 2013

support GitLab 6.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment