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