Add start delay to monit for jnlp daemon
[releng.git] / utils / jenkins-jnlp-connect.sh
1 #!/bin/bash
2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2016 Linux Foundation and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 #Monit setup script for opnfv jnlp slave connections
12
13 test_firewall() {
14     echo "testing that the firewall is open for us at build.opnfv.org"
15     test=$(echo "blah"| nc -w 4 build.opnfv.org 57387 > /dev/null 2>&1; echo $?)
16     if [[ $test == 0 ]]; then
17         echo "Firewall is open for us at build.opnfv.org"
18         exit 0
19     else
20         cat << EOF
21 LF firewall not open, please send a report to helpdesk with your gpg key attached, or better yet upload it to the key servers. (I should be able to find it with gpg --search-keys your@company.email.com
22 opnfv-helpdesk@rt.linuxfoundation.org
23 Jenkins Home: $jenkinshome
24 Jenkins User: $jenkinsuser
25 Slave Name: $slave_name
26 IP Address: $(curl -s http://icanhazip.com)
27 EOF
28         exit 1
29     fi
30 }
31
32 main () {
33     #tests
34     if [[ -z $jenkinsuser || -z $jenkinshome ]]; then
35         echo "jenkinsuser or home not defined, please edit this file to define it"
36         exit 1
37     fi
38
39     if [[ $(pwd) != "$jenkinshome" ]]; then
40         echo "This script needs to be run from the jenkins users home dir"
41         exit 1
42     fi
43
44     if [[ -z $slave_name || -z $slave_secret ]]; then
45         echo "slave name or secret not defined, please edit this file to define it"
46         exit 1
47     fi
48
49     if [[ $(whoami) != "root" && $(whoami) != "$jenkinsuser"  ]]; then
50         echo "This script must be run as user root or jenkins user"
51         exit 1
52     fi
53
54     if [[ $(whoami) != "root" ]]; then
55       if sudo -l | grep "requiretty"  | grep -v "\!requiretty"; then
56         echo "please comment out Defaults requiretty from /etc/sudoers"
57         exit 1
58       fi
59     fi
60
61     #make pid dir
62     pidfile="/var/run/$jenkinsuser/jenkins_jnlp_pid"
63     if ! [ -d /var/run/$jenkinsuser/ ]; then
64         sudo mkdir /var/run/$jenkinsuser/
65         sudo chown $jenkinsuser:$jenkinsuser /var/run/$jenkinsuser/
66     fi
67
68     if [[ $skip_monit != true ]]; then
69         #check for monit
70         if [ $(which monit) ]; then
71             echo "monit installed"
72         else
73             if [ -n "$(command -v yum)" ]; then
74                 echo "please install monit; eg: yum -y install monit"
75                 exit 1
76             elif [ -n "$(command -v apt-get)" ]; then
77                 echo "please install monit; eg: apt-get install -y monit"
78                 exit 1
79             else
80                 echo "system not supported plese contact help desk"
81                 exit 1
82             fi
83         fi
84
85         if [ -d /etc/monit/conf.d ]; then
86             monitconfdir="/etc/monit/conf.d/"
87             monitconfig="/etc/monit/monitrc"
88             #add start delay
89             sed -i '/^#.* delay /s/^#//' "$monitconfig"
90         elif [ -d /etc/monit.d ]; then
91             monitconfdir="/etc/monit.d"
92             monitconfig="/etc/monitrc"
93             #add start delay
94             sed -i '/^#.* delay /s/^#//' "$monitconfig"
95         else
96             echo "Could not determine the location of the monit configuration file."
97             echo "Make sure monit is installed."
98             exit 1
99         fi
100
101         chown=$(type -p chown)
102         mkdir=$(type -p mkdir)
103
104         makemonit () {
105             echo "Writing the following as monit config:"
106         cat << EOF | tee $monitconfdir/jenkins
107 check directory jenkins_piddir path /var/run/$jenkinsuser
108 if does not exist then exec "$mkdir -p /var/run/$jenkinsuser"
109 if failed uid $jenkinsuser then exec "$chown $jenkinsuser /var/run/$jenkinsuser"
110 if failed gid $jenkinsuser then exec "$chown :$jenkinsuser /var/run/$jenkinsuser"
111
112 check process jenkins with pidfile /var/run/$jenkinsuser/jenkins_jnlp_pid
113 start program = "/usr/bin/sudo -u $jenkinsuser /bin/bash -c 'cd $jenkinshome; export started_monit=true; $0 $@' with timeout 60 seconds"
114 stop program = "/bin/bash -c '/bin/kill \$(/bin/cat /var/run/$jenkinsuser/jenkins_jnlp_pid)'"
115 depends on jenkins_piddir
116 EOF
117         }
118
119         if [[ -f $monitconfdir/jenkins ]]; then
120             #test for diff
121             if [[ "$(diff $monitconfdir/jenkins <(echo "\
122 check directory jenkins_piddir path /var/run/$jenkinsuser
123 if does not exist then exec \"$mkdir -p /var/run/$jenkinsuser\"
124 if failed uid $jenkinsuser then exec \"$chown $jenkinsuser /var/run/$jenkinsuser\"
125 if failed gid $jenkinsuser then exec \"$chown :$jenkinsuser /var/run/$jenkinsuser\"
126
127 check process jenkins with pidfile /var/run/$jenkinsuser/jenkins_jnlp_pid
128 start program = \"/usr/bin/sudo -u $jenkinsuser /bin/bash -c 'cd $jenkinshome; export started_monit=true; $0 $@' with timeout 60 seconds\"
129 stop program = \"/bin/bash -c '/bin/kill \$(/bin/cat /var/run/$jenkinsuser/jenkins_jnlp_pid)'\"
130 depends on jenkins_piddir\
131 ") )" ]]; then
132                 echo "Updating monit config..."
133                 makemonit $@
134             fi
135         else
136             makemonit $@
137         fi
138     fi
139
140     if [[ $started_monit == "true" ]]; then
141         wget --timestamping https://build.opnfv.org/ci/jnlpJars/slave.jar && true
142         chown $jenkinsuser:$jenkinsuser slave.jar
143
144         if [[ -f /var/run/$jenkinsuser/jenkins_jnlp_pid ]]; then
145             echo "pid file found"
146             if ! kill -0 "$(/bin/cat /var/run/$jenkinsuser/jenkins_jnlp_pid)"; then
147                 echo "no java process running cleaning up pid file"
148                 rm -f /var/run/$jenkinsuser/jenkins_jnlp_pid;
149             else
150                 echo "java connection process found and running already running quitting."
151                 exit 1
152             fi
153         fi
154
155         if [[ $run_in_foreground == true ]]; then
156             $connectionstring
157         else
158             exec $connectionstring &
159             echo $! > /var/run/$jenkinsuser/jenkins_jnlp_pid
160         fi
161     else
162         echo "you are ready to start monit"
163         echo "eg: service monit start"
164         echo "example debug mode if you are having problems:  /usr/bin/monit -Ivv -c /etc/monit.conf "
165         exit 0
166     fi
167 }
168
169 usage() {
170     cat << EOF
171
172 **this file must be copied to the jenkins home directory to work**
173 jenkins-jnlp-connect.sh configures monit to keep slave connection up
174 Checks for new versions of slave.jar
175 run as root to create pid directory and create monit config.
176 can be run as root additional times if you change variables and need to update monit config.
177 after running as root you should see "you are ready to start monit"
178
179 usage: $0 [OPTIONS]
180  -h  show this message
181  -j  set jenkins home
182  -u  set jenkins user
183  -n  set slave name
184  -s  set secret key
185  -t  test the connection string by connecting without monit
186  -f  test firewall
187
188 Example: $0 -j /home/jenkins -u jenkins -n lab1 -s 727fdefoofoofoofoofoofoofof800
189 note: a trailing slash on -j /home/jenkins will break the script
190 EOF
191
192     exit 1
193 }
194
195 if [[ -z "$@" ]]; then
196     usage
197 fi
198
199 while getopts "j:u:n:s:htf" OPTION
200 do
201     case $OPTION in
202         j ) jenkinshome="$OPTARG" ;;
203         u ) jenkinsuser="$OPTARG" ;;
204         n ) slave_name="$OPTARG" ;;
205         s ) slave_secret="$OPTARG";;
206         h ) usage ;;
207         t ) started_monit=true
208             skip_monit=true
209             run_in_foreground=true ;;
210         f ) test_firewall ;;
211         \? ) echo "Unknown option: -$OPTARG" >&2; exit 1;;
212     esac
213 done
214
215 connectionstring="java -jar slave.jar -jnlpUrl https://build.opnfv.org/ci/computer/"$slave_name"/slave-agent.jnlp -secret "$slave_secret" -noCertificateCheck "
216
217 main "$@"