This repository was archived by the owner on May 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathgitlab_ci_runner
More file actions
149 lines (130 loc) · 3.68 KB
/
gitlab_ci_runner
File metadata and controls
149 lines (130 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#! /bin/bash
### BEGIN INIT INFO
# Provides: gitlab-ci-runner
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: GitLab CI Runner init.d script
# Description: Enables automatic start of runners at boot time in the background.
### END INIT INFO
APP_ROOT="/home/gitlab_ci_runner/gitlab-ci-runner"
APP_USER="gitlab_ci_runner"
PID_PATH="$APP_ROOT/tmp/pids"
PROCESS_NAME="ruby ./bin/runner"
RUNNERS_PID="$PID_PATH/runners.pid"
RUNNERS_NUM=1 # number of runners to spawn
START_RUNNER="nohup bundle exec ./bin/runner"
NAME="gitlab-ci-runner"
DESC="GitLab CI runner"
RUNNERS_REGISTERED=0
RUNNERS_RUNNING=0
INIT_LOG="/var/log/gitlab_ci_runner.log"
check_pid() {
# Number of registered runners in PID file
RUNNERS_REGISTERED=0
# Number of active runner processes from PID file
RUNNERS_RUNNING=0
if [ -f $RUNNERS_PID ]; then
# Registered
RUNNERS_REGISTERED=`cat $RUNNERS_PID | wc -l`
# Running
while read pid; do
if [ -d /proc/$pid ]; then
# Process is running
((RUNNERS_RUNNING++))
fi
done < $RUNNERS_PID
fi
echo "Number of registered runners in PID file=$RUNNERS_REGISTERED"
echo "Number of running runners=$RUNNERS_RUNNING"
}
execute() {
sudo -u $APP_USER -H bash -l -c "$1"
}
start() {
cd $APP_ROOT
check_pid
if [ "$RUNNERS_REGISTERED" -ne 0 -o "$RUNNERS_RUNNING" -ne 0 ]; then
# Program is running, Exit with error code.
echo "Error! $DESC(s) ($NAME) appear to be running already! Try stopping them first. Exiting."
exit 1
else
if [ `whoami` = root ]; then
[ ! -f $PID_PATH ] && execute "mkdir -p $PID_PATH"
[ -f $RUNNERS_PID ] && execute "rm -f $RUNNERS_PID"
# Spawn runners
for (( i=1; i<=$RUNNERS_NUM; i++ ))
do
# Check log file
if [ ! -f $INIT_LOG ]; then
touch $INIT_LOG
chown $APP_USER $INIT_LOG
fi
echo "Starting runner #$i"
execute "$START_RUNNER >> $INIT_LOG 2>&1 & echo \$! >> $RUNNERS_PID"
done
echo "SUCCESS: Started $RUNNERS_NUM $DESC(s)."
fi
fi
}
stop() {
check_pid
# Exit if there are no runners
if [ $RUNNERS_REGISTERED -eq 0 -a $RUNNERS_RUNNING -eq 0 ]; then
echo "No runners have been found. Exiting."
fi
# Runners found. Check if there are any ghost runners.
KILL_GHOSTS=0;
if [ $RUNNERS_REGISTERED -ne $RUNNERS_RUNNING ]; then
echo "WARNING: Numbers of registered runners don't match number of running runners. Will try to stop them all"
echo "Registered runners=$RUNNERS_REGISTERED"
echo "Running runners=$RUNNERS_RUNNING"
KILL_GHOSTS=1;
fi
echo -n "Trying to stop registered runners..."
if [ $RUNNERS_REGISTERED -gt 0 ]; then
execute "cat $RUNNERS_PID | xargs kill -USR2"
rm -f $RUNNERS_PID
echo "OK"
else
echo "FAILED!"
echo "Couldn't stop registered runners as there is no record of such in $RUNNERS_PID file".
fi
if [ $KILL_GHOSTS -eq 1 ]; then
echo -ne "Trying to kill ghost runners..."
ps -C "$PROCESS_NAME" -o "%p" h | xargs kill -USR2
[ $? -eq 0 ] && echo "OK"
else
echo "No ghost runners have been found.This is good."
fi
}
status() {
echo "Here is what we have at the moment:"
check_pid
}
## Check to see if we are running as root first.
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root"
exit 1
fi
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: sudo service gitlab_ci_runner {start|stop|restart|status}" >&2
exit 1
;;
esac
exit 0