-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonevnetar.sh
executable file
·74 lines (65 loc) · 2 KB
/
onevnetar.sh
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
#!/bin/sh
#args: <name> <template> [ar_uniq_key]
set -e
. "$1"
CONFIG="$_ansible_tmpdir/vnetar.conf"
AR_UNIQ_KEY=${ar_uniq_key:-IP}
if [ -z "$name" ]; then
echo '{"changed":false, "failed":true, "msg": "name is required"}'
exit -1
fi
# Search vnet
if OUTPUT="$(onevnet list -lID,USER,NAME --csv -fNAME="$name" $user)"; then
ID="$(echo "$OUTPUT" | awk -F, 'FNR==2{print $1}')"
else
RC=$?; echo "$OUTPUT"; exit $RC
fi
if [ -z "$template" ]; then
echo '{"changed":false, "failed":true, "msg": "template is required"}'
exit -1
fi
# Write template
echo "$template" > "$CONFIG"
# Search unique value for AR_UNIQ_KEY
AR_UNIQ_VAL="$(sed -n 's/.*'"${AR_UNIQ_KEY}"' *= *"\?\([^",]\+\)"\?.*/\1/p' "$CONFIG")"
if [ -z "$AR_UNIQ_VAL" ]; then
echo "" >&2
echo '{"changed":false, "failed":true, "msg": "Template have no $AR_UNIQ_KEY attribute"}'
exit -1
fi
# Search address range
if OUTPUT="$(onevnet show -x "$ID")"; then
AR_ID="$(echo "$OUTPUT" | ruby -r rexml/document -e 'include REXML; p XPath.first(Document.new($stdin), "/VNET/AR_POOL/AR['"${AR_UNIQ_KEY}"'=\"'"${AR_UNIQ_VAL}"'\"]/AR_ID/text()")' | grep -o '[0-9]\+' || true)"
else
RC=$?; echo "$OUTPUT"; exit $RC
fi
if [ "$state" = "absent" ]; then
if [ -z "$AR_ID" ]; then
# Delete address range
onevnet rmar "$ID" "$AR_ID"
echo '{"changed":true}'
else
# Address range is not exist
echo '{"changed":false}'
fi
exit 0
elif [ -n "$state" ] && [ "$state" != "present" ]; then
echo '{"changed":false, "failed":true, "msg": "value of state must be one of: present, absent, got: '"$state"'"}'
exit -1
fi
if [ -z "$AR_ID" ]; then
# New address range
onevnet addar "$ID" "$CONFIG"
echo '{"changed":true}'
else
# Existing address range
BEFORE="$(onevnet show -x "$ID" | sha256sum )"
sed -zi "s/\(AR *= *\[\)/\1 AR_ID=$AR_ID, /" "$CONFIG"
onevnet updatear "$ID" "$AR_ID" "$CONFIG"
AFTER="$(onevnet show -x "$ID" | sha256sum )"
if [ "$BEFORE" != "$AFTER" ]; then
echo '{"changed":true}'
else
echo '{"changed":false}'
fi
fi