forked from M0nsieurChat/lizardfs-flexvolume
-
Notifications
You must be signed in to change notification settings - Fork 2
/
lizardfs
executable file
·210 lines (168 loc) · 5.9 KB
/
lizardfs
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
#!/bin/bash
##################################
# Credits to the original authors:
# https://github.com/lowet84 (original author)
# https://github.com/M0nsieurChat (updates for Kubernetes >=1.6.3 compatibility)
##################################
#########
# Derived from M0nsieurChat's version and overhauled for Kubernetes >= 1.7.x compatiblity
#########
VERSION='v1.8.0'
DEBUG=${MFSDEBUG:-0}
[[ -f /.mfsdebug ]] && DEBUG=1
date=$(date '+%Y%m%d/%H:%M:%S')
d_log='/var/log/flex-volume/lizardfs'
op="$1"
if [[ ! -d "${d_log}" ]]; then
mkdir -p "${d_log}"
chmod o= "${d_log}"
fi
usage() {
exec 1>&2
printf "usage: %s {init|mount|unmount} ...\n" "$(basename $0)"
echo " init"
echo " mount <mount dir> <json params>"
echo " unmount <mount dir>"
echo "version: ${VERSION}"
exit 1
}
err() {
(( DEBUG )) && echo "${date}:ERROR: $*" >> "${d_log}/debug.log"
echo -ne $* 1>&2 '\n'
}
log() {
(( DEBUG )) && echo "${date}:OUTPUT: $*" >> "${d_log}/debug.log"
echo -ne $* >&1
}
logNotSupported() {
log '{"status": "Not supported"}'
exit 0
}
logSuccess() {
log '{"status": "Success"}'
exit 0
}
ismounted() {
local MNTPATH="$1"
if [[ ! -d "${MNTPATH}" ]]; then
return 1
fi
local MOUNT=$(findmnt -n "${MNTPATH}" 2>/dev/null | cut -d' ' -f1)
if [[ "${MOUNT}" = "${MNTPATH}" ]]; then
return 0
fi
return 1
}
unmount() {
(( DEBUG )) && echo "${date}:DOUNMOUNT: $@" >> "${d_log}/debug.log"
local MNTPATH="$1"
if ! ismounted "${MNTPATH}"; then
logSuccess
fi
if ! umount "${MNTPATH}" &> /dev/null; then
err '{"status": "Failed", "message": "Failed to unmount volume at: '"${MNTPATH}"'}'
exit 1
fi
if ! rmdir "${MNTPATH}" &> /dev/null; then
# Only emit an error message, but allow as success since it was unmounted correctly above
err '{"status": "Failed", "message": "Failed to unmount volume at: '"${MNTPATH}"'}'
fi
logSuccess
}
domount() {
(( DEBUG )) && echo "${date}:DOMOUNT: $@" >> "${d_log}/debug.log"
local MNTPATH="$1"
local LFSFOLDER="${MNTPATH##*/}"
local f_config=
local from_secret=0
local msgPassword=
local HOST=
local PASSWORD=
# Okay, I want to provision the folder in the root of my LizardFS : each pod bound to a PVC bound to a PV has a folder created (folder name is the PV name bound to the PVC)
# It appears the controllers do a first request on mount with the following arguments :
# /var/lib/kubelet/plugins/kubernetes.io/flexvolume/lowet84/lizardfs/mounts/mfs-volume /dev/bounded-local {"host":"mfsmaster","kubernetes.io/fsType":"","kubernetes.io/readwrite":"rw","port":"9421"}
# $PATH (we don't want to mount that obvisouly) $DEV $JSON_OPTS
# I don't know the usefulness of this, thus, we return success for the subsequent query with the correct parameters
#TT:FIXME???: I'm not sure if this is still the case for 1.7.x with init() emitting "capabilities": {"attach": false}
if (( $# == 3 )); then
logSuccess
elif ismounted "${MNTPATH}"; then
exit 0
fi
# /var/lib/kubelet/pods/c6f7f490-6009-11e7-83d0-005056af1192/volumes/lowet84~lizardfs/mfs-volume {"host":"mfsmaster","kubernetes.io/fsType":"","kubernetes.io/readwrite":"rw","port":"9421"}
# $PATH $JSON_OPTS
HOST=$(jq -r '.host // empty' <<<"$2")
if [[ -z "${HOST}" ]]; then
# Error out if 'host' isn't provided
err '{"status": "Failure", "message": "Cannot mount lizardfs volume without a specified host"}'
exit 1
fi
# Defaults to port 9421
PORT=$(jq -r '.port // "9421"' <<<"$2")
# Special handling is needed if mfsfolder is in play
MFSSUBFOLDER=$(jq -r '.mfssubfolder // empty' <<<"$2")
# Override derived LFSFOLDER with given MFSSUBFOLDER
if [[ -n "${MFSSUBFOLDER}" ]]; then
LFSFOLDER="${MFSSUBFOLDER}"
fi
#Password precedence:
# 1) mfspasswd option
# 2) kubernetes secret
PASSWORD=$(jq -r '.mfspassword // empty' <<<"$2")
if [[ -z "${PASSWORD}" ]]; then
#pull from secret
#NOTES
# 1) @base64d requires: jq > v1.5
# 2) Secret object must have spec type: fq/lizardfs
#PASSWORD=$(jq -r '.["kubernetes.io/secret/mfspassword"] | @base64d // empty' <<<"$2")
PASSWORD=$(jq -r '.["kubernetes.io/secret/mfspassword"] // empty' <<<"$2" | base64 -d)
[[ -z "${PASSWD}" ]] && from_secret=1
fi
if ! mkdir -p "${MNTPATH}" &> /dev/null; then
err '{"status": "Failure", "message": "Failed to create MNTPATH directory: '"${MNTPATH}"'}'
exit 1
fi
# Past the point of no return, safe to setup mfsconfig
if f_config=$(mktemp --tmpdir=/tmp mfsmount-XXXXXXXX.conf); then
chmod 600 "${f_config}"
# Make sure any sensitive info is erased
(( ! DEBUG )) && trap "/bin/rm -f ${f_config} &> /dev/null" EXIT
else
err '{"status": "Failure", "message": "Failed to setup mfsmount config file"}'
exit 1
fi
# Push out mfs* option to the config file converting JSON object to 'key=value' store
#NOTE: if there is a more idiomatic jq way to accomplish this, please let me know
{
jq -r 'to_entries[] | if .key|startswith("mfs") then .key+"="+.value else empty end' <<<"$2"
(( from_secret )) && echo "mfspassword=${PASSWORD}"
} > "${f_config}"
(( DEBUG )) && echo "${date}:CALL: mfsmount -H ${HOST} -P ${PORT} -c ${f_config} -o big_writes,nosuid,nodev,noatime -S ${LFSFOLDER} ${MNTPATH} // JSON: $2" | scrub_password >> "${d_log}/debug.log"
MFSMOUNT_OUTPUT=$(mfsmount -H "${HOST}" -P "${PORT}" -c ${f_config} -o big_writes,nosuid,nodev,noatime -S "${LFSFOLDER}" "${MNTPATH}" 2>&1)
if [ "$?" -ne 0 ]; then
err '{"status": "Failure", "message": "Failed to mount lizardfs at: '"${MNTPATH}"' :: ['"${MFSMOUNT_OUTPUT}"']"}'
exit 1
fi
logSuccess
}
scrub_password() {
if (( DEBUG )); then
cat
else
sed -e 's/\(mfspassword"\):"[^"]\+"/\1:"<redacted>"/g'
fi
}
if [[ "$op" = "init" ]]; then
(( DEBUG )) && echo "${date}:INIT: $@" >> "${d_log}/debug.log"
log '{"status": "Success", "capabilities": {"attach": false, "selinuxRelabel": false}}'
exit 0
fi
if (( $# < 2 )); then
usage
fi
shift
case "$op" in
mount) domount $*;;
unmount) unmount $*;;
*) logNotSupported;;
esac