Merge "docs: fix |today|"
[releng.git] / utils / jenkins-jnlp-connect.sh
index d048859..6fb6827 100755 (executable)
@@ -1,4 +1,13 @@
 #!/bin/bash
+# SPDX-license-identifier: Apache-2.0
+##############################################################################
+# Copyright (c) 2016 Linux Foundation and others.
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
 #Monit setup script for opnfv jnlp slave connections
 
 test_firewall() {
@@ -78,7 +87,7 @@ makemonit () {
 echo "Writing the following as monit config:"
 cat << EOF | tee $monitconfdir/jenkins
 check process jenkins with pidfile /var/run/$jenkinsuser/jenkins_jnlp_pid
-start program = "/bin/bash -c 'cd $dir; export started_monit=true; $0 $@'" as uid "$jenkinsuser" and gid "$jenkinsuser"
+start program = "/usr/bin/sudo -u $jenkinsuser /bin/bash -c 'cd $dir; export started_monit=true; $0 $@'"
 stop program = "/bin/bash -c '/bin/kill \$(/bin/cat /var/run/$jenkinsuser/jenkins_jnlp_pid)'"
 EOF
 }
@@ -87,7 +96,7 @@ if [[ -f $monitconfdir/jenkins ]]; then
   #test for diff
   if [[ "$(diff $monitconfdir/jenkins <(echo "\
 check process jenkins with pidfile /var/run/$jenkinsuser/jenkins_jnlp_pid
-start program = \"/bin/bash -c 'cd $dir; export started_monit=true; $0 $@'\" as uid \"$jenkinsuser\" and gid \"$jenkinsuser\"
+start program = \"usr/bin/sudo -u $jenkinsuser /bin/bash -c 'cd $dir; export started_monit=true; $0 $@'\"
 stop program = \" /bin/bash -c '/bin/kill \$(/bin/cat /var/run/$jenkinsuser/jenkins_jnlp_pid)'\"\
 ") )" ]]; then
     echo "Updating monit config..."
@@ -100,6 +109,7 @@ fi
 
 if [[ $started_monit == "true" ]]; then
 wget --timestamping https://build.opnfv.org/ci/jnlpJars/slave.jar && true
+chown $jenkinsuser:$jenkinsuser slave.jar
 
 if [[ -f /var/run/$jenkinsuser/jenkins_jnlp_pid ]];
 then echo "pid file found"
@@ -113,9 +123,12 @@ then echo "pid file found"
         fi
 fi
 
-exec $connectionstring &
-echo $! > /var/run/$jenkinsuser/jenkins_jnlp_pid
-#trap 'rm -f "$pidfile"; exit' EXIT SIGQUIT SIGINT SIGSTOP SIGTERM
+if [[ run_in_foreground == true ]]; then
+  $connectionstring
+else
+  exec $connectionstring &
+  echo $! > /var/run/$jenkinsuser/jenkins_jnlp_pid
+fi
 
 else
   echo "you are ready to start monit"
@@ -165,7 +178,8 @@ do
                 s ) slave_secret="$OPTARG";;
                 h ) usage; exit;;
                 t ) started_monit=true
-                    skip_monit=true ;;
+                    skip_monit=true
+                    run_in_foreground=true ;;
                 f ) test_firewall ;;
                 \? ) echo "Unknown option: -$OPTARG" >&2; exit 1;;
         esac