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)
33 dir=$(cd $(dirname $0); pwd)
36 if [[ -z $jenkinsuser || -z $jenkinshome ]]; then
37 echo "jenkinsuser or home not defined, please edit this file to define it"
41 if [[ -z $slave_name || -z $slave_secret ]]; then
42 echo "slave name or secret not defined, please edit this file to define it"
46 if [[ $(whoami) != "root" && $(whoami) != "$jenkinsuser" ]]; then
47 echo "This script must be run as user root or jenkins user"
51 if [ -d /etc/monit/conf.d ]; then
52 monitconfdir="/etc/monit/conf.d/"
53 elif [ -d /etc/monit.d ]; then
54 monitconfdir="/etc/monit.d"
56 echo "Could not determine the location of the monit configuration file."
57 echo "Make sure monit is installed."
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/
70 if [[ $skip_monit == true ]]; then
74 if [ $(which monit) ]; then
75 echo "monit installed"
77 if [ -n "$(command -v yum)" ]; then
78 echo "please install monit; eg: yum -y install monit"
80 elif [ -n "$(command -v apt-get)" ]; then
81 echo "please install monit eg: apt-get install -y monit"
84 echo "system not supported plese contact help desk"
91 echo "Writing the following as monit config:"
92 cat << EOF | tee $monitconfdir/jenkins
93 check process jenkins with pidfile /var/run/$jenkinsuser/jenkins_jnlp_pid
94 start program = "/usr/bin/sudo -u $jenkinsuser /bin/bash -c 'cd $dir; export started_monit=true; $0 $@'"
95 stop program = "/bin/bash -c '/bin/kill \$(/bin/cat /var/run/$jenkinsuser/jenkins_jnlp_pid)'"
99 if [[ -f $monitconfdir/jenkins ]]; then
101 if [[ "$(diff $monitconfdir/jenkins <(echo "\
102 check process jenkins with pidfile /var/run/$jenkinsuser/jenkins_jnlp_pid
103 start program = \"/usr/bin/sudo -u $jenkinsuser /bin/bash -c 'cd $dir; export started_monit=true; $0 $@'\"
104 stop program = \"/bin/bash -c '/bin/kill \$(/bin/cat /var/run/$jenkinsuser/jenkins_jnlp_pid)'\"\
106 echo "Updating monit config..."
114 if [[ $started_monit == "true" ]]; then
115 wget --timestamping https://build.opnfv.org/ci/jnlpJars/slave.jar && true
116 chown $jenkinsuser:$jenkinsuser slave.jar
118 if [[ -f /var/run/$jenkinsuser/jenkins_jnlp_pid ]];
119 then echo "pid file found"
120 if ! kill -0 "$(/bin/cat /var/run/$jenkinsuser/jenkins_jnlp_pid)"; then
121 echo "no java process running cleaning up pid file"
122 rm -f /var/run/$jenkinsuser/jenkins_jnlp_pid;
124 echo "java connection process found and running already running quitting."
130 if [[ $run_in_foreground == true ]]; then
133 exec $connectionstring &
134 echo $! > /var/run/$jenkinsuser/jenkins_jnlp_pid
138 echo "you are ready to start monit"
139 echo "eg: service monit start"
140 echo "example debug mode if you are having problems: /usr/bin/monit -Ivv -c /etc/monit.conf "
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"
161 -t test the connection string by connecting without monit
164 Example: $0 -j /home/jenkins/ -u jenkins -n lab1 -s 727fdefoofoofoofoofoofoofof800
172 if [[ -z "$@" ]]; then usage
176 while getopts "j:u:n:s:htf" OPTION
179 j ) jenkinshome="$OPTARG" ;;
180 u ) jenkinsuser="$OPTARG" ;;
181 n ) slave_name="$OPTARG" ;;
182 s ) slave_secret="$OPTARG";;
184 t ) started_monit=true
186 run_in_foreground=true ;;
188 \? ) echo "Unknown option: -$OPTARG" >&2; exit 1;;
192 connectionstring="java -jar slave.jar -jnlpUrl https://build.opnfv.org/ci/computer/"$slave_name"/slave-agent.jnlp -secret "$slave_secret" -noCertificateCheck "