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

# GITLAB
4 5
# Maintainer: @dzaporozhets
# Authors: rovanion.luckey@gmail.com, @dzaporozhets
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's avatar
Rovanion committed
27 28
### Environment variables
RAILS_ENV="production"
29
EXPERIMENTAL_PUMA=""
30

31 32
# Script variable names should be lower-case not to conflict with
# internal /bin/sh variables such as PATH, EDITOR or SHELL.
33
app_user="git"
34
app_root="/home/$app_user/gitlab"
Rovanion's avatar
Rovanion committed
35 36
pid_path="$app_root/tmp/pids"
socket_path="$app_root/tmp/sockets"
37
rails_socket="$socket_path/gitlab.socket"
Rovanion's avatar
Rovanion committed
38 39
web_server_pid_path="$pid_path/unicorn.pid"
sidekiq_pid_path="$pid_path/sidekiq.pid"
Douwe Maan's avatar
Douwe Maan committed
40 41
mail_room_enabled=false
mail_room_pid_path="$pid_path/mail_room.pid"
42
gitlab_workhorse_dir=$(cd $app_root/../gitlab-workhorse 2> /dev/null && pwd)
Jacob Vosmaer's avatar
Jacob Vosmaer committed
43
gitlab_workhorse_pid_path="$pid_path/gitlab-workhorse.pid"
44
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"
Jacob Vosmaer's avatar
Jacob Vosmaer committed
45
gitlab_workhorse_log="$app_root/log/gitlab-workhorse.log"
46 47 48 49 50
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"
51
shell_path="/bin/bash"
52
gitaly_enabled=true
53 54 55
gitaly_dir=$(cd $app_root/../gitaly 2> /dev/null && pwd)
gitaly_pid_path="$pid_path/gitaly.pid"
gitaly_log="$app_root/log/gitaly.log"
Rovanion's avatar
Rovanion committed
56

57 58
# Read configuration variable file if it is present
test -f /etc/default/gitlab && . /etc/default/gitlab
Rovanion's avatar
Rovanion committed
59 60

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

65
# Switch to the gitlab path, exit on failure.
Rovanion's avatar
Rovanion committed
66 67 68 69
if ! cd "$app_root" ; then
 echo "Failed to cd into $app_root, exiting!";  exit 1
fi

70 71 72 73 74 75 76
# Select the web server to use
if [ -z "$EXPERIMENTAL_PUMA" ]; then
  use_web_server="unicorn"
else
  use_web_server="puma"
fi

77

Rovanion's avatar
Rovanion committed
78 79
### Init Script functions

80
## Gets the pids from the files
Rovanion's avatar
Rovanion committed
81 82 83 84 85
check_pids(){
  if ! mkdir -p "$pid_path"; then
    echo "Could not create the path $pid_path needed to store the pids."
    exit 1
  fi
86
  # If there exists a file which should hold the value of the web server pid: read it.
Rovanion's avatar
Rovanion committed
87 88 89 90 91 92 93
  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")
94
  else
Rovanion's avatar
Rovanion committed
95
    spid=0
96
  fi
Jacob Vosmaer's avatar
Jacob Vosmaer committed
97 98
  if [ -f "$gitlab_workhorse_pid_path" ]; then
    hpid=$(cat "$gitlab_workhorse_pid_path")
99 100 101
  else
    hpid=0
  fi
Douwe Maan's avatar
Douwe Maan committed
102 103 104 105 106 107 108
  if [ "$mail_room_enabled" = true ]; then
    if [ -f "$mail_room_pid_path" ]; then
      mpid=$(cat "$mail_room_pid_path")
    else
      mpid=0
    fi
  fi
109 110 111 112 113 114 115
  if [ "$gitlab_pages_enabled" = true ]; then
    if [ -f "$gitlab_pages_pid_path" ]; then
      gppid=$(cat "$gitlab_pages_pid_path")
    else
      gppid=0
    fi
  fi
116 117 118 119 120 121 122
  if [ "$gitaly_enabled" = true ]; then
    if [ -f "$gitaly_pid_path" ]; then
      gapid=$(cat "$gitaly_pid_path")
    else
      gapid=0
    fi
  fi
123 124
}

125 126
## Called when we have started the two processes and are waiting for their pid files.
wait_for_pids(){
127
  # We are sleeping a bit here mostly because sidekiq is slow at writing its pid
128
  i=0;
129
  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 ]; } || { [ "$gitaly_enabled" = true ] && [ ! -f $gitaly_pid_path ]; }; do
130 131 132 133 134 135 136 137 138 139 140 141
    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
142
# We use the pids in so many parts of the script it makes sense to always check them.
143
# Only after start() is run should the pids change. Sidekiq sets its own pid.
Rovanion's avatar
Rovanion committed
144 145 146
check_pids


147
## Checks whether the different parts of the service are already running or not.
Rovanion's avatar
Rovanion committed
148 149 150 151 152 153 154
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="$?"
155 156
  else
    web_status="-1"
Rovanion's avatar
Rovanion committed
157 158 159 160
  fi
  if [ $spid -ne 0 ]; then
    kill -0 "$spid" 2>/dev/null
    sidekiq_status="$?"
161 162
  else
    sidekiq_status="-1"
Rovanion's avatar
Rovanion committed
163
  fi
164 165
  if [ $hpid -ne 0 ]; then
    kill -0 "$hpid" 2>/dev/null
Jacob Vosmaer's avatar
Jacob Vosmaer committed
166
    gitlab_workhorse_status="$?"
167
  else
Jacob Vosmaer's avatar
Jacob Vosmaer committed
168
    gitlab_workhorse_status="-1"
169
  fi
170 171 172 173 174 175 176
  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's avatar
Douwe Maan committed
177
  fi
178 179 180 181 182 183 184 185
  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
186 187 188 189 190 191 192 193 194
  if [ "$gitaly_enabled" = true ]; then
    if [ $gapid -ne 0 ]; then
      kill -0 "$gapid" 2>/dev/null
      gitaly_status="$?"
    else
      gitaly_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 ]; } && { [ "$gitaly_enabled" != true ] || [ $gitaly_status = 0 ]; }; then
195 196 197 198 199 200
    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
201 202
}

203
## Check for stale pids and remove them if necessary.
Rovanion's avatar
Rovanion committed
204 205 206 207
check_stale_pids(){
  check_status
  # If there is a pid it is something else than 0, the service is running if
  # *_status is == 0.
208
  if [ "$wpid" != "0" ] && [ "$web_status" != "0" ]; then
209
    echo "Removing stale web server pid. This is most likely caused by the web server crashing the last time it ran."
210
    if ! rm "$web_server_pid_path"; then
211
      echo "Unable to remove stale pid, exiting."
212 213
      exit 1
    fi
Rovanion's avatar
Rovanion committed
214
  fi
215
  if [ "$spid" != "0" ] && [ "$sidekiq_status" != "0" ]; then
216
    echo "Removing stale Sidekiq job dispatcher pid. This is most likely caused by Sidekiq crashing the last time it ran."
217 218 219 220
    if ! rm "$sidekiq_pid_path"; then
      echo "Unable to remove stale pid, exiting"
      exit 1
    fi
Rovanion's avatar
Rovanion committed
221
  fi
Jacob Vosmaer's avatar
Jacob Vosmaer committed
222
  if [ "$hpid" != "0" ] && [ "$gitlab_workhorse_status" != "0" ]; then
223
    echo "Removing stale GitLab Workhorse pid. This is most likely caused by GitLab Workhorse crashing the last time it ran."
Jacob Vosmaer's avatar
Jacob Vosmaer committed
224
    if ! rm "$gitlab_workhorse_pid_path"; then
225 226 227 228
      echo "Unable to remove stale pid, exiting"
      exit 1
    fi
  fi
229
  if [ "$mail_room_enabled" = true ] && [ "$mpid" != "0" ] && [ "$mail_room_status" != "0" ]; then
Douwe Maan's avatar
Douwe Maan committed
230 231 232 233 234 235
    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
236 237 238 239 240 241 242
  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
243 244 245 246 247 248 249
  if [ "$gitaly_enabled" = true ] && [ "$gapid" != "0" ] && [ "$gitaly_status" != "0" ]; then
    echo "Removing stale Gitaly pid. This is most likely caused by Gitaly crashing the last time it ran."
    if ! rm "$gitaly_pid_path"; then
      echo "Unable to remove stale pid, exiting"
      exit 1
    fi
  fi
Rovanion's avatar
Rovanion committed
250 251
}

252
## If no parts of the service is running, bail out.
253
exit_if_not_running(){
Rovanion's avatar
Rovanion committed
254
  check_stale_pids
255
  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" ]; } && { [ "$gitaly_enabled" != true ] || [ "$gitaly_status" != "0" ]; }; then
Rovanion's avatar
Rovanion committed
256 257 258 259 260
    echo "GitLab is not running."
    exit
  fi
}

261
## Starts web server and Sidekiq if they're not running.
262
start_gitlab() {
Rovanion's avatar
Rovanion committed
263 264
  check_stale_pids

Douwe Maan's avatar
Douwe Maan committed
265
  if [ "$web_status" != "0" ]; then
266
    echo "Starting GitLab web server ($use_web_server)"
Douwe Maan's avatar
Douwe Maan committed
267 268
  fi
  if [ "$sidekiq_status" != "0" ]; then
Douwe Maan's avatar
Douwe Maan committed
269
    echo "Starting GitLab Sidekiq"
270
  fi
Jacob Vosmaer's avatar
Jacob Vosmaer committed
271
  if [ "$gitlab_workhorse_status" != "0" ]; then
272
    echo "Starting GitLab Workhorse"
273
  fi
274
  if [ "$mail_room_enabled" = true ] && [ "$mail_room_status" != "0" ]; then
Douwe Maan's avatar
Douwe Maan committed
275
    echo "Starting GitLab MailRoom"
Douwe Maan's avatar
Douwe Maan committed
276
  fi
277
  if [ "$gitlab_pages_enabled" = true ] && [ "$gitlab_pages_status" != "0" ]; then
278 279
    echo "Starting GitLab Pages"
  fi
280 281 282
  if [ "$gitaly_enabled" = true ] && [ "$gitaly_status" != "0" ]; then
    echo "Starting Gitaly"
  fi
283

Rovanion's avatar
Rovanion committed
284 285
  # Then check if the service is running. If it is: don't start again.
  if [ "$web_status" = "0" ]; then
286
    echo "The web server already running with pid $wpid, not restarting."
287
  else
Rovanion's avatar
Rovanion committed
288
    # Remove old socket if it exists
fbretel's avatar
fbretel committed
289
    rm -f "$rails_socket" 2>/dev/null
290
    # Start the web server
291
    RAILS_ENV=$RAILS_ENV USE_WEB_SERVER=$use_web_server bin/web start
292 293
  fi

Rovanion's avatar
Rovanion committed
294 295 296
  # 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"
297
  else
298
    RAILS_ENV=$RAILS_ENV bin/background_jobs start &
299
  fi
Rovanion's avatar
Rovanion committed
300

Jacob Vosmaer's avatar
Jacob Vosmaer committed
301
  if [ "$gitlab_workhorse_status" = "0" ]; then
Semen Romanov's avatar
Semen Romanov committed
302
    echo "The GitLab Workhorse is already running with pid $hpid, not restarting"
303
  else
304 305 306
    # No need to remove a socket, gitlab-workhorse does this itself.
    # Because gitlab-workhorse has multiple executables we need to fix
    # the PATH.
Jacob Vosmaer's avatar
Jacob Vosmaer committed
307
    $app_root/bin/daemon_with_pidfile $gitlab_workhorse_pid_path  \
308 309
      /usr/bin/env PATH=$gitlab_workhorse_dir:$PATH \
        gitlab-workhorse $gitlab_workhorse_options \
Jacob Vosmaer's avatar
Jacob Vosmaer committed
310
      >> $gitlab_workhorse_log 2>&1 &
311 312
  fi

Douwe Maan's avatar
Douwe Maan committed
313 314 315
  if [ "$mail_room_enabled" = true ]; then
    # If MailRoom is already running, don't start it again.
    if [ "$mail_room_status" = "0" ]; then
316
      echo "The MailRoom email processor is already running with pid $mpid, not restarting"
Douwe Maan's avatar
Douwe Maan committed
317 318 319 320 321
    else
      RAILS_ENV=$RAILS_ENV bin/mail_room start &
    fi
  fi

322 323
  if [ "$gitlab_pages_enabled" = true ]; then
    if [ "$gitlab_pages_status" = "0" ]; then
Semen Romanov's avatar
Semen Romanov committed
324
      echo "The GitLab Pages is already running with pid $gppid, not restarting"
325 326 327 328 329 330 331
    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

332 333 334 335 336
  if [ "$gitaly_enabled" = true ]; then
    if [ "$gitaly_status" = "0" ]; then
      echo "Gitaly is already running with pid $gapid, not restarting"
    else
      $app_root/bin/daemon_with_pidfile $gitaly_pid_path \
337
          $gitaly_dir/gitaly $gitaly_dir/config.toml >> $gitaly_log 2>&1 &
338 339 340
    fi
  fi

341 342
  # Wait for the pids to be planted
  wait_for_pids
Rovanion's avatar
Rovanion committed
343
  # Finally check the status to tell wether or not GitLab is running
344
  print_status
345 346
}

347
## Asks web server, Sidekiq and MailRoom if they would be so kind as to stop, if not kills them.
348
stop_gitlab() {
349
  exit_if_not_running
350

Douwe Maan's avatar
Douwe Maan committed
351
  if [ "$web_status" = "0" ]; then
352
    echo "Shutting down GitLab web server"
353
    RAILS_ENV=$RAILS_ENV USE_WEB_SERVER=$use_web_server bin/web stop
Douwe Maan's avatar
Douwe Maan committed
354 355
  fi
  if [ "$sidekiq_status" = "0" ]; then
Douwe Maan's avatar
Douwe Maan committed
356
    echo "Shutting down GitLab Sidekiq"
357
    RAILS_ENV=$RAILS_ENV bin/background_jobs stop
358
  fi
Jacob Vosmaer's avatar
Jacob Vosmaer committed
359
  if [ "$gitlab_workhorse_status" = "0" ]; then
360
    echo "Shutting down GitLab Workhorse"
Jacob Vosmaer's avatar
Jacob Vosmaer committed
361
    kill -- $(cat $gitlab_workhorse_pid_path)
362
  fi
363
  if [ "$mail_room_enabled" = true ] && [ "$mail_room_status" = "0" ]; then
364
    echo "Shutting down GitLab MailRoom"
Douwe Maan's avatar
Douwe Maan committed
365 366
    RAILS_ENV=$RAILS_ENV bin/mail_room stop
  fi
367 368 369 370
  if [ "$gitlab_pages_status" = "0" ]; then
    echo "Shutting down gitlab-pages"
    kill -- $(cat $gitlab_pages_pid_path)
  fi
371 372 373 374
  if [ "$gitaly_status" = "0" ]; then
    echo "Shutting down Gitaly"
    kill -- $(cat $gitaly_pid_path)
  fi
Rovanion's avatar
Rovanion committed
375 376

  # If something needs to be stopped, lets wait for it to stop. Never use SIGKILL in a script.
377
  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" ]; } || { [ "$gitaly_enabled" = true ] && [ "$gitaly_status" = "0" ]; }; do
Rovanion's avatar
Rovanion committed
378 379
    sleep 1
    check_status
380
    printf "."
381
    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" ]; } && { [ "$gitaly_enabled" != true ] || [ "$gitaly_status" != "0" ]; }; then
Rovanion's avatar
Rovanion committed
382 383 384 385
      printf "\n"
      break
    fi
  done
386

Rovanion's avatar
Rovanion committed
387 388 389
  sleep 1
  # Cleaning up unused pids
  rm "$web_server_pid_path" 2>/dev/null
390
  # rm "$sidekiq_pid_path" 2>/dev/null # Sidekiq seems to be cleaning up its own pid.
Jacob Vosmaer's avatar
Jacob Vosmaer committed
391
  rm -f "$gitlab_workhorse_pid_path"
Douwe Maan's avatar
Douwe Maan committed
392 393 394
  if [ "$mail_room_enabled" = true ]; then
    rm "$mail_room_pid_path" 2>/dev/null
  fi
395
  rm -f "$gitlab_pages_pid_path"
396
  rm -f "$gitaly_pid_path"
Rovanion's avatar
Rovanion committed
397

398
  print_status
399 400
}

401
## Prints the status of GitLab and its components.
402
print_status() {
403
  check_status
404
  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" ]; } && { [ "$gitaly_enabled" != true ] || [ "$gitaly_status" != "0" ]; }; then
405 406 407
    echo "GitLab is not running."
    return
  fi
Rovanion's avatar
Rovanion committed
408
  if [ "$web_status" = "0" ]; then
409
      echo "The GitLab web server with pid $wpid is running."
410
  else
411
      printf "The GitLab web server is \033[31mnot running\033[0m.\n"
Rovanion's avatar
Rovanion committed
412 413 414 415 416 417
  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
Jacob Vosmaer's avatar
Jacob Vosmaer committed
418
  if [ "$gitlab_workhorse_status" = "0" ]; then
419
      echo "The GitLab Workhorse with pid $hpid is running."
420
  else
421
      printf "The GitLab Workhorse is \033[31mnot running\033[0m.\n"
422
  fi
Douwe Maan's avatar
Douwe Maan committed
423 424
  if [ "$mail_room_enabled" = true ]; then
    if [ "$mail_room_status" = "0" ]; then
425
        echo "The GitLab MailRoom email processor with pid $mpid is running."
Douwe Maan's avatar
Douwe Maan committed
426 427 428
    else
        printf "The GitLab MailRoom email processor is \033[31mnot running\033[0m.\n"
    fi
Douwe Maan's avatar
Douwe Maan committed
429
  fi
430 431
  if [ "$gitlab_pages_enabled" = true ]; then
    if [ "$gitlab_pages_status" = "0" ]; then
Semen Romanov's avatar
Semen Romanov committed
432
        echo "The GitLab Pages with pid $gppid is running."
433 434 435 436
    else
        printf "The GitLab Pages is \033[31mnot running\033[0m.\n"
    fi
  fi
437 438 439 440 441 442 443 444
  if [ "$gitaly_enabled" = true ]; then
    if [ "$gitaly_status" = "0" ]; then
        echo "Gitaly with pid $gapid is running."
    else
        printf "Gitaly 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" ]; } && { [ "$gitaly_enabled" != true ] || [ "$gitaly_status" = "0" ]; }; then
445
    printf "GitLab and all its components are \033[32mup and running\033[0m.\n"
446 447 448
  fi
}

449
## Tells web server to reload its config and Sidekiq to restart
450
reload_gitlab(){
451
  exit_if_not_running
Rovanion's avatar
Rovanion committed
452
  if [ "$wpid" = "0" ];then
453
    echo "The GitLab web server Web server is not running thus its configuration can't be reloaded."
454
    exit 1
Rovanion's avatar
Rovanion committed
455
  fi
456
  printf "Reloading GitLab web server configuration... "
457
  RAILS_ENV=$RAILS_ENV USE_WEB_SERVER=$use_web_server bin/web reload
Rovanion's avatar
Rovanion committed
458
  echo "Done."
Douwe Maan's avatar
Douwe Maan committed
459

460
  echo "Restarting GitLab Sidekiq since it isn't capable of reloading its config..."
461
  RAILS_ENV=$RAILS_ENV bin/background_jobs restart
462

Douwe Maan's avatar
Douwe Maan committed
463 464 465 466 467
  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

468 469
  wait_for_pids
  print_status
Rovanion's avatar
Rovanion committed
470 471
}

472
## Restarts Sidekiq and web server.
473
restart_gitlab(){
474
  check_status
475
  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" ]; } || { [ "$gitaly_enabled" = true ] && [ "$gitaly_status" = "0" ]; }; then
476
    stop_gitlab
477
  fi
478
  start_gitlab
479 480
}

Rovanion's avatar
Rovanion committed
481

482
### Finally the input handling.
483 484 485

case "$1" in
  start)
486
        start_gitlab
487 488
        ;;
  stop)
489
        stop_gitlab
490 491
        ;;
  restart)
492
        restart_gitlab
493 494
        ;;
  reload|force-reload)
495
	reload_gitlab
496 497
        ;;
  status)
498
        print_status
499
        exit $gitlab_status
500 501
        ;;
  *)
Rovanion's avatar
Rovanion committed
502
        echo "Usage: service gitlab {start|stop|restart|reload|status}"
503 504 505 506
        exit 1
        ;;
esac

Rovanion's avatar
Rovanion committed
507
exit