gitlab 8.22 KB
Newer Older
Rovanion's avatar
Rovanion committed
1
#! /bin/sh
2 3 4

# GITLAB
# Maintainer: @randx
Rovanion's avatar
Rovanion committed
5
# Authors: rovanion.luckey@gmail.com, @randx
6 7 8 9 10 11 12 13 14 15 16

### 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

17 18 19 20 21

###
# DO NOT EDIT THIS FILE!
# This file will be overwritten on update.
# Instead add/change your variables in /etc/default/gitlab
22
# An example defaults file can be found in lib/support/init.d/gitlab.default.example
23 24 25
###


Rovanion's avatar
Rovanion committed
26 27
### Environment variables
RAILS_ENV="production"
28

29 30
# Script variable names should be lower-case not to conflict with
# internal /bin/sh variables such as PATH, EDITOR or SHELL.
31
app_user="git"
32
app_root="/home/$app_user/gitlab"
Rovanion's avatar
Rovanion committed
33 34 35 36 37
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"

38 39
# Read configuration variable file if it is present
test -f /etc/default/gitlab && . /etc/default/gitlab
Rovanion's avatar
Rovanion committed
40 41 42

# Switch to the app_user if it is not he/she who is running the script.
if [ "$USER" != "$app_user" ]; then
43
  sudo -u "$app_user" -H -i $0 "$@"; exit;
Rovanion's avatar
Rovanion committed
44 45
fi

46
# Switch to the gitlab path, exit on failure.
Rovanion's avatar
Rovanion committed
47 48 49 50
if ! cd "$app_root" ; then
 echo "Failed to cd into $app_root, exiting!";  exit 1
fi

51

Rovanion's avatar
Rovanion committed
52 53
### Init Script functions

54
## Gets the pids from the files
Rovanion's avatar
Rovanion committed
55 56 57 58 59 60 61 62 63 64 65 66 67
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")
68
  else
Rovanion's avatar
Rovanion committed
69
    spid=0
70 71 72
  fi
}

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
## Called when we have started the two processes and are waiting for their pid files.
wait_for_pids(){
  # We are sleeping a bit here mostly because sidekiq is slow at writing it's pid
  i=0;
  while [ ! -f $web_server_pid_path -o ! -f $sidekiq_pid_path ]; do
    sleep 0.1;
    i=$((i+1))
    if [ $((i%10)) = 0 ]; then
      echo -n "."
    elif [ $((i)) = 301 ]; then
      echo "Waited 30s for the processes to write their pids, something probably went wrong."
      exit 1;
    fi
  done
  echo
}

Rovanion's avatar
Rovanion committed
90 91 92 93 94
# 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


95
## Checks whether the different parts of the service are already running or not.
Rovanion's avatar
Rovanion committed
96 97 98 99 100 101 102
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="$?"
103 104
  else
    web_status="-1"
Rovanion's avatar
Rovanion committed
105 106 107 108
  fi
  if [ $spid -ne 0 ]; then
    kill -0 "$spid" 2>/dev/null
    sidekiq_status="$?"
109 110
  else
    sidekiq_status="-1"
Rovanion's avatar
Rovanion committed
111
  fi
112 113 114 115 116 117 118
  if [ $web_status = 0 -a $sidekiq_status = 0 ]; then
    gitlab_status=0
  else
    # http://refspecs.linuxbase.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
    # code 3 means 'program is not running'
    gitlab_status=3
  fi
rezigned's avatar
rezigned committed
119 120
}

121
## Check for stale pids and remove them if necessary.
Rovanion's avatar
Rovanion committed
122 123 124 125 126
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
127 128
    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
129
      echo "Unable to remove stale pid, exiting."
130 131
      exit 1
    fi
Rovanion's avatar
Rovanion committed
132 133
  fi
  if [ "$spid" != "0" -a "$sidekiq_status" != "0" ]; then
134 135 136 137 138
    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
Rovanion's avatar
Rovanion committed
139 140 141
  fi
}

142
## If no parts of the service is running, bail out.
143
exit_if_not_running(){
Rovanion's avatar
Rovanion committed
144 145 146 147 148 149 150
  check_stale_pids
  if [ "$web_status" != "0" -a "$sidekiq_status" != "0" ]; then
    echo "GitLab is not running."
    exit
  fi
}

