Commit df9e1db9040db43eecb9f2a449055a28adfc5707
Committed by
Rovanion Luckey
1 parent
79f0858a
Exists in
master
and in
4 other branches
Rewrote init script.
Showing
2 changed files
with
175 additions
and
78 deletions
Show diff stats
CHANGELOG
1 | -#! /bin/bash | |
1 | +#! /bin/sh | |
2 | 2 | |
3 | 3 | # GITLAB |
4 | 4 | # Maintainer: @randx |
5 | +# Authors: rovanion.luckey@gmail.com, @randx | |
5 | 6 | # App Version: 6.0 |
6 | 7 | |
7 | 8 | ### BEGIN INIT INFO |
... | ... | @@ -14,102 +15,198 @@ |
14 | 15 | # Description: GitLab git repository management |
15 | 16 | ### END INIT INFO |
16 | 17 | |
18 | +### Environment variables | |
19 | +RAILS_ENV="production" | |
17 | 20 | |
18 | -APP_ROOT="/home/git/gitlab" | |
19 | -APP_USER="git" | |
20 | -DAEMON_OPTS="-c $APP_ROOT/config/unicorn.rb -E production" | |
21 | -PID_PATH="$APP_ROOT/tmp/pids" | |
22 | -SOCKET_PATH="$APP_ROOT/tmp/sockets" | |
23 | -WEB_SERVER_PID="$PID_PATH/unicorn.pid" | |
24 | -SIDEKIQ_PID="$PID_PATH/sidekiq.pid" | |
25 | -STOP_SIDEKIQ="RAILS_ENV=production bundle exec rake sidekiq:stop" | |
26 | -START_SIDEKIQ="RAILS_ENV=production bundle exec rake sidekiq:start" | |
27 | -NAME="gitlab" | |
28 | -DESC="GitLab service" | |
29 | - | |
30 | -check_pid(){ | |
31 | - if [ -f $WEB_SERVER_PID ]; then | |
32 | - PID=`cat $WEB_SERVER_PID` | |
33 | - SPID=`cat $SIDEKIQ_PID` | |
34 | - STATUS=`ps aux | grep $PID | grep -v grep | wc -l` | |
21 | +# Script variable names should be lower-case not to conflict with internal | |
22 | +# /bin/sh variables such as PATH, EDITOR or SHELL. | |
23 | +app_root="/home/gitlab/gitlab" | |
24 | +app_user="gitlab" | |
25 | +unicorn_conf="$app_root/config/unicorn.rb" | |
26 | +pid_path="$app_root/tmp/pids" | |
27 | +socket_path="$app_root/tmp/sockets" | |
28 | +web_server_pid_path="$pid_path/unicorn.pid" | |
29 | +sidekiq_pid_path="$pid_path/sidekiq.pid" | |
30 | + | |
31 | + | |
32 | + | |
33 | +### Here ends user configuration ### | |
34 | + | |
35 | + | |
36 | +# Switch to the app_user if it is not he/she who is running the script. | |
37 | +if [ "$USER" != "$app_user" ]; then | |
38 | + sudo -u "$app_user" -H /etc/init.d/gitlab "$@"; exit; | |
39 | +fi | |
40 | + | |
41 | +# Switch to the gitlab path, if it fails exit with an error. | |
42 | +if ! cd "$app_root" ; then | |
43 | + echo "Failed to cd into $app_root, exiting!"; exit 1 | |
44 | +fi | |
45 | + | |
46 | +### Init Script functions | |
47 | + | |
48 | +check_pids(){ | |
49 | + if ! mkdir -p "$pid_path"; then | |
50 | + echo "Could not create the path $pid_path needed to store the pids." | |
51 | + exit 1 | |
52 | + fi | |
53 | + # If there exists a file which should hold the value of the Unicorn pid: read it. | |
54 | + if [ -f "$web_server_pid_path" ]; then | |
55 | + wpid=$(cat "$web_server_pid_path") | |
56 | + else | |
57 | + wpid=0 | |
58 | + fi | |
59 | + if [ -f "$sidekiq_pid_path" ]; then | |
60 | + spid=$(cat "$sidekiq_pid_path") | |
35 | 61 | else |
36 | - STATUS=0 | |
37 | - PID=0 | |
62 | + spid=0 | |
38 | 63 | fi |
39 | 64 | } |
40 | 65 | |
41 | -execute() { | |
42 | - sudo -u $APP_USER -H bash -l -c "$1" | |
66 | +# We use the pids in so many parts of the script it makes sense to always check them. | |
67 | +# Only after start() is run should the pids change. Sidekiq sets it's own pid. | |
68 | +check_pids | |
69 | + | |
70 | + | |
71 | +# Checks wether the different parts of the server is already running or not. | |
72 | +check_status(){ | |
73 | + check_pids | |
74 | + # If the web server is running kill -0 $wpid returns true, or rather 0. | |
75 | + # Checks of *_status should only check for == 0 or != 0, never anything else. | |
76 | + if [ $wpid -ne 0 ]; then | |
77 | + kill -0 "$wpid" 2>/dev/null | |
78 | + web_status="$?" | |
79 | + fi | |
80 | + if [ $spid -ne 0 ]; then | |
81 | + kill -0 "$spid" 2>/dev/null | |
82 | + sidekiq_status="$?" | |
83 | + fi | |
43 | 84 | } |
44 | 85 | |
86 | +# Check for stale pids and remove them if necessary | |
87 | +check_stale_pids(){ | |
88 | + check_status | |
89 | + # If there is a pid it is something else than 0, the service is running if | |
90 | + # *_status is == 0. | |
91 | + if [ "$wpid" != "0" -a "$web_status" != "0" ]; then | |
92 | + echo "Found stale Unicorn web server pid, removing. This is most likely caused by the web server crashing the last time it ran." | |
93 | + rm "$web_server_pid_path" | |
94 | + fi | |
95 | + if [ "$spid" != "0" -a "$sidekiq_status" != "0" ]; then | |
96 | + echo "Found stale Sidekiq web server pid, removing. This is most likely caused by the Sidekiq crashing the last time it ran." | |
97 | + rm "$sidekiq_pid_path" | |
98 | + fi | |
99 | +} | |
100 | + | |
101 | +# If no parts of the service is running, bail out. | |
102 | +check_not_running(){ | |
103 | + check_stale_pids | |
104 | + if [ "$web_status" != "0" -a "$sidekiq_status" != "0" ]; then | |
105 | + echo "GitLab is not running." | |
106 | + exit | |
107 | + fi | |
108 | +} | |
109 | + | |
110 | +# Starts Unicorn and Sidekiq. | |
45 | 111 | start() { |
46 | - cd $APP_ROOT | |
47 | - check_pid | |
48 | - if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then | |
49 | - # Program is running, exit with error code 1. | |
50 | - echo "Error! $DESC $NAME is currently running!" | |
51 | - exit 1 | |
112 | + check_stale_pids | |
113 | + | |
114 | + # Then check if the service is running. If it is: don't start again. | |
115 | + if [ "$web_status" = "0" ]; then | |
116 | + echo "The Unicorn web server already running with pid $wpid, not restarting." | |
52 | 117 | else |
53 | - if [ `whoami` = root ]; then | |
54 | - execute "rm -f $SOCKET_PATH/gitlab.socket" | |
55 | - execute "RAILS_ENV=production bundle exec unicorn_rails $DAEMON_OPTS > /dev/null 2>&1 &" | |
56 | - execute "mkdir -p $PID_PATH && $START_SIDEKIQ > /dev/null 2>&1 &" | |
57 | - echo "$DESC started" | |
58 | - fi | |
118 | + echo "Starting the GitLab Unicorn web server..." | |
119 | + # Remove old socket if it exists | |
120 | + rm -f "$socket_path"/gitlab.socket | |
121 | + # Start the webserver | |
122 | + bundle exec unicorn_rails -D -c "$unicorn_conf" -E "$RAILS_ENV" | |
59 | 123 | fi |
60 | -} | |
61 | 124 | |
62 | -stop() { | |
63 | - cd $APP_ROOT | |
64 | - check_pid | |
65 | - if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then | |
66 | - ## Program is running, stop it. | |
67 | - kill -QUIT `cat $WEB_SERVER_PID` | |
68 | - execute "mkdir -p $PID_PATH && $STOP_SIDEKIQ > /dev/null 2>&1 &" | |
69 | - rm "$WEB_SERVER_PID" >> /dev/null | |
70 | - echo "$DESC stopped" | |
125 | + # If sidekiq is already running, don't start it again. | |
126 | + if [ "$sidekiq_status" = "0" ]; then | |
127 | + echo "The Sidekiq job dispatcher is already running with pid $spid, not restarting" | |
71 | 128 | else |
72 | - ## Program is not running, exit with error. | |
73 | - echo "Error! $DESC not started!" | |
74 | - exit 1 | |
129 | + echo "Starting the GitLab Sidekiq event dispatcher..." | |
130 | + RAILS_ENV=$RAILS_ENV bundle exec rake sidekiq:start | |
131 | + # We are sleeping a bit here because sidekiq is slow at writing it's pid | |
132 | + sleep 2 | |
75 | 133 | fi |
134 | + | |
135 | + # Finally check the status to tell wether or not GitLab is running | |
136 | + status | |
76 | 137 | } |
77 | 138 | |
78 | -restart() { | |
79 | - cd $APP_ROOT | |
80 | - check_pid | |
81 | - if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then | |
82 | - echo "Restarting $DESC..." | |
83 | - kill -USR2 `cat $WEB_SERVER_PID` | |
84 | - execute "mkdir -p $PID_PATH && $STOP_SIDEKIQ > /dev/null 2>&1" | |
85 | - if [ `whoami` = root ]; then | |
86 | - execute "mkdir -p $PID_PATH && $START_SIDEKIQ > /dev/null 2>&1 &" | |
87 | - fi | |
88 | - echo "$DESC restarted." | |
139 | +# Asks the Unicorn and the Sidekiq if they would be so kind as to stop, if not kills them. | |
140 | +stop() { | |
141 | + check_not_running | |
142 | + # If the Unicorn web server is running, tell it to stop; | |
143 | + if [ "$web_status" = "0" ]; then | |
144 | + kill -QUIT "$wpid" & | |
145 | + echo "Stopping the GitLab Unicorn web server..." | |
146 | + stopping=true | |
89 | 147 | else |
90 | - echo "Error, $NAME not running!" | |
91 | - exit 1 | |
148 | + echo "The Unicorn web was not running, doing nothing." | |
149 | + fi | |
150 | + # And do the same thing for the Sidekiq. | |
151 | + if [ "$sidekiq_status" = "0" ]; then | |
152 | + printf "Stopping Sidekiq job dispatcher." | |
153 | + RAILS_ENV=$RAILS_ENV bundle exec rake sidekiq:stop & | |
154 | + stopping=true | |
155 | + else | |
156 | + echo "The Sidekiq was not running, must have run out of breath." | |
92 | 157 | fi |
158 | + | |
159 | + | |
160 | + # If something needs to be stopped, lets wait for it to stop. Never use SIGKILL in a script. | |
161 | + while [ "$stopping" = "true" ]; do | |
162 | + sleep 1 | |
163 | + check_status | |
164 | + if [ "$web_status" = "0" -o "$sidekiq_status" = "0" ]; then | |
165 | + printf "." | |
166 | + else | |
167 | + printf "\n" | |
168 | + break | |
169 | + fi | |
170 | + done | |
171 | + sleep 1 | |
172 | + # Cleaning up unused pids | |
173 | + rm "$web_server_pid_path" 2>/dev/null | |
174 | + # rm "$sidekiq_pid_path" # Sidekiq seems to be cleaning up it's own pid. | |
175 | + | |
176 | + status | |
93 | 177 | } |
94 | 178 | |
179 | +# Returns the status of GitLab and it's components | |
95 | 180 | status() { |
96 | - cd $APP_ROOT | |
97 | - check_pid | |
98 | - if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then | |
99 | - echo "$DESC / Unicorn with PID $PID is running." | |
100 | - echo "$DESC / Sidekiq with PID $SPID is running." | |
181 | + check_not_running | |
182 | + if [ "$web_status" = "0" ]; then | |
183 | + echo "The GitLab Unicorn webserver with pid $wpid is running." | |
101 | 184 | else |
102 | - echo "$DESC is not running." | |
103 | - exit 1 | |
185 | + printf "The GitLab Unicorn webserver is \033[31mnot running\033[0m.\n" | |
186 | + fi | |
187 | + if [ "$sidekiq_status" = "0" ]; then | |
188 | + echo "The GitLab Sidekiq job dispatcher with pid $spid is running." | |
189 | + else | |
190 | + printf "The GitLab Sidekiq job dispatcher is \033[31mnot running\033[0m.\n" | |
191 | + fi | |
192 | + if [ "$web_status" = "0" -a "$sidekiq_status" = "0" ]; then | |
193 | + printf "GitLab and all it's components are \033[32mup and running\033[0m.\n" | |
104 | 194 | fi |
105 | 195 | } |
106 | 196 | |
107 | -## Check to see if we are running as root first. | |
108 | -## Found at http://www.cyberciti.biz/tips/shell-root-user-check-script.html | |
109 | -if [ "$(id -u)" != "0" ]; then | |
110 | - echo "This script must be run as root" | |
197 | +reload(){ | |
198 | + check_not_running | |
199 | + if [ "$wpid" = "0" ];then | |
200 | + echo "The GitLab Unicorn Web server is not running thus it's configuration can't be reloaded." | |
111 | 201 | exit 1 |
112 | -fi | |
202 | + fi | |
203 | + printf "Reloading GitLab configuration... " | |
204 | + kill -HUP "$wpid" | |
205 | + echo "Done." | |
206 | +} | |
207 | + | |
208 | + | |
209 | +## Finally the input handling. | |
113 | 210 | |
114 | 211 | case "$1" in |
115 | 212 | start) |
... | ... | @@ -119,20 +216,19 @@ case "$1" in |
119 | 216 | stop |
120 | 217 | ;; |
121 | 218 | restart) |
122 | - restart | |
219 | + stop | |
220 | + start | |
123 | 221 | ;; |
124 | 222 | reload|force-reload) |
125 | - echo -n "Reloading $NAME configuration: " | |
126 | - kill -HUP `cat $PID` | |
127 | - echo "done." | |
223 | + reload | |
128 | 224 | ;; |
129 | 225 | status) |
130 | 226 | status |
131 | 227 | ;; |
132 | 228 | *) |
133 | - echo "Usage: sudo service gitlab {start|stop|restart|reload}" >&2 | |
229 | + echo "Usage: service gitlab {start|stop|restart|reload|status}" | |
134 | 230 | exit 1 |
135 | 231 | ;; |
136 | 232 | esac |
137 | 233 | |
138 | -exit 0 | |
234 | +exit | |
139 | 235 | \ No newline at end of file | ... | ... |