Commit 5dad053a authored by Jacob Vosmaer's avatar Jacob Vosmaer

Merge pull request #4920 from Rovanion/init-rewrite

Rewrote init script.
parents 54faf787 fe8fa9a5
v 6.1.0 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 - Link issues, merge requests, and commits when they reference each other with GFM
- Close issues automatically when pushing commits with a special message - Close issues automatically when pushing commits with a special message
- Project internal ids for issues, mr, milestones - Project internal ids for issues, mr, milestones
......
#! /bin/bash #! /bin/sh
# GITLAB # GITLAB
# Maintainer: @randx # Maintainer: @randx
# Authors: rovanion.luckey@gmail.com, @randx
# App Version: 6.0 # App Version: 6.0
### BEGIN INIT INFO ### BEGIN INIT INFO
...@@ -14,102 +15,227 @@ ...@@ -14,102 +15,227 @@
# Description: GitLab git repository management # Description: GitLab git repository management
### END INIT INFO ### END INIT INFO
### Environment variables
RAILS_ENV="production"
APP_ROOT="/home/git/gitlab" # Script variable names should be lower-case not to conflict with internal
APP_USER="git" # /bin/sh variables such as PATH, EDITOR or SHELL.
DAEMON_OPTS="-c $APP_ROOT/config/unicorn.rb -E production" app_root="/home/git/gitlab"
PID_PATH="$APP_ROOT/tmp/pids" app_user="git"
SOCKET_PATH="$APP_ROOT/tmp/sockets" unicorn_conf="$app_root/config/unicorn.rb"
WEB_SERVER_PID="$PID_PATH/unicorn.pid" pid_path="$app_root/tmp/pids"
SIDEKIQ_PID="$PID_PATH/sidekiq.pid" socket_path="$app_root/tmp/sockets"
STOP_SIDEKIQ="RAILS_ENV=production bundle exec rake sidekiq:stop" web_server_pid_path="$pid_path/unicorn.pid"
START_SIDEKIQ="RAILS_ENV=production bundle exec rake sidekiq:start" sidekiq_pid_path="$pid_path/sidekiq.pid"
NAME="gitlab"
DESC="GitLab service"
check_pid(){ ### Here ends user configuration ###
if [ -f $WEB_SERVER_PID ]; then
PID=`cat $WEB_SERVER_PID`
SPID=`cat $SIDEKIQ_PID` # Switch to the app_user if it is not he/she who is running the script.
STATUS=`ps aux | grep $PID | grep -v grep | wc -l` if [ "$USER" != "$app_user" ]; then
sudo -u "$app_user" -H $0 "$@"; 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 else
STATUS=0 wpid=0
PID=0 fi
if [ -f "$sidekiq_pid_path" ]; then
spid=$(cat "$sidekiq_pid_path")
else
spid=0
fi fi
} }
execute() { # We use the pids in so many parts of the script it makes sense to always check them.
sudo -u $APP_USER -H bash -l -c "$1" # Only after start() is run should the pids change. Sidekiq sets it's own pid.
check_pids
# Checks whether the different parts of the service are 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="$?"
else
web_status="-1"
fi
if [ $spid -ne 0 ]; then
kill -0 "$spid" 2>/dev/null
sidekiq_status="$?"
else
sidekiq_status="-1"
fi
} }
start() { # Check for stale pids and remove them if necessary
cd $APP_ROOT check_stale_pids(){
check_pid check_status
if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then # If there is a pid it is something else than 0, the service is running if
# Program is running, exit with error code 1. # *_status is == 0.
echo "Error! $DESC $NAME is currently running!" if [ "$wpid" != "0" -a "$web_status" != "0" ]; then
echo "Removing stale Unicorn web server pid. This is most likely caused by the web server crashing the last time it ran."
if ! rm "$web_server_pid_path"; then
echo "Unable to remove stale pid, exiting"
exit 1
fi
fi
if [ "$spid" != "0" -a "$sidekiq_status" != "0" ]; then
echo "Removing stale Sidekiq web server pid. This is most likely caused by the Sidekiq crashing the last time it ran."
if ! rm "$sidekiq_pid_path"; then
echo "Unable to remove stale pid, exiting"
exit 1 exit 1
fi
fi
}
# If no parts of the service is running, bail out.
exit_if_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() {
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 else
if [ `whoami` = root ]; then echo "Starting the GitLab Unicorn web server..."
execute "rm -f $SOCKET_PATH/gitlab.socket" # Remove old socket if it exists
execute "RAILS_ENV=production bundle exec unicorn_rails $DAEMON_OPTS > /dev/null 2>&1 &" rm -f "$socket_path"/gitlab.socket 2>/dev/null
execute "mkdir -p $PID_PATH && $START_SIDEKIQ > /dev/null 2>&1 &" # Start the webserver
echo "$DESC started" bundle exec unicorn_rails -D -c "$unicorn_conf" -E "$RAILS_ENV"
fi fi
# 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
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 fi
# Finally check the status to tell wether or not GitLab is running
status
} }
# Asks the Unicorn and the Sidekiq if they would be so kind as to stop, if not kills them.
stop() { stop() {
cd $APP_ROOT exit_if_not_running
check_pid # If the Unicorn web server is running, tell it to stop;
if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then if [ "$web_status" = "0" ]; then
## Program is running, stop it. kill -QUIT "$wpid" &
kill -QUIT `cat $WEB_SERVER_PID` echo "Stopping the GitLab Unicorn web server..."
execute "mkdir -p $PID_PATH && $STOP_SIDEKIQ > /dev/null 2>&1 &" stopping=true
rm "$WEB_SERVER_PID" >> /dev/null
echo "$DESC stopped"
else else
## Program is not running, exit with error. echo "The Unicorn web was not running, doing nothing."
echo "Error! $DESC not started!" fi
exit 1 # 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 fi
}
restart() {
cd $APP_ROOT # If something needs to be stopped, lets wait for it to stop. Never use SIGKILL in a script.
check_pid while [ "$stopping" = "true" ]; do
if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then sleep 1
echo "Restarting $DESC..." check_status
kill -USR2 `cat $WEB_SERVER_PID` if [ "$web_status" = "0" -o "$sidekiq_status" = "0" ]; then
execute "mkdir -p $PID_PATH && $STOP_SIDEKIQ > /dev/null 2>&1" printf "."
if [ `whoami` = root ]; then
execute "mkdir -p $PID_PATH && $START_SIDEKIQ > /dev/null 2>&1 &"
fi
echo "$DESC restarted."
else else
echo "Error, $NAME not running!" printf "\n"
exit 1 break
fi 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() { status() {
cd $APP_ROOT check_status
check_pid if [ "$web_status" != "0" -a "$sidekiq_status" != "0" ]; then
if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then echo "GitLab is not running."
echo "$DESC / Unicorn with PID $PID is running." return
echo "$DESC / Sidekiq with PID $SPID is running." fi
if [ "$web_status" = "0" ]; then
echo "The GitLab Unicorn webserver with pid $wpid is running."
else else
echo "$DESC is not running." printf "The GitLab Unicorn webserver is \033[31mnot running\033[0m.\n"
exit 1 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 fi
} }
## Check to see if we are running as root first. reload(){
## Found at http://www.cyberciti.biz/tips/shell-root-user-check-script.html exit_if_not_running
if [ "$(id -u)" != "0" ]; then if [ "$wpid" = "0" ];then
echo "This script must be run as root" echo "The GitLab Unicorn Web server is not running thus its configuration can't be reloaded."
exit 1 exit 1
fi fi
printf "Reloading GitLab Unicorn configuration... "
kill -USR2 "$wpid"
echo "Done."
echo "Restarting GitLab Sidekiq since it isn't capable of reloading its config..."
RAILS_ENV=$RAILS_ENV bundle exec rake sidekiq:stop
echo "Starting Sidekiq..."
RAILS_ENV=$RAILS_ENV bundle exec rake sidekiq:start
# Waiting 2 seconds for sidekiq to write it.
sleep 2
status
}
restart(){
check_status
if [ "$web_status" = "0" -o "$sidekiq_status" = "0" ]; then
stop
fi
start
}
## Finally the input handling.
case "$1" in case "$1" in
start) start)
...@@ -122,17 +248,15 @@ case "$1" in ...@@ -122,17 +248,15 @@ case "$1" in
restart restart
;; ;;
reload|force-reload) reload|force-reload)
echo -n "Reloading $NAME configuration: " reload
kill -HUP `cat $PID`
echo "done."
;; ;;
status) status)
status status
;; ;;
*) *)
echo "Usage: sudo service gitlab {start|stop|restart|reload}" >&2 echo "Usage: service gitlab {start|stop|restart|reload|status}"
exit 1 exit 1
;; ;;
esac esac
exit 0 exit
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment