summaryrefslogtreecommitdiffstats
path: root/heartbeat/aliyun-vpc-move-ip
blob: 1a3a1a014c200a3782933d4a0a9531f2d2dba3cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/bin/sh
#
# OCF resource agent to move an IP address within a VPC in the Aliyun
# Based on code of Markus Guertler (GitHub AWS-VPC-move-IP)
# Based on code of Adam Gandelman (GitHub ec2-resource-agents/elasticip)
#

#######################################################################
# Initialization:
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

# Parameter defaults

OCF_RESKEY_address_default=""
OCF_RESKEY_routing_table_default=""
OCF_RESKEY_interface_default="eth0"
OCF_RESKEY_profile_default="default"
OCF_RESKEY_endpoint_default="vpc.aliyuncs.com"
OCF_RESKEY_aliyuncli_default="detect"


: ${OCF_RESKEY_address=${OCF_RESKEY_address_default}}
: ${OCF_RESKEY_routing_table=${OCF_RESKEY_routing_table_default}}
: ${OCF_RESKEY_interface=${OCF_RESKEY_interface_default}}
: ${OCF_RESKEY_profile=${OCF_RESKEY_profile_default}}
: ${OCF_RESKEY_endpoint=${OCF_RESKEY_endpoint_default}}
: ${OCF_RESKEY_aliyuncli=${OCF_RESKEY_aliyuncli_default}}

#######################################################################

# aliyun cli doesnt work without HOME parameter
export HOME="/root"

USAGE="usage: $0 {start|stop|status|meta-data}";

if [ "${OCF_RESKEY_aliyuncli}" = "detect" ]; then
	OCF_RESKEY_aliyuncli="$(which aliyuncli 2> /dev/null || which aliyun 2> /dev/null)"
fi

if [ "${OCF_RESKEY_aliyuncli##*/}" = 'aliyuncli' ]; then
	OUTPUT="text"
	EXECUTING='{ print $3 }'
	IFS_=" "
	ENDPOINT=""
elif [ "${OCF_RESKEY_aliyuncli##*/}" = 'aliyun' ]; then
	OUTPUT="table cols=InstanceId,DestinationCidrBlock rows=RouteTables.RouteTable[].RouteEntrys.RouteEntry[]"
	EXECUTING='{ gsub (" ", "", $0); print $1 }'
	IFS_="|"
	ENDPOINT="--endpoint $OCF_RESKEY_endpoint"
fi
###############################################################################


###############################################################################
#
# Functions
#
###############################################################################

request_create_route_entry() {
	cmd="${OCF_RESKEY_aliyuncli} vpc CreateRouteEntry --RouteTableId $OCF_RESKEY_routing_table --DestinationCidrBlock ${OCF_RESKEY_address}/32 --NextHopId $ECS_INSTANCE_ID --NextHopType Instance ${ENDPOINT}"
	ocf_log debug "executing command: $cmd"
	res=$($cmd  2>&1)
	rc=$?
	if [ $rc -eq 0 ]
	then
		ocf_log debug "result: $res; rc: $rc"
	else
		ocf_log err "result: $res; cmd: $cmd; rc: $rc"
	fi
	return $rc
}

request_delete_route_entry() {
	cmd="${OCF_RESKEY_aliyuncli} vpc DeleteRouteEntry --RouteTableId $OCF_RESKEY_routing_table --DestinationCidrBlock ${OCF_RESKEY_address}/32 --NextHopId $ROUTE_TO_INSTANCE ${ENDPOINT}"
	ocf_log debug "executing command: $cmd"
	res=$($cmd)
	rc=$?
	if [ $rc -eq 0 ]
	then
		ocf_log debug "result: $res; rc: $rc"
	else
		ocf_log err "result: $res; cmd: $cmd; rc: $rc"
	fi
	return $rc
}

request_describe_route_tables() {
	cmd="${OCF_RESKEY_aliyuncli} vpc DescribeRouteTables --RouteTableId $OCF_RESKEY_routing_table --output ${OUTPUT} ${ENDPOINT}"
	ocf_log debug "executing command: $cmd"
	res=$($cmd)
	rc=$?
	if [ $rc -eq 0 ]
	then
		ROUTE_TO_INSTANCE=$(echo "$res" |grep "\s${OCF_RESKEY_address}/" | awk -F "${IFS_}" "${EXECUTING}")
		ocf_log debug "ROUTE_TO_INSTANCE: $ROUTE_TO_INSTANCE"
	else
		ocf_log err "result: $res; cmd: $cmd; rc: $rc"
	fi
}

ip_get_and_configure() {
	ocf_log debug "function: ip_get_and_configure"

	request_describe_route_tables
	if [ "$ECS_INSTANCE_ID" != "$ROUTE_TO_INSTANCE" ]; then
		if [ -n "$ROUTE_TO_INSTANCE" ]; then
			ip_drop
		fi
		request_create_route_entry
		rc=$?
		while [ $rc -ne 0 ]; do
			sleep 1
			request_create_route_entry
			rc=$?
		done
		wait_for_started
	fi


	# Reconfigure the local ip address
	ip addr add "${OCF_RESKEY_address}/32" dev $OCF_RESKEY_interface
	rc=$?
	if [ $rc -ne 0 ]; then
		ocf_log err "command failed, rc: $rc"
		return $OCF_ERR_GENERIC
	fi

	ocf_log debug "IP added"

	return $OCF_SUCCESS
}

ip_drop() {
	ocf_log debug "function: ip_drop"
	cmd="ip addr delete ${OCF_RESKEY_address}/32 dev $OCF_RESKEY_interface"
	ocf_log debug "executing command: $cmd"
	res=$($cmd)
	rc=$?
	if [ $rc -ne 0 ] && [ $rc -ne 2 ]; then
		ocf_log err "command failed, rc: $rc; cmd: $cmd; result: $res"
		return $OCF_ERR_GENERIC
	fi
	request_delete_route_entry
	rc=$?
	if [ $rc -ne 0 ]; then
		ocf_log err "command failed, rc: $rc"
		return $OCF_ERR_GENERIC
	fi
	wait_for_deleted

	ocf_log debug "IP dropped"

	return $OCF_SUCCESS
}

wait_for_started() {
	request_describe_route_tables
	while [ "$ECS_INSTANCE_ID" != "$ROUTE_TO_INSTANCE" ]; do
		sleep 3
		request_describe_route_tables
	done
}

wait_for_deleted() {
	request_describe_route_tables
	 while [ ! -z "$ROUTE_TO_INSTANCE" ]; do
		sleep 1
		request_describe_route_tables
	 done
}

ecs_ip_metadata() {
	cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="aliyun-vpc-move-ip" version="2.0">
<version>1.0</version>
<longdesc lang="en">
Resource Agent to move IP addresses within a VPC of the Aliyun Webservices ECS
by changing an entry in an specific routing table
</longdesc>
<shortdesc lang="en">Move IP within a VPC of the Aliyun ECS</shortdesc>

<parameters>
<parameter name="aliyuncli" required="0">
<longdesc lang="en">
Path to command line tools for Aliyun
</longdesc>
<shortdesc lang="en">Path to Aliyun CLI tools</shortdesc>
<content type="string" default="${OCF_RESKEY_aliyuncli_default}" />
</parameter>

<parameter name="address" required="1">
<longdesc lang="en">
VPC private IP address
</longdesc>
<shortdesc lang="en">vpc ip</shortdesc>
<content type="string" default="${OCF_RESKEY_address_default}" />
</parameter>

<parameter name="routing_table" required="1">
<longdesc lang="en">
Name of the routing table, where the route for the IP address should be changed, i.e. vtb-...
</longdesc>
<shortdesc lang="en">routing table name</shortdesc>
<content type="string" default="${OCF_RESKEY_routing_table_default}" />
</parameter>

<parameter name="interface" required="1">
<longdesc lang="en">
Name of the network interface, i.e. eth0
</longdesc>
<shortdesc lang="en">network interface name</shortdesc>
<content type="string" default="${OCF_RESKEY_interface_default}" />
</parameter>

<parameter name="endpoint" required="0">
<longdesc lang="en">
An endpoint is the service entry of an Alibaba Cloud service, i.e. vpc.cn-beijing.aliyuncs.com
</longdesc>
<shortdesc lang="en">service endpoint</shortdesc>
<content type="string" default="${OCF_RESKEY_endpoint_default}" />
</parameter>

<parameter name="profile" required="0">
<longdesc lang="en">
Valid Aliyun CLI profile name (see 'aliyun cli configure').
See https://www.alibabacloud.com/help/zh/product/29991.htm for more information about aliyun cli.
</longdesc>
<shortdesc lang="en">profile name</shortdesc>
<content type="string" default="${OCF_RESKEY_profile_default}" />
</parameter>
</parameters>

<actions>
<action name="start" timeout="180s" />
<action name="stop" timeout="180s" />
<action name="monitor" depth="0" timeout="30s" interval="30s" />
<action name="validate-all" timeout="5s" />
<action name="meta-data" timeout="5s" />
</actions>
</resource-agent>
END
}

