|
| 1 | +#! /bin/bash |
| 2 | + |
| 3 | +# GITLAB CI |
| 4 | +# Maintainer: @randx |
| 5 | +# App Version: 1.1 |
| 6 | + |
| 7 | +### BEGIN INIT INFO |
| 8 | +# Provides: gitlab_ci |
| 9 | +# Required-Start: $local_fs $remote_fs $network $syslog redis-server |
| 10 | +# Required-Stop: $local_fs $remote_fs $network $syslog |
| 11 | +# Default-Start: 2 3 4 5 |
| 12 | +# Default-Stop: 0 1 6 |
| 13 | +# Short-Description: GitLab git repository management |
| 14 | +# Description: GitLab git repository management |
| 15 | +### END INIT INFO |
| 16 | + |
| 17 | + |
| 18 | +APP_ROOT="/home/gitlab_ci/gitlab-ci" |
| 19 | +DAEMON_OPTS="-p 3000 -d -e production" |
| 20 | +PID_PATH="$APP_ROOT/tmp/pids" |
| 21 | +THIN_PID="$PID_PATH/thin.pid" |
| 22 | +RESQUE_PID="$PID_PATH/resque_worker.pid" |
| 23 | +NAME="thin" |
| 24 | +DESC="Gitlab CI service" |
| 25 | + |
| 26 | +check_pid(){ |
| 27 | + if [ -f $THIN_PID ]; then |
| 28 | + PID=`cat $THIN_PID` |
| 29 | + STATUS=`ps aux | grep $PID | grep -v grep | wc -l` |
| 30 | + else |
| 31 | + STATUS=0 |
| 32 | + PID=0 |
| 33 | + fi |
| 34 | +} |
| 35 | + |
| 36 | +start() { |
| 37 | + cd $APP_ROOT |
| 38 | + check_pid |
| 39 | + if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then |
| 40 | + # Program is running, exit with error code 1. |
| 41 | + echo "Error! $DESC $NAME is currently running!" |
| 42 | + exit 1 |
| 43 | + else |
| 44 | + if [ `whoami` = root ]; then |
| 45 | + sudo -u gitlab_ci -H sh -l -c "nohup bundle exec thin start $DAEMON_OPTS > /dev/null 2>&1 &" |
| 46 | + sudo -u gitlab_ci -H sh -l -c "mkdir -p $PID_PATH && nohup bundle exec rake environment resque:work QUEUE=runner RAILS_ENV=production PIDFILE=$RESQUE_PID > /dev/null 2>&1 &" |
| 47 | + echo "$DESC started" |
| 48 | + fi |
| 49 | + fi |
| 50 | +} |
| 51 | + |
| 52 | +stop() { |
| 53 | + cd $APP_ROOT |
| 54 | + check_pid |
| 55 | + if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then |
| 56 | + ## Program is running, stop it. |
| 57 | + kill -QUIT `cat $THIN_PID` |
| 58 | + kill -QUIT `cat $RESQUE_PID` |
| 59 | + rm "$THIN_PID" >> /dev/null |
| 60 | + rm "$RESQUE_PID" >> /dev/null |
| 61 | + echo "$DESC stopped" |
| 62 | + else |
| 63 | + ## Program is not running, exit with error. |
| 64 | + echo "Error! $DESC not started!" |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | +} |
| 68 | + |
| 69 | +restart() { |
| 70 | + cd $APP_ROOT |
| 71 | + check_pid |
| 72 | + if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then |
| 73 | + echo -n "Restarting $DESC: " |
| 74 | + kill -USR2 `cat $THIN_PID` |
| 75 | + kill -USR2 `cat $RESQUE_PID` |
| 76 | + echo "$NAME." |
| 77 | + else |
| 78 | + echo "Error, $NAME not running!" |
| 79 | + exit 1 |
| 80 | + fi |
| 81 | +} |
| 82 | + |
| 83 | +status() { |
| 84 | + cd $APP_ROOT |
| 85 | + check_pid |
| 86 | + if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then |
| 87 | + echo "$DESC with PID $PID is running." |
| 88 | + else |
| 89 | + echo "$DESC is not running." |
| 90 | + exit 1 |
| 91 | + fi |
| 92 | +} |
| 93 | + |
| 94 | +## Check to see if we are running as root first. |
| 95 | +## Found at http://www.cyberciti.biz/tips/shell-root-user-check-script.html |
| 96 | +if [ "$(id -u)" != "0" ]; then |
| 97 | + echo "This script must be run as root" |
| 98 | + exit 1 |
| 99 | +fi |
| 100 | + |
| 101 | +case "$1" in |
| 102 | + start) |
| 103 | + start |
| 104 | + ;; |
| 105 | + stop) |
| 106 | + stop |
| 107 | + ;; |
| 108 | + restart) |
| 109 | + restart |
| 110 | + ;; |
| 111 | + reload|force-reload) |
| 112 | + echo -n "Reloading $DESC configuration: " |
| 113 | + kill -HUP `cat $PID` |
| 114 | + echo "$NAME." |
| 115 | + ;; |
| 116 | + status) |
| 117 | + status |
| 118 | + ;; |
| 119 | + *) |
| 120 | + echo "Usage: $NAME {start|stop|restart|reload}" >&2 |
| 121 | + exit 1 |
| 122 | + ;; |
| 123 | +esac |
| 124 | + |
| 125 | +exit 0 |
0 commit comments