Merge "I will figure out how to add a check for this later"
[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     dir=$(cd $(dirname $0); pwd)
34
35     #tests
36     if [[ -z $jenkinsuser || -z $jenkinshome ]]; then
37         echo "jenkinsuser or home not defined, please edit this file to define it"
38         exit 1
39     fi
40
41     if [[ $(pwd) != "$jenkinshome" ]]; then
42         echo "This script needs to be run from the jenkins users home dir"
43         exit 1
44     fi
45
46     if [[ -z $slave_name || -z $slave_secret ]]; then
47         echo "slave name or secret not defined, please edit this file to define it"
48         exit 1
49     fi
50
51     if [[ $(whoami) != "root" && $(whoami) != "$jenkinsuser"  ]]; then
52         echo "This script must be run as user root or jenkins user"
53         exit 1
54     fi
55
56     if [[ $(whoami) != "root" ]]; then
57       if sudo -l | grep "requiretty"; then
58         echo "please comment out Defaults requiretty from /etc/sudoers"
59         exit 1
60       fi
61     fi
62
63
64     if [ -d /etc/monit/conf.d ]; then
65         monitconfdir="/etc/monit/conf.d/"
66     elif [ -d /etc/monit.d ]; then
67         monitconfdir="/etc/monit.d"
68     else
69         echo "Could not determine the location of the monit configuration file."
70         echo "Make sure monit is installed."
71         exit 1
72     fi
73
74     #make pid dir
75     pidfile="/var/run/$jenkinsuser/jenkins_jnlp_pid"
76     if ! [ -d /var/run/$jenkinsuser/ ]; then
77         mkdir /var/run/$jenkinsuser/
78         chown $jenkinsuser:$jenkinsuser /var/run/$jenkinsuser/
79     fi
80
81     if [[ $skip_monit != true ]]; then
82         #check for monit
83         if [ $(which monit) ]; then
84             echo "monit installed"
85         else
86             if [ -n "$(command -v yum)" ]; then
87                 echo "please install monit; eg: yum -y install monit"
88                 exit 1
89             elif [ -n "$(command -v apt-get)" ]; then
90                 echo "please install monit; eg: apt-get install -y monit"
91                 exit 1
92             else
93                 echo "system not supported plese contact help desk"
94                 exit 1
95             fi
96         fi
97     fi
98
99     makemonit () {
100         echo "Writing the following as monit config:"
101         cat << EOF | tee $monitconfdir/jenkins
102 check process jenkins with pidfile /var/run/$jenkinsuser/jenkins_jnlp_pid
103 start program = "/usr/bin/sudo -u $jenkinsuser /bin/bash -c 'cd $dir; export started_monit=true; $0 $@' with timeout 60 seconds"
104 stop program = "/bin/bash -c '/bin/kill \$(/bin/cat /var/run/$jenkinsuser/jenkins_jnlp_pid)'"
105 EOF
106     }
107
108     if [[ -f $monitconfdir/jenkins ]]; then
109         #test for diff
110         if [[ "$(diff $monitconfdir/jenkins <(echo "\
111 check process jenkins with pidfile /var/run/$jenkinsuser/jenkins_jnlp_pid
112 start program = \"/usr/bin/sudo -u $jenkinsuser /bin/bash -c 'cd $dir; export started_monit=true; $0 $@' with timeout 60 seconds\"
113 stop program = \"/bin/bash -c '/bin/kill \$(/bin/cat /var/run/$jenkinsuser/jenkins_jnlp_pid)'\"\
114 ") )" ]]; then
115             echo "Updating monit config..."
116             makemonit $@
117         fi
118     else
119         makemonit $@
120     fi
121
122 if [[ $started_monit == "true" ]]; then
123     wget --timestamping https://build.opnfv.org/ci/jnlpJars/slave.jar && true
124     chown $jenkinsuser:$jenkinsuser slave.jar
125
126     if [[ -f /var/run/$jenkinsuser/jenkins_jnlp_pid ]]; then
127         echo "pid file found"
128         if ! kill -0 "$(/bin/cat /var/run/$jenkinsuser/jenkins_jnlp_pid)"; then
129             echo "no java process running cleaning up pid file"
130             rm -f /var/run/$jenkinsuser/jenkins_jnlp_pid;
131         else
132             echo "java connection process found and running already running quitting."
133             exit 1
134         fi
135     fi
136
137     if [[ $run_in_foreground == true ]]; then
138         $connectionstring
139     else
140         exec $connectionstring &
141         echo $! > /var/run/$jenkinsuser/jenkins_jnlp_pid
142     fi
143 else
144     echo "you are ready to start monit"
145     echo "eg: service monit start"
146     echo "example debug mode if you are having problems:  /usr/bin/monit -Ivv -c /etc/monit.conf "
147     exit 0
148 fi
149 }
150
151 usage() {
152     cat << EOF
153
154 **this file must be copied to the jenkins home directory to work**
155 jenkins-jnlp-connect.sh configures monit to keep slave connection up
156 Checks for new versions of slave.jar
157 run as root to create pid directory and create monit config.
158 can be run as root additional times if you change variables and need to update monit config.
159 after running as root you should see "you are ready to start monit"
160
161 usage: $0 [OPTIONS]
162  -h  show this message
163  -j  set jenkins home
164  -u  set jenkins user
165  -n  set slave name
166  -s  set secret key
167  -t  test the connection string by connecting without monit
168  -f  test firewall
169
170 Example: $0 -j /home/jenkins/ -u jenkins -n lab1 -s 727fdefoofoofoofoofoofoofof800
171 EOF
172
173     exit 1
174 }
175
176 if [[ -z "$@" ]]; then
177     usage
178 fi
179
180 while getopts "j:u:n:s:htf" OPTION
181 do
182     case $OPTION in
183         j ) jenkinshome="$OPTARG" ;;
184         u ) jenkinsuser="$OPTARG" ;;
185         n ) slave_name="$OPTARG" ;;
186         s ) slave_secret="$OPTARG";;
187         h ) usage ;;
188         t ) started_monit=true
189             skip_monit=true
190             run_in_foreground=true ;;
191         f ) test_firewall ;;
192         \? ) echo "Unknown option: -$OPTARG" >&2; exit 1;;
193     esac
194 done
195
196 connectionstring="java -jar slave.jar -jnlpUrl https://build.opnfv.org/ci/computer/"$slave_name"/slave-agent.jnlp -secret "$slave_secret" -noCertificateCheck "
197
198 main "$@"