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 akraino jnlp agent connections
14 jenkins_hostname="${jenkins_hostname:-jenkins.akraino.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 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)
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"
44 echo "You are at $(pwd); jenkins home is set at $jenkinshome"
48 if [[ -z $agent_name || -z $agent_secret ]]; then
49 echo "agent name or secret not defined, please edit this file to define it"
53 if [[ $(whoami) != "root" && $(whoami) != "$jenkinsuser" ]]; then
54 echo "This script must be run as user root or jenkins user"
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"
66 if [[ ${SANDBOX} == true ]]; then
67 monit_service="sandbox"
69 monit_service="jenkins"
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"/
78 if [[ $skip_monit != true ]]; then
80 if [ -n "$(command which monit)" ]; then
81 echo "monit installed"
83 if [ -n "$(command -v yum)" ]; then
84 echo "please install monit; eg: yum -y install monit"
86 elif [ -n "$(command -v apt-get)" ]; then
87 echo "please install monit; eg: apt-get install -y monit"
90 echo "system not supported plese contact help desk"
95 if [ -d /etc/monit/conf.d ]; then
96 monitconfdir="/etc/monit/conf.d/"
97 monitconfig="/etc/monit/monitrc"
99 sed -i '/^#.* delay /s/^#//' "$monitconfig"
100 elif [ -d /etc/monit.d ]; then
101 monitconfdir="/etc/monit.d"
102 monitconfig="/etc/monitrc"
104 sed -i '/^#.* delay /s/^#//' "$monitconfig"
106 echo "Could not determine the location of the monit configuration file."
107 echo "Make sure monit is installed."
111 chown=$(type -p chown)
112 mkdir=$(type -p mkdir)
115 echo "Writing the following as monit config:"
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
129 if [[ -f "$monitconfdir/$monit_service" ]]; then
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\
141 echo "Updating monit config..."
149 if [[ $started_monit == "true" ]]; then
150 wget --timestamping https://"$jenkins_hostname"/jnlpJars/agent.jar && true
151 chown "$jenkinsuser":"$jenkinsuser" agent.jar
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"
159 echo "java connection process found and running already running quitting."
164 if [[ $run_in_foreground == true ]]; then
167 exec ${connectionstring} &
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 "
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"
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
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
203 if [[ -z "$@" ]]; then
207 while getopts "j:u:n:s:l:bhtf" OPTION
210 j ) jenkinshome="$OPTARG" ;;
211 u ) jenkinsuser="$OPTARG" ;;
212 n ) agent_name="$OPTARG" ;;
213 s ) agent_secret="$OPTARG";;
214 l ) jenkins_hostname="$OPTARG" ;;
217 t ) started_monit=true
219 run_in_foreground=true ;;
221 \? ) echo "Unknown option: -$OPTARG" >&2; exit 1;;
225 if [[ ${SANDBOX} == true ]]; then
226 jenkins_hostname="${jenkins_hostname:-jenkins.akraino.org/sandbox}"
228 jenkins_hostname="${jenkins_hostname:-jenkins.akraino.org}"
230 echo "hostname is ${jenkins_hostname}"
232 connectionstring="java -jar agent.jar -jnlpUrl https://$jenkins_hostname/computer/$agent_name/slave-agent.jnlp -secret $agent_secret -noCertificateCheck "