Adding coverage report to merge job
[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 = "/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)'"
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 = \"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)'\"\
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 chown $jenkinsuser:$jenkinsuser slave.jar
104
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;
110         else
111                 echo "java connection process found and running already running quitting."
112                 exit 1
113
114         fi
115 fi
116
117 if [[ run_in_foreground == true ]]; then
118   $connectionstring
119 else
120   exec $connectionstring &
121   echo $! > /var/run/$jenkinsuser/jenkins_jnlp_pid
122 fi
123
124 else
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 "
128   exit 0
129 fi
130
131 }
132
133 usage() {
134 cat << EOF
135
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"
141
142 usage: $0 [OPTIONS]
143  -h  show this message
144  -j  set jenkins home
145  -u  set jenkins user
146  -n  set slave name
147  -s  set secret key
148  -t  test the connection string by connecting without monit
149  -f  test firewall
150
151 Example: $0 -j /home/jenkins/ -u jenkins -n lab1 -s 727fdefoofoofoofoofoofoofof800
152
153 EOF
154
155 exit 1
156
157 }
158
159 if [[ -z "$@" ]]; then usage
160 fi
161
162
163 while getopts "j:u:n:s:htf" OPTION
164 do
165         case $OPTION in
166                 j ) jenkinshome="$OPTARG" ;;
167                 u ) jenkinsuser="$OPTARG" ;;
168                 n ) slave_name="$OPTARG" ;;
169                 s ) slave_secret="$OPTARG";;
170                 h ) usage; exit;;
171                 t ) started_monit=true
172                     skip_monit=true
173                     run_in_foreground=true ;;
174                 f ) test_firewall ;;
175                 \? ) echo "Unknown option: -$OPTARG" >&2; exit 1;;
176         esac
177 done
178
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)"
181
182 main "$@"