gitlab 7.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
# App Version: 6.0
7 8 9 10 11 12 13 14 15 16 17

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

Rovanion's avatar
Rovanion committed
18 19
### Environment variables
RAILS_ENV="production"
20

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



### Here ends user configuration ###


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

# Switch to the gitlab path, if it fails exit with an error.
if ! cd "$app_root" ; then
 echo "Failed to cd into $app_root, exiting!";  exit 1
fi

### Init Script functions

47
## Gets the pids from the files
Rovanion's avatar
Rovanion committed
48 49 50 51 52 53 54 55 56 57 58 59 60
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")
61
  else
Rovanion's avatar
Rovanion committed
62
    spid=0
63 64 65
  fi
}

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
## 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
83 84 85 86 87
# 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


88
## Checks whether the different parts of the service are already running or not.
Rovanion's avatar
Rovanion committed
89 90 91 92 93 94 95
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="$?"
96 97
  else
    web_status="-1"
Rovanion's avatar
Rovanion committed
98 99 100 101
  fi
  if [ $spid -ne 0 ]; then
    kill -0 "$spid" 2>/dev/null
    sidekiq_status="$?"
102 103
  else
    sidekiq_status="-1"
Rovanion's avatar
Rovanion committed
104
  fi
rezigned's avatar
rezigned committed
105 106
}

107
## Check for stale pids and remove them if necessary.
Rovanion's avatar
Rovanion committed
108 109 110 111 112
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
113 114
    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
115
      echo "Unable to remove stale pid, exiting."
116 117
      exit 1
    fi
Rovanion's avatar
Rovanion committed
118 119
  fi
  if [ "$spid" != "0" -a "$sidekiq_status" != "0" ]; then
120 121 122 123 124
    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
125 126 127
  fi
}

128
## If no parts of the service is running, bail out.
129
exit_if_not_running(){
Rovanion's avatar
Rovanion committed
130 131 132 133 134 135 136
  check_stale_pids
  if [ "$web_status" != "0" -a "$sidekiq_status" != "0" ]; then
    echo "GitLab is not running."
    exit
  fi
}

137
## Starts Unicorn and Sidekiq if they're not running.
138
start() {
Rovanion's avatar
Rovanion committed
139 140
  check_stale_pids

141 142 143 144 145 146 147 148
  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
149 150 151
  # 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."
152
  else
Rovanion's avatar
Rovanion committed
153
    # Remove old socket if it exists
Rovanion's avatar
Rovanion committed
154
    rm -f "$socket_path"/gitlab.socket 2>/dev/null
155 156
    # Start the web server
    RAILS_ENV=$RAILS_ENV script/web start &
157 158
  fi

Rovanion's avatar
Rovanion committed
159 160 161
  # 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"
162
  else
163
    RAILS_ENV=$RAILS_ENV script/background_jobs start &
164
  fi
Rovanion's avatar
Rovanion committed
165

166 167
  # Wait for the pids to be planted
  wait_for_pids
Rovanion's avatar
Rovanion committed
168
  # Finally check the status to tell wether or not GitLab is running
169
  print_status
170 171
}

172
## Asks the Unicorn and the Sidekiq if they would be so kind as to stop, if not kills them.
Rovanion's avatar
Rovanion committed
173
stop() {
174
  exit_if_not_running
175 176 177 178 179 180 181 182 183

  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
184 185
  # If the Unicorn web server is running, tell it to stop;
  if [ "$web_status" = "0" ]; then
186
     RAILS_ENV=$RAILS_ENV script/web stop
Rovanion's avatar
Rovanion committed
187 188 189
  fi
  # And do the same thing for the Sidekiq.
  if [ "$sidekiq_status" = "0" ]; then
190
    RAILS_ENV=$RAILS_ENV script/background_jobs stop
191
  fi
Rovanion's avatar
Rovanion committed
192 193

  # If something needs to be stopped, lets wait for it to stop. Never use SIGKILL in a script.
194
  while [ "$web_status" = "0" -o "$sidekiq_status" = "0" ]; do
Rovanion's avatar
Rovanion committed
195 196
    sleep 1
    check_status
197 198
    printf "."
    if [ "$web_status" != "0" -a "$sidekiq_status" != "0" ]; then
Rovanion's avatar
Rovanion committed
199 200 201 202
      printf "\n"
      break
    fi
  done
203

Rovanion's avatar
Rovanion committed
204 205 206 207 208
  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.

209
  print_status
210 211
}

212 213
## Prints the status of GitLab and it's components.
print_status() {
214 215 216 217 218
  check_status
  if [ "$web_status" != "0" -a "$sidekiq_status" != "0" ]; then
    echo "GitLab is not running."
    return
  fi
Rovanion's avatar
Rovanion committed
219
  if [ "$web_status" = "0" ]; then
220
      echo "The GitLab Unicorn web server with pid $wpid is running."
221
  else
222
      printf "The GitLab Unicorn web server is \033[31mnot running\033[0m.\n"
Rovanion's avatar
Rovanion committed
223 224 225 226 227 228 229
  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
230
    printf "GitLab and all its components are \033[32mup and running\033[0m.\n"
231 232 233
  fi
}

234
## Tells unicorn to reload it's config and Sidekiq to restart
Rovanion's avatar
Rovanion committed
235
reload(){
236
  exit_if_not_running
Rovanion's avatar
Rovanion committed
237
  if [ "$wpid" = "0" ];then
238
    echo "The GitLab Unicorn Web server is not running thus its configuration can't be reloaded."
239
    exit 1
Rovanion's avatar
Rovanion committed
240
  fi
241
  printf "Reloading GitLab Unicorn configuration... "
242
  RAILS_ENV=$RAILS_ENV script/web reload
Rovanion's avatar
Rovanion committed
243
  echo "Done."
244
  echo "Restarting GitLab Sidekiq since it isn't capable of reloading its config..."
245
  RAILS_ENV=$RAILS_ENV script/background_jobs restart
246 247 248

  wait_for_pids
  print_status
Rovanion's avatar
Rovanion committed
249 250
}

251
## Restarts Sidekiq and Unicorn.
252 253 254 255 256 257 258 259
restart(){
  check_status
  if [ "$web_status" = "0" -o "$sidekiq_status" = "0" ]; then
    stop
  fi
  start
}

Rovanion's avatar
Rovanion committed
260

261
### Finally the input handling.
262 263 264 265 266 267 268 269 270

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
271
        restart
272 273
        ;;
  reload|force-reload)
Rovanion's avatar
Rovanion committed
274
	reload
275 276
        ;;
  status)
277
        print_status
278 279
        ;;
  *)
Rovanion's avatar
Rovanion committed
280
        echo "Usage: service gitlab {start|stop|restart|reload|status}"
281 282 283 284
        exit 1
        ;;
esac

Rovanion's avatar
Rovanion committed
285
exit