| 1 |
#!/bin/sh |
| 2 |
# |
| 3 |
# |
| 4 |
# OCF resource agent to move a virtual address in an Openstack tenant. |
| 5 |
# |
| 6 |
# Copyright (c) 2018 Mathieu GRZYBEK |
| 7 |
# Based on code of Markus Guertler |
| 8 |
# All Rights Reserved. |
| 9 |
# |
| 10 |
# This program is free software; you can redistribute it and/or modify |
| 11 |
# it under the terms of version 2 of the GNU General Public License as |
| 12 |
# published by the Free Software Foundation. |
| 13 |
# |
| 14 |
# This program is distributed in the hope that it would be useful, but |
| 15 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 17 |
# |
| 18 |
# Further, this software is distributed without any warranty that it is |
| 19 |
# free of the rightful claim of any third person regarding infringement |
| 20 |
# or the like. Any license provided herein, whether implied or |
| 21 |
# otherwise, applies only to this software file. Patent licenses, if |
| 22 |
# any, provided herein do not apply to combinations of this program with |
| 23 |
# other software, or any other product whatsoever. |
| 24 |
# |
| 25 |
# You should have received a copy of the GNU General Public License |
| 26 |
# along with this program; if not, write the Free Software Foundation, |
| 27 |
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA. |
| 28 |
# |
| 29 |
|
| 30 |
|
| 31 |
####################################################################### |
| 32 |
# Initialization: |
| 33 |
|
| 34 |
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat} |
| 35 |
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs |
| 36 |
|
| 37 |
. ${OCF_FUNCTIONS_DIR}/openstack-common.sh |
| 38 |
|
| 39 |
# Defaults |
| 40 |
|
| 41 |
####################################################################### |
| 42 |
|
| 43 |
|
| 44 |
USAGE="usage: $0 {start|stop|status|meta-data}"; |
| 45 |
############################################################################### |
| 46 |
|
| 47 |
|
| 48 |
############################################################################### |
| 49 |
# |
| 50 |
# Functions |
| 51 |
# |
| 52 |
############################################################################### |
| 53 |
|
| 54 |
|
| 55 |
metadata() { |
| 56 |
cat <<END |
| 57 |
<?xml version="1.0"?> |
| 58 |
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd"> |
| 59 |
<resource-agent name="openstack-virtual-ip" version="2.0"> |
| 60 |
<version>1.0</version> |
| 61 |
<longdesc lang="en"> |
| 62 |
Resource Agent to move a virtual IP address from an instance to another one |
| 63 |
by adding an allowed-address pair associated with an instance port. |
| 64 |
It relies on attributes given by openstack-info resource agent (openstack_ports, openstack_id attributes). |
| 65 |
The attribute called "openstack_virtual_ip" is updated. |
| 66 |
</longdesc> |
| 67 |
<shortdesc lang="en">Move a virtual IP</shortdesc> |
| 68 |
|
| 69 |
<parameters> |
| 70 |
END |
| 71 |
|
| 72 |
common_meta_data |
| 73 |
|
| 74 |
cat <<END |
| 75 |
<parameter name="ip" required="1"> |
| 76 |
<longdesc lang="en"> |
| 77 |
Virtual IP Address. |
| 78 |
</longdesc> |
| 79 |
<shortdesc lang="en">IP Address</shortdesc> |
| 80 |
<content type="string" /> |
| 81 |
</parameter> |
| 82 |
|
| 83 |
<parameter name="subnet_id" required="1"> |
| 84 |
<longdesc lang="en"> |
| 85 |
Subnet Identifier to use to attach the address. |
| 86 |
</longdesc> |
| 87 |
<shortdesc lang="en">Subnet ID</shortdesc> |
| 88 |
<content type="string" /> |
| 89 |
</parameter> |
| 90 |
|
| 91 |
</parameters> |
| 92 |
|
| 93 |
<actions> |
| 94 |
<action name="start" timeout="180s" /> |
| 95 |
<action name="stop" timeout="180s" /> |
| 96 |
<action name="monitor" depth="0" timeout="180s" interval="60s" /> |
| 97 |
<action name="validate-all" timeout="5s" /> |
| 98 |
<action name="meta-data" timeout="5s" /> |
| 99 |
</actions> |
| 100 |
</resource-agent> |
| 101 |
END |
| 102 |
} |
| 103 |
|
| 104 |
osvip_port_id() { |
| 105 |
# Get port_id from subnet_id |
| 106 |
node_port_ids=$(${HA_SBIN_DIR}/attrd_updater --query -n openstack_ports -N $(crm_node -n) \ |
| 107 |
| awk '{gsub("value=","") ; gsub("\"","") ; print $NF}') |
| 108 |
|
| 109 |
node_port_id=$(echo $node_port_ids \ |
| 110 |
| tr ',' '\n' \ |
| 111 |
| awk -F: "/$OCF_RESKEY_subnet_id/ {print \$2}") |
| 112 |
|
| 113 |
echo ${node_port_id} |
| 114 |
} |
| 115 |
|
| 116 |
osvip_validate() { |
| 117 |
check_binary "$OCF_RESKEY_openstackcli" |
| 118 |
|
| 119 |
get_config |
| 120 |
|
| 121 |
${HA_SBIN_DIR}/attrd_updater --query -n openstack_ports -N $(crm_node -n) > /dev/null 2>&1 |
| 122 |
if [ $? -ne 0 ] && ! ocf_is_probe; then |
| 123 |
ocf_log warn "attr_updater failed to get openstack_ports attribute of node $OCF_RESOURCE_INSTANCE" |
| 124 |
return $OCF_ERR_GENERIC |
| 125 |
fi |
| 126 |
|
| 127 |
return $OCF_SUCCESS |
| 128 |
} |
| 129 |
|
| 130 |
osvip_monitor() { |
| 131 |
local result |
| 132 |
|
| 133 |
node_port_id=$(osvip_port_id) |
| 134 |
|
| 135 |
result=$(run_openstackcli "port show \ |
| 136 |
--format value \ |
| 137 |
--column allowed_address_pairs \ |
| 138 |
${node_port_id}") |
| 139 |
if echo "$result" | grep -q "$OCF_RESKEY_ip"; then |
| 140 |
${HA_SBIN_DIR}/attrd_updater ${OCF_RESKEY_delay} -n openstack_virtual_ip -v $OCF_RESKEY_ip |
| 141 |
|
| 142 |
return $OCF_SUCCESS |
| 143 |
fi |
| 144 |
|
| 145 |
${HA_SBIN_DIR}/attrd_updater ${OCF_RESKEY_delay} -D -n openstack_virtual_ip |
| 146 |
ocf_log warn "$OCF_RESKEY_ip is not attached to any fixed address" |
| 147 |
return $OCF_NOT_RUNNING |
| 148 |
} |
| 149 |
|
| 150 |
osvip_stop() { |
| 151 |
node_port_id=$(osvip_port_id) |
| 152 |
|
| 153 |
ocf_log info "Bringing down IP address $OCF_RESKEY_ip" |
| 154 |
|
| 155 |
osvip_monitor |
| 156 |
if [ $? = $OCF_NOT_RUNNING ]; then |
| 157 |
ocf_log info "Address $OCF_RESKEY_ip already down" |
| 158 |
return $OCF_SUCCESS |
| 159 |
fi |
| 160 |
|
| 161 |
mac_address=$(run_openstackcli "port show \ |
| 162 |
--format value \ |
| 163 |
--column mac_address \ |
| 164 |
$node_port_id") |
| 165 |
echo "${mac_address}" | grep -q -P "^([0-9a-f]{2}:){5}[0-9a-f]{2}$" |
| 166 |
if [ $? -ne 0 ]; then |
| 167 |
ocf_log error "MAC address '${mac_address}' is not valid." |
| 168 |
return $OCF_ERR_GENERIC |
| 169 |
fi |
| 170 |
|
| 171 |
if ! run_openstackcli "port unset \ |
| 172 |
--allowed-address \ |
| 173 |
ip-address=$OCF_RESKEY_ip,mac-address=${mac_address} \ |
| 174 |
$node_port_id"; then |
| 175 |
return $OCF_ERR_GENERIC |
| 176 |
fi |
| 177 |
|
| 178 |
osvip_monitor |
| 179 |
if [ $? != $OCF_NOT_RUNNING ]; then |
| 180 |
ocf_log error "Couldn't unset IP address $OCF_RESKEY_ip." |
| 181 |
return $OCF_ERR_GENERIC |
| 182 |
fi |
| 183 |
|
| 184 |
ocf_log info "Successfully brought down $OCF_RESKEY_ip" |
| 185 |
return $OCF_SUCCESS |
| 186 |
} |
| 187 |
|
| 188 |
osvip_start() { |
| 189 |
node_port_id=$(osvip_port_id) |
| 190 |
|
| 191 |
osvip_monitor |
| 192 |
if [ $? = $OCF_SUCCESS ]; then |
| 193 |
ocf_log info "$OCF_RESKEY_ip already started" |
| 194 |
return $OCF_SUCCESS |
| 195 |
fi |
| 196 |
|
| 197 |
ocf_log info "Moving IP address $OCF_RESKEY_ip to port ID $node_port_id" |
| 198 |
|
| 199 |
run_openstackcli "port set \ |
| 200 |
--allowed-address ip-address=$OCF_RESKEY_ip \ |
| 201 |
$node_port_id" |
| 202 |
if [ $? != $OCF_SUCCESS ]; then |
| 203 |
ocf_log error "$OCF_RESKEY_ip Cannot be set to port $node_port_id" |
| 204 |
return $OCF_ERR_GENERIC |
| 205 |
fi |
| 206 |
|
| 207 |
osvip_monitor |
| 208 |
if [ $? != $OCF_SUCCESS ]; then |
| 209 |
ocf_log error "$OCF_RESKEY_ip Cannot be set to port $node_port_id" |
| 210 |
return $OCF_ERR_GENERIC |
| 211 |
fi |
| 212 |
|
| 213 |
ocf_log info "Successfully brought up $OCF_RESKEY_ip" |
| 214 |
return $OCF_SUCCESS |
| 215 |
} |
| 216 |
|
| 217 |
############################################################################### |
| 218 |
# |
| 219 |
# MAIN |
| 220 |
# |
| 221 |
############################################################################### |
| 222 |
|
| 223 |
case $__OCF_ACTION in |
| 224 |
meta-data) |
| 225 |
metadata |
| 226 |
exit $OCF_SUCCESS |
| 227 |
;; |
| 228 |
usage|help) |
| 229 |
echo $USAGE |
| 230 |
exit $OCF_SUCCESS |
| 231 |
;; |
| 232 |
esac |
| 233 |
|
| 234 |
if ! ocf_is_root; then |
| 235 |
ocf_log err "You must be root for $__OCF_ACTION operation." |
| 236 |
exit $OCF_ERR_PERM |
| 237 |
fi |
| 238 |
|
| 239 |
case $__OCF_ACTION in |
| 240 |
start) |
| 241 |
osvip_validate || exit $? |
| 242 |
osvip_start;; |
| 243 |
stop) |
| 244 |
osvip_validate || exit $? |
| 245 |
osvip_stop;; |
| 246 |
monitor) |
| 247 |
osvip_validate || exit $? |
| 248 |
osvip_monitor;; |
| 249 |
validate-all) |
| 250 |
osvip_validate |
| 251 |
;; |
| 252 |
*) |
| 253 |
echo $USAGE |
| 254 |
exit $OCF_ERR_UNIMPLEMENTED |
| 255 |
;; |
| 256 |
esac |
| 257 |
|
| 258 |
exit $? |