| 1 |
#!/bin/sh |
| 2 |
# |
| 3 |
# |
| 4 |
# An OCF RA that manages a symlink |
| 5 |
# |
| 6 |
# Copyright (c) 2011 Dominik Klein |
| 7 |
# |
| 8 |
# This program is free software; you can redistribute it and/or modify |
| 9 |
# it under the terms of version 2 of the GNU General Public License as |
| 10 |
# published by the Free Software Foundation. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it would be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 |
# |
| 16 |
# Further, this software is distributed without any warranty that it is |
| 17 |
# free of the rightful claim of any third person regarding infringement |
| 18 |
# or the like. Any license provided herein, whether implied or |
| 19 |
# otherwise, applies only to this software file. Patent licenses, if |
| 20 |
# any, provided herein do not apply to combinations of this program with |
| 21 |
# other software, or any other product whatsoever. |
| 22 |
# |
| 23 |
# You should have received a copy of the GNU General Public License |
| 24 |
# along with this program; if not, write the Free Software Foundation, |
| 25 |
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA. |
| 26 |
# |
| 27 |
|
| 28 |
####################################################################### |
| 29 |
# Initialization: |
| 30 |
|
| 31 |
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat} |
| 32 |
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs |
| 33 |
|
| 34 |
####################################################################### |
| 35 |
|
| 36 |
meta_data() { |
| 37 |
cat <<END |
| 38 |
<?xml version="1.0"?> |
| 39 |
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd"> |
| 40 |
<resource-agent name="symlink" version="1.1"> |
| 41 |
<version>1.0</version> |
| 42 |
|
| 43 |
<longdesc lang="en"> |
| 44 |
This resource agent that manages a symbolic link (symlink). |
| 45 |
|
| 46 |
It is primarily intended to manage configuration files which should be |
| 47 |
enabled or disabled based on where the resource is running, such as |
| 48 |
cron job definitions and the like. |
| 49 |
</longdesc> |
| 50 |
<shortdesc lang="en">Manages a symbolic link</shortdesc> |
| 51 |
<parameters> |
| 52 |
<parameter name="link" required="1"> |
| 53 |
<longdesc lang="en"> |
| 54 |
Full path of the symbolic link to be managed. This must obviously be |
| 55 |
in a filesystem that supports symbolic links. |
| 56 |
</longdesc> |
| 57 |
<shortdesc lang="en">Full path of the symlink</shortdesc> |
| 58 |
<content type="string"/> |
| 59 |
</parameter> |
| 60 |
<parameter name="target" required="1"> |
| 61 |
<longdesc lang="en"> |
| 62 |
Full path to the link target (the file or directory which the symlink points to). |
| 63 |
</longdesc> |
| 64 |
<shortdesc lang="en">Full path to the link target</shortdesc> |
| 65 |
<content type="string" /> |
| 66 |
</parameter> |
| 67 |
<parameter name="backup_suffix"> |
| 68 |
<longdesc lang="en"> |
| 69 |
A suffix to append to any files that the resource agent moves out of |
| 70 |
the way because they clash with "link". |
| 71 |
|
| 72 |
If this is unset (the default), then the resource agent will simply |
| 73 |
refuse to create a symlink if it clashes with an existing file. |
| 74 |
</longdesc> |
| 75 |
<shortdesc lang="en">Suffix to append to backup files</shortdesc> |
| 76 |
<content type="string" /> |
| 77 |
</parameter> |
| 78 |
</parameters> |
| 79 |
<actions> |
| 80 |
<action name="start" timeout="15s" /> |
| 81 |
<action name="stop" timeout="15s" /> |
| 82 |
<action name="monitor" depth="0" timeout="15s" interval="60s"/> |
| 83 |
<action name="meta-data" timeout="5s" /> |
| 84 |
<action name="validate-all" timeout="10s" /> |
| 85 |
</actions> |
| 86 |
</resource-agent> |
| 87 |
END |
| 88 |
} |
| 89 |
|
| 90 |
symlink_monitor() { |
| 91 |
# This applies the following logic: |
| 92 |
# |
| 93 |
# * If $OCF_RESKEY_link does not exist, then the resource is |
| 94 |
# definitely stopped. |
| 95 |
# |
| 96 |
# * If $OCF_RESKEY_link exists and is a symlink that points to |
| 97 |
# ${OCF_RESKEY_target}, then the resource is definitely started. |
| 98 |
# |
| 99 |
# * If $OCF_RESKEY_link exists, but is anything other than a |
| 100 |
# symlink to ${OCF_RESKEY_target}, then the status depends on whether |
| 101 |
# ${OCF_RESKEY_backup_suffix} is set: |
| 102 |
# |
| 103 |
# - if ${OCF_RESKEY_backup_suffix} is set, then the resource is |
| 104 |
# simply not running. The existing file will be moved out of |
| 105 |
# the way, to ${OCF_RESKEY_link}${OCF_RESKEY_backup_suffix}, |
| 106 |
# when the resource starts. |
| 107 |
# |
| 108 |
# - if ${OCF_RESKEY_backup_suffix} is not set, then an existing |
| 109 |
# file ${OCF_RESKEY_link} is an error condition, and the |
| 110 |
# resource can't start here. |
| 111 |
rc=$OCF_ERR_GENERIC |
| 112 |
|
| 113 |
# Using ls here instead of "test -e", as "test -e" returns false |
| 114 |
# if the file does exist, but it a symlink to a file that doesn't |
| 115 |
if ! ls "$OCF_RESKEY_link" >/dev/null 2>&1; then |
| 116 |
ocf_log debug "$OCF_RESKEY_link does not exist" |
| 117 |
rc=$OCF_NOT_RUNNING |
| 118 |
elif [ ! -L "$OCF_RESKEY_link" ]; then |
| 119 |
if [ -z "$OCF_RESKEY_backup_suffix" ]; then |
| 120 |
ocf_exit_reason "$OCF_RESKEY_link exists but is not a symbolic link!" |
| 121 |
exit $OCF_ERR_INSTALLED |
| 122 |
else |
| 123 |
ocf_log debug "$OCF_RESKEY_link exists but is not a symbolic link, will be moved to ${OCF_RESKEY_link}${OCF_RESKEY_backup_suffix} on start" |
| 124 |
rc=$OCF_NOT_RUNNING |
| 125 |
fi |
| 126 |
elif readlink -m "$OCF_RESKEY_link" | $EGREP -q "^$(readlink -m ${OCF_RESKEY_target})$"; then |
| 127 |
ocf_log debug "$OCF_RESKEY_link exists and is a symbolic link to ${OCF_RESKEY_target}." |
| 128 |
rc=$OCF_SUCCESS |
| 129 |
else |
| 130 |
if [ -z "$OCF_RESKEY_backup_suffix" ]; then |
| 131 |
ocf_exit_reason "$OCF_RESKEY_link does not point to ${OCF_RESKEY_target}!" |
| 132 |
exit $OCF_ERR_INSTALLED |
| 133 |
else |
| 134 |
ocf_log debug "$OCF_RESKEY_link does not point to ${OCF_RESKEY_target}, will be moved to ${OCF_RESKEY_link}${OCF_RESKEY_backup_suffix} on start" |
| 135 |
rc=$OCF_NOT_RUNNING |
| 136 |
fi |
| 137 |
fi |
| 138 |
return $rc |
| 139 |
} |
| 140 |
|
| 141 |
symlink_start() { |
| 142 |
if ! symlink_monitor; then |
| 143 |
if [ -e "$OCF_RESKEY_link" ]; then |
| 144 |
if [ -z "$OCF_RESKEY_backup_suffix" ]; then |
| 145 |
# Shouldn't happen, because symlink_monitor should |
| 146 |
# have errored out. But there is a chance that |
| 147 |
# something else put that file there after |
| 148 |
# symlink_monitor ran. |
| 149 |
ocf_exit_reason "$OCF_RESKEY_link exists and no backup_suffix is set, won't overwrite." |
| 150 |
exit $OCF_ERR_GENERIC |
| 151 |
else |
| 152 |
ocf_log debug "Found $OCF_RESKEY_link, moving to ${OCF_RESKEY_link}${OCF_RESKEY_backup_suffix}" |
| 153 |
ocf_run mv -v "$OCF_RESKEY_link" "${OCF_RESKEY_link}${OCF_RESKEY_backup_suffix}" \ |
| 154 |
|| exit $OCF_ERR_GENERIC |
| 155 |
fi |
| 156 |
fi |
| 157 |
ocf_run ln -sv "$OCF_RESKEY_target" "$OCF_RESKEY_link" |
| 158 |
symlink_monitor |
| 159 |
return $? |
| 160 |
else |
| 161 |
return $OCF_SUCCESS |
| 162 |
fi |
| 163 |
} |
| 164 |
|
| 165 |
symlink_stop() { |
| 166 |
if symlink_monitor; then |
| 167 |
ocf_run rm -vf "$OCF_RESKEY_link" || exit $OCF_ERR_GENERIC |
| 168 |
if ! symlink_monitor; then |
| 169 |
if [ -e "${OCF_RESKEY_link}${OCF_RESKEY_backup_suffix}" ]; then |
| 170 |
ocf_log debug "Found backup ${OCF_RESKEY_link}${OCF_RESKEY_backup_suffix}, moving to $OCF_RESKEY_link" |
| 171 |
# if restoring the backup fails then still return with |
| 172 |
# $OCF_SUCCESS, but log a warning |
| 173 |
ocf_run -warn mv "${OCF_RESKEY_link}${OCF_RESKEY_backup_suffix}" "$OCF_RESKEY_link" |
| 174 |
fi |
| 175 |
return $OCF_SUCCESS |
| 176 |
else |
| 177 |
ocf_exit_reason "Removing $OCF_RESKEY_link failed." |
| 178 |
return $OCF_ERR_GENERIC |
| 179 |
fi |
| 180 |
else |
| 181 |
return $OCF_SUCCESS |
| 182 |
fi |
| 183 |
} |
| 184 |
|
| 185 |
symlink_validate_all() { |
| 186 |
if [ "x${OCF_RESKEY_link}" = "x" ]; then |
| 187 |
ocf_exit_reason "Mandatory parameter link is unset" |
| 188 |
exit $OCF_ERR_CONFIGURED |
| 189 |
fi |
| 190 |
if [ "x${OCF_RESKEY_target}" = "x" ]; then |
| 191 |
ocf_exit_reason "Mandatory parameter target is unset" |
| 192 |
exit $OCF_ERR_CONFIGURED |
| 193 |
fi |
| 194 |
|
| 195 |
# Having a non-existant target is technically not an error, as |
| 196 |
# symlinks are allowed to point to non-existant paths. But it |
| 197 |
# still doesn't hurt to warn people if the target does not exist |
| 198 |
# (but only during non-probes). |
| 199 |
if [ ! -e "${OCF_RESKEY_target}" ]; then |
| 200 |
ocf_log warn "${OCF_RESKEY_target} does not exist!" |
| 201 |
fi |
| 202 |
} |
| 203 |
|
| 204 |
symlink_usage() { |
| 205 |
cat <<EOF |
| 206 |
usage: $0 {start|stop|monitor|validate-all|meta-data} |
| 207 |
Expects to have a fully populated OCF RA-compliant environment set. |
| 208 |
EOF |
| 209 |
} |
| 210 |
|
| 211 |
if [ $# -ne 1 ]; then |
| 212 |
symlink_usage |
| 213 |
exit $OCF_ERR_ARGS |
| 214 |
fi |
| 215 |
|
| 216 |
case $__OCF_ACTION in |
| 217 |
meta-data) |
| 218 |
meta_data |
| 219 |
exit $OCF_SUCCESS |
| 220 |
;; |
| 221 |
usage) |
| 222 |
symlink_usage |
| 223 |
exit $OCF_SUCCESS |
| 224 |
esac |
| 225 |
|
| 226 |
# Everything except usage and meta-data must pass the validate test |
| 227 |
symlink_validate_all || exit |
| 228 |
|
| 229 |
case $__OCF_ACTION in |
| 230 |
start) |
| 231 |
symlink_start |
| 232 |
;; |
| 233 |
stop) |
| 234 |
symlink_stop |
| 235 |
;; |
| 236 |
status|monitor) |
| 237 |
symlink_monitor |
| 238 |
;; |
| 239 |
validate-all) |
| 240 |
;; |
| 241 |
*) |
| 242 |
symlink_usage |
| 243 |
exit $OCF_ERR_UNIMPLEMENTED |
| 244 |
esac |
| 245 |
# exit code is the exit code (return code) of the last command (shell function) |