BigW Consortium Gitlab

gitlab 15.1 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 46 47 48 49
gitlab_pages_enabled=false
gitlab_pages_dir=$(cd $app_root/../gitlab-pages 2> /dev/null && pwd)
gitlab_pages_pid_path="$pid_path/gitlab-pages.pid"
gitlab_pages_options="-pages-domain example.com -pages-root $app_root/shared/pages -listen-proxy 127.0.0.1:8090"
gitlab_pages_log="$app_root/log/gitlab-pages.log"
50
shell_path="/bin/bash"
Rovanion committed
51

52 53
# Read configuration variable file if it is present
test -f /etc/default/gitlab && . /etc/default/gitlab
Rovanion committed
54 55

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

60
# Switch to the gitlab path, exit on failure.
Rovanion committed
61 62 63 64
if ! cd "$app_root" ; then
 echo "Failed to cd into $app_root, exiting!";  exit 1
fi

65

Rovanion committed
66 67
### Init Script functions

68
## Gets the pids from the files
Rovanion committed
69 70 71 72 73 74 75 76 77 78 79 80 81
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")
82
  else
Rovanion committed
83
    spid=0
84
  fi
85 86
  if [ -f "$gitlab_workhorse_pid_path" ]; then
    hpid=$(cat "$gitlab_workhorse_pid_path")
87 88 89
  else
    hpid=0
  fi
Douwe Maan committed
90 91 92 93 94 95 96
  if [ "$mail_room_enabled" = true ]; then
    if [ -f "$mail_room_pid_path" ]; then
      mpid=$(cat "$mail_room_pid_path")
    else
      mpid=0
    fi
  fi
97 98 99 100 101 102 103
  if [ "$gitlab_pages_enabled" = true ]; then
    if [ -f "$gitlab_pages_pid_path" ]; then
      gppid=$(cat "$gitlab_pages_pid_path")
    else
      gppid=0
    fi
  fi
104 105
}

106 107
## Called when we have started the two processes and are waiting for their pid files.
wait_for_pids(){
108
  # We are sleeping a bit here mostly because sidekiq is slow at writing its pid
109
  i=0;
110
  while [ ! -f $web_server_pid_path ] || [ ! -f $sidekiq_pid_path ] || [ ! -f $gitlab_workhorse_pid_path ] || { [ "$mail_room_enabled" = true ] && [ ! -f $mail_room_pid_path ]; } || { [ "$gitlab_pages_enabled" = true ] && [ ! -f $gitlab_pages_pid_path ]; }; do
111 112 113 114 115 116 117 118 119 120 121 122
    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
123
# We use the pids in so many parts of the script it makes sense to always check them.
124
# Only after start() is run should the pids change. Sidekiq sets its own pid.
Rovanion committed
125 126 127
check_pids


128
## Checks whether the different parts of the service are already running or not.
Rovanion committed
129 130 131 132 133 134 135
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="$?"
136 137
  else
    web_status="-1"
Rovanion committed
138 139 140 141
  fi
  if [ $spid -ne 0 ]; then
    kill -0 "$spid" 2>/dev/null
    sidekiq_status="$?"
142 143
  else
    sidekiq_status="-1"
Rovanion committed
144
  fi
145 146
  if [ $hpid -ne 0 ]; then
    kill -0 "$hpid" 2>/dev/null
147
    gitlab_workhorse_status="$?"
148
  else
149
    gitlab_workhorse_status="-1"
150
  fi
151 152 153 154 155 156 157
  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
158
  fi
159 160 161 162 163 164 165 166 167
  if [ "$gitlab_pages_enabled" = true ]; then
    if [ $gppid -ne 0 ]; then
      kill -0 "$gppid" 2>/dev/null
      gitlab_pages_status="$?"
    else
      gitlab_pages_status="-1"
    fi
  fi
  if [ $web_status = 0 ] && [ $sidekiq_status = 0 ] && [ $gitlab_workhorse_status = 0 ] && { [ "$mail_room_enabled" != true ] || [ $mail_room_status = 0 ]; } && { [ "$gitlab_pages_enabled" != true ] || [ $gitlab_pages_status = 0 ]; }; then
168 169 170 171 172 173
    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
174 175
}

