summaryrefslogtreecommitdiffstats
path: root/SOURCES/gitlab.init
diff options
context:
space:
mode:
Diffstat (limited to 'SOURCES/gitlab.init')
-rwxr-xr-xSOURCES/gitlab.init241
1 files changed, 171 insertions, 70 deletions
diff --git a/SOURCES/gitlab.init b/SOURCES/gitlab.init
index a000af5..e8bb023 100755
--- a/SOURCES/gitlab.init
+++ b/SOURCES/gitlab.init
@@ -1,102 +1,203 @@
-#!/bin/bash
-#
-# GitLab
-# Contributors : @elvanja, @troyanov, @eiyaya, @foyo23, @nielsbasjes, @relip, @JasonMing, @andronat, @axilleas
-# App Version : 6.x
+#! /bin/sh
-# chkconfig: 2345 82 55
-# processname: sidekiq
-# description: Runs sidekiq for nginx integration.
+# GITLAB
+# Maintainer: @randx
+# Authors: rovanion.luckey@gmail.com, @randx
+# App Version: 6.0
-# Related (kudos @4sak3n0ne):
-# https://github.com/gitlabhq/gitlabhq/issues/1049#issuecomment-8386882
-# https://gist.github.com/3062860
+### BEGIN INIT INFO
+# Provides: gitlab
+# Required-Start: $local_fs $remote_fs $network $syslog redis-server
+# Required-Stop: $local_fs $remote_fs $network $syslog
+# Default-Start: 2 3 4 5
+# Default-Stop: 0 1 6
+# Short-Description: GitLab git repository management
+# Description: GitLab git repository management
+### END INIT INFO
-# Save original $PATH
-# /etc/rc.d/init.d/functions resets $PATH to default(/sbin:/usr/sbin:/bin:/usr/bin).
-# Consequently, rvm and compiled ruby with custom path (which isn't /usr/bin) cannot be executed.
-ORIGINAL_PATH=$PATH
+### Environment variables
+RAILS_ENV="production"
-# Include RedHat function library
-. /etc/rc.d/init.d/functions
+# Script variable names should be lower-case not to conflict with internal
+# /bin/sh variables such as PATH, EDITOR or SHELL.
+app_root="/var/www/gitlab/gitlab"
+app_user="git"
+pid_path="$app_root/tmp/pids"
+socket_path="$app_root/tmp/sockets"
+sidekiq_pid_path="$pid_path/sidekiq.pid"
-# Restore original $PATH
-PATH=$ORIGINAL_PATH
-# The name of the service
-NAME=git
-# The username and path to the gitlab source
-USER=git
-APP_PATH=/var/www/gitlab/gitlab
+### Here ends user configuration ###
-# The PID and LOCK files used by unicorn and sidekiq
-SPID=$APP_PATH/tmp/pids/sidekiq.pid
-SLOCK=/var/lock/subsys/sidekiq
-# Ruby related path update
-export PATH="/opt/ruby-1.9.3/bin:$PATH"
+# 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 -i $0 "$@"; exit;
+fi
-start() {
- cd $APP_PATH
+# 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
- # Start sidekiq
- echo -n $"Starting sidekiq: "
- daemon --pidfile=$SPID --user=$USER "RAILS_ENV=production bundle exec rake sidekiq:start"
- sidekiq=$?
- [ $sidekiq -eq 0 ] && touch $SLOCK
- echo
+### Init Script functions
- return $sidekiq
+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 [ -f "$sidekiq_pid_path" ]; then
+ spid=$(cat "$sidekiq_pid_path")
+ else
+ spid=0
+ fi
}
-stop() {
- cd $APP_PATH
+# 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
- # Stop sidekiq
- echo -n $"Stopping sidekiq: "
- killproc -p $SPID
- sidekiq=$?
- [ $sidekiq -eq 0 ] && rm -f $SLOCK
- echo
- return $sidekiq
+# Checks whether the different parts of the service are already running or not.
+check_status(){
+ check_pids
+ if [ $spid -ne 0 ]; then
+ kill -0 "$spid" 2>/dev/null
+ sidekiq_status="$?"
+ else
+ sidekiq_status="-1"
+ fi
}
-restart() {
- stop
- start
+# 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 [ "$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
+ fi
+ fi
+}
+
+# If no parts of the service is running, bail out.
+exit_if_not_running(){
+ check_stale_pids
+ if [ "$sidekiq_status" != "0" ]; then
+ echo "GitLab is not running."
+ exit
+ fi
+}
+
+# Starts Unicorn and Sidekiq.
+start() {
+ check_stale_pids
+
+ # 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 script/background_jobs 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
+}
+
+# Asks the Unicorn and the Sidekiq if they would be so kind as to stop, if not kills them.
+stop() {
+ exit_if_not_running
+ # And do the same thing for the Sidekiq.
+ if [ "$sidekiq_status" = "0" ]; then
+ printf "Stopping Sidekiq job dispatcher."
+ RAILS_ENV=$RAILS_ENV script/background_jobs 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
+ done
+ sleep 1
+ # Cleaning up unused pids
+ # 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() {
+ check_status
+ if [ "$sidekiq_status" != "0" ]; then
+ echo "GitLab is not running."
+ return
+ 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 [ "$sidekiq_status" = "0" ]; then
+ printf "GitLab and all its components are \033[32mup and running\033[0m.\n"
+ fi
}
-get_status() {
- status -p $SPID sidekiq
+reload(){
+ exit_if_not_running
+ if [ "$wpid" = "0" ];then
+ echo "The GitLab Unicorn Web server is not running thus its configuration can't be reloaded."
+ exit 1
+ fi
+ echo "Restarting GitLab Sidekiq since it isn't capable of reloading its config..."
+ RAILS_ENV=$RAILS_ENV script/background_jobs restart
+ # Waiting 2 seconds for sidekiq to write it.
+ sleep 2
+ status
}
-query_status() {
- get_status >/dev/null 2>&1
+restart(){
+ check_status
+ if [ "$sidekiq_status" = "0" ]; then
+ stop
+ fi
+ start
}
+
+## Finally the input handling.
+
case "$1" in
start)
- query_status && exit 0
- start
- ;;
+ start
+ ;;
stop)
- query_status || exit 0
- stop
- ;;
+ stop
+ ;;
restart)
- restart
- ;;
+ restart
+ ;;
+ reload|force-reload)
+ reload
+ ;;
status)
- get_status
- ;;
+ status
+ ;;
*)
- N=/etc/init.d/$NAME
- echo "Usage: $N {start|stop|restart|status}" >&2
- exit 1
- ;;
+ echo "Usage: service gitlab {start|stop|restart|reload|status}"
+ exit 1
+ ;;
esac
-exit 0
-
+exit