Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kazuhiko Shiozaki
gitlab-ce
Commits
5dad053a
Commit
5dad053a
authored
Sep 03, 2013
by
Jacob Vosmaer
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4920 from Rovanion/init-rewrite
Rewrote init script.
parents
54faf787
fe8fa9a5
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
202 additions
and
77 deletions
+202
-77
CHANGELOG
CHANGELOG
+1
-0
lib/support/init.d/gitlab
lib/support/init.d/gitlab
+201
-77
No files found.
CHANGELOG
View file @
5dad053a
v 6.1.0
v 6.1.0
- Rewrite: Init script now less prone to errors and keeps better track of the service.
- Link issues, merge requests, and commits when they reference each other with GFM
- Link issues, merge requests, and commits when they reference each other with GFM
- Close issues automatically when pushing commits with a special message
- Close issues automatically when pushing commits with a special message
- Project internal ids for issues, mr, milestones
- Project internal ids for issues, mr, milestones
...
...
lib/support/init.d/gitlab
100644 → 100755
View file @
5dad053a
#! /bin/
ba
sh
#! /bin/sh
# GITLAB
# GITLAB
# Maintainer: @randx
# Maintainer: @randx
# Authors: rovanion.luckey@gmail.com, @randx
# App Version: 6.0
# App Version: 6.0
### BEGIN INIT INFO
### BEGIN INIT INFO
...
@@ -14,102 +15,227 @@
...
@@ -14,102 +15,227 @@
# Description: GitLab git repository management
# Description: GitLab git repository management
### END INIT INFO
### END INIT INFO
### Environment variables
RAILS_ENV
=
"production"
APP_ROOT
=
"/home/git/gitlab"
# Script variable names should be lower-case not to conflict with internal
APP_USER
=
"git"
# /bin/sh variables such as PATH, EDITOR or SHELL.
DAEMON_OPTS
=
"-c
$APP_ROOT
/config/unicorn.rb -E production"
app_root
=
"/home/git/gitlab"
PID_PATH
=
"
$APP_ROOT
/tmp/pids"
app_user
=
"git"
SOCKET_PATH
=
"
$APP_ROOT
/tmp/sockets"
unicorn_conf
=
"
$app_root
/config/unicorn.rb"
WEB_SERVER_PID
=
"
$PID_PATH
/unicorn.pid"
pid_path
=
"
$app_root
/tmp/pids"
SIDEKIQ_PID
=
"
$PID_PATH
/sidekiq.pid"
socket_path
=
"
$app_root
/tmp/sockets"
STOP_SIDEKIQ
=
"RAILS_ENV=production bundle exec rake sidekiq:stop"
web_server_pid_path
=
"
$pid_path
/unicorn.pid"
START_SIDEKIQ
=
"RAILS_ENV=production bundle exec rake sidekiq:start"
sidekiq_pid_path
=
"
$pid_path
/sidekiq.pid"
NAME
=
"gitlab"
DESC
=
"GitLab service"
check_pid
(){
### Here ends user configuration ###
if
[
-f
$WEB_SERVER_PID
]
;
then
PID
=
`
cat
$WEB_SERVER_PID
`
SPID
=
`
cat
$SIDEKIQ_PID
`
# Switch to the app_user if it is not he/she who is running the script.
STATUS
=
`
ps aux |
grep
$PID
|
grep
-v
grep
|
wc
-l
`
if
[
"
$USER
"
!=
"
$app_user
"
]
;
then
sudo
-u
"
$app_user
"
-H
$0
"
$@
"
;
exit
;
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
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
else
STATUS
=
0
wpid
=
0
PID
=
0
fi
if
[
-f
"
$sidekiq_pid_path
"
]
;
then
spid
=
$(
cat
"
$sidekiq_pid_path
"
)
else
spid
=
0
fi
fi
}
}
execute
()
{
# We use the pids in so many parts of the script it makes sense to always check them.
sudo
-u
$APP_USER
-H
bash
-l
-c
"
$1
"
# Only after start() is run should the pids change. Sidekiq sets it's own pid.
}
check_pids
start
()
{
cd
$APP_ROOT
# Checks whether the different parts of the service are already running or not.
check_pid
check_status
(){
if
[
"
$PID
"
-ne
0
-a
"
$STATUS
"
-ne
0
]
;
then
check_pids
# Program is running, exit with error code 1.
# If the web server is running kill -0 $wpid returns true, or rather 0.
echo
"Error!
$DESC
$NAME
is currently running!"
# Checks of *_status should only check for == 0 or != 0, never anything else.
exit
1
if
[
$wpid
-ne
0
]
;
then
kill
-0
"
$wpid
"
2>/dev/null
web_status
=
"
$?
"
else
else
if
[
`
whoami
`
=
root
]
;
then
web_status
=
"-1"
execute
"rm -f
$SOCKET_PATH
/gitlab.socket"
fi
execute
"RAILS_ENV=production bundle exec unicorn_rails
$DAEMON_OPTS
> /dev/null 2>&1 &"
if
[
$spid
-ne
0
]
;
then
execute
"mkdir -p
$PID_PATH
&&
$START_SIDEKIQ
> /dev/null 2>&1 &"
kill
-0
"
$spid
"
2>/dev/null
echo
"
$DESC
started"
sidekiq_status
=
"
$?
"
else
sidekiq_status
=
"-1"
fi
}
# Check for stale pids and remove them if necessary
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
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
echo
"Unable to remove stale pid, exiting"
exit
1
fi
fi
if
[
"
$spid
"
!=
"0"
-a
"
$sidekiq_status
"
!=
"0"
]
;
then
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
fi
fi
fi
}
}
stop
()
{
# If no parts of the service is running, bail out.
cd
$APP_ROOT
exit_if_not_running
(){
check_pid
check_stale_pids
if
[
"
$PID
"
-ne
0
-a
"
$STATUS
"
-ne
0
]
;
then
if
[
"
$web_status
"
!=
"0"
-a
"
$sidekiq_status
"
!=
"0"
]
;
then
## Program is running, stop it.
echo
"GitLab is not running."
kill
-QUIT
`
cat
$WEB_SERVER_PID
`
exit
execute
"mkdir -p
$PID_PATH
&&
$STOP_SIDEKIQ
> /dev/null 2>&1 &"
fi
rm
"
$WEB_SERVER_PID
"
>>
/dev/null
}
echo
"
$DESC
stopped"
# Starts Unicorn and Sidekiq.
start
()
{
check_stale_pids
# 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."
else
else
## Program is not running, exit with error.
echo
"Starting the GitLab Unicorn web server..."
echo
"Error!
$DESC
not started!"
# Remove old socket if it exists
exit
1
rm
-f
"
$socket_path
"
/gitlab.socket 2>/dev/null
# Start the webserver
bundle
exec
unicorn_rails
-D
-c
"
$unicorn_conf
"
-E
"
$RAILS_ENV
"
fi
# 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"
else
echo
"Starting the GitLab Sidekiq event dispatcher..."
RAILS_ENV
=
$RAILS_ENV
bundle
exec
rake sidekiq:start
# We are sleeping a bit here because sidekiq is slow at writing it's pid
sleep
2
fi
fi
# Finally check the status to tell wether or not GitLab is running
status
}
}
restart
()
{
# Asks the Unicorn and the Sidekiq if they would be so kind as to stop, if not kills them.
cd
$APP_ROOT
stop
()
{
check_pid
exit_if_not_running
if
[
"
$PID
"
-ne
0
-a
"
$STATUS
"
-ne
0
]
;
then
# If the Unicorn web server is running, tell it to stop;
echo
"Restarting
$DESC
..."
if
[
"
$web_status
"
=
"0"
]
;
then
kill
-USR2
`
cat
$WEB_SERVER_PID
`
kill
-QUIT
"
$wpid
"
&
execute
"mkdir -p
$PID_PATH
&&
$STOP_SIDEKIQ
> /dev/null 2>&1"
echo
"Stopping the GitLab Unicorn web server..."
if
[
`
whoami
`
=
root
]
;
then
stopping
=
true
execute
"mkdir -p
$PID_PATH
&&
$START_SIDEKIQ
> /dev/null 2>&1 &"
fi
echo
"
$DESC
restarted."
else
else
echo
"Error,
$NAME
not running!"
echo
"The Unicorn web was not running, doing nothing."
exit
1
fi
# And do the same thing for the Sidekiq.
if
[
"
$sidekiq_status
"
=
"0"
]
;
then
printf
"Stopping Sidekiq job dispatcher."
RAILS_ENV
=
$RAILS_ENV
bundle
exec
rake sidekiq:stop &
stopping
=
true
else
echo
"The Sidekiq was not running, must have run out of breath."
fi
fi
# If something needs to be stopped, lets wait for it to stop. Never use SIGKILL in a script.
while
[
"
$stopping
"
=
"true"
]
;
do
sleep
1
check_status
if
[
"
$web_status
"
=
"0"
-o
"
$sidekiq_status
"
=
"0"
]
;
then
printf
"."
else
printf
"
\n
"
break
fi
done
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.
status
}
}
# Returns the status of GitLab and it's components
status
()
{
status
()
{
cd
$APP_ROOT
check_status
check_pid
if
[
"
$web_status
"
!=
"0"
-a
"
$sidekiq_status
"
!=
"0"
]
;
then
if
[
"
$PID
"
-ne
0
-a
"
$STATUS
"
-ne
0
]
;
then
echo
"GitLab is not running."
echo
"
$DESC
/ Unicorn with PID
$PID
is running."
return
echo
"
$DESC
/ Sidekiq with PID
$SPID
is running."
fi
if
[
"
$web_status
"
=
"0"
]
;
then
echo
"The GitLab Unicorn webserver with pid
$wpid
is running."
else
else
echo
"
$DESC
is not running."
printf
"The GitLab Unicorn webserver is
\0
33[31mnot running
\0
33[0m.
\n
"
exit
1
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
\0
33[31mnot running
\0
33[0m.
\n
"
fi
if
[
"
$web_status
"
=
"0"
-a
"
$sidekiq_status
"
=
"0"
]
;
then
printf
"GitLab and all it's components are
\0
33[32mup and running
\0
33[0m.
\n
"
fi
fi
}
}
## Check to see if we are running as root first.
reload
(){
## Found at http://www.cyberciti.biz/tips/shell-root-user-check-script.html
exit_if_not_running
if
[
"
$(
id
-u
)
"
!=
"0"
]
;
then
if
[
"
$wpid
"
=
"0"
]
;
then
echo
"Th
is script must be run as root
"
echo
"Th
e GitLab Unicorn Web server is not running thus its configuration can't be reloaded.
"
exit
1
exit
1
fi
fi
printf
"Reloading GitLab Unicorn configuration... "
kill
-USR2
"
$wpid
"
echo
"Done."
echo
"Restarting GitLab Sidekiq since it isn't capable of reloading its config..."
RAILS_ENV
=
$RAILS_ENV
bundle
exec
rake sidekiq:stop
echo
"Starting Sidekiq..."
RAILS_ENV
=
$RAILS_ENV
bundle
exec
rake sidekiq:start
# Waiting 2 seconds for sidekiq to write it.
sleep
2
status
}
restart
(){
check_status
if
[
"
$web_status
"
=
"0"
-o
"
$sidekiq_status
"
=
"0"
]
;
then
stop
fi
start
}
## Finally the input handling.
case
"
$1
"
in
case
"
$1
"
in
start
)
start
)
...
@@ -122,17 +248,15 @@ case "$1" in
...
@@ -122,17 +248,15 @@ case "$1" in
restart
restart
;;
;;
reload|force-reload
)
reload|force-reload
)
echo
-n
"Reloading
$NAME
configuration: "
reload
kill
-HUP
`
cat
$PID
`
echo
"done."
;;
;;
status
)
status
)
status
status
;;
;;
*
)
*
)
echo
"Usage: s
udo service gitlab {start|stop|restart|reload}"
>
&2
echo
"Usage: s
ervice gitlab {start|stop|restart|reload|status}"
exit
1
exit
1
;;
;;
esac
esac
exit
0
exit
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment