BigW Consortium Gitlab

gitlab 12.6 KB
Newer Older
Rovanion committed
1
#! /bin/sh
2 3 4

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

### 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
15
# chkconfig: - 85 14
16 17
### END INIT INFO

18 19 20 21 22

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


Rovanion committed
27 28
### Environment variables
RAILS_ENV="production"
29

30 31
# Script variable names should be lower-case not to conflict with
# internal /bin/sh variables such as PATH, EDITOR or SHELL.
32
app_user="git"
33
app_root="/home/$app_user/gitlab"
Rovanion committed
34 35
pid_path="$app_root/tmp/pids"
socket_path="$app_root/tmp/sockets"
36
rails_socket="$socket_path/gitlab.socket"
Rovanion committed
37 38
web_server_pid_path="$pid_path/unicorn.pid"
sidekiq_pid_path="$pid_path/sidekiq.pid"
Douwe Maan committed
39 40
mail_room_enabled=false
mail_room_pid_path="$pid_path/mail_room.pid"
41
gitlab_workhorse_dir=$(cd $app_root/../gitlab-workhorse 2> /dev/null && pwd)
42
gitlab_workhorse_pid_path="$pid_path/gitlab-workhorse.pid"
43
gitlab_workhorse_options="-listenUmask 0 -listenNetwork unix -listenAddr $socket_path/gitlab-workhorse.socket -authBackend http://127.0.0.1:8080 -authSocket $rails_socket -documentRoot $app_root/public"
44
gitlab_workhorse_log="$app_root/log/gitlab-workhorse.log"
45
shell_path="/bin/bash"
Rovanion committed
46

47 48
# Read configuration variable file if it is present
test -f /etc/default/gitlab && . /etc/default/gitlab
Rovanion committed
49 50

# Switch to the app_user if it is not he/she who is running the script.
51
if [ `whoami` != "$app_user" ]; then
52
  eval su - "$app_user" -c $(echo \")$shell_path -l -c \'$0 "$@"\'$(echo \"); exit;
Rovanion committed
53 54
fi

55
# Switch to the gitlab path, exit on failure.
Rovanion committed
56 57 58 59
if ! cd "$app_root" ; then
 echo "Failed to cd into $app_root, exiting!";  exit 1
fi

60

Rovanion committed
61 62
### Init Script functions

63
## Gets the pids from the files
Rovanion committed
64 65 66 67 68 69 70 71 72 73 74 75 76
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")
77
  else
Rovanion committed
78
    spid=0
79
  fi
80 81
  if [ -f "$gitlab_workhorse_pid_path" ]; then
    hpid=$(cat "$gitlab_workhorse_pid_path")
82 83 84
  else
    hpid=0
  fi
Douwe Maan committed
85 86 87 88 89 90 91
  if [ "$mail_room_enabled" = true ]; then
    if [ -f "$mail_room_pid_path" ]; then
      mpid=$(cat "$mail_room_pid_path")
    else
      mpid=0
    fi
  fi
92 93
}

94 95
## Called when we have started the two processes and are waiting for their pid files.
wait_for_pids(){
96
  # We are sleeping a bit here mostly because sidekiq is slow at writing its pid
97
  i=0;
98
  while [ ! -f $web_server_pid_path ] || [ ! -f $sidekiq_pid_path ] || [ ! -f $gitlab_workhorse_pid_path ] || { [ "$mail_room_enabled" = true ] && [ ! -f $mail_room_pid_path ]; }; do
99 100 101 102 103 104 105 106 107 108 109 110
    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 committed
111
# We use the pids in so many parts of the script it makes sense to always check them.
112
# Only after start() is run should the pids change. Sidekiq sets its own pid.
Rovanion committed
113 114 115
check_pids


116
## Checks whether the different parts of the service are already running or not.
Rovanion committed
117 118 119 120 121 122 123
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="$?"
124 125
  else
    web_status="-1"
Rovanion committed
126 127 128 129
  fi
  if [ $spid -ne 0 ]; then
    kill -0 "$spid" 2>/dev/null
    sidekiq_status="$?"
130 131
  else
    sidekiq_status="-1"
Rovanion committed
132
  fi
133 134
  if [ $hpid -ne 0 ]; then
    kill -0 "$hpid" 2>/dev/null
135
    gitlab_workhorse_status="$?"
136
  else
137
    gitlab_workhorse_status="-1"
138
  fi
139 140 141 142 143 144 145
  if [ "$mail_room_enabled" = true ]; then
    if [ $mpid -ne 0 ]; then
      kill -0 "$mpid" 2>/dev/null
      mail_room_status="$?"
    else
      mail_room_status="-1"
    fi
Douwe Maan committed
146
  fi
147
  if [ $web_status = 0 ] && [ $sidekiq_status = 0 ] && [ $gitlab_workhorse_status = 0 ] && { [ "$mail_room_enabled" != true ] || [ $mail_room_status = 0 ]; }; then
148 149 150 151 152 153
    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 committed
154 155
}

