2 #Monit setup script for opnfv jnlp slave connections
5 echo "testing that the firewall is open for us at build.opnfv.org"
6 test=$(echo "blah"| nc -w 4 build.opnfv.org 57387 > /dev/null 2>&1; echo $?)
7 if [[ $test == 0 ]]; then
8 echo "Firewall is open for us at build.opnfv.org"
12 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
13 opnfv-helpdesk@rt.linuxfoundation.org
14 Jenkins Home: $jenkinshome
15 Jenkins User: $jenkinsuser
16 Slave Name: $slave_name
17 IP Address: $(curl -s http://icanhazip.com)
24 dir=$(cd $(dirname $0); pwd)
27 if [[ -z $jenkinsuser || -z $jenkinshome ]]; then
28 echo "jenkinsuser or home not defined, please edit this file to define it"
32 if [[ -z $slave_name || -z $slave_secret ]]; then
33 echo "slave name or secret not defined, please edit this file to define it"
37 if [[ $(whoami) != "root" && $(whoami) != "$jenkinsuser" ]]; then
38 echo "This script must be run as user root or jenkins user"
42 if [[ $distro == Debian || $distro == Ubuntu ]]; then
43 monitconfdir="/etc/monit/conf.d/"
44 elif [[ $distro == Fedora || $distro == CentOS || $distro == Redhat ]]; then
45 monitconfdir="/etc/monit.d"
49 pidfile="/var/run/$jenkinsuser/jenkins_jnlp_pid"
50 if ! [ -d /var/run/$jenkinsuser/ ]; then
51 mkdir /var/run/$jenkinsuser/
52 chown $jenkinsuser:$jenkinsuser /var/run/$jenkinsuser/
57 if [[ $skip_monit == true ]]; then
61 if [ $(which monit) ]; then
62 echo "monit installed"
64 if [ -n "$(command -v yum)" ]; then
65 echo "please install monit; eg: yum -y install monit"
67 elif [ -n "$(command -v apt-get)" ]; then
68 echo "please install monit eg: apt-get install -y monit"
71 echo "system not supported plese contact help desk"
78 echo "Writing the following as monit config:"
79 cat << EOF | tee $monitconfdir/jenkins
80 check process jenkins with pidfile /var/run/$jenkinsuser/jenkins_jnlp_pid
81 start program = "/bin/bash -c 'cd $dir; export started_monit=true; $0 $@'" as uid "$jenkinsuser" and gid "$jenkinsuser"
82 stop program = "/bin/bash -c '/bin/kill \$(/bin/cat /var/run/$jenkinsuser/jenkins_jnlp_pid)'"
86 if [[ -f $monitconfdir/jenkins ]]; then
88 if [[ "$(diff $monitconfdir/jenkins <(echo "\
89 check process jenkins with pidfile /var/run/$jenkinsuser/jenkins_jnlp_pid
90 start program = \"/bin/bash -c 'cd $dir; export started_monit=true; $0 $@'\" as uid \"$jenkinsuser\" and gid \"$jenkinsuser\"
91 stop program = \" /bin/bash -c '/bin/kill \$(/bin/cat /var/run/$jenkinsuser/jenkins_jnlp_pid)'\"\
93 echo "Updating monit config..."
101 if [[ $started_monit == "true" ]]; then
102 wget --timestamping https://build.opnfv.org/ci/jnlpJars/slave.jar && true
104 if [[ -f /var/run/$jenkinsuser/jenkins_jnlp_pid ]];
105 then echo "pid file found"
106 if ! kill -0 "$(/bin/cat /var/run/$jenkinsuser/jenkins_jnlp_pid)"; then
107 echo "no java process running cleaning up pid file"
108 rm -f /var/run/$jenkinsuser/jenkins_jnlp_pid;
110 echo "java connection process found and running already running quitting."
116 exec $connectionstring &
117 echo $! > /var/run/$jenkinsuser/jenkins_jnlp_pid
118 #trap 'rm -f "$pidfile"; exit' EXIT SIGQUIT SIGINT SIGSTOP SIGTERM
121 echo "you are ready to start monit"
122 echo "eg: service monit start"
123 echo "example debug mode if you are having problems: /usr/bin/monit -Ivv -c /etc/monit.conf "
132 jenkins-jnlp-connect.sh configures monit to keep slave connection up
133 Checks for new versions of slave.jar
134 run as root to create pid directory and create monit config.
135 can be run as root additional times if you change variables and need to update monit config.
136 after running as root you should see "you are ready to start monit"
144 -t test the connection string by connecting without monit
147 Example: $0 -j /home/jenkins/ -u jenkins -n lab1 -s 727fdefoofoofoofoofoofoofof800
155 if [[ -z "$@" ]]; then usage
159 while getopts "j:u:n:s:htf" OPTION
162 j ) jenkinshome="$OPTARG" ;;
163 u ) jenkinsuser="$OPTARG" ;;
164 n ) slave_name="$OPTARG" ;;
165 s ) slave_secret="$OPTARG";;
167 t ) started_monit=true
170 \? ) echo "Unknown option: -$OPTARG" >&2; exit 1;;
174 connectionstring="java -jar slave.jar -jnlpUrl https://build.opnfv.org/ci/computer/"$slave_name"/slave-agent.jnlp -secret "$slave_secret" -noCertificateCheck "
175 distro="$(tr -s ' \011' '\012' < /etc/issue | head -n 1)"