/
common.sh
executable file
·270 lines (223 loc) · 8.22 KB
/
common.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/env bash
#
# This file is part of MinIO DirectPV
# Copyright (c) 2021, 2022 MinIO, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
set -ex
export LV_LOOP_DEVICE=
export VG_NAME="testvg${RANDOM}"
export LV_DEVICE=
export LUKS_LOOP_DEVICE=
export LUKS_DEVICE=
export DIRECTPV_CLIENT=
# usage: create_loop <newfile> <size>
function create_loop() {
truncate --size="$2" "$1"
sudo losetup --find "$1"
if [ -n "${RHEL7_TEST}" ]; then
sudo losetup --output NAME --associated "$1" | tail -n +2
else
sudo losetup --noheadings --output NAME --associated "$1"
fi
}
function setup_lvm() {
LV_LOOP_DEVICE=$(create_loop testpv.img 1G)
sudo pvcreate "${LV_LOOP_DEVICE}"
sudo vgcreate "${VG_NAME}" "${LV_LOOP_DEVICE}"
sudo lvcreate --name=testlv --extents=100%FREE "${VG_NAME}"
LV_DEVICE=$(basename "$(readlink -f "/dev/${VG_NAME}/testlv")")
}
function remove_lvm() {
sudo lvchange --quiet --activate n "${VG_NAME}/testlv"
sudo lvremove --quiet --yes "${VG_NAME}/testlv"
sudo vgremove --quiet "${VG_NAME}"
sudo pvremove --quiet "${LV_LOOP_DEVICE}"
sudo losetup --detach "${LV_LOOP_DEVICE}"
rm -f testpv.img
}
function setup_luks() {
LUKS_LOOP_DEVICE=$(create_loop testluks.img 1G)
echo "mylukspassword" > lukspassfile
yes YES | sudo cryptsetup luksFormat "${LUKS_LOOP_DEVICE}" lukspassfile
sudo cryptsetup luksOpen "${LUKS_LOOP_DEVICE}" myluks --key-file=lukspassfile
LUKS_DEVICE=$(basename "$(readlink -f /dev/mapper/myluks)")
}
function remove_luks() {
sudo cryptsetup luksClose myluks
sudo losetup --detach "${LUKS_LOOP_DEVICE}"
rm -f testluks.img
}
# wait_for_service <service>
function wait_for_service() {
service="$1"
endpoints=$(kubectl get endpoints -n directpv "${service}" --no-headers | awk '{ print $2 }')
while [[ $endpoints == '<none>' ]]; do
echo "$ME: waiting for ${service} available"
sleep 5
endpoints=$(kubectl get endpoints -n directpv "${service}" --no-headers | awk '{ print $2 }')
done
}
# install_directpv <pod_count>
function install_directpv() {
"${DIRECTPV_CLIENT}" install --quiet
required_count="$1"
running_count=0
while [[ $running_count -lt $required_count ]]; do
echo "$ME: waiting for $(( required_count - running_count )) DirectPV pods to come up"
sleep $(( required_count - running_count ))
running_count=$(kubectl get pods --field-selector=status.phase=Running --no-headers --namespace=directpv | wc -l)
done
while ! "${DIRECTPV_CLIENT}" info --quiet; do
echo "$ME: waiting for DirectPV to come up"
sleep 5
done
wait_for_service node-api-server-hl
wait_for_service admin-service
sleep 1m
}
# uninstall_directpv <pod_count>
function uninstall_directpv() {
"${DIRECTPV_CLIENT}" uninstall --quiet
pending="$1"
while [[ $pending -gt 0 ]]; do
echo "$ME: waiting for ${pending} DirectPV pods to go down"
sleep "${pending}"
pending=$(kubectl get pods --field-selector=status.phase=Running --no-headers --namespace=directpv | wc -l)
done
while kubectl get namespace directpv --no-headers | grep -q .; do
echo "$ME: waiting for directpv namespace to be removed"
sleep 5
done
return 0
}
# usage: check_drives_status <status>
function check_drives_status() {
status="$1"
if ! "${DIRECTPV_CLIENT}" list drives -d "${LV_DEVICE}" --no-headers | grep -q -e "${LV_DEVICE}.*${status}"; then
echo "$ME: error: LVM device ${LV_DEVICE} not found in ${status} state"
return 1
fi
if ! "${DIRECTPV_CLIENT}" list drives -d "${LUKS_DEVICE}" --no-headers | grep -q -e "${LUKS_DEVICE}.*${status}"; then
echo "$ME: error: LUKS device ${LUKS_DEVICE} not found in ${status} state"
return 1
fi
}
function add_drives() {
url=$(minikube service --namespace=directpv admin-service --url)
admin_server=${url#"http://"}
config_file="$(mktemp)"
if ! "${DIRECTPV_CLIENT}" discover --admin-server "${admin_server}" --output-file "${config_file}"; then
echo "$ME: error: failed to discover the devices"
rm "${config_file}"
return 1
fi
if ! echo Yes | "${DIRECTPV_CLIENT}" init "${config_file}" --admin-server "${admin_server}"; then
echo "$ME: error: failed to initialize the drives"
rm "${config_file}"
return 1
fi
rm "${config_file}"
# Show output for manual debugging.
"${DIRECTPV_CLIENT}" list drives --all
check_drives_status Ready
}
function remove_drives() {
"${DIRECTPV_CLIENT}" remove --all --quiet
}
# usage: deploy_minio <minio-yaml>
function deploy_minio() {
minio_yaml="$1"
kubectl apply -f "${minio_yaml}"
required_count=4
running_count=0
while [[ $running_count -lt $required_count ]]; do
echo "$ME: waiting for $(( required_count - running_count )) minio pods to come up"
sleep $(( required_count - running_count ))
running_count=$(kubectl get pods --field-selector=status.phase=Running --no-headers | grep -c '^minio-' || true)
done
}
# usage: delete_minio <minio-yaml>
function delete_minio() {
minio_yaml="$1"
kubectl delete -f "${minio_yaml}"
pending=4
retry_count=0
while [[ $pending -gt 0 ]]; do
if [[ $retry_count -gt 50 ]]; then
kubectl delete pods --all --force --grace-period 0
fi
retry_count=$((retry_count + 1))
echo "$ME: waiting for ${pending} minio pods to go down"
sleep "${pending}"
pending=$(kubectl get pods --field-selector=status.phase=Running --no-headers | grep -c '^minio-' || true)
done
}
# usage: uninstall_minio <minio-yaml>
function uninstall_minio() {
minio_yaml="$1"
delete_minio "${minio_yaml}"
kubectl delete pvc --all --force
sleep 3
# release all the volumes
"${DIRECTPV_CLIENT}" release --all || true
while true; do
count=$("${DIRECTPV_CLIENT}" list volumes --all --no-headers | tee /dev/stderr | wc -l)
if [[ $count -eq 0 ]]; then
break
fi
echo "$ME: waiting for ${count} volumes to be removed"
sleep 3
done
# Show output for manual debugging.
"${DIRECTPV_CLIENT}" list drives --all
}
# usage: install_directcsi <plugin> <pod-count>
function install_directcsi() {
directcsi_client="$1"
required_count="$2"
"${directcsi_client}" install
running_count=0
while [[ $running_count -lt $required_count ]]; do
echo "$ME: waiting for $(( required_count - running_count )) DirectCSI pods to come up"
sleep $(( required_count - running_count ))
running_count=$(kubectl get pods --field-selector=status.phase=Running --no-headers --namespace=direct-csi-min-io | wc -l)
done
while ! "${directcsi_client}" info; do
echo "$ME: waiting for DirectCSI to come up"
sleep 5
done
sleep 1m
}
# usage: install_directcsi <plugin> <pod-count>
function uninstall_directcsi() {
directcsi_client="$1"
pending="$2"
"${directcsi_client}" uninstall
while [[ $pending -gt 0 ]]; do
echo "$ME: waiting for ${pending} direct-csi pods to go down"
sleep "${pending}"
pending=$(kubectl get pods --field-selector=status.phase=Running --no-headers --namespace=direct-csi-min-io | wc -l)
done
while kubectl get namespace direct-csi-min-io --no-headers | grep -q .; do
echo "$ME: waiting for direct-csi-min-io namespace to be removed"
sleep 5
done
}
# usage: force_install_directcsi <plugin>
function force_uninstall_directcsi() {
directcsi_client="$1"
"${directcsi_client}" uninstall --force --crd
sleep 5
}