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 ##############################################################################
11 #Monit setup script for opnfv jnlp slave connections
14 jenkins_hostname="${jenkins_hostname:-build.opnfv.org}"
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"
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: $slave_name
29 IP Address: $(curl -s http://icanhazip.com)
37 if [[ -z $jenkinsuser || -z $jenkinshome ]]; then
38 echo "jenkinsuser or home not defined, please edit this file to define it"
42 if [[ $(pwd) != "$jenkinshome" ]]; then
43 echo "This script needs to be run from the jenkins users home dir"
47 if [[ -z $slave_name || -z $slave_secret ]]; then
48 echo "slave name or secret not defined, please edit this file to define it"
52 if [[ $(whoami) != "root" && $(whoami) != "$jenkinsuser" ]]; then
53 echo "This script must be run as user root or jenkins user"
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"
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/
71 if [[ $skip_monit != true ]]; then
73 if [ $(which monit) ]; then
74 echo "monit installed"
76 if [ -n "$(command -v yum)" ]; then
77 echo "please install monit; eg: yum -y install monit"
79 elif [ -n "$(command -v apt-get)" ]; then
80 echo "please install monit; eg: apt-get install -y monit"
83 echo "system not supported plese contact help desk"
88 if [ -d /etc/monit/conf.d ]; then
89 monitconfdir="/etc/monit/conf.d/"
90 monitconfig="/etc/monit/monitrc"
92 sed -i '/^#.* delay /s/^#//' "$monitconfig"
93 elif [ -d /etc/monit.d ]; then
94 monitconfdir="/etc/monit.d"
95 monitconfig="/etc/monitrc"
97 sed -i '/^#.* delay /s/^#//' "$monitconfig"
99 echo "Could not determine the location of the monit configuration file."
100 echo "Make sure monit is installed."
104 chown=$(type -p chown)
105 mkdir=$(type -p mkdir)
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"
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
122 if [[ -f $monitconfdir/jenkins ]]; then
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\"
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\
135 echo "Updating monit config..."
143 if [[ $started_monit == "true" ]]; then
144 wget --timestamping https://"$jenkins_hostname"/jnlpJars/slave.jar && true
145 chown $jenkinsuser:$jenkinsuser slave.jar
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;
153 echo "java connection process found and running already running quitting."
158 if [[ $run_in_foreground == true ]]; then
161 exec $connectionstring &
162 echo $! > /var/run/$jenkinsuser/jenkins_jnlp_pid
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 "
175 **this file must be copied to the jenkins home directory to work**
176 jenkins-jnlp-connect.sh configures monit to keep slave connection up
177 Checks for new versions of slave.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"
188 -l set host, default is build.opnfv.org/ci
189 -t test the connection string by connecting without monit
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
199 if [[ -z "$@" ]]; then
203 while getopts "j:u:n:s:l:htf" OPTION
206 j ) jenkinshome="$OPTARG" ;;
207 u ) jenkinsuser="$OPTARG" ;;
208 n ) slave_name="$OPTARG" ;;
209 s ) slave_secret="$OPTARG";;
210 l ) jenkins_hostname="$OPTARG" ;;
212 t ) started_monit=true
214 run_in_foreground=true ;;
216 \? ) echo "Unknown option: -$OPTARG" >&2; exit 1;;
220 jenkins_hostname="${jenkins_hostname:-build.opnfv.org/ci}"
221 connectionstring="java -jar slave.jar -jnlpUrl https://"$jenkins_hostname"/computer/"$slave_name"/slave-agent.jnlp -secret "$slave_secret" -noCertificateCheck "