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