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 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"
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)
34 if [[ -z $jenkinsuser || -z $jenkinshome ]]; then
35 echo "jenkinsuser or home not defined, please edit this file to define it"
39 if [[ $(pwd) != "$jenkinshome" ]]; then
40 echo "This script needs to be run from the jenkins users home dir"
44 if [[ -z $slave_name || -z $slave_secret ]]; then
45 echo "slave name or secret not defined, please edit this file to define it"
49 if [[ $(whoami) != "root" && $(whoami) != "$jenkinsuser" ]]; then
50 echo "This script must be run as user root or jenkins user"
54 if [[ $(whoami) != "root" ]]; then
55 if sudo -l | grep "requiretty"; then
56 echo "please comment out Defaults requiretty from /etc/sudoers"
62 pidfile="/var/run/$jenkinsuser/jenkins_jnlp_pid"
63 if ! [ -d /var/run/$jenkinsuser/ ]; then
64 mkdir /var/run/$jenkinsuser/
65 chown $jenkinsuser:$jenkinsuser /var/run/$jenkinsuser/
68 if [[ $skip_monit != true ]]; then
70 if [ $(which monit) ]; then
71 echo "monit installed"
73 if [ -n "$(command -v yum)" ]; then
74 echo "please install monit; eg: yum -y install monit"
76 elif [ -n "$(command -v apt-get)" ]; then
77 echo "please install monit; eg: apt-get install -y monit"
80 echo "system not supported plese contact help desk"
85 if [ -d /etc/monit/conf.d ]; then
86 monitconfdir="/etc/monit/conf.d/"
87 elif [ -d /etc/monit.d ]; then
88 monitconfdir="/etc/monit.d"
90 echo "Could not determine the location of the monit configuration file."
91 echo "Make sure monit is installed."
96 echo "Writing the following as monit config:"
97 cat << EOF | tee $monitconfdir/jenkins
98 check directory jenkins_piddir path /var/run/$jenkinsuser
99 if does not exist then exec "/usr/bin/mkdir -p /var/run/$jenkinsuser"
100 if failed uid $jenkinsuser then exec "/usr/bin/chown $jenkinsuser /var/run/$jenkinsuser"
101 if failed gid $jenkinsuser then exec "/usr/bin/chown :$jenkinsuser /var/run/$jenkinsuser"
103 check process jenkins with pidfile /var/run/$jenkinsuser/jenkins_jnlp_pid
104 start program = "/usr/bin/sudo -u $jenkinsuser /bin/bash -c 'cd $jenkinshome; export started_monit=true; $0 $@' with timeout 60 seconds"
105 stop program = "/bin/bash -c '/bin/kill \$(/bin/cat /var/run/$jenkinsuser/jenkins_jnlp_pid)'"
106 depends on jenkins_piddir
110 if [[ -f $monitconfdir/jenkins ]]; then
112 if [[ "$(diff $monitconfdir/jenkins <(echo "\
113 check directory jenkins_piddir path /var/run/$jenkinsuser
114 if does not exist then exec \"/usr/bin/mkdir -p /var/run/$jenkinsuser\"
115 if failed uid $jenkinsuser then exec \"/usr/bin/chown $jenkinsuser /var/run/$jenkinsuser\"
116 if failed gid $jenkinsuser then exec \"/usr/bin/chown :$jenkinsuser /var/run/$jenkinsuser\"
118 check process jenkins with pidfile /var/run/$jenkinsuser/jenkins_jnlp_pid
119 start program = \"/usr/bin/sudo -u $jenkinsuser /bin/bash -c 'cd $jenkinshome; export started_monit=true; $0 $@' with timeout 60 seconds\"
120 stop program = \"/bin/bash -c '/bin/kill \$(/bin/cat /var/run/$jenkinsuser/jenkins_jnlp_pid)'\"
121 depends on jenkins_piddir\
123 echo "Updating monit config..."
131 if [[ $started_monit == "true" ]]; then
132 wget --timestamping https://build.opnfv.org/ci/jnlpJars/slave.jar && true
133 chown $jenkinsuser:$jenkinsuser slave.jar
135 if [[ -f /var/run/$jenkinsuser/jenkins_jnlp_pid ]]; then
136 echo "pid file found"
137 if ! kill -0 "$(/bin/cat /var/run/$jenkinsuser/jenkins_jnlp_pid)"; then
138 echo "no java process running cleaning up pid file"
139 rm -f /var/run/$jenkinsuser/jenkins_jnlp_pid;
141 echo "java connection process found and running already running quitting."
146 if [[ $run_in_foreground == true ]]; then
149 exec $connectionstring &
150 echo $! > /var/run/$jenkinsuser/jenkins_jnlp_pid
153 echo "you are ready to start monit"
154 echo "eg: service monit start"
155 echo "example debug mode if you are having problems: /usr/bin/monit -Ivv -c /etc/monit.conf "
163 **this file must be copied to the jenkins home directory to work**
164 jenkins-jnlp-connect.sh configures monit to keep slave connection up
165 Checks for new versions of slave.jar
166 run as root to create pid directory and create monit config.
167 can be run as root additional times if you change variables and need to update monit config.
168 after running as root you should see "you are ready to start monit"
176 -t test the connection string by connecting without monit
179 Example: $0 -j /home/jenkins -u jenkins -n lab1 -s 727fdefoofoofoofoofoofoofof800
180 note: a trailing slash on -j /home/jenkins will break the script
186 if [[ -z "$@" ]]; then
190 while getopts "j:u:n:s:htf" OPTION
193 j ) jenkinshome="$OPTARG" ;;
194 u ) jenkinsuser="$OPTARG" ;;
195 n ) slave_name="$OPTARG" ;;
196 s ) slave_secret="$OPTARG";;
198 t ) started_monit=true
200 run_in_foreground=true ;;
202 \? ) echo "Unknown option: -$OPTARG" >&2; exit 1;;
206 connectionstring="java -jar slave.jar -jnlpUrl https://build.opnfv.org/ci/computer/"$slave_name"/slave-agent.jnlp -secret "$slave_secret" -noCertificateCheck "