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 = "/usr/bin/sudo -u $jenkinsuser /bin/bash -c 'cd $dir; export started_monit=true; $0 $@'"
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 = \"usr/bin/sudo -u $jenkinsuser /bin/bash -c 'cd $dir; export started_monit=true; $0 $@'\"
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
103 chown $jenkinsuser:$jenkinsuser slave.jar
105 if [[ -f /var/run/$jenkinsuser/jenkins_jnlp_pid ]];
106 then echo "pid file found"
107 if ! kill -0 "$(/bin/cat /var/run/$jenkinsuser/jenkins_jnlp_pid)"; then
108 echo "no java process running cleaning up pid file"
109 rm -f /var/run/$jenkinsuser/jenkins_jnlp_pid;
111 echo "java connection process found and running already running quitting."
117 if [[ run_in_foreground == true ]]; then
120 exec $connectionstring &
121 echo $! > /var/run/$jenkinsuser/jenkins_jnlp_pid
125 echo "you are ready to start monit"
126 echo "eg: service monit start"
127 echo "example debug mode if you are having problems: /usr/bin/monit -Ivv -c /etc/monit.conf "
136 jenkins-jnlp-connect.sh configures monit to keep slave connection up
137 Checks for new versions of slave.jar
138 run as root to create pid directory and create monit config.
139 can be run as root additional times if you change variables and need to update monit config.
140 after running as root you should see "you are ready to start monit"
148 -t test the connection string by connecting without monit
151 Example: $0 -j /home/jenkins/ -u jenkins -n lab1 -s 727fdefoofoofoofoofoofoofof800
159 if [[ -z "$@" ]]; then usage
163 while getopts "j:u:n:s:htf" OPTION
166 j ) jenkinshome="$OPTARG" ;;
167 u ) jenkinsuser="$OPTARG" ;;
168 n ) slave_name="$OPTARG" ;;
169 s ) slave_secret="$OPTARG";;
171 t ) started_monit=true
173 run_in_foreground=true ;;
175 \? ) echo "Unknown option: -$OPTARG" >&2; exit 1;;
179 connectionstring="java -jar slave.jar -jnlpUrl https://build.opnfv.org/ci/computer/"$slave_name"/slave-agent.jnlp -secret "$slave_secret" -noCertificateCheck "
180 distro="$(tr -s ' \011' '\012' < /etc/issue | head -n 1)"