1#!@BASH_SHELL@
2#
3####################################################################
4# Description: OCF Resource Agent to manage JIRA software.
5# Author     : Saleh A. (saleh.abbas.saber@gmail.com)
6#
7# License    : WTFPL 2
8#
9#        DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
10#                    Version 2, December 2004
11#
12# Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
13#
14# Everyone is permitted to copy and distribute verbatim or modified
15# copies of this license document, and changing it is allowed as long
16# as the name is changed.
17#
18#            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
19#   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
20#
21#  0. You just DO WHAT THE FUCK YOU WANT TO.
22#
23####################################################################
24# Parameters:
25# OCF_RESKEY_statusurl         : Status URL to monitor JIRA
26#               (default: http://localhost:8080/status)
27# OCF_RESKEY_java_home         : Java Home
28#               (default: /usr/lib/jvm/jre)
29# OCF_RESKEY_jira_installation : Jira installtion directory
30# OCF_RESKEY_jira_user         : User running Jira software
31#               (by default: jira)
32####################################################################
33
34# Initialization
35# Source ocf-shellfuncs
36: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
37. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
38
39# Parameter defaults
40
41OCF_RESKEY_statusurl_default="http://localhost:8080/status"
42OCF_RESKEY_java_home_default="/usr/lib/jvm/jre"
43OCF_RESKEY_jira_user_default="jira"
44
45: ${OCF_RESKEY_statusurl=${OCF_RESKEY_statusurl_default}}
46: ${OCF_RESKEY_java_home=${OCF_RESKEY_java_home_default}}
47: ${OCF_RESKEY_jira_user=${OCF_RESKEY_jira_user_default}}
48
49# Usage
50jira_usage() {
51  cat <<_EOF
52Usage: $0 action
53
54Supported Actions:
55    start       : start jira
56    stop        : stop jira 
57    monitor     : show jira status 
58    meta-data   : show the meta-data
59    validate-all: validate the RA configuration
60_EOF
61}
62
63# Start
64jira_start() {
65    # exit immediately if configuration is not valid
66    jira_validate_all || exit $?
67
68    # if resource is already running, bail out early
69    if jira_monitor; then
70        ocf_log info "Resource is already running"
71        return $OCF_SUCCESS
72    fi
73
74    # Starting Jira
75    waittime=300
76    su -m $jira_user -c "$jira_installation/bin/startup.sh &> /dev/null"
77    while [[ $waittime -gt 0 ]]; do
78      if $(curl --connect-timeout 1 --max-time 3 -s ${statusurl} | grep '{"state":"RUNNING"}' > /dev/null); then
79        waittime=0
80      else
81        sleep 1
82        waittime=$(($waittime - 1))
83      fi
84    done
85
86    # Verify jira is running
87    jira_monitor
88    rc=$?
89
90    return $?
91}
92
93# Stop
94jira_stop() {
95    local rc
96
97    # exit immediately if configuration is not valid
98    jira_validate_all || exit $?
99
100    jira_monitor
101    rc=$?
102    case "$rc" in
103        "$OCF_SUCCESS")
104            # Currently running. Normal, expected behavior.
105            ocf_log debug "Resource is currently running"
106            ;;
107        "$OCF_NOT_RUNNING")
108            # Currently not running. Nothing to do.
109            ocf_log info "Resource is already stopped"
110            return $OCF_SUCCESS
111            ;;
112    esac
113
114    # Stopping Jira
115    waittime=300
116    su -m $jira_user -c "$jira_installation/bin/shutdown.sh &> /dev/null"
117    while [[ $waittime -gt 0 ]]; do
118      if $(kill -0 $(cat ${jira_installation}/work/catalina.pid 2> /dev/null) 2> /dev/null) ; then
119        sleep 1
120        waittime=$(($waittime - 1))
121      else
122        waittime=0
123      fi
124    done
125
126    # Stop JIRA forcely if it failed
127    if $(kill -0 $(cat ${jira_installation}/work/catalina.pid 2> /dev/null) 2> /dev/null) ; then
128      kill -9 $(cat ${jira_installation}/work/catalina.pid)
129      sleep 1
130    fi
131
132    # Verify jira is stopped
133    jira_monitor
134    rc=$?
135
136    return $rc
137
138}
139
140# Monitor
141jira_monitor() {
142    local rc
143
144    # exit immediately if configuration is not valid
145    jira_validate_all || exit $?
146
147    if $(kill -0 $(cat ${jira_installation}/work/catalina.pid 2> /dev/null) 2> /dev/null) ; then
148      # Is jira working
149      if $(curl --connect-timeout 1 --max-time 3 -s ${statusurl} | grep '{"state":"RUNNING"}' > /dev/null) ; then
150        rc=0
151      else
152        # Jira has a problem
153        rc=2
154      fi
155    else
156      # Tomcat is stopped (and Jira)
157      rc=1
158    fi
159
160    case "$rc" in
161        0)
162            rc=$OCF_SUCCESS
163            ocf_log debug "Resource is running"
164            ;;
165        1)
166            rc=$OCF_NOT_RUNNING
167            ocf_log debug "Resource is not running"
168            ;;
169        *)
170            ocf_log err "Resource has failed"
171            exit $OCF_ERR_GENERIC
172    esac
173
174    return $rc
175}
176
177# Validat All
178jira_validate_all() {
179
180    # Check if java is installed
181    if ! [ -d $OCF_RESKEY_java_home ]; then
182      ocf_log err "$OCF_RESKEY_java_home does not exist. \
183      Please ensure that Java is installed and configured correctly"
184      exit $OCF_ERR_INSTALLED
185    fi
186
187    # Check if JIRA installation directory exists
188    if ! [ -d $OCF_RESKEY_jira_installation ]; then
189      ocf_log err "$OCF_RESKEY_jira_installation does not exist."
190      exit $OCF_ERR_INSTALLED
191    fi
192
193    return $OCF_SUCCESS
194}
195
196# Meta-data
197jira_meta_data(){
198    cat <<EOF
199<?xml version="1.0"?>
200<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
201<resource-agent name="jira">
202  <version>0.1</version>
203  <longdesc lang="en">
204OCF Resource Agent to manage JIRA software
205  </longdesc>
206  <shortdesc lang="en">JIRA OCF RA</shortdesc>
207  <parameters>
208    
209    <parameter name="statusurl" unique="0" required="0">
210      <longdesc lang="en">
211      Status URL for JIRA monitoring
212      </longdesc>
213      <shortdesc lang="en">JIRA status url</shortdesc>
214      <content type="string" default="${OCF_RESKEY_statusurl_default}"/>
215    </parameter>
216    
217    <parameter name="java_home" unique="0" required="0">
218      <longdesc lang="en">
219      Java Home in the Linux instance
220      </longdesc>
221      <shortdesc lang="en">Java Home</shortdesc>
222      <content type="string" default="${OCF_RESKEY_java_home_default}"/>
223    </parameter>
224
225
226    <parameter name="jira_installation" unique="0" required="1">
227      <longdesc lang="en">
228      JIRA installation directory (binaries, ... etc)
229      </longdesc>
230      <shortdesc lang="en">JIRA installation directory</shortdesc>
231      <content type="string"/>
232    </parameter>
233
234    <parameter name="jira_user" unique="0" required="0">
235      <longdesc lang="en">
236      User to run Jira software with 
237      </longdesc>
238      <shortdesc lang="en">Jira user</shortdesc>
239      <content type="string" default="${OCF_RESKEY_jira_user_default}"/>
240    </parameter>
241
242  </parameters>
243  <actions>
244    <action name="start"        timeout="300s" />
245    <action name="stop"         timeout="300s" />
246    <action name="monitor"      timeout="30s"
247                                interval="10s" depth="0" />
248    <action name="meta-data"    timeout="5s" />
249    <action name="validate-all"   timeout="20s" />
250  </actions>
251</resource-agent>
252EOF
253}
254
255# Execution
256
257# Set vars from defined OCF env vars
258statusurl=${OCF_RESKEY_statusurl-${OCF_RESKEY_statusurl_default}}
259java_home=${OCF_RESKEY_java_home-${OCF_RESKEY_java_home_default}}
260jira_installation=${OCF_RESKEY_jira_installation}
261jira_user=${OCF_RESKEY_jira_user-${OCF_RESKEY_jira_user_default}}
262
263# Export JAVA_HOME env variable
264export JAVA_HOME=${OCF_RESKEY_java_home}
265
266# Make sure meta-data and usage always succeed
267case $__OCF_ACTION in
268meta-data)      jira_meta_data
269                exit $OCF_SUCCESS
270                ;;
271usage|help)     jira_usage
272                exit $OCF_SUCCESS
273                ;;
274esac
275
276# Anything other than meta-data and usage must pass validation
277jira_validate_all || exit $?
278
279# Translate each action into the appropriate function call
280case $__OCF_ACTION in
281start)          jira_start;;
282stop)           jira_stop;;
283status|monitor) jira_monitor;;
284validate-all)   ;;
285*)              jira_usage
286                exit $OCF_ERR_UNIMPLEMENTED
287                ;;
288esac
289rc=$?
290
291exit $rc
292