ecs_ip_validate() {
	ocf_log debug "function: validate"

	if [ -z "${OCF_RESKEY_aliyuncli}" ]; then
		ocf_exit_reason "unable to detect aliyuncli binary"
		exit $OCF_ERR_INSTALLED
	fi

	# IP address
	if [ -z "$OCF_RESKEY_address" ]; then
		ocf_log err "IP address parameter not set $OCF_RESKEY_ADDRESS!"
		exit $OCF_ERR_CONFIGURED
	fi

	# Network Interface
	if [ -z "$OCF_RESKEY_interface" ]; then
		ocf_log err "Network interface parameter not set $OCF_RESKEY_INTERFACE!"
		exit $OCF_ERR_CONFIGURED
	fi

	# Routing Table
	if [ -z "$OCF_RESKEY_routing_table" ]; then
		ocf_log err "Routing table parameter not set $OCF_RESKEY_ROUTING_TABLE!"
		exit $OCF_ERR_CONFIGURED
	fi

	if [ -z "${ECS_INSTANCE_ID}" ]; then
		ocf_exit_reason "Instance ID not found. Is this a ECS instance?"
		return $OCF_ERR_GENERIC
	fi

	return $OCF_SUCCESS
}

ecs_ip_start() {
	ocf_log info "ECS: Moving IP address $OCF_RESKEY_address to this host by adjusting routing table $OCF_RESKEY_routing_table"

	ecs_ip_monitor
	if [ $? = $OCF_SUCCESS ]; then
		ocf_log info "ECS: $OCF_RESKEY_address already started"
		return $OCF_SUCCESS
	fi

	ocf_log info "ECS: Adjusting routing table and locally configuring IP address"
	ip_get_and_configure
	rc=$?
	if [ $rc -ne 0 ]; then
		ocf_log err "Received $rc from 'aliyun cli'"
		return $OCF_ERR_GENERIC
	fi

	ecs_ip_monitor
	rc=$?
	if [ $rc -ne $OCF_SUCCESS ]; then
		ocf_log err "IP address couldn't be configured on this host (IP: $OCF_RESKEY_address, Interface: $OCF_RESKEY_interface)"
		return $rc
	fi

	return $OCF_SUCCESS
}

ecs_ip_stop() {
	ocf_log info "ECS: Bringing down IP address $OCF_RESKEY_address"

	ecs_ip_monitor
	if [ $? = $OCF_NOT_RUNNING ]; then
		ocf_log info "ECS: Address $OCF_RESKEY_address already down"
		return $OCF_SUCCESS
	fi

	ip_drop
	if [ $? -ne $OCF_SUCCESS ]; then
		ocf_log err "ECS: Couldn't drop IP address $OCF_RESKEY_address on interface $OCF_RESKEY_interface."
		return $OCF_ERR_GENERIC
	fi

	ecs_ip_monitor
	if [ $? = $OCF_NOT_RUNNING ]; then
		ocf_log info "ECS: Successfully brought down $OCF_RESKEY_address"
		return $OCF_SUCCESS
	fi

	ocf_log err "ECS: Couldn't bring down IP address $OCF_RESKEY_address on interface $OCF_RESKEY_interface."
	return $OCF_ERR_GENERIC
}

ecs_ip_monitor() {
	ocf_log debug "function: ecsip_monitor: check routing table"
	request_describe_route_tables

	if [ "$ECS_INSTANCE_ID" != "$ROUTE_TO_INSTANCE" ]; then
		ocf_log debug "not routed to this instance ($ECS_INSTANCE_ID) but to instance $ROUTE_TO_INSTANCE"
		return $OCF_NOT_RUNNING
	fi

	cmd="ping -W 1 -c 1 $OCF_RESKEY_address"
	ocf_log debug "executing command: $cmd"
	$cmd > /dev/null
	if [ $? -ne 0 ]; then
		ocf_log debug "IP $OCF_RESKEY_address not locally reachable via ping on this system"
		return $OCF_NOT_RUNNING
	fi
	ocf_log debug "routed in VPC and locally reachable"
	return $OCF_SUCCESS
}


###############################################################################
#
# MAIN
#
###############################################################################

case $__OCF_ACTION in
	meta-data) ecs_ip_metadata
		   exit $OCF_SUCCESS;;
	validate-all) ecs_ip_validate;;
esac

ECS_INSTANCE_ID="$(curl -s http://100.100.100.200/latest/meta-data/instance-id)"

case $__OCF_ACTION in
	start)
		ecs_ip_validate
		ecs_ip_start;;
	stop)
		ecs_ip_stop;;
	monitor)
		ecs_ip_monitor;;
	*)	exit $OCF_ERR_UNIMPLEMENTED;;
esac