Debian Codegraph @

heartbeat/ocivip

raw
1 #!/bin/sh
2 #
3 #
4 # Manage Secondary Private IP in Oracle Cloud Infrastructure with Pacemaker
5 #
6 #
7 # Copyright 2016-2018 Lorenzo Garuti <[email protected]>
8 #
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file except in compliance with the License.
11 # You may obtain a copy of the License at
12 #
13 # http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20 #
21 #
22
23 #
24 # Prerequisites:
25 #
26 # - OCI CLI installed (https://docs.oracle.com/en-us/iaas/Content/API/SDKDocs/climanualinst.htm)
27 # - jq installed
28 # - dynamic group with a policy attacched
29 # - the policy must have this statement:
30 # allow dynamic-group <GROUP_NAME> to use virtual-network-family in compartment id <COMPARTMENT_ID>
31 # - a reserved secondary private IP address for Compute Instances high availability
32 #
33
34 #######################################################################
35 # Initialization:
36
37 : ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
38 . ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
39
40 #######################################################################
41
42 #
43 # Defaults
44 #
45 OCF_RESKEY_ocicli_default="/usr/local/bin/oci"
46 OCF_RESKEY_api_delay_default="3"
47 OCF_RESKEY_cidr_netmask_default="24"
48 OCF_RESKEY_interface_alias_default="0"
49 export OCI_CLI_AUTH=instance_principal
50
51 : ${OCF_RESKEY_ocicli=${OCF_RESKEY_ocicli_default}}
52 : ${OCF_RESKEY_api_delay=${OCF_RESKEY_api_delay_default}}
53 : ${OCF_RESKEY_cidr_netmask=${OCF_RESKEY_cidr_netmask_default}}
54 : ${OCF_RESKEY_interface_alias=${OCF_RESKEY_interface_alias_default}}
55
56 meta_data() {
57 cat <<END
58 <?xml version="1.0"?>
59 <!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
60 <resource-agent name="ocivip">
61 <version>1.0</version>
62
63 <longdesc lang="en">
64 Resource Agent for OCI Compute instance Secondary Private IP Addresses.
65
66 It manages OCI Secondary Private IP Addresses for Compute instances with oci cli.
67
68 See https://docs.oracle.com/en-us/iaas/Content/API/Concepts/cliconcepts.htm for more information about oci cli.
69
70 Prerequisites:
71
72 - OCI CLI installed (https://docs.oracle.com/en-us/iaas/Content/API/SDKDocs/climanualinst.htm)
73 - jq installed
74 - dynamic group with a policy attacched
75 - the policy must have this statement: allow dynamic-group GROUP_NAME to use virtual-network-family in compartment id COMPARTMENT_ID
76 - a reserved secondary private IP address for Compute Instances high availability
77
78 </longdesc>
79 <shortdesc lang="en">OCI Secondary Private IP Address for Compute instances Resource Agent</shortdesc>
80
81 <parameters>
82
83 <parameter name="ocicli" unique="0">
84 <longdesc lang="en">
85 OCI Command line interface (CLI) tools
86 </longdesc>
87 <shortdesc lang="en">OCI cli tools</shortdesc>
88 <content type="string" default="${OCF_RESKEY_ocicli_default}" />
89 </parameter>
90
91 <parameter name="secondary_private_ip" unique="1" required="1">
92 <longdesc lang="en">
93 reserved secondary private ip for compute instance
94 </longdesc>
95 <shortdesc lang="en">reserved secondary private ip for compute instance</shortdesc>
96 <content type="string" default="" />
97 </parameter>
98
99 <parameter name="cidr_netmask" unique="0">
100 <longdesc lang="en">
101 netmask for the secondary_private_ip
102 </longdesc>
103 <shortdesc lang="en">netmask for the secondary_private_ip</shortdesc>
104 <content type="integer" default="${OCF_RESKEY_cidr_netmask_default}" />
105 </parameter>
106
107 <parameter name="interface_alias" unique="0">
108 <longdesc lang="en">
109 numeric alias for the interface
110 </longdesc>
111 <shortdesc lang="en">numeric alias for the interface</shortdesc>
112 <content type="integer" default="${OCF_RESKEY_interface_alias_default}" />
113 </parameter>
114
115 <parameter name="api_delay" unique="0">
116 <longdesc lang="en">
117 a short delay between API calls, to avoid sending API too quick
118 </longdesc>
119 <shortdesc lang="en">a short delay between API calls</shortdesc>
120 <content type="integer" default="${OCF_RESKEY_api_delay_default}" />
121 </parameter>
122
123 </parameters>
124
125 <actions>
126 <action name="start" timeout="30s" />
127 <action name="stop" timeout="30s" />
128 <action name="monitor" timeout="30s" interval="20s" depth="0" />
129 <action name="migrate_to" timeout="30s" />
130 <action name="migrate_from" timeout="30s" />
131 <action name="meta-data" timeout="5s" />
132 <action name="validate" timeout="10s" />
133 <action name="validate-all" timeout="10s" />
134 </actions>
135 </resource-agent>
136 END
137 }
138
139 #######################################################################
140
141 ocivip_usage() {
142 cat <<END
143 usage: $0 {start|stop|monitor|migrate_to|migrate_from|validate|validate-all|meta-data}
144
145 Expects to have a fully populated OCF RA-compliant environment set.
146 END
147 }
148
149 ocivip_start() {
150 ocivip_monitor && return $OCF_SUCCESS
151
152 $OCICLI network vnic assign-private-ip --vnic-id $VNIC_ID \
153 --unassign-if-already-assigned \
154 --ip-address ${SECONDARY_PRIVATE_IP}
155 RETOCI=$?
156 ip addr add ${SECONDARY_PRIVATE_IP}/${CIDR_NETMASK} dev ${PRIMARY_IFACE} label ${PRIMARY_IFACE}:${INTERFACE_ALIAS}
157 RETIP=$?
158
159 # delay to avoid sending request too fast
160 sleep ${OCF_RESKEY_api_delay}
161
162 if [ $RETOCI -ne 0 ] || [ $RETIP -ne 0 ]; then
163 return $OCF_NOT_RUNNING
164 fi
165
166 ocf_log info "secondary_private_ip has been successfully brought up (${SECONDARY_PRIVATE_IP})"
167 return $OCF_SUCCESS
168 }
169
170 ocivip_stop() {
171 ocivip_monitor || return $OCF_SUCCESS
172
173 $OCICLI network vnic unassign-private-ip --vnic-id $VNIC_ID \
174 --ip-address ${SECONDARY_PRIVATE_IP}
175 RETOCI=$?
176 ip addr del ${SECONDARY_PRIVATE_IP}/${CIDR_NETMASK} dev ${PRIMARY_IFACE}:${INTERFACE_ALIAS}
177 RETIP=$?
178
179 # delay to avoid sending request too fast
180 sleep ${OCF_RESKEY_api_delay}
181
182 if [ $RETOCI -ne 0 ] || [ $RETIP -ne 0 ]; then
183 return $OCF_NOT_RUNNING
184 fi
185
186 ocf_log info "secondary_private_ip has been successfully brought down (${SECONDARY_PRIVATE_IP})"
187 return $OCF_SUCCESS
188 }
189
190 ocivip_monitor() {
191 $OCICLI network private-ip list --vnic-id $VNIC_ID | grep -q "${SECONDARY_PRIVATE_IP}"
192 RETOCI=$?
193
194 if [ $RETOCI -ne 0 ]; then
195 return $OCF_NOT_RUNNING
196 fi
197 return $OCF_SUCCESS
198 }
199
200 ocivip_validate() {
201 check_binary ${OCICLI}
202 check_binary jq
203
204 if [ -z "${VNIC_ID}" ]; then
205 ocf_exit_reason "vnic_id not found. Is this a Compute instance?"
206 return $OCF_ERR_GENERIC
207 fi
208
209 return $OCF_SUCCESS
210 }
211
212 case $__OCF_ACTION in
213 meta-data)
214 meta_data
215 exit $OCF_SUCCESS
216 ;;
217 esac
218
219 OCICLI="${OCF_RESKEY_ocicli}"
220 SECONDARY_PRIVATE_IP="${OCF_RESKEY_secondary_private_ip}"
221 CIDR_NETMASK="${OCF_RESKEY_cidr_netmask}"
222 INTERFACE_ALIAS="${OCF_RESKEY_interface_alias}"
223 VNIC_ID="$(curl -s -H "Authorization: Bearer Oracle" -L http://169.254.169.254/opc/v2/vnics/ | jq -r '.[0].vnicId')"
224 PRIMARY_IFACE=$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)' | head -n1)
225
226 case $__OCF_ACTION in
227 start)
228 ocivip_validate || exit $?
229 ocivip_start
230 ;;
231 stop)
232 ocivip_stop
233 ;;
234 monitor)
235 ocivip_monitor
236 ;;
237 migrate_to)
238 ocf_log info "Migrating ${OCF_RESOURCE_INSTANCE} to ${OCF_RESKEY_CRM_meta_migrate_target}."
239 ocivip_stop
240 ;;
241 migrate_from)
242 ocf_log info "Migrating ${OCF_RESOURCE_INSTANCE} from ${OCF_RESKEY_CRM_meta_migrate_source}."
243 ocivip_start
244 ;;
245 reload)
246 ocf_log info "Reloading ${OCF_RESOURCE_INSTANCE} ..."
247 ;;
248 validate|validate-all)
249 ocivip_validate
250 ;;
251 usage|help)
252 ocivip_usage
253 exit $OCF_SUCCESS
254 ;;
255 *)
256 ocivip_usage
257 exit $OCF_ERR_UNIMPLEMENTED
258 ;;
259 esac
260
261 rc=$?
262 ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc"
263 exit $rc