Commit df9e1db9 authored by Rovanion's avatar Rovanion Committed by Rovanion Luckey

Rewrote init script.

parent 79f0858a
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
......
#! /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,198 @@ ...@@ -14,102 +15,198 @@
# 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/gitlab/gitlab"
PID_PATH="$APP_ROOT/tmp/pids" app_user="gitlab"
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 /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 else
STATUS=0 spid=0
PID=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 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() { start() {
cd $APP_ROOT check_stale_pids
check_pid
if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then # Then check if the service is running. If it is: don't start again.
# Program is running, exit with error code 1. if [ "$web_status" = "0" ]; then
echo "Error! $DESC $NAME is currently running!" echo "The Unicorn web server already running with pid $wpid, not restarting."
exit 1
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
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 fi
}
stop() { # If sidekiq is already running, don't start it again.
cd $APP_ROOT if [ "$sidekiq_status" = "0" ]; then
check_pid echo "The Sidekiq job dispatcher is already running with pid $spid, not restarting"
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"
else else
## Program is not running, exit with error. echo "Starting the GitLab Sidekiq event dispatcher..."
echo "Error! $DESC not started!" RAILS_ENV=$RAILS_ENV bundle exec rake sidekiq:start
exit 1 # 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
} }
restart() { # Asks the Unicorn and the Sidekiq if they would be so kind as to stop, if not kills them.
cd $APP_ROOT stop() {
check_pid check_not_running
if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then # If the Unicorn web server is running, tell it to stop;
echo "Restarting $DESC..." if [ "$web_status" = "0" ]; then
kill -USR2 `cat $WEB_SERVER_PID` kill -QUIT "$wpid" &
execute "mkdir -p $PID_PATH && $STOP_SIDEKIQ > /dev/null 2>&1" echo "Stopping the GitLab Unicorn web server..."
if [ `whoami` = root ]; then stopping=true
execute "mkdir -p $PID_PATH && $START_SIDEKIQ > /dev/null 2>&1 &"
fi
echo "$DESC restarted."
else else
echo "Error, $NAME not running!" echo "The Unicorn web was not running, doing nothing."
exit 1 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 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() { status() {
cd $APP_ROOT check_not_running
check_pid if [ "$web_status" = "0" ]; then
if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then echo "The GitLab Unicorn webserver with pid $wpid is running."
echo "$DESC / Unicorn with PID $PID is running."
echo "$DESC / Sidekiq with PID $SPID 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 check_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 it's configuration can't be reloaded."
exit 1 exit 1
fi fi
printf "Reloading GitLab configuration... "
kill -HUP "$wpid"
echo "Done."
}
## Finally the input handling.
case "$1" in case "$1" in
start) start)
...@@ -119,20 +216,19 @@ case "$1" in ...@@ -119,20 +216,19 @@ case "$1" in
stop stop
;; ;;
restart) restart)
restart stop
start
;; ;;
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
\ No newline at end of file
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