156
## Check for stale pids and remove them if necessary.
Rovanion committed
157 158 159 160
check_stale_pids(){
  check_status
  # If there is a pid it is something else than 0, the service is running if
  # *_status is == 0.
161
  if [ "$wpid" != "0" ] && [ "$web_status" != "0" ]; then
162 163
    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
164
      echo "Unable to remove stale pid, exiting."
165 166
      exit 1
    fi
Rovanion committed
167
  fi
168
  if [ "$spid" != "0" ] && [ "$sidekiq_status" != "0" ]; then
169
    echo "Removing stale Sidekiq job dispatcher pid. This is most likely caused by Sidekiq crashing the last time it ran."
170 171 172 173
    if ! rm "$sidekiq_pid_path"; then
      echo "Unable to remove stale pid, exiting"
      exit 1
    fi
Rovanion committed
174
  fi
175
  if [ "$hpid" != "0" ] && [ "$gitlab_workhorse_status" != "0" ]; then
176
    echo "Removing stale GitLab Workhorse pid. This is most likely caused by GitLab Workhorse crashing the last time it ran."
177
    if ! rm "$gitlab_workhorse_pid_path"; then
178 179 180 181
      echo "Unable to remove stale pid, exiting"
      exit 1
    fi
  fi
182
  if [ "$mail_room_enabled" = true ] && [ "$mpid" != "0" ] && [ "$mail_room_status" != "0" ]; then
Douwe Maan committed
183 184 185 186 187 188
    echo "Removing stale MailRoom job dispatcher pid. This is most likely caused by MailRoom crashing the last time it ran."
    if ! rm "$mail_room_pid_path"; then
      echo "Unable to remove stale pid, exiting"
      exit 1
    fi
  fi
Rovanion committed
189 190
}

191
## If no parts of the service is running, bail out.
192
exit_if_not_running(){
Rovanion committed
193
  check_stale_pids
194
  if [ "$web_status" != "0" ] && [ "$sidekiq_status" != "0" ] && [ "$gitlab_workhorse_status" != "0" ] && { [ "$mail_room_enabled" != true ] || [ "$mail_room_status" != "0" ]; }; then
Rovanion committed
195 196 197 198 199
    echo "GitLab is not running."
    exit
  fi
}

