Advertisement
Guest User

rhel6 gitlab init.d script

a guest
Feb 12th, 2015
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.67 KB | None | 0 0
  1. [ec2-user@awsappmgt init.d]$ cat gitlab
  2. #! /bin/sh
  3.  
  4. # GITLAB
  5. # Maintainer: @randx
  6. # Authors: rovanion.luckey@gmail.com, @randx
  7.  
  8. ### BEGIN INIT INFO
  9. # Provides: gitlab
  10. # Required-Start: $local_fs $remote_fs $network $syslog redis-server
  11. # Required-Stop: $local_fs $remote_fs $network $syslog
  12. # Default-Start: 2 3 4 5
  13. # Default-Stop: 0 1 6
  14. # Short-Description: GitLab git repository management
  15. # Description: GitLab git repository management
  16. # chkconfig: - 85 14
  17. ### END INIT INFO
  18.  
  19.  
  20. ###
  21. # DO NOT EDIT THIS FILE!
  22. # This file will be overwritten on update.
  23. # Instead add/change your variables in /etc/default/gitlab
  24. # An example defaults file can be found in lib/support/init.d/gitlab.default.example
  25. ###
  26.  
  27.  
  28. ### Environment variables
  29. RAILS_ENV="production"
  30.  
  31. # Script variable names should be lower-case not to conflict with
  32. # internal /bin/sh variables such as PATH, EDITOR or SHELL.
  33. app_user="git"
  34. app_root="/home/$app_user/gitlab"
  35. pid_path="$app_root/tmp/pids"
  36. socket_path="$app_root/tmp/sockets"
  37. web_server_pid_path="$pid_path/unicorn.pid"
  38. sidekiq_pid_path="$pid_path/sidekiq.pid"
  39.  
  40. # Read configuration variable file if it is present
  41. test -f /etc/default/gitlab && . /etc/default/gitlab
  42.  
  43. # Switch to the app_user if it is not he/she who is running the script.
  44. if [ "$USER" != "$app_user" ]; then
  45. eval su - "$app_user" -c $(echo \")$0 "$@"$(echo \"); exit;
  46. fi
  47.  
  48. # Switch to the gitlab path, exit on failure.
  49. if ! cd "$app_root" ; then
  50. echo "Failed to cd into $app_root, exiting!"; exit 1
  51. fi
  52.  
  53.  
  54. ### Init Script functions
  55.  
  56. ## Gets the pids from the files
  57. check_pids(){
  58. if ! mkdir -p "$pid_path"; then
  59. echo "Could not create the path $pid_path needed to store the pids."
  60. exit 1
  61. fi
  62. # If there exists a file which should hold the value of the Unicorn pid: read it.
  63. if [ -f "$web_server_pid_path" ]; then
  64. wpid=$(cat "$web_server_pid_path")
  65. else
  66. wpid=0
  67. fi
  68. if [ -f "$sidekiq_pid_path" ]; then
  69. spid=$(cat "$sidekiq_pid_path")
  70. else
  71. spid=0
  72. fi
  73. }
  74.  
  75. ## Called when we have started the two processes and are waiting for their pid files.
  76. wait_for_pids(){
  77. # We are sleeping a bit here mostly because sidekiq is slow at writing it's pid
  78. i=0;
  79. while [ ! -f $web_server_pid_path -o ! -f $sidekiq_pid_path ]; do
  80. sleep 0.1;
  81. i=$((i+1))
  82. if [ $((i%10)) = 0 ]; then
  83. echo -n "."
  84. elif [ $((i)) = 301 ]; then
  85. echo "Waited 30s for the processes to write their pids, something probably went wrong."
  86. exit 1;
  87. fi
  88. done
  89. echo
  90. }
  91.  
  92. # We use the pids in so many parts of the script it makes sense to always check them.
  93. # Only after start() is run should the pids change. Sidekiq sets it's own pid.
  94. check_pids
  95.  
  96.  
  97. ## Checks whether the different parts of the service are already running or not.
  98. check_status(){
  99. check_pids
  100. # If the web server is running kill -0 $wpid returns true, or rather 0.
  101. # Checks of *_status should only check for == 0 or != 0, never anything else.
  102. if [ $wpid -ne 0 ]; then
  103. kill -0 "$wpid" 2>/dev/null
  104. web_status="$?"
  105. else
  106. web_status="-1"
  107. fi
  108. if [ $spid -ne 0 ]; then
  109. kill -0 "$spid" 2>/dev/null
  110. sidekiq_status="$?"
  111. else
  112. sidekiq_status="-1"
  113. fi
  114. if [ $web_status = 0 -a $sidekiq_status = 0 ]; then
  115. gitlab_status=0
  116. else
  117. # http://refspecs.linuxbase.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
  118. # code 3 means 'program is not running'
  119. gitlab_status=3
  120. fi
  121. }
  122.  
  123. ## Check for stale pids and remove them if necessary.
  124. check_stale_pids(){
  125. check_status
  126. # If there is a pid it is something else than 0, the service is running if
  127. # *_status is == 0.
  128. if [ "$wpid" != "0" -a "$web_status" != "0" ]; then
  129. echo "Removing stale Unicorn web server pid. This is most likely caused by the web server crashing the last time it ran."
  130. if ! rm "$web_server_pid_path"; then
  131. echo "Unable to remove stale pid, exiting."
  132. exit 1
  133. fi
  134. fi
  135. if [ "$spid" != "0" -a "$sidekiq_status" != "0" ]; then
  136. echo "Removing stale Sidekiq job dispatcher pid. This is most likely caused by Sidekiq crashing the last time it ran."
  137. if ! rm "$sidekiq_pid_path"; then
  138. echo "Unable to remove stale pid, exiting"
  139. exit 1
  140. fi
  141. fi
  142. }
  143.  
  144. ## If no parts of the service is running, bail out.
  145. exit_if_not_running(){
  146. check_stale_pids
  147. if [ "$web_status" != "0" -a "$sidekiq_status" != "0" ]; then
  148. echo "GitLab is not running."
  149. exit
  150. fi
  151. }
  152.  
  153. ## Starts Unicorn and Sidekiq if they're not running.
  154. start_gitlab() {
  155. check_stale_pids
  156.  
  157. if [ "$web_status" != "0" -a "$sidekiq_status" != "0" ]; then
  158. echo -n "Starting both the GitLab Unicorn and Sidekiq"
  159. elif [ "$web_status" != "0" ]; then
  160. echo -n "Starting GitLab Unicorn"
  161. elif [ "$sidekiq_status" != "0" ]; then
  162. echo -n "Starting GitLab Sidekiq"
  163. fi
  164.  
  165. # Then check if the service is running. If it is: don't start again.
  166. if [ "$web_status" = "0" ]; then
  167. echo "The Unicorn web server already running with pid $wpid, not restarting."
  168. else
  169. # Remove old socket if it exists
  170. rm -f "$socket_path"/gitlab.socket 2>/dev/null
  171. # Start the web server
  172. RAILS_ENV=$RAILS_ENV bin/web start
  173. fi
  174.  
  175. # If sidekiq is already running, don't start it again.
  176. if [ "$sidekiq_status" = "0" ]; then
  177. echo "The Sidekiq job dispatcher is already running with pid $spid, not restarting"
  178. else
  179. RAILS_ENV=$RAILS_ENV bin/background_jobs start &
  180. fi
  181.  
  182. # Wait for the pids to be planted
  183. wait_for_pids
  184. # Finally check the status to tell wether or not GitLab is running
  185. print_status
  186. }
  187.  
  188. ## Asks the Unicorn and the Sidekiq if they would be so kind as to stop, if not kills them.
  189. stop_gitlab() {
  190. exit_if_not_running
  191.  
  192. if [ "$web_status" = "0" -a "$sidekiq_status" = "0" ]; then
  193. echo -n "Shutting down both Unicorn and Sidekiq"
  194. elif [ "$web_status" = "0" ]; then
  195. echo -n "Shutting down Unicorn"
  196. elif [ "$sidekiq_status" = "0" ]; then
  197. echo -n "Shutting down Sidekiq"
  198. fi
  199.  
  200. # If the Unicorn web server is running, tell it to stop;
  201. if [ "$web_status" = "0" ]; then
  202. RAILS_ENV=$RAILS_ENV bin/web stop
  203. fi
  204. # And do the same thing for the Sidekiq.
  205. if [ "$sidekiq_status" = "0" ]; then
  206. RAILS_ENV=$RAILS_ENV bin/background_jobs stop
  207. fi
  208.  
  209. # If something needs to be stopped, lets wait for it to stop. Never use SIGKILL in a script.
  210. while [ "$web_status" = "0" -o "$sidekiq_status" = "0" ]; do
  211. sleep 1
  212. check_status
  213. printf "."
  214. if [ "$web_status" != "0" -a "$sidekiq_status" != "0" ]; then
  215. printf "\n"
  216. break
  217. fi
  218. done
  219.  
  220. sleep 1
  221. # Cleaning up unused pids
  222. rm "$web_server_pid_path" 2>/dev/null
  223. # rm "$sidekiq_pid_path" # Sidekiq seems to be cleaning up it's own pid.
  224.  
  225. print_status
  226. }
  227.  
  228. ## Prints the status of GitLab and it's components.
  229. print_status() {
  230. check_status
  231. if [ "$web_status" != "0" -a "$sidekiq_status" != "0" ]; then
  232. echo "GitLab is not running."
  233. return
  234. fi
  235. if [ "$web_status" = "0" ]; then
  236. echo "The GitLab Unicorn web server with pid $wpid is running."
  237. else
  238. printf "The GitLab Unicorn web server is \033[31mnot running\033[0m.\n"
  239. fi
  240. if [ "$sidekiq_status" = "0" ]; then
  241. echo "The GitLab Sidekiq job dispatcher with pid $spid is running."
  242. else
  243. printf "The GitLab Sidekiq job dispatcher is \033[31mnot running\033[0m.\n"
  244. fi
  245. if [ "$web_status" = "0" -a "$sidekiq_status" = "0" ]; then
  246. printf "GitLab and all its components are \033[32mup and running\033[0m.\n"
  247. fi
  248. }
  249.  
  250. ## Tells unicorn to reload it's config and Sidekiq to restart
  251. reload_gitlab(){
  252. exit_if_not_running
  253. if [ "$wpid" = "0" ];then
  254. echo "The GitLab Unicorn Web server is not running thus its configuration can't be reloaded."
  255. exit 1
  256. fi
  257. printf "Reloading GitLab Unicorn configuration... "
  258. RAILS_ENV=$RAILS_ENV bin/web reload
  259. echo "Done."
  260. echo "Restarting GitLab Sidekiq since it isn't capable of reloading its config..."
  261. RAILS_ENV=$RAILS_ENV bin/background_jobs restart
  262.  
  263. wait_for_pids
  264. print_status
  265. }
  266.  
  267. ## Restarts Sidekiq and Unicorn.
  268. restart_gitlab(){
  269. check_status
  270. if [ "$web_status" = "0" -o "$sidekiq_status" = "0" ]; then
  271. stop_gitlab
  272. fi
  273. start_gitlab
  274. }
  275.  
  276.  
  277. ### Finally the input handling.
  278.  
  279. case "$1" in
  280. start)
  281. start_gitlab
  282. ;;
  283. stop)
  284. stop_gitlab
  285. ;;
  286. restart)
  287. restart_gitlab
  288. ;;
  289. reload|force-reload)
  290. reload_gitlab
  291. ;;
  292. status)
  293. print_status
  294. exit $gitlab_status
  295. ;;
  296. *)
  297. echo "Usage: service gitlab {start|stop|restart|reload|status}"
  298. exit 1
  299. ;;
  300. esac
  301.  
  302. exit
  303. [ec2-user@awsappmgt init.d]$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement