If possible, replace symbolic links in place
[ganeti-local] / doc / examples / ganeti-master-role.ocf.in
1 #!/bin/bash
2 # ganeti master role OCF resource
3 # See http://linux-ha.org/wiki/OCF_Resource_Agents
4
5 set -e -u
6
7 @SHELL_ENV_INIT@
8
9 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
10
11 SCRIPTNAME="@LIBDIR@/ocf/resource.d/ganeti/ganeti-master-role"
12
13 # Master candidates list file
14 MCFILE="$DATA_DIR/ssconf_master_candidates"
15
16 # We'll need the hostname in a few places, so we'll get it once, now.
17 MYHOSTNAME=$(hostname --fqdn)
18
19 is_master() {
20   local -r master=$(gnt-cluster getmaster)
21   [[ "$MYHOSTNAME" == "$master" ]]
22 }
23
24 is_candidate() {
25   grep -Fx $MYHOSTNAME $MCFILE
26 }
27
28 start_action() {
29   if is_master; then
30     exit 0
31   elif is_candidate; then
32     gnt-cluster master-failover || exit 1 # OCF_ERR_GENERIC
33   else
34     exit 5 # OCF_ERR_INSTALLED (vital component missing)
35   fi
36 }
37
38 stop_action() {
39   # We can't really "stop" being a master.
40   # TODO: investigate whether a fake approach will do.
41   exit 1 # OCF_ERR_GENERIC
42 }
43
44 recover_action() {
45   if is_master; then
46     gnt-cluster redist-conf || exit 1 # OCF_ERR_GENERIC
47   elif is_candidate; then
48     gnt-cluster master-failover || exit 1 # OCF_ERR_GENERIC
49   else
50     exit 5 # OCF_ERR_INSTALLED (vital component missing)
51   fi
52 }
53
54 monitor_action() {
55   # monitor should exit:
56   #    7 if the resource is not running
57   #    1 if it failed
58   #    0 if it's running
59   if is_master; then
60     exit 0
61   elif is_candidate; then
62     exit 7 # OCF_NOT_RUNNING
63   else
64     exit 5 # OCF_ERR_INSTALLED (vital component missing)
65   fi
66 }
67
68 return_meta() {
69 cat <<END
70 <?xml version="1.0"?>
71 <!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
72 <resource-agent name="ganeti-master-role" version="0.1">
73 <version>0.1</version>
74 <longdesc lang="en">
75 OCF script to manage the ganeti master role in a cluster.
76
77 Can be used to failover the ganeti master between master candidate nodes.
78 </longdesc>
79 <shortdesc lang="en">Manages the ganeti cluster master</shortdesc>
80
81 <parameters/>
82 <actions>
83 <action name="start" timeout="300s" />
84 <action name="stop" timeout="50s" />
85 <action name="monitor" depth="0" timeout="10s" interval="30s" />
86 <action name="meta-data" timeout="5s" />
87 <action name="recover" timeout="20s" />
88 <action name="reload" timeout="5s" />
89 </actions>
90 </resource-agent>
91 END
92 exit 0
93 }
94
95 case "$1" in
96   # Mandatory OCF commands
97   start)
98     start_action
99     ;;
100   stop)
101     stop_action
102     ;;
103   monitor)
104     monitor_action
105     ;;
106   meta-data)
107     return_meta
108     ;;
109   # Optional OCF commands
110   recover)
111     recover_action
112     ;;
113   reload)
114     # The ganeti master role has no "configuration" that is reloadable on
115     # the pacemaker side. We declare the operation anyway to make sure
116     # pacemaker doesn't decide to stop and start the service needlessly.
117     exit 0
118     ;;
119   promote|demote|migrate_to|migrate_from|validate-all)
120     # Not implemented (nor declared by meta-data)
121     exit 3 # OCF_ERR_UNIMPLEMENTED
122     ;;
123   *)
124     log_success_msg "Usage: $SCRIPTNAME {start|stop|monitor|meta-data|recover|reload}"
125     exit 1
126     ;;
127 esac
128
129 exit 0