Script to configure monit for jnlp slave connections
[releng.git] / utils / jenkins-jnlp-connect.sh
1 #!/bin/bash
2 #Monit setup script for opnfv jnlp slave connections
3
4 test_firewall() {
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"
9   exit 0
10 else
11 cat << EOF
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)
18 EOF
19 exit 1
20 fi
21 }
22
23 main () {
24 dir=$(cd $(dirname $0); pwd)
25
26 #tests
27 if [[ -z $jenkinsuser || -z $jenkinshome ]]; then
28   echo "jenkinsuser or home not defined, please edit this file to define it"
29   exit 1
30 fi
31
32 if [[ -z $slave_name || -z $slave_secret ]]; then
33   echo "slave name or secret not defined, please edit this file to define it"
34   exit 1
35 fi
36
37 if [[ $(whoami) != "root" && $(whoami) != "$jenkinsuser"  ]]; then
38   echo "This script must be run as user root or jenkins user"
39   exit 1
40 fi
41
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"
46 fi
47
48 #make pid dir
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/
53 fi
54
55
56
57 if [[ $skip_monit == true ]]; then
58   :
59 else
60   #check for monit
61   if [ $(which monit) ]; then
62     echo "monit installed"
63   else
64     if [ -n "$(command -v yum)" ]; then
65       echo "please install monit; eg: yum -y install monit"
66       exit 1
67     elif [ -n "$(command -v apt-get)" ]; then
68       echo "please install monit  eg: apt-get install -y monit"
69       exit 1
70     else
71       echo "system not supported plese contact help desk"
72       exit 1
73     fi
74   fi
75 fi
76
77 makemonit () {
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)'"
83 EOF
84 }
85
86 if [[ -f $monitconfdir/jenkins ]]; then
87   #test for diff
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)'\"\
92 ") )" ]]; then
93     echo "Updating monit config..."
94     makemonit $@
95   fi
96 else
97   makemonit $@
98 fi
99
100
101 if [[ $started_monit == "true" ]]; then
102 wget --timestamping https://build.opnfv.org/ci/jnlpJars/slave.jar && true
103
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;
109         else
110                 echo "java connection process found and running already running quitting."
111                 exit 1
112
113         fi
114 fi
115
116 exec $connectionstring &
117 echo $! > /var/run/$jenkinsuser/jenkins_jnlp_pid
118 #trap 'rm -f "$pidfile"; exit' EXIT SIGQUIT SIGINT SIGSTOP SIGTERM
119
120 else
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 "
124   exit 0
125 fi
126
127 }
128
129 usage() {
130 cat << EOF
131
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"
137
138 usage: $0 [OPTIONS]
139  -h  show this message
140  -j  set jenkins home
141  -u  set jenkins user
142  -n  set slave name
143  -s  set secret key
144  -t  test the connection string by connecting without monit
145  -f  test firewall
146
147 Example: $0 -j /home/jenkins/ -u jenkins -n lab1 -s 727fdefoofoofoofoofoofoofof800
148
149 EOF
150
151 exit 1
152
153 }
154
155 if [[ -z "$@" ]]; then usage
156 fi
157
158
159 while getopts "j:u:n:s:htf" OPTION
160 do
161         case $OPTION in
162                 j ) jenkinshome="$OPTARG" ;;
163                 u ) jenkinsuser="$OPTARG" ;;
164                 n ) slave_name="$OPTARG" ;;
165                 s ) slave_secret="$OPTARG";;
166                 h ) usage; exit;;
167                 t ) started_monit=true
168                     skip_monit=true ;;
169                 f ) test_firewall ;;
170                 \? ) echo "Unknown option: -$OPTARG" >&2; exit 1;;
171         esac
172 done
173
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)"
176
177 main "$@"