176
## Check for stale pids and remove them if necessary.
Rovanion committed
177 178 179 180
check_stale_pids(){
  check_status
  # If there is a pid it is something else than 0, the service is running if
  # *_status is == 0.
181
  if [ "$wpid" != "0" ] && [ "$web_status" != "0" ]; then
182 183
    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
184
      echo "Unable to remove stale pid, exiting."
185 186
      exit 1
    fi
Rovanion committed
187
  fi
188
  if [ "$spid" != "0" ] && [ "$sidekiq_status" != "0" ]; then
189
    echo "Removing stale Sidekiq job dispatcher pid. This is most likely caused by Sidekiq crashing the last time it ran."
190 191 192 193
    if ! rm "$sidekiq_pid_path"; then
      echo "Unable to remove stale pid, exiting"
      exit 1
    fi
Rovanion committed
194
  fi
195
  if [ "$hpid" != "0" ] && [ "$gitlab_workhorse_status" != "0" ]; then
196
    echo "Removing stale GitLab Workhorse pid. This is most likely caused by GitLab Workhorse crashing the last time it ran."
197
    if ! rm "$gitlab_workhorse_pid_path"; then
198 199 200 201
      echo "Unable to remove stale pid, exiting"
      exit 1
    fi
  fi
202
  if [ "$mail_room_enabled" = true ] && [ "$mpid" != "0" ] && [ "$mail_room_status" != "0" ]; then
Douwe Maan committed
203 204 205 206 207 208
    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
209 210 211 212 213 214 215
  if [ "$gitlab_pages_enabled" = true ] && [ "$gppid" != "0" ] && [ "$gitlab_pages_status" != "0" ]; then
    echo "Removing stale GitLab Pages job dispatcher pid. This is most likely caused by GitLab Pages crashing the last time it ran."
    if ! rm "$gitlab_pages_pid_path"; then
      echo "Unable to remove stale pid, exiting"
      exit 1
    fi
  fi
Rovanion committed
216 217
}

218
## If no parts of the service is running, bail out.
219
exit_if_not_running(){
Rovanion committed
220
  check_stale_pids
221
  if [ "$web_status" != "0" ] && [ "$sidekiq_status" != "0" ] && [ "$gitlab_workhorse_status" != "0" ] && { [ "$mail_room_enabled" != true ] || [ "$mail_room_status" != "0" ]; } && { [ "$gitlab_pages_enabled" != true ] || [ "$gitlab_pages_status" != "0" ]; }; then
Rovanion committed
222 223 224 225 226
    echo "GitLab is not running."
    exit
  fi
}

227
## Starts Unicorn and Sidekiq if they're not running.
228
start_gitlab() {
Rovanion committed
229 230
  check_stale_pids

Douwe Maan committed
231
  if [ "$web_status" != "0" ]; then
Douwe Maan committed
232
    echo "Starting GitLab Unicorn"
Douwe Maan committed
233 234
  fi
  if [ "$sidekiq_status" != "0" ]; then
Douwe Maan committed
235
    echo "Starting GitLab Sidekiq"
236
  fi
237
  if [ "$gitlab_workhorse_status" != "0" ]; then
238
    echo "Starting GitLab Workhorse"
239
  fi
240
  if [ "$mail_room_enabled" = true ] && [ "$mail_room_status" != "0" ]; then
Douwe Maan committed
241
    echo "Starting GitLab MailRoom"
Douwe Maan committed
242
  fi
243
  if [ "$gitlab_pages_enabled" = true ] && [ "$gitlab_pages_status" != "0" ]; then
244 245
    echo "Starting GitLab Pages"
  fi
246

Rovanion committed
247 248 249
  # 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."
250
  else
Rovanion committed
251
    # Remove old socket if it exists
fbretel committed
252
    rm -f "$rails_socket" 2>/dev/null
253
    # Start the web server
254
    RAILS_ENV=$RAILS_ENV bin/web start
255 256
  fi

Rovanion committed
257 258 259
  # 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"
260
  else
261
    RAILS_ENV=$RAILS_ENV bin/background_jobs start &
262
  fi
Rovanion committed
263

264
  if [ "$gitlab_workhorse_status" = "0" ]; then
265
    echo "The GitLab Workhorse is already running with pid $spid, not restarting"
266
  else
267 268 269
    # No need to remove a socket, gitlab-workhorse does this itself.
    # Because gitlab-workhorse has multiple executables we need to fix
    # the PATH.
270
    $app_root/bin/daemon_with_pidfile $gitlab_workhorse_pid_path  \
271 272
      /usr/bin/env PATH=$gitlab_workhorse_dir:$PATH \
        gitlab-workhorse $gitlab_workhorse_options \
273
      >> $gitlab_workhorse_log 2>&1 &
274 275
  fi

Douwe Maan committed
276 277 278
  if [ "$mail_room_enabled" = true ]; then
    # If MailRoom is already running, don't start it again.
    if [ "$mail_room_status" = "0" ]; then
279
      echo "The MailRoom email processor is already running with pid $mpid, not restarting"
Douwe Maan committed
280 281 282 283 284
    else
      RAILS_ENV=$RAILS_ENV bin/mail_room start &
    fi
  fi

285 286 287 288 289 290 291 292 293 294
  if [ "$gitlab_pages_enabled" = true ]; then
    if [ "$gitlab_pages_status" = "0" ]; then
      echo "The GitLab Pages is already running with pid $spid, not restarting"
    else
      $app_root/bin/daemon_with_pidfile $gitlab_pages_pid_path  \
          $gitlab_pages_dir/gitlab-pages $gitlab_pages_options \
        >> $gitlab_pages_log 2>&1 &
    fi
  fi

295 296
  # Wait for the pids to be planted
  wait_for_pids
Rovanion committed
297
  # Finally check the status to tell wether or not GitLab is running
298
  print_status
299 300
}

