Skip to content
Snippets Groups Projects
Commit 515adbc4 authored by Rovanion's avatar Rovanion Committed by Wes Gurney
Browse files

Rewrote init script.

parent 060abcb3
No related branches found
No related tags found
1 merge request!4954Add support to configure webhook_timeout in gitlab.yaml
v 6.1.0
- Rewrite: Init script now less prone to errors and keeps better track of the service.
- Link issues, merge requests, and commits when they reference each other with GFM
- Close issues automatically when pushing commits with a special message
- Project internal ids for issues, mr, milestones
Loading
Loading
#! /bin/bash
#! /bin/sh
 
# GITLAB
# Maintainer: @randx
# Authors: rovanion.luckey@gmail.com, @randx
# App Version: 6.0
 
### BEGIN INIT INFO
Loading
Loading
@@ -14,102 +15,198 @@
# Description: GitLab git repository management
### END INIT INFO
 
### Environment variables
RAILS_ENV="production"
 
APP_ROOT="/home/git/gitlab"
APP_USER="git"
DAEMON_OPTS="-c $APP_ROOT/config/unicorn.rb -E production"
PID_PATH="$APP_ROOT/tmp/pids"
SOCKET_PATH="$APP_ROOT/tmp/sockets"
WEB_SERVER_PID="$PID_PATH/unicorn.pid"
SIDEKIQ_PID="$PID_PATH/sidekiq.pid"
STOP_SIDEKIQ="RAILS_ENV=production bundle exec rake sidekiq:stop"
START_SIDEKIQ="RAILS_ENV=production bundle exec rake sidekiq:start"
NAME="gitlab"
DESC="GitLab service"
check_pid(){
if [ -f $WEB_SERVER_PID ]; then
PID=`cat $WEB_SERVER_PID`
SPID=`cat $SIDEKIQ_PID`
STATUS=`ps aux | grep $PID | grep -v grep | wc -l`
# Script variable names should be lower-case not to conflict with internal
# /bin/sh variables such as PATH, EDITOR or SHELL.
app_root="/home/gitlab/gitlab"
app_user="gitlab"
unicorn_conf="$app_root/config/unicorn.rb"
pid_path="$app_root/tmp/pids"
socket_path="$app_root/tmp/sockets"
web_server_pid_path="$pid_path/unicorn.pid"
sidekiq_pid_path="$pid_path/sidekiq.pid"
### Here ends user configuration ###
# Switch to the app_user if it is not he/she who is running the script.
if [ "$USER" != "$app_user" ]; then
sudo -u "$app_user" -H /etc/init.d/gitlab "$@"; exit;
fi
# Switch to the gitlab path, if it fails exit with an error.
if ! cd "$app_root" ; then
echo "Failed to cd into $app_root, exiting!"; exit 1
fi
### Init Script functions
check_pids(){
if ! mkdir -p "$pid_path"; then
echo "Could not create the path $pid_path needed to store the pids."
exit 1
fi
# If there exists a file which should hold the value of the Unicorn pid: read it.
if [ -f "$web_server_pid_path" ]; then
wpid=$(cat "$web_server_pid_path")
else
wpid=0
fi
if [ -f "$sidekiq_pid_path" ]; then
spid=$(cat "$sidekiq_pid_path")
else
STATUS=0
PID=0
spid=0
fi
}
 
execute() {
sudo -u $APP_USER -H bash -l -c "$1"
# We use the pids in so many parts of the script it makes sense to always check them.
# Only after start() is run should the pids change. Sidekiq sets it's own pid.
check_pids
# Checks wether the different parts of the server is already running or not.
check_status(){
check_pids
# If the web server is running kill -0 $wpid returns true, or rather 0.
# Checks of *_status should only check for == 0 or != 0, never anything else.
if [ $wpid -ne 0 ]; then
kill -0 "$wpid" 2>/dev/null
web_status="$?"
fi
if [ $spid -ne 0 ]; then
kill -0 "$spid" 2>/dev/null
sidekiq_status="$?"
fi
}
 
# Check for stale pids and remove them if necessary
check_stale_pids(){
check_status
# If there is a pid it is something else than 0, the service is running if
# *_status is == 0.
if [ "$wpid" != "0" -a "$web_status" != "0" ]; then
echo "Found stale Unicorn web server pid, removing. This is most likely caused by the web server crashing the last time it ran."
rm "$web_server_pid_path"
fi
if [ "$spid" != "0" -a "$sidekiq_status" != "0" ]; then
echo "Found stale Sidekiq web server pid, removing. This is most likely caused by the Sidekiq crashing the last time it ran."
rm "$sidekiq_pid_path"
fi
}
# If no parts of the service is running, bail out.
check_not_running(){
check_stale_pids
if [ "$web_status" != "0" -a "$sidekiq_status" != "0" ]; then
echo "GitLab is not running."
exit
fi
}
# Starts Unicorn and Sidekiq.
start() {
cd $APP_ROOT
check_pid
if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then
# Program is running, exit with error code 1.
echo "Error! $DESC $NAME is currently running!"
exit 1
check_stale_pids
# Then check if the service is running. If it is: don't start again.
if [ "$web_status" = "0" ]; then
echo "The Unicorn web server already running with pid $wpid, not restarting."
else
if [ `whoami` = root ]; then
execute "rm -f $SOCKET_PATH/gitlab.socket"
execute "RAILS_ENV=production bundle exec unicorn_rails $DAEMON_OPTS > /dev/null 2>&1 &"
execute "mkdir -p $PID_PATH && $START_SIDEKIQ > /dev/null 2>&1 &"
echo "$DESC started"
fi
echo "Starting the GitLab Unicorn web server..."
# Remove old socket if it exists
rm -f "$socket_path"/gitlab.socket
# Start the webserver
bundle exec unicorn_rails -D -c "$unicorn_conf" -E "$RAILS_ENV"
fi
}
 
stop() {
cd $APP_ROOT
check_pid
if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then
## Program is running, stop it.
kill -QUIT `cat $WEB_SERVER_PID`
execute "mkdir -p $PID_PATH && $STOP_SIDEKIQ > /dev/null 2>&1 &"
rm "$WEB_SERVER_PID" >> /dev/null
echo "$DESC stopped"
# If sidekiq is already running, don't start it again.
if [ "$sidekiq_status" = "0" ]; then
echo "The Sidekiq job dispatcher is already running with pid $spid, not restarting"
else
## Program is not running, exit with error.
echo "Error! $DESC not started!"
exit 1
echo "Starting the GitLab Sidekiq event dispatcher..."
RAILS_ENV=$RAILS_ENV bundle exec rake sidekiq:start
# We are sleeping a bit here because sidekiq is slow at writing it's pid
sleep 2
fi
# Finally check the status to tell wether or not GitLab is running
status
}
 
restart() {
cd $APP_ROOT
check_pid
if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then
echo "Restarting $DESC..."
kill -USR2 `cat $WEB_SERVER_PID`
execute "mkdir -p $PID_PATH && $STOP_SIDEKIQ > /dev/null 2>&1"
if [ `whoami` = root ]; then
execute "mkdir -p $PID_PATH && $START_SIDEKIQ > /dev/null 2>&1 &"
fi
echo "$DESC restarted."
# Asks the Unicorn and the Sidekiq if they would be so kind as to stop, if not kills them.
stop() {
check_not_running
# If the Unicorn web server is running, tell it to stop;
if [ "$web_status" = "0" ]; then
kill -QUIT "$wpid" &
echo "Stopping the GitLab Unicorn web server..."
stopping=true
else
echo "Error, $NAME not running!"
exit 1
echo "The Unicorn web was not running, doing nothing."
fi
# And do the same thing for the Sidekiq.
if [ "$sidekiq_status" = "0" ]; then
printf "Stopping Sidekiq job dispatcher."
RAILS_ENV=$RAILS_ENV bundle exec rake sidekiq:stop &
stopping=true
else
echo "The Sidekiq was not running, must have run out of breath."
fi
# If something needs to be stopped, lets wait for it to stop. Never use SIGKILL in a script.
while [ "$stopping" = "true" ]; do
sleep 1
check_status
if [ "$web_status" = "0" -o "$sidekiq_status" = "0" ]; then
printf "."
else
printf "\n"
break
fi
done
sleep 1
# Cleaning up unused pids
rm "$web_server_pid_path" 2>/dev/null
# rm "$sidekiq_pid_path" # Sidekiq seems to be cleaning up it's own pid.
status
}
 
# Returns the status of GitLab and it's components
status() {
cd $APP_ROOT
check_pid
if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then
echo "$DESC / Unicorn with PID $PID is running."
echo "$DESC / Sidekiq with PID $SPID is running."
check_not_running
if [ "$web_status" = "0" ]; then
echo "The GitLab Unicorn webserver with pid $wpid is running."
else
echo "$DESC is not running."
exit 1
printf "The GitLab Unicorn webserver is \033[31mnot running\033[0m.\n"
fi
if [ "$sidekiq_status" = "0" ]; then
echo "The GitLab Sidekiq job dispatcher with pid $spid is running."
else
printf "The GitLab Sidekiq job dispatcher is \033[31mnot running\033[0m.\n"
fi
if [ "$web_status" = "0" -a "$sidekiq_status" = "0" ]; then
printf "GitLab and all it's components are \033[32mup and running\033[0m.\n"
fi
}
 
## Check to see if we are running as root first.
## Found at http://www.cyberciti.biz/tips/shell-root-user-check-script.html
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root"
reload(){
check_not_running
if [ "$wpid" = "0" ];then
echo "The GitLab Unicorn Web server is not running thus it's configuration can't be reloaded."
exit 1
fi
fi
printf "Reloading GitLab configuration... "
kill -HUP "$wpid"
echo "Done."
}
## Finally the input handling.
 
case "$1" in
start)
Loading
Loading
@@ -119,20 +216,19 @@ case "$1" in
stop
;;
restart)
restart
stop
start
;;
reload|force-reload)
echo -n "Reloading $NAME configuration: "
kill -HUP `cat $PID`
echo "done."
reload
;;
status)
status
;;
*)
echo "Usage: sudo service gitlab {start|stop|restart|reload}" >&2
echo "Usage: service gitlab {start|stop|restart|reload|status}"
exit 1
;;
esac
 
exit 0
exit
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment