gitlab 12.7 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

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

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's avatar
Rovanion committed
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"
Douwe Maan's avatar
Douwe Maan committed
38 39
mail_room_enabled=false
mail_room_pid_path="$pid_path/mail_room.pid"
40 41 42 43
gitlab_git_http_server_pid_path="$pid_path/gitlab-git-http-server.pid"
gitlab_git_http_server_options="-listenUmask 0 -listenNetwork unix -listenAddr $socket_path/gitlab-git-http-server.socket -authBackend http://127.0.0.1:8080"
gitlab_git_http_server_repo_root='/home/git/repositories'
gitlab_git_http_server_log="$app_root/log/gitlab-git-http-server.log"
44
shell_path="/bin/bash"
Rovanion's avatar
Rovanion committed
45

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

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

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

59

Rovanion's avatar
Rovanion committed
60 61
### Init Script functions

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

93 94 95 96
## 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;
97
  while [ ! -f $web_server_pid_path ] || [ ! -f $sidekiq_pid_path ] || [ ! -f $gitlab_git_http_server_pid_path ] || { [ "$mail_room_enabled" = true ] && [ ! -f $mail_room_pid_path ]; }; do
98 99 100 101 102 103 104 105 106 107 108 109
    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
110 111 112 113 114
# 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


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

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

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

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

Douwe Maan's avatar
Douwe Maan committed
203
  if [ "$web_status" != "0" ]; then
Douwe Maan's avatar
Douwe Maan committed
204
    echo "Starting GitLab Unicorn"
Douwe Maan's avatar
Douwe Maan committed
205 206
  fi
  if [ "$sidekiq_status" != "0" ]; then
Douwe Maan's avatar
Douwe Maan committed
207
    echo "Starting GitLab Sidekiq"
208
  fi
209 210 211
  if [ "$gitlab_git_http_server_status" != "0" ]; then
    echo "Starting gitlab-git-http-server"
  fi
212
  if [ "$mail_room_enabled" = true ] && [ "$mail_room_status" != "0" ]; then
Douwe Maan's avatar
Douwe Maan committed
213
    echo "Starting GitLab MailRoom"
Douwe Maan's avatar
Douwe Maan committed
214
  fi
215

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

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

233 234 235 236 237 238 239 240 241 242 243
  if [ "$gitlab_git_http_server_status" = "0" ]; then
    echo "The gitlab-git-http-server is already running with pid $spid, not restarting"
  else
    # No need to remove a socket, gitlab-git-http-server does this itself
    $app_root/bin/daemon_with_pidfile $gitlab_git_http_server_pid_path  \
      $app_root/../gitlab-git-http-server/gitlab-git-http-server \
        $gitlab_git_http_server_options \
        $gitlab_git_http_server_repo_root \
      >> $gitlab_git_http_server_log 2>&1 &
  fi

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

253 254
  # Wait for the pids to be planted
  wait_for_pids
Rovanion's avatar
Rovanion committed
255
  # Finally check the status to tell wether or not GitLab is running
256
  print_status
257 258
}

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

Douwe Maan's avatar
Douwe Maan committed
263
  if [ "$web_status" = "0" ]; then
Douwe Maan's avatar
Douwe Maan committed
264
    echo "Shutting down GitLab Unicorn"
265
    RAILS_ENV=$RAILS_ENV bin/web stop
Douwe Maan's avatar
Douwe Maan committed
266 267
  fi
  if [ "$sidekiq_status" = "0" ]; then
Douwe Maan's avatar
Douwe Maan committed
268
    echo "Shutting down GitLab Sidekiq"
269
    RAILS_ENV=$RAILS_ENV bin/background_jobs stop
270
  fi
271 272
  if [ "$gitlab_git_http_server_status" = "0" ]; then
    echo "Shutting down gitlab-git-http-server"
273
    kill -- $(cat $gitlab_git_http_server_pid_path)
274
  fi
275
  if [ "$mail_room_enabled" = true ] && [ "$mail_room_status" = "0" ]; then
276
    echo "Shutting down GitLab MailRoom"
Douwe Maan's avatar
Douwe Maan committed
277 278
    RAILS_ENV=$RAILS_ENV bin/mail_room stop
  fi
Rovanion's avatar
Rovanion committed
279 280

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

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

300
  print_status
301 302
}

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

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

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

Douwe Maan's avatar
Douwe Maan committed
351 352 353 354 355
  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

356 357
  wait_for_pids
  print_status
Rovanion's avatar
Rovanion committed
358 359
}

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

Rovanion's avatar
Rovanion committed
369

370
### Finally the input handling.
371 372 373

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

Rovanion's avatar
Rovanion committed
395
exit