forked from dell-powerflex/scaleio-flex
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scaleio-simple
executable file
·236 lines (201 loc) · 5.5 KB
/
scaleio-simple
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
#!/bin/sh
#
# ScaleIO FlexVolume driver
#
# Do not modify thist script, configuration settings can be found at
# /opt/emc/scaleio/flexvolume/cfg/config
# stderr
err() {
debug $*
shift
printf "%b" "$*" 1>&2
}
# stdout
log() {
debug $*
shift
printf "%b" "$*" >&1
}
# log to file (if debug)
debug() {
if [ "$SCALEIO_DEBUG" = "1" ]; then
logtofile $*
fi
}
# log to file
logtofile() {
function=$1
shift
printf "%s pid=%s func=%s msg=\"%s\"\n" "$(date +"%Y-%m-%d %H:%M:%S")" "$$" "${function}" "$*" >> ${LOGFILE}
}
success() {
log $1 '{"status": "Success"}'
exit 0
}
usage() {
err $FUNCNAME "error: invalid usage\n"
err $FUNCNAME "\t$0 init\n"
err $FUNCNAME "\t$0 attach <json params> <nodename>\n"
err $FUNCNAME "\t$0 detach <mount device> <nodename>\n"
err $FUNCNAME "\t$0 waitforattach <mount device> <json params>\n"
err $FUNCNAME "\t$0 mountdevice <mount dir> <mount device> <json params>\n"
err $FUNCNAME "\t$0 unmountdevice <mount dir>\n"
err $FUNCNAME "\t$0 isattached <json params> <nodename>\n"
exit 1
}
ismounted() {
MOUNT=$(findmnt -n "${MNTPATH}" 2>/dev/null | cut -d' ' -f1)
if [ "${MOUNT}" = "${MNTPATH}" ]; then echo 1; else echo 0; fi
}
# Requires $JSON_PARAMS to be set
# Sets $VOLUMEID
volidfromjson() {
VOLUMEID=$(echo "${JSON_PARAMS}" | grep -Po '"volumeID":".*"?[^\\]"' | cut -d: -f 2 | cut -d\" -f 2)
if [ -z "${VOLUMEID}" ] || [ "${VOLUMEID}" = null ]; then
err $FUNCNAME "{\"status\": \"Failure\", \"message\": \"Unable to extract volumeID\"}"
exit 1
fi
}
getvolumename() {
JSON_PARAMS=$1
VOLUMEID=$(echo "${JSON_PARAMS}" | grep -Po '"volumeID":".*"?[^\\]"' | cut -d: -f 2 | cut -d\" -f 2)
if [ -z "${VOLUMEID}" ] || [ "${VOLUMEID}" = null ]; then
err $FUNCNAME "{\"status\": \"Failure\", \"message\": \"Unable to extract volumeID\"}"
exit 1
fi
log $FUNCNAME "{\"status\": \"Success\", \"volumeName\": \"${VOLUMEID}\"}"
exit 0
}
attach() {
JSON_PARAMS=$1
NODE_NAME=$2
volidfromjson
if [ -z "$VOLUMEID" ]; then
err $FUNCNAME '{"status": "Failure", "message": "Unable to extract volumeID"}'
exit 1
fi
log $FUNCNAME "{\"status\": \"Success\", \"device\":\"${VOLUMEID}\"}"
exit 0
}
detach() {
VOLUMEID=$1
NODE_NAME=$2
success $FUNCNAME
}
mountdevice() {
MNTPATH=$1
DEV=$2
FSTYPE=$(echo "$3" | grep -Po '"kubernetes.io/fsType":".*"?[^\\]"' | cut -d: -f 2 | cut -d\" -f 2)
if [ ! -b "${DEV}" ]; then
err $FUNCNAME "{\"status\": \"Failure\", \"message\": \"${DEV} does not exist\"}"
exit 1
fi
if [ "$(ismounted)" -eq "1" ] ; then
success $FUNCNAME
fi
VOLFSTYPE=$(blkid -o udev "${DEV}" 2>/dev/null | grep "ID_FS_TYPE"| cut -d"=" -f2)
if [ "${VOLFSTYPE}" = "" ]; then
CMD="mkfs -t ${FSTYPE}"
if [ "$FSTYPE" = "ext4" ]; then
CMD="${CMD} -F"
elif [ "$FSTYPE" = "xfs" ]; then
CMD="${CMD} -f"
fi
if ! ${CMD} "${DEV}" > /dev/null 2>&1; then
err $FUNCNAME "{\"status\": \"Failure\", \"message\": \"Failed to create fs ${FSTYPE} on device ${DEV}\"}"
exit 1
fi
fi
mkdir -p "${MNTPATH}" > /dev/null 2>&1
SELINUX_STATUS=$(sestatus |grep 'SELinux status' | grep disabled)
SELINUX_ENABLED=$(echo $?)
if ! mount --make-shared "${DEV}" "${MNTPATH}" > /dev/null 2>&1; then
debug $FUNCNAME "mount" "Failed to mount device ${DEV} at ${MNTPATH}"
err $FUNCNAME "{\"status\": \"Failure\", \"message\": \"Failed to mount device ${DEV} at ${MNTPATH}\"}"
exit 1
fi
if [ "${SELINUX_ENABLED}" = "1" ]; then
if ! chcon -t svirt_sandbox_file_t "${MNTPATH}" > /dev/null 2>&1; then
debug $FUNCNAME "mount" "-" "Failed to modify SELINUX properties for ${MNTPATH}"
err $FUNCNAME "{\"status\": \"Failure\", \"message\": \"Failed to modify SELINUX properties for ${MNTPATH}\"}"
exit 1
fi
fi
success $FUNCNAME
}
unmountdevice() {
MNTPATH=$1
if [ "$(ismounted)" -eq "0" ] ; then success $FUNCNAME; fi
if ! umount "${MNTPATH}" > /dev/null 2>&1; then
err $FUNCNAME "{ \"status\": \"Failed\", \"message\": \"Failed to unmount volume at ${MNTPATH}\"}"
exit 1
fi
sleep 2
VOLUMEID=${MNTPATH##*/}
success $FUNCNAME
}
isattached() {
log $FUNCNAME "{\"status\": \"Success\", \"attached\":true}"
exit 0
}
waitforattach() {
EXPECTED_DEV=$1
JSON_PARAMS=$2
volidfromjson
DEV="/dev/disk/by-id/$(ls -1 /dev/disk/by-id | grep "\-${VOLUMEID}$")"
if [ -z "$DEV" ]; then
if [ "$SCALEIO_DEBUG" = "1" ]; then
printf "%s - waitforattach - %s - volume not found or mapped: %s\n" "$(date +"%Y-%m-%d %H:%M:%S")" "${VOLUMEID}" >> ${LOGFILE}
fi
err $FUNCNAME "{\"status\": \"Failure\", \"message\": \"Volume not attached\"}"
exit 1
fi
log $FUNCNAME "{\"status\": \"Success\", \"device\":\"${DEV}\"}"
exit 0
}
# MAIN
CONFIGFILE="/opt/emc/scaleio/flexvolume/cfg/config"
if [ -f "${CONFIGFILE}" ]; then
# source the config file
source "${CONFIGFILE}"
fi
# in case some values were not specific in the config file, set their values
SCALEIO_DEBUG="${SCALEIO_DEBUG:-1}"
KUBECONFIG="${KUBECONFIG:-/root/.kube/config}"
TOKEN_SCRIPT="${TOKEN_SCRIPT:-/opt/emc/scaleio/flexvolume/bin/get-token.sh}"
LOGFILE="${LOGFILE:-/var/log/scaleio-flexvol.log}"
op=$1
if [ "$op" = "init" ]; then
log "main" "{\"status\":\"Success\",\"capabilities\":{\"attach\":true}}"
exit 0
fi
if [ "$#" -lt "2" ]; then usage; fi
shift
debug "main" "$op" "$*"
case "$op" in
attach)
attach "$@"
;;
detach)
detach "$@"
;;
waitforattach)
waitforattach "$@"
;;
mountdevice)
mountdevice "$@"
;;
unmountdevice)
unmountdevice "$@"
;;
isattached)
isattached "$@"
;;
getvolumename)
getvolumename "$@"
;;
*)
err "main" "{\"status\": \"Not supported\"}"
exit 1
esac
exit 1