-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoneimage.sh
executable file
·116 lines (106 loc) · 2.74 KB
/
oneimage.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
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
#!/bin/sh
#args: <name> <datastore> <path> [state] [template] [type] [chmod] [user] [group]
set -e
fix_permissions() {
if [ -n "$chmod" ]; then
oneimage chmod "$ID" "$chmod"
fi
if [ -n "$user" ]; then
if OUTPUT="$(oneimage chown "$ID" "$user")"; then
true
else
RC=$?
if ! echo "$OUTPUT" | grep -q "already owns"; then
echo "$OUTPUT"; exit $RC
fi
fi
fi
if [ -n "$group" ]; then
if OUTPUT="$(oneimage chgrp "$ID" "$group")"; then
true
else
RC=$?
if ! echo "$OUTPUT" | grep -q "already owns"; then
echo "$OUTPUT"; exit $RC
fi
fi
fi
}
. "$1"
CONFIG="$_ansible_tmpdir/image.conf"
TMPNAME="$(echo "$_ansible_tmpdir" | awk -F/ '{print $(NF-1)}')"
if [ -z "$name" ]; then
echo '{"changed":false, "failed":true, "msg": "name is required"}'
exit -1
fi
# Search image
if OUTPUT="$(oneimage 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 [ "$state" = "absent" ]; then
if [ -z "$ID" ]; then
# Delete image
oneimage delete "$ID"
echo '{"changed":true}'
else
# Image 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 "$datastore" ]; then
echo '{"changed":false, "failed":true, "msg": "datastore is required"}'
exit -1
fi
if [ -z "$path" ] && [ -z "$size" ]; then
echo '{"changed":false, "failed":true, "msg": "path (or size) is required"}'
exit -1
fi
# Write template
echo "$template" > "$CONFIG"
if [ -z "$ID" ]; then
# New image
echo "NAME=\"$TMPNAME\"" >> "$CONFIG"
if [ -n "$path" ]; then
echo "PATH=\"$path\"" >> "$CONFIG"
fi
if [ -n "$size" ]; then
echo "SIZE=\"$size\"" >> "$CONFIG"
fi
if [ -n "$type" ]; then
echo "TYPE=\"$type\"" >> "$CONFIG"
fi
if OUTPUT="$(oneimage create -d "$datastore" "$CONFIG")"; then
ID="$(echo "$OUTPUT" | awk '{print $NF}' )"
else
RC=$?; echo "$OUTPUT"; exit $RC
fi
oneimage unlock "$ID"
fix_permissions
oneimage rename "$ID" "$name"
echo '{"changed":true}'
else
# Existing image
OUTPUT="$(oneimage show -x "$ID")"
if echo "$OUTPUT" | grep -q '<STATE>5</STATE>'; then
echo '{"changed":false, "failed":true, "msg": "image in error state!"}'
exit -1
fi
BEFORE="$(echo "$OUTPUT" | sha256sum )"
oneimage update "$ID" "$CONFIG"
if [ -n "$type" ]; then
oneimage chtype "$ID" "$type"
fi
fix_permissions
AFTER="$(oneimage show -x "$ID" | sha256sum )"
if [ "$BEFORE" != "$AFTER" ]; then
echo '{"changed":true}'
else
echo '{"changed":false}'
fi
fi