200
## Starts Unicorn and Sidekiq if they're not running.
201
start_gitlab() {
Rovanion committed
202 203
  check_stale_pids

Douwe Maan committed
204
  if [ "$web_status" != "0" ]; then
Douwe Maan committed
205
    echo "Starting GitLab Unicorn"
Douwe Maan committed
206 207
  fi
  if [ "$sidekiq_status" != "0" ]; then
Douwe Maan committed
208
    echo "Starting GitLab Sidekiq"
209
  fi
210
  if [ "$gitlab_workhorse_status" != "0" ]; then
211
    echo "Starting GitLab Workhorse"
212
  fi
213
  if [ "$mail_room_enabled" = true ] && [ "$mail_room_status" != "0" ]; then
Douwe Maan committed
214
    echo "Starting GitLab MailRoom"
Douwe Maan committed
215
  fi
216

Rovanion committed
217 218 219
  # 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."
220
  else
Rovanion committed
221
    # Remove old socket if it exists
fbretel committed
222
    rm -f "$rails_socket" 2>/dev/null
223
    # Start the web server
224
    RAILS_ENV=$RAILS_ENV bin/web start
225 226
  fi

Rovanion committed
227 228 229
  # 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"
230
  else
231
    RAILS_ENV=$RAILS_ENV bin/background_jobs start &
232
  fi
Rovanion committed
233

234
  if [ "$gitlab_workhorse_status" = "0" ]; then
235
    echo "The GitLab Workhorse is already running with pid $spid, not restarting"
236
  else
237 238 239
    # No need to remove a socket, gitlab-workhorse does this itself.
    # Because gitlab-workhorse has multiple executables we need to fix
    # the PATH.
240
    $app_root/bin/daemon_with_pidfile $gitlab_workhorse_pid_path  \
241 242
      /usr/bin/env PATH=$gitlab_workhorse_dir:$PATH \
        gitlab-workhorse $gitlab_workhorse_options \
243
      >> $gitlab_workhorse_log 2>&1 &
244 245
  fi

Douwe Maan committed
246 247 248
  if [ "$mail_room_enabled" = true ]; then
    # If MailRoom is already running, don't start it again.
    if [ "$mail_room_status" = "0" ]; then
249
      echo "The MailRoom email processor is already running with pid $mpid, not restarting"
Douwe Maan committed
250 251 252 253 254
    else
      RAILS_ENV=$RAILS_ENV bin/mail_room start &
    fi
  fi

255 256
  # Wait for the pids to be planted
  wait_for_pids
Rovanion committed
257
  # Finally check the status to tell wether or not GitLab is running
258
  print_status
259 260
}

Douwe Maan committed
261
## Asks Unicorn, Sidekiq and MailRoom if they would be so kind as to stop, if not kills them.
262
stop_gitlab() {
263
  exit_if_not_running
264

Douwe Maan committed
265
  if [ "$web_status" = "0" ]; then
Douwe Maan committed
266
    echo "Shutting down GitLab Unicorn"
267
    RAILS_ENV=$RAILS_ENV bin/web stop
Douwe Maan committed
268 269
  fi
  if [ "$sidekiq_status" = "0" ]; then
Douwe Maan committed
270
    echo "Shutting down GitLab Sidekiq"
271
    RAILS_ENV=$RAILS_ENV bin/background_jobs stop
272
  fi
273
  if [ "$gitlab_workhorse_status" = "0" ]; then
274
    echo "Shutting down GitLab Workhorse"
275
    kill -- $(cat $gitlab_workhorse_pid_path)
276
  fi
277
  if [ "$mail_room_enabled" = true ] && [ "$mail_room_status" = "0" ]; then
278
    echo "Shutting down GitLab MailRoom"
Douwe Maan committed
279 280
    RAILS_ENV=$RAILS_ENV bin/mail_room stop
  fi
Rovanion committed
281 282

  # If something needs to be stopped, lets wait for it to stop. Never use SIGKILL in a script.
283
  while [ "$web_status" = "0" ] || [ "$sidekiq_status" = "0" ] || [ "$gitlab_workhorse_status" = "0" ] || { [ "$mail_room_enabled" = true ] && [ "$mail_room_status" = "0" ]; }; do
Rovanion committed
284 285
    sleep 1
    check_status
286
    printf "."
287
    if [ "$web_status" != "0" ] && [ "$sidekiq_status" != "0" ] && [ "$gitlab_workhorse_status" != "0" ] && { [ "$mail_room_enabled" != true ] || [ "$mail_room_status" != "0" ]; }; then
Rovanion committed
288 289 290 291
      printf "\n"
      break
    fi
  done
292

Rovanion committed
293 294 295
  sleep 1
  # Cleaning up unused pids
  rm "$web_server_pid_path" 2>/dev/null
296
  # rm "$sidekiq_pid_path" 2>/dev/null # Sidekiq seems to be cleaning up its own pid.
297
  rm -f "$gitlab_workhorse_pid_path"
Douwe Maan committed
298 299 300
  if [ "$mail_room_enabled" = true ]; then
    rm "$mail_room_pid_path" 2>/dev/null
  fi
Rovanion committed
301

302
  print_status
303 304
}

