-
Notifications
You must be signed in to change notification settings - Fork 0
/
onehost.sh
executable file
·77 lines (67 loc) · 1.69 KB
/
onehost.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
75
76
77
#!/bin/sh
set -x
#args: <name> <im_mad> <vmm_mad> [template] [state] [cluster]
set -e
add_to_cluster() {
if [ -n "$cluster" ]; then
onecluster addhost "$cluster" "$ID"
fi
}
. "$1"
CONFIG="$_ansible_tmpdir/host.conf"
if [ -z "$name" ]; then
echo '{"changed":false, "failed":true, "msg": "name is required"}'
exit -1
fi
# Search host
if OUTPUT="$(onehost list -lID,NAME --csv -fNAME="$name")"; then
ID="$(echo "$OUTPUT" | awk -F, 'FNR==2{print $1}')"
else
RC=$?; echo "$OUTPUT"; exit $RC
fi
if [ "$state" = "absent" ]; then
if [ -z "$ID" ]; then
# Delete host
onehost delete "$ID"
echo '{"changed":true}'
else
# Host 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 "$im_mad" ]; then
echo '{"changed":false, "failed":true, "msg": "im_mad is required"}'
exit -1
fi
if [ -z "$vmm_mad" ]; then
echo '{"changed":false, "failed":true, "msg": "vmm_mad is required"}'
exit -1
fi
# Write template
echo "$template" > "$CONFIG"
if [ -z "$ID" ]; then
# New host
if OUTPUT="$(onehost create -i "$im_mad" -v "$vmm_mad" "$name")"; then
ID="$(echo "$OUTPUT" | awk '{print $NF}' )"
onehost update "$ID" "$CONFIG"
else
RC=$?; echo "$OUTPUT"; exit $RC
fi
add_to_cluster
echo '{"changed":true}'
else
# Existing host
BEFORE="$(onehost show -x "$ID" | sha256sum )"
onehost update "$ID" "$CONFIG"
add_to_cluster
AFTER="$(onehost show -x "$ID" | sha256sum )"
if [ "$BEFORE" != "$AFTER" ]; then
echo '{"changed":true}'
else
echo '{"changed":false}'
fi
fi