-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjenkins-init
executable file
·382 lines (357 loc) · 8.35 KB
/
jenkins-init
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/bin/bash
# jenkins service test.
# This script checks all required services, ports, and processes are up and running. Script can be used in monitoring jenkins application.
# Run this script against jenkins hosts where jenkins is installed.
# Run this script after jenkins deployed.
#BEGIN
#export SYSTEMCTL_SKIP_REDIRECT=1
# Calling functions from library
source /etc/init.d/functions
HOSTNAME=$(hostname -s)
DOMAIN=$(hostname -d)
rc=0
fc=1
execute_nicely() {
local -r msg=$1
local -r cmd=$2
local -i rc=0
echo -n "$msg"
local output
output=$($cmd 2>&1)
rc=$?
if [ $rc -ne 0 ]; then
echo
echo -n "$output"
failure
echo
else
success
echo
fi
return $rc
}
start() {
execute_nicely \
"Starting jenkins" \
"/etc/init.d/jenkins start" \
|| return $?
return 0
}
stop() {
execute_nicely \
"Shtting down jenkins" \
"/etc/init.d/jenkins stop" \
|| return $?
return 0
}
status() {
local -i rc=0
execute_nicely \
"Status of jenkins" \
"/etc/init.d/jenkins status"
if [ $? -ne $rc ]; then
rc=$?
fi
return $rc
}
test() {
echo
if [[ "$HOSTNAME" = jenkins* ]]; then
echo "###############################################"
echo "# RUNNING SMOKE TEST #"
echo "###############################################"
else
exit 1
fi
sleep 0.3
# Test suit on nginx.
echo "-----------------------------------------------"
echo " Checking Nginx "
echo "-----------------------------------------------"
# Check: domain specific certs in /etc/nginx/ssl
for VALUE in key pem
do
if [ -s /etc/nginx/ssl/"$DOMAIN.$VALUE" ]; then
echo -n "$DOMAIN.$VALUE is 0 size and valid"
success
logger -t jenkins-init "Cert:$DOMAIN.$VALUE test=Present"
sleep 0.3
echo
else
echo -n "$DOMAIN.$VALUE is invalid or absent"
failure
logger -t jenkins-init "Certs:$DOMAIN.$VALUE test=Absent"
sleep 0.3
echo
rc=$fc
fi
done
sleep 0.3
# Check: nginx service status
systemctl status nginx &> /dev/null
#rc=$?
if [ $? -eq 0 ]; then
echo -n "Nginx Process is running"
success
logger -t jenkins-init "Process:nginx test=Running"
sleep 0.3
echo
else
echo -n "Nginx process is not running"
failure
logger -t jenkins-init "Process:nginx test=Stopped"
sleep 0.3
echo
rc=$fc
fi
sleep 0.3
# check: respective ports assigned to nginx.
PORT_FFT=$(netstat -nlp | grep nginx | grep 443 | cut -d ":" -f2 | cut -d " " -f1)
PORT_EZ=$(netstat -nlp | grep nginx | grep 80 | grep -m1 "" | grep 80 | cut -d ":" -f2 | cut -d " " -f1)
NGINX_SVC=$(netstat -nlp | grep nginx | grep -m1 " " | cut -d "/" -f2 | cut -d ":" -f1)
if [ "$NGINX_SVC" = "nginx" ] && [ "$PORT_FFT" -eq 443 ]; then
echo -n "Nginx is using port $PORT_FFT"
success
logger -t jenkins "nginx-port:443 test=Assigned"
sleep 0.3
echo
else
echo -n "Nginx is not using port $PORT_FFT"
failure
logger -t jenkins "nginx-port:443 test=Unassigned"
sleep 0.3
echo
rc=$fc
fi
sleep 0.3
if [ "$NGINX_SVC" = "nginx" ] && [ "$PORT_EZ" -eq 80 ]; then
echo -n "Nginx is using port $PORT_EZ"
success
logger -t jenkins "nginx-port:80 test=Assigned"
sleep 0.3
echo
else
echo -n "Nginx is not using port $PORT_EZ"
failure
logger -t jenkins "nginx-port:80 test=unassigned"
sleep 0.3
echo
rc=$fc
fi
sleep 0.3
# Test Suit on jenkins.
echo "-----------------------------------------------"
echo " Checking jenkins "
echo "-----------------------------------------------"
# check: jenkins config file in location.
if [ -f /opt/jenkins/jenkins_home/config.xml ]; then
echo -n "Jenkins config file is placed"
success
logger -t jenkins-init "file:Config test=Present"
sleep 0.3
echo
else
echo -n "Jenkins config file missing"
failure
logger -t jenkins-init "file :Config test=Absent"
sleep 0.3
echo
rc=$fc
fi
sleep 0.3
# Check: jenkins's version
WAR_VERSION=$(grep "/version" /opt/jenkins/jenkins_home/config.xml | cut -d ">" -f2 | cut -d "<" -f1)
echo -n jenkins war version is $WAR_VERSION
success
echo
sleep 0.3
# check: defult job available in jobs folder
if [ -d /opt/jenkins/jenkins_home/jobs/Deploy-Jobs ]; then
echo -n "Default job is present"
success
logger -t jenkins-init "job:Deploy-Jobs test=Present"
sleep 0.3
echo
else
echo -n "Default job is missing"
failure
logger -t jenkins-init "job:Deploy-Jobs test=Absent"
sleep 0.3
echo
rc=$fc
fi
sleep 0.3
# check: directory structure is proper for jenkins
Jhome=(/opt/jenkins/jenkins_home)
Jinstall=(/opt/jenkins/jenkins_install)
Jwork=(/opt/jenkins_workdir)
for dir in $Jhome $Jinstall $Jwork
do
if [ -d $dir ]; then
echo -n "$dir is placed properly"
success
logger -t jenkins-init "type=$dir test=Placed Correctly"
sleep 0.3
echo
else
echo -n "$dir is not placed properly"
failure
logger -t jenkins-init "type=$dir test=Placed Wrongly"
sleep 0.3
echo
rc=$fc
fi
done
sleep 0.3
# Check: Old version where jenkins_workdir is inside jenkins dir.
if [ -d /opt/jenkins/jenkins_workdir ]; then
echo -n "/opt/jenkins/jenkins_workdir:From old version"
warning
logger -t jenkins-init "type=/opt/jenkins/jenkins_workdir test=Placed Wrongly"
echo
fi
sleep 0.3
# Check: jenkins service status
systemctl status jenkins &>/dev/null
#rc=$?
if [ $? -eq 0 ]; then
echo -n "Service jenkins is active"
success
logger -t jenkins-init "type=service-jenkins test:Running"
echo
else
echo -n "Service jenkins is stopped"
failure
logger -t jenkins-init "type=service-jenkins test:Stopped"
echo
rc=$fc
fi
sleep 0.3
# Test suit on ansible
echo "-----------------------------------------------"
echo " Checking Ansible "
echo "-----------------------------------------------"
# check: ansible user is present
user=$(getent passwd ansible | cut -d ":" -f1)
if [ $user = ansible ]; then
echo -n "Ansible user exists"
success
logger -t jenkins-init "user:Ansible test=Present"
sleep 0.3
echo
else
echo -n "Ansible user is missing"
failure
logger -t jenkins-init "user:Ansible test=Absent"
sleep 0.3
echo
rc=$fc
fi
sleep 0.3
# check: user id for ansible.
user_id=$(id ansible | cut -d "=" -f2 | cut -d "(" -f1)
if [ $user_id -eq 1098 ]; then
echo -n "Ansible user id is 1098"
success
logger -t jenkins-init "user:Id test=Correct"
sleep 0.3
echo
else
echo -n "Ansible user id is not appropriate"
failure
logger -t jenkins-init "user:Id test=Incorrect"
sleep 0.3
echo
rc=$fc
fi
sleep 0.3
# check: ansible is installed on hosts
ansible --version &> /dev/null
#rc=$?
if [ $? -eq 0 ]; then
echo -n "Ansible is installed"
success
logger -t jenkins-init "package:Ansible test=Installed"
sleep 0.3
echo
else
echo -n "Ansible might not be installed"
failure
logger -t jenkins-init "package:Ansible test=missing"
sleep 0.3
echo
rc=$fc
fi
sleep 0.3
# Test suit on connection
echo "-----------------------------------------------"
echo " Checking Connections "
echo "-----------------------------------------------"
# Check: connection to host
curl http://host &> /dev/null
#rc=$?
if [ $? -eq 0 ]; then
echo -n "host is reachable from jenkins"
success
logger -t jenkins-init "connection to: host test=connected"
sleep 0.3
echo
else
echo -n "host is not reachable from jenkins"
failure
logger -t jenkins-init "connection to: host test=dropped"
sleep 0.3
echo
rc=$fc
fi
# check: connection to swarmmaster01/02/03 and swamworker01/02/03
for host in swarmmaster01 swarmmaster02 swarmmaster03 swarmworker01 swarmworker02 swarmworker03
do
curl http://$host:8080 &> /dev/null
# rc=$?
if [ $? -eq 0 ]; then
echo -n "$host is reachable from jenkins"
success
logger -t jenkins-init "connection to: $host test=connected"
sleep 0.3
echo
else
echo -n "$host is not reachable from jenkins"
failure
logger -t jenkins-init "connection to: $host test=dropped"
sleep 0.3
echo
rc=$fc
fi
done
return $rc
}
case "$1" in
start)
start
exit $?
;;
stop)
stop
exit $?
;;
restart)
stop
sleep 2
start
exit $?
;;
status)
status
exit $?
;;
test)
test
exit $?
;;
*)
echo "Usage: $0 {start|stop|restart|status|test}"
exit $?
esac
return $rc
# End