Douwe Maan committed
301
## Asks Unicorn, Sidekiq and MailRoom if they would be so kind as to stop, if not kills them.
302
stop_gitlab() {
303
  exit_if_not_running
304

Douwe Maan committed
305
  if [ "$web_status" = "0" ]; then
Douwe Maan committed
306
    echo "Shutting down GitLab Unicorn"
307
    RAILS_ENV=$RAILS_ENV bin/web stop
Douwe Maan committed
308 309
  fi
  if [ "$sidekiq_status" = "0" ]; then
Douwe Maan committed
310
    echo "Shutting down GitLab Sidekiq"
311
    RAILS_ENV=$RAILS_ENV bin/background_jobs stop
312
  fi
313
  if [ "$gitlab_workhorse_status" = "0" ]; then
314
    echo "Shutting down GitLab Workhorse"
315
    kill -- $(cat $gitlab_workhorse_pid_path)
316
  fi
317
  if [ "$mail_room_enabled" = true ] && [ "$mail_room_status" = "0" ]; then
318
    echo "Shutting down GitLab MailRoom"
Douwe Maan committed
319 320
    RAILS_ENV=$RAILS_ENV bin/mail_room stop
  fi
321 322 323 324
  if [ "$gitlab_pages_status" = "0" ]; then
    echo "Shutting down gitlab-pages"
    kill -- $(cat $gitlab_pages_pid_path)
  fi
Rovanion committed
325 326

  # If something needs to be stopped, lets wait for it to stop. Never use SIGKILL in a script.
327
  while [ "$web_status" = "0" ] || [ "$sidekiq_status" = "0" ] || [ "$gitlab_workhorse_status" = "0" ] || { [ "$mail_room_enabled" = true ] && [ "$mail_room_status" = "0" ]; } || { [ "$gitlab_pages_enabled" = true ] && [ "$gitlab_pages_status" = "0" ]; }; do
Rovanion committed
328 329
    sleep 1
    check_status
330
    printf "."
331
    if [ "$web_status" != "0" ] && [ "$sidekiq_status" != "0" ] && [ "$gitlab_workhorse_status" != "0" ] && { [ "$mail_room_enabled" != true ] || [ "$mail_room_status" != "0" ]; } && { [ "$gitlab_pages_enabled" != true ] || [ "$gitlab_pages_status" != "0" ]; }; then
Rovanion committed
332 333 334 335
      printf "\n"
      break
    fi
  done
336

Rovanion committed
337 338 339
  sleep 1
  # Cleaning up unused pids
  rm "$web_server_pid_path" 2>/dev/null
340
  # rm "$sidekiq_pid_path" 2>/dev/null # Sidekiq seems to be cleaning up its own pid.
341
  rm -f "$gitlab_workhorse_pid_path"
Douwe Maan committed
342 343 344
  if [ "$mail_room_enabled" = true ]; then
    rm "$mail_room_pid_path" 2>/dev/null
  fi
345
  rm -f "$gitlab_pages_pid_path"
Rovanion committed
346

347
  print_status
348 349
}

350
## Prints the status of GitLab and its components.
351
print_status() {
352
  check_status
353
  if [ "$web_status" != "0" ] && [ "$sidekiq_status" != "0" ] && [ "$gitlab_workhorse_status" != "0" ] && { [ "$mail_room_enabled" != true ] || [ "$mail_room_status" != "0" ]; } && { [ "$gitlab_pages_enabled" != true ] || [ "$gitlab_pages_status" != "0" ]; }; then
354 355 356
    echo "GitLab is not running."
    return
  fi
Rovanion committed
357
  if [ "$web_status" = "0" ]; then
358
      echo "The GitLab Unicorn web server with pid $wpid is running."
359
  else
360
      printf "The GitLab Unicorn web server is \033[31mnot running\033[0m.\n"
Rovanion committed
361 362 363 364 365 366
  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
367
  if [ "$gitlab_workhorse_status" = "0" ]; then
368
      echo "The GitLab Workhorse with pid $hpid is running."
369
  else
370
      printf "The GitLab Workhorse is \033[31mnot running\033[0m.\n"
371
  fi
Douwe Maan committed
372 373
  if [ "$mail_room_enabled" = true ]; then
    if [ "$mail_room_status" = "0" ]; then
374
        echo "The GitLab MailRoom email processor with pid $mpid is running."
Douwe Maan committed
375 376 377
    else
        printf "The GitLab MailRoom email processor is \033[31mnot running\033[0m.\n"
    fi
Douwe Maan committed
378
  fi
379 380 381 382 383 384 385 386
  if [ "$gitlab_pages_enabled" = true ]; then
    if [ "$gitlab_pages_status" = "0" ]; then
        echo "The GitLab Pages with pid $mpid is running."
    else
        printf "The GitLab Pages is \033[31mnot running\033[0m.\n"
    fi
  fi
  if [ "$web_status" = "0" ] && [ "$sidekiq_status" = "0" ] && [ "$gitlab_workhorse_status" = "0" ] && { [ "$mail_room_enabled" != true ] || [ "$mail_room_status" = "0" ]; } && { [ "$gitlab_pages_enabled" != true ] || [ "$gitlab_pages_status" = "0" ]; }; then
387
    printf "GitLab and all its components are \033[32mup and running\033[0m.\n"
388 389 390
  fi
}

391
## Tells unicorn to reload its config and Sidekiq to restart
392
reload_gitlab(){
393
  exit_if_not_running
Rovanion committed
394
  if [ "$wpid" = "0" ];then
395
    echo "The GitLab Unicorn Web server is not running thus its configuration can't be reloaded."
396
    exit 1
Rovanion committed
397
  fi
398
  printf "Reloading GitLab Unicorn configuration... "
399
  RAILS_ENV=$RAILS_ENV bin/web reload
Rovanion committed
400
  echo "Done."
Douwe Maan committed
401

402
  echo "Restarting GitLab Sidekiq since it isn't capable of reloading its config..."
403
  RAILS_ENV=$RAILS_ENV bin/background_jobs restart
404

Douwe Maan committed
405 406 407 408 409
  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

410 411
  wait_for_pids
  print_status
Rovanion committed
412 413
}

414
## Restarts Sidekiq and Unicorn.
415
restart_gitlab(){
416
  check_status
417
  if [ "$web_status" = "0" ] || [ "$sidekiq_status" = "0" ] || [ "$gitlab_workhorse" = "0" ] || { [ "$mail_room_enabled" = true ] && [ "$mail_room_status" = "0" ]; } || { [ "$gitlab_pages_enabled" = true ] && [ "$gitlab_pages_status" = "0" ]; }; then
418
    stop_gitlab
419
  fi
420
  start_gitlab
421 422
}

Rovanion committed
423

424
### Finally the input handling.
425 426 427

case "$1" in
  start)
428
        start_gitlab
429 430
        ;;
  stop)
431
        stop_gitlab
432 433
        ;;
  restart)
434
        restart_gitlab
435 436
        ;;
  reload|force-reload)
437
	reload_gitlab
438 439
        ;;
  status)
440
        print_status
441
        exit $gitlab_status
442 443
        ;;
  *)
Rovanion committed
444
        echo "Usage: service gitlab {start|stop|restart|reload|status}"
445 446 447 448
        exit 1
        ;;
esac

449
exit