4
# An OCF RA that manages a symlink
6
# Copyright (c) 2011 Dominik Klein
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.
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.
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.
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.
28
#######################################################################
31
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
32
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
34
#######################################################################
39
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
40
<resource-agent name="symlink">
41
<version>1.1</version>
44
This resource agent that manages a symbolic link (symlink).
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.
50
<shortdesc lang="en">Manages a symbolic link</shortdesc>
52
<parameter name="link" required="1">
54
Full path of the symbolic link to be managed. This must obviously be
55
in a filesystem that supports symbolic links.
57
<shortdesc lang="en">Full path of the symlink</shortdesc>
58
<content type="string"/>
60
<parameter name="target" required="1">
62
Full path to the link target (the file or directory which the symlink points to).
64
<shortdesc lang="en">Full path to the link target</shortdesc>
65
<content type="string" />
67
<parameter name="backup_suffix">
69
A suffix to append to any files that the resource agent moves out of
70
the way because they clash with "link".
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.
75
<shortdesc lang="en">Suffix to append to backup files</shortdesc>
76
<content type="string" />
80
<action name="start" timeout="15" />
81
<action name="stop" timeout="15" />
82
<action name="monitor" depth="0" timeout="15" interval="60"/>
83
<action name="meta-data" timeout="5" />
84
<action name="validate-all" timeout="10" />
91
# This applies the following logic:
93
# * If $OCF_RESKEY_link does not exist, then the resource is
96
# * If $OCF_RESKEY_link exists and is a symlink that points to
97
# ${OCF_RESKEY_target}, then the resource is definitely started.
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:
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.
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.
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"
118
elif [ ! -L "$OCF_RESKEY_link" ]; then
119
if [ -z "$OCF_RESKEY_backup_suffix" ]; then
120
ocf_log err "$OCF_RESKEY_link exists but is not a symbolic link!"
121
exit $OCF_ERR_INSTALLED
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"
126
elif readlink -f "$OCF_RESKEY_link" | egrep -q "^${OCF_RESKEY_target}$"; then
127
ocf_log debug "$OCF_RESKEY_link exists and is a symbolic link to ${OCF_RESKEY_target}."
130
if [ -z "$OCF_RESKEY_backup_suffix" ]; then
131
ocf_log err "$OCF_RESKEY_link does not point to ${OCF_RESKEY_target}!"
132
exit $OCF_ERR_INSTALLED
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"
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_log err "$OCF_RESKEY_link exists and no backup_suffix is set, won't overwrite."
150
exit $OCF_ERR_GENERIC
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
157
ocf_run ln -sv "$OCF_RESKEY_target" "$OCF_RESKEY_link"
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"
177
ocf_log err "Removing $OCF_RESKEY_link failed."
178
return $OCF_ERR_GENERIC
185
symlink_validate_all() {
186
if [ "x${OCF_RESKEY_link}" = "x" ]; then
187
ocf_log err "Mandatory parameter link is unset"
188
exit $OCF_ERR_CONFIGURED
190
if [ "x${OCF_RESKEY_target}" = "x" ]; then
191
ocf_log err "Mandatory parameter target is unset"
192
exit $OCF_ERR_CONFIGURED
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!"
206
usage: $0 {start|stop|monitor|validate-all|meta-data}
207
Expects to have a fully populated OCF RA-compliant environment set.
211
if [ $# -ne 1 ]; then
216
case $__OCF_ACTION in
226
# Everything except usage and meta-data must pass the validate test
227
symlink_validate_all || exit
229
case $__OCF_ACTION in
243
exit $OCF_ERR_UNIMPLEMENTED
245
# exit code is the exit code (return code) of the last command (shell function)