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