#!/bin/sh
#
# OCF resource agent for mapping and unmapping
# @author zhangtianjiong
#
# Initialization:
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
# Convenience variables
# When sysconfdir isn't passed in as a configure flag,
# it's defined in terms of prefix
prefix=@prefix@
# Defaults
OCF_RESKEY_tgt_ip_default="localhost"
: ${OCF_RESKEY_tgt_ip=${OCF_RESKEY_tgt_ip_default}}
iscsi_meta_data() {
cat <<EOF
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="iscsi" version="0.1">
<version>0.1</version>
<longdesc>
Manages tgt device as a highly available
resource. Login and logout stg as needed.
</longdesc>
<shortdesc>Maps and unmaps stg Devices</shortdesc>
<parameters>
<parameter name="tgt_name" unique="0" required="1">
<longdesc>
Name of the Tgt device.
</longdesc>
<shortdesc>tgt device name</shortdesc>
<content type="string"/>
</parameter>
<parameter name="tgt_ip" unique="0" required="0">
<longdesc>
Ip of the Tgt service.
</longdesc>
<shortdesc>tgt ip</shortdesc>
<content type="string"/>
</parameter>
</parameters>
<actions>
<action name="start" timeout="20" />
<action name="stop" timeout="20" />
<action name="monitor" timeout="20"
interval="10" depth="0" />
<action name="meta-data" timeout="5" />
<action name="validate-all" timeout="20" />
</actions>
</resource-agent>
EOF
}
iscsi_usage() {
cat <<EOF
usage: $0 {start|stop|status|monitor|validate-all|meta-data}
Expects to have a fully populated OCF RA-compliant environment set.
EOF
}
iscsi_validate_all() {
if [ -z "${OCF_RESKEY_tgt_name}" ]; then
ocf_log err 'Required parameter "tgt_name" is unset!'
exit $OCF_ERR_CONFIGURED
fi
return $OCF_SUCCESS
}
iscsi_monitor() {
local rc
local tgt_name_session
if [ ! -d /etc/iscsi ]; then
ocf_log info "Iscsi intiator is not installed"
exit $OCF_ERR_INSTALLED
fi
tgt_name_session=$(iscsiadm -m session | grep ${OCF_RESKEY_tgt_name})
ocf_log info "${tgt_name_session}"
if [ -z "${tgt_name_session}" ]; then
ocf_log info "$tgt_name_session ---tgt device is unlogin"
rc=$OCF_NOT_RUNNING
elif [ -n "${tgt_name_session}" ]; then
ocf_log info "tgt device is login"
rc=$OCF_SUCCESS
else
ocf_log err "unknow error type!"
rc=$OCF_ERR_GENERIC
fi
return $rc
}
iscsi_start() {
local scsiadm_option
# if resource is already running, bail out early
if iscsi_monitor; then
ocf_log info "Resource is already running"
return $OCF_SUCCESS
fi
# if iscsi-initiator not install
if [ ! -d /etc/iscsi ]; then
ocf_log info "Iscsi intiator is not installed"
exit $OCF_ERR_INSTALLED
fi
if [ -n "${OCF_RESKEY_tgt_name}" ]; then
scsiadm_option="-T ${OCF_RESKEY_tgt_name}"
fi
if [ -n "${OCF_RESKEY_tgt_ip}" ]; then
scsiadm_option="$scsiadm_option -l -p ${OCF_RESKEY_tgt_ip}"
fi
ocf_log info "iscsi:$scsiadm_option"
ocf_run iscsiadm -m node $scsiadm_option || exit $OCF_ERR_GENERIC
while ! iscsi_monitor; do
ocf_log debug "Resource has not started yet, waiting"
sleep 1
done
# only return $OCF_SUCCESS if _everything_ succeeded as expected
return $OCF_SUCCESS
}
iscsi_stop() {
local rc
iscsi_monitor
rc=$?
case "$rc" in
"$OCF_SUCCESS")
# Currently running. Normal, expected behavior.
ocf_log debug "Resource is currently running"
;;
"$OCF_NOT_RUNNING")
# Currently not running. Nothing to do.
ocf_log info "Resource is already stopped"
return $OCF_SUCCESS
;;
esac
# actually shut down the resource here (make sure to immediately
# exit with an $OCF_ERR_ error code if anything goes seriously
# wrong)
ocf_run iscsiadm -m node -T $OCF_RESKEY_tgt_name --logout || exit $OCF_ERR_GENERIC
while iscsi_monitor; do
ocf_log debug "Resource has not stopped yet, waiting"
sleep 1
done
# only return $OCF_SUCCESS if _everything_ succeeded as expected
return $OCF_SUCCESS
}
# Make sure meta-data and usage always succeed
case $__OCF_ACTION in
meta-data) iscsi_meta_data
exit $OCF_SUCCESS
;;
usage|help) iscsi_usage
exit $OCF_SUCCESS
;;
esac
# Anything other than meta-data and usage must pass validation
iscsi_validate_all || exit $?
# Translate each action into the appropriate function call
case $__OCF_ACTION in
start) iscsi_start;;
stop) iscsi_stop;;
status|monitor) iscsi_monitor;;
validate-all) ;;
*) iscsi_usage
exit $OCF_ERR_UNIMPLEMENTED
;;
esac
rc=$?
# The resource agent may optionally log a debug message
ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION returned $rc"
exit $rc
转载于:https://my.oschina.net/hanhanztj/blog/475365