bugfix: verify compass exclude doc
[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 [[ -z $slave_name || -z $slave_secret ]]; then
42   echo "slave name or secret not defined, please edit this file to define it"
43   exit 1
44 fi
45
46 if [[ $(whoami) != "root" && $(whoami) != "$jenkinsuser"  ]]; then
47   echo "This script must be run as user root or jenkins user"
48   exit 1
49 fi
50
51 if [[ $distro == Debian || $distro == Ubuntu ]]; then
52   monitconfdir="/etc/monit/conf.d/"
53 elif [[ $distro == Fedora || $distro == CentOS || $distro == Redhat ]]; then
54   monitconfdir="/etc/monit.d"
55 fi
56
57 #make pid dir
58 pidfile="/var/run/$jenkinsuser/jenkins_jnlp_pid"
59 if ! [ -d /var/run/$jenkinsuser/ ]; then
60   mkdir   /var/run/$jenkinsuser/
61   chown $jenkinsuser:$jenkinsuser /var/run/$jenkinsuser/
62 fi
63
64
65
66 if [[ $skip_monit == true ]]; then
67   :
68 else
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 fi
85
86 makemonit () {
87 echo "Writing the following as monit config:"
88 cat << EOF | tee $monitconfdir/jenkins
89 check process jenkins with pidfile /var/run/$jenkinsuser/jenkins_jnlp_pid
90 start program = "/usr/bin/sudo -u $jenkinsuser /bin/bash -c 'cd $dir; export started_monit=true; $0 $@'"
91 stop program = "/bin/bash -c '/bin/kill \$(/bin/cat /var/run/$jenkinsuser/jenkins_jnlp_pid)'"
92 EOF
93 }
94
95 if [[ -f $monitconfdir/jenkins ]]; then
96   #test for diff
97   if [[ "$(diff $monitconfdir/jenkins <(echo "\
98 check process jenkins with pidfile /var/run/$jenkinsuser/jenkins_jnlp_pid
99 start program = \"usr/bin/sudo -u $jenkinsuser /bin/bash -c 'cd $dir; export started_monit=true; $0 $@'\"
100 stop program = \" /bin/bash -c '/bin/kill \$(/bin/cat /var/run/$jenkinsuser/jenkins_jnlp_pid)'\"\
101 ") )" ]]; then
102     echo "Updating monit config..."
103     makemonit $@
104   fi
105 else
106   makemonit $@
107 fi
108
109
110 if [[ $started_monit == "true" ]]; then
111 wget --timestamping https://build.opnfv.org/ci/jnlpJars/slave.jar && true
112 chown $jenkinsuser:$jenkinsuser slave.jar
113
114 if [[ -f /var/run/$jenkinsuser/jenkins_jnlp_pid ]];
115 then echo "pid file found"
116         if ! kill -0 "$(/bin/cat /var/run/$jenkinsuser/jenkins_jnlp_pid)"; then
117                 echo "no java process running cleaning up pid file"
118                 rm -f /var/run/$jenkinsuser/jenkins_jnlp_pid;
119         else
120                 echo "java connection process found and running already running quitting."
121                 exit 1
122
123         fi
124 fi
125
126 if [[ run_in_foreground == true ]]; then
127   $connectionstring
128 else
129   exec $connectionstring &
130   echo $! > /var/run/$jenkinsuser/jenkins_jnlp_pid
131 fi
132
133 else
134   echo "you are ready to start monit"
135   echo "eg: service monit start"
136   echo "example debug mode if you are having problems:  /usr/bin/monit -Ivv -c /etc/monit.conf "
137   exit 0
138 fi
139
140 }
141
142 usage() {
143 cat << EOF
144
145 jenkins-jnlp-connect.sh configures monit to keep slave connection up
146 Checks for new versions of slave.jar
147 run as root to create pid directory and create monit config.
148 can be run as root additional times if you change variables and need to update monit config.
149 after running as root you should see "you are ready to start monit"
150
151 usage: $0 [OPTIONS]
152  -h  show this message
153  -j  set jenkins home
154  -u  set jenkins user
155  -n  set slave name
156  -s  set secret key
157  -t  test the connection string by connecting without monit
158  -f  test firewall
159
160 Example: $0 -j /home/jenkins/ -u jenkins -n lab1 -s 727fdefoofoofoofoofoofoofof800
161
162 EOF
163
164 exit 1
165
166 }
167
168 if [[ -z "$@" ]]; then usage
169 fi
170
171
172 while getopts "j:u:n:s:htf" OPTION
173 do
174         case $OPTION in
175                 j ) jenkinshome="$OPTARG" ;;
176                 u ) jenkinsuser="$OPTARG" ;;
177                 n ) slave_name="$OPTARG" ;;
178                 s ) slave_secret="$OPTARG";;
179                 h ) usage; exit;;
180                 t ) started_monit=true
181                     skip_monit=true
182                     run_in_foreground=true ;;
183                 f ) test_firewall ;;
184                 \? ) echo "Unknown option: -$OPTARG" >&2; exit 1;;
185         esac
186 done
187
188 connectionstring="java -jar slave.jar -jnlpUrl https://build.opnfv.org/ci/computer/"$slave_name"/slave-agent.jnlp -secret "$slave_secret" -noCertificateCheck "
189 distro="$(tr -s ' \011' '\012' < /etc/issue | head -n 1)"
190
191 main "$@"