305
## Prints the status of GitLab and its components.
306
print_status() {
307
  check_status
308
  if [ "$web_status" != "0" ] && [ "$sidekiq_status" != "0" ] && [ "$gitlab_workhorse_status" != "0" ] && { [ "$mail_room_enabled" != true ] || [ "$mail_room_status" != "0" ]; }; then
309 310 311
    echo "GitLab is not running."
    return
  fi
Rovanion committed
312
  if [ "$web_status" = "0" ]; then
313
      echo "The GitLab Unicorn web server with pid $wpid is running."
314
  else
315
      printf "The GitLab Unicorn web server is \033[31mnot running\033[0m.\n"
Rovanion committed
316 317 318 319 320 321
  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
322
  if [ "$gitlab_workhorse_status" = "0" ]; then
323
      echo "The GitLab Workhorse with pid $hpid is running."
324
  else
325
      printf "The GitLab Workhorse is \033[31mnot running\033[0m.\n"
326
  fi
Douwe Maan committed
327 328
  if [ "$mail_room_enabled" = true ]; then
    if [ "$mail_room_status" = "0" ]; then
329
        echo "The GitLab MailRoom email processor with pid $mpid is running."
Douwe Maan committed
330 331 332
    else
        printf "The GitLab MailRoom email processor is \033[31mnot running\033[0m.\n"
    fi
Douwe Maan committed
333
  fi
334
  if [ "$web_status" = "0" ] && [ "$sidekiq_status" = "0" ] && [ "$gitlab_workhorse_status" = "0" ] && { [ "$mail_room_enabled" != true ] || [ "$mail_room_status" = "0" ]; }; then
335
    printf "GitLab and all its components are \033[32mup and running\033[0m.\n"
336 337 338
  fi
}

339
## Tells unicorn to reload its config and Sidekiq to restart
340
reload_gitlab(){
341
  exit_if_not_running
Rovanion committed
342
  if [ "$wpid" = "0" ];then
343
    echo "The GitLab Unicorn Web server is not running thus its configuration can't be reloaded."
344
    exit 1
Rovanion committed
345
  fi
346
  printf "Reloading GitLab Unicorn configuration... "
347
  RAILS_ENV=$RAILS_ENV bin/web reload
Rovanion committed
348
  echo "Done."
Douwe Maan committed
349

350
  echo "Restarting GitLab Sidekiq since it isn't capable of reloading its config..."
351
  RAILS_ENV=$RAILS_ENV bin/background_jobs restart
352

Douwe Maan committed
353 354 355 356 357
  if [ "$mail_room_enabled" != true ]; then
    echo "Restarting GitLab MailRoom since it isn't capable of reloading its config..."
    RAILS_ENV=$RAILS_ENV bin/mail_room restart
  fi

358 359
  wait_for_pids
  print_status
Rovanion committed
360 361
}

362
## Restarts Sidekiq and Unicorn.
363
restart_gitlab(){
364
  check_status
365
  if [ "$web_status" = "0" ] || [ "$sidekiq_status" = "0" ] || [ "$gitlab_workhorse" = "0" ] || { [ "$mail_room_enabled" = true ] && [ "$mail_room_status" = "0" ]; }; then
366
    stop_gitlab
367
  fi
368
  start_gitlab
369 370
}

Rovanion committed
371

372
### Finally the input handling.
373 374 375

case "$1" in
  start)
376
        start_gitlab
377 378
        ;;
  stop)
379
        stop_gitlab
380 381
        ;;
  restart)
382
        restart_gitlab
383 384
        ;;
  reload|force-reload)
385
	reload_gitlab
386 387
        ;;
  status)
388
        print_status
389
        exit $gitlab_status
390 391
        ;;
  *)
Rovanion committed
392
        echo "Usage: service gitlab {start|stop|restart|reload|status}"
393 394 395 396
        exit 1
        ;;
esac

397
exit