151
## Starts Unicorn and Sidekiq if they're not running.
152
start() {
Rovanion's avatar
Rovanion committed
153 154
  check_stale_pids

155 156 157 158 159 160 161 162
  if [ "$web_status" != "0" -a "$sidekiq_status" != "0" ]; then
    echo -n "Starting both the GitLab Unicorn and Sidekiq"
  elif [ "$web_status" != "0" ]; then
    echo -n "Starting GitLab Sidekiq"
  elif [ "$sidekiq_status" != "0" ]; then
    echo -n "Starting GitLab Unicorn"
  fi

Rovanion's avatar
Rovanion committed
163 164 165
  # 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."
166
  else
Rovanion's avatar
Rovanion committed
167
    # Remove old socket if it exists
Rovanion's avatar
Rovanion committed
168
    rm -f "$socket_path"/gitlab.socket 2>/dev/null
169 170
    # Start the web server
    RAILS_ENV=$RAILS_ENV script/web start &
171 172
  fi

Rovanion's avatar
Rovanion committed
173 174 175
  # 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"
176
  else
177
    RAILS_ENV=$RAILS_ENV script/background_jobs start &
178
  fi
Rovanion's avatar
Rovanion committed
179

180 181
  # Wait for the pids to be planted
  wait_for_pids
Rovanion's avatar
Rovanion committed
182
  # Finally check the status to tell wether or not GitLab is running
183
  print_status
184 185
}

186
## Asks the Unicorn and the Sidekiq if they would be so kind as to stop, if not kills them.
Rovanion's avatar
Rovanion committed
187
stop() {
188
  exit_if_not_running
189 190 191 192 193 194 195 196 197

  if [ "$web_status" = "0" -a "$sidekiq_status" = "0" ]; then
    echo -n "Shutting down both Unicorn and Sidekiq"
  elif [ "$web_status" = "0" ]; then
    echo -n "Shutting down Sidekiq"
  elif [ "$sidekiq_status" = "0" ]; then
    echo -n "Shutting down Unicorn"
  fi

Rovanion's avatar
Rovanion committed
198 199
  # If the Unicorn web server is running, tell it to stop;
  if [ "$web_status" = "0" ]; then
200
     RAILS_ENV=$RAILS_ENV script/web stop
Rovanion's avatar
Rovanion committed
201 202 203
  fi
  # And do the same thing for the Sidekiq.
  if [ "$sidekiq_status" = "0" ]; then
204
    RAILS_ENV=$RAILS_ENV script/background_jobs stop
205
  fi
Rovanion's avatar
Rovanion committed
206 207

  # If something needs to be stopped, lets wait for it to stop. Never use SIGKILL in a script.
208
  while [ "$web_status" = "0" -o "$sidekiq_status" = "0" ]; do
Rovanion's avatar
Rovanion committed
209 210
    sleep 1
    check_status
211 212
    printf "."
    if [ "$web_status" != "0" -a "$sidekiq_status" != "0" ]; then
Rovanion's avatar
Rovanion committed
213 214 215 216
      printf "\n"
      break
    fi
  done
217

Rovanion's avatar
Rovanion committed
218 219 220 221 222
  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.

223
  print_status
224 225
}

226 227
## Prints the status of GitLab and it's components.
print_status() {
228 229 230 231 232
  check_status
  if [ "$web_status" != "0" -a "$sidekiq_status" != "0" ]; then
    echo "GitLab is not running."
    return
  fi
Rovanion's avatar
Rovanion committed
233
  if [ "$web_status" = "0" ]; then
234
      echo "The GitLab Unicorn web server with pid $wpid is running."
235
  else
236
      printf "The GitLab Unicorn web server is \033[31mnot running\033[0m.\n"
Rovanion's avatar
Rovanion committed
237 238 239 240 241 242 243
  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
244
    printf "GitLab and all its components are \033[32mup and running\033[0m.\n"
245 246 247
  fi
}

248
## Tells unicorn to reload it's config and Sidekiq to restart
Rovanion's avatar
Rovanion committed
249
reload(){
250
  exit_if_not_running
Rovanion's avatar
Rovanion committed
251
  if [ "$wpid" = "0" ];then
252
    echo "The GitLab Unicorn Web server is not running thus its configuration can't be reloaded."
253
    exit 1
Rovanion's avatar
Rovanion committed
254
  fi
255
  printf "Reloading GitLab Unicorn configuration... "
256
  RAILS_ENV=$RAILS_ENV script/web reload
Rovanion's avatar
Rovanion committed
257
  echo "Done."
258
  echo "Restarting GitLab Sidekiq since it isn't capable of reloading its config..."
259
  RAILS_ENV=$RAILS_ENV script/background_jobs restart
260 261 262

  wait_for_pids
  print_status
Rovanion's avatar
Rovanion committed
263 264
}

265
## Restarts Sidekiq and Unicorn.
266 267 268 269 270 271 272 273
restart(){
  check_status
  if [ "$web_status" = "0" -o "$sidekiq_status" = "0" ]; then
    stop
  fi
  start
}

Rovanion's avatar
Rovanion committed
274

275
### Finally the input handling.
276 277 278 279 280 281 282 283 284

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
285
        restart
286 287
        ;;
  reload|force-reload)
Rovanion's avatar
Rovanion committed
288
	reload
289 290
        ;;
  status)
291
        print_status
292
        exit $gitlab_status
293 294
        ;;
  *)
Rovanion's avatar
Rovanion committed
295
        echo "Usage: service gitlab {start|stop|restart|reload|status}"
296 297 298 299
        exit 1
        ;;
esac

Rovanion's avatar
Rovanion committed
300
exit