-
Notifications
You must be signed in to change notification settings - Fork 4
/
cvmfs_server_container_helper.sh
446 lines (375 loc) · 17.9 KB
/
cvmfs_server_container_helper.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#!/bin/bash
# This file is subject to the terms and conditions defined by
# the LICENSE.md file and was developed by
# Gabriele Gaetano Fronzé, Sara Vallero and Stefano Lusso from
# INFN sezione Torino (IT).
# For abuse reports and other communications write to
# <gabriele.fronze at to.infn.it>
export CVMFS_SERVER_GIT_URL=https://github.com/gabrielefronze/cvmfs-server-container.git
export CVMFS_SERVER_LOCAL_GIT_REPO=~/cvmfs-server-container
export CVMFS_CONTAINER_BASE_IMAGE_NAME=slidspitfire/cvmfs
export CVMFS_STRATUM_CONTAINER="dummy"
export DEFAULT_HOST_CVMFS_ROOT_DIR=/local/cvmfs-docker
export CVMFS_LOG_DIR=/var/log/cvmfs-server-container
if [[ ! -d "$CVMFS_LOG_DIR" ]]; then
mkdir -p "$CVMFS_LOG_DIR"
fi
function prompt_stratum_selection {
if [[ "$CVMFS_STRATUM_CONTAINER" == "dummy" || -z "$CVMFS_STRATUM_CONTAINER" ]]; then
if [[ $(docker ps | grep -c "cvmfs-stratum0") == 1 ]]; then
if [[ $(docker ps | grep -c "cvmfs-stratum1") == 1 ]]; then
read -p "Both stratum0 and stratum1 are running. Please enter [0/1]: " strindex
case "$strindex" in
0)
CVMFS_STRATUM_CONTAINER="cvmfs-stratum0"
;;
1)
CVMFS_STRATUM_CONTAINER="cvmfs-stratum1"
;;
*)
echo "FATAL: Unsupported option. Please use [0/1]"
return
;;
esac
else
CVMFS_STRATUM_CONTAINER="cvmfs-stratum0"
fi
elif [[ $(docker ps | grep -c "cvmfs-stratum1") == 1 ]]; then
CVMFS_STRATUM_CONTAINER="cvmfs-stratum1"
else
echo "FATAL: No cvmfs container found. Please run it or manually set the CVMFS_STRATUM_CONTAINER environment variable."
return
fi
echo "Running next operation on $CVMFS_STRATUM_CONTAINER."
fi
}
function containsElement {
for e in "${@:2}"; do
[[ "$e" == "$1" ]] && echo "found" && return;
done;
echo "not found";
}
function cvmfs_server_container {
MODE=$1
case "$MODE" in
# Clone the remote git repo locally
get)
if [[ ! -d "$CVMFS_SERVER_LOCAL_GIT_REPO"/.git ]]; then
echo "Cloning git repo from $CVMFS_SERVER_GIT_URL in $CVMFS_SERVER_LOCAL_GIT_REPO... "
mkdir -p "$CVMFS_SERVER_LOCAL_GIT_REPO"
git clone "$CVMFS_SERVER_GIT_URL" "$CVMFS_SERVER_LOCAL_GIT_REPO"
else
echo "Pulling git repo from $CVMFS_SERVER_GIT_URL in $CVMFS_SERVER_LOCAL_GIT_REPO... "
git --git-dir="$CVMFS_SERVER_LOCAL_GIT_REPO"/.git --work-tree="$CVMFS_SERVER_LOCAL_GIT_REPO" stash
git --git-dir="$CVMFS_SERVER_LOCAL_GIT_REPO"/.git --work-tree="$CVMFS_SERVER_LOCAL_GIT_REPO" pull
git --git-dir="$CVMFS_SERVER_LOCAL_GIT_REPO"/.git --work-tree="$CVMFS_SERVER_LOCAL_GIT_REPO" stash apply
source "$CVMFS_SERVER_LOCAL_GIT_REPO"/$(basename $BASH_SOURCE)
fi
echo "done"
;;
# Option to build the base container image
build)
rm -f "$CVMFS_LOG_DIR"/build.log
REQ="$2"
case "$REQ" in
0|1)
echo -n "Building base cvmfs-server image... "
docker build -t slidspitfire/cvmfs-server-base "$CVMFS_SERVER_LOCAL_GIT_REPO"/cvmfs-server-base >> "$CVMFS_LOG_DIR"/build.log
echo "done"
echo -n "Building cvmfs stratum$REQ base image with name $CVMFS_CONTAINER_BASE_IMAGE_NAME-stratum$REQ-base... "
docker build -t "$CVMFS_CONTAINER_BASE_IMAGE_NAME"-stratum"$REQ"-base "$CVMFS_SERVER_LOCAL_GIT_REPO"/cvmfs-stratum"$REQ" >> "$CVMFS_LOG_DIR"/build.log
echo "done"
;;
pub)
echo -n "Building base cvmfs-server image... "
docker build -t slidspitfire/cvmfs-server-base "$CVMFS_SERVER_LOCAL_GIT_REPO"/cvmfs-server-base >> "$CVMFS_LOG_DIR"/build.log
echo "done"
echo -n "Building cvmfs publisher base image with name cvmfs-publisher... "
docker build -t "$CVMFS_CONTAINER_BASE_IMAGE_NAME"-publisher "$CVMFS_SERVER_LOCAL_GIT_REPO"/cvmfs-publisher >> "$CVMFS_LOG_DIR"/build.log
echo "done"
;;
client)
echo -n "Building cvmfs client image with name cvmfs-client... "
docker build -t "$CVMFS_CONTAINER_BASE_IMAGE_NAME"-client "$CVMFS_SERVER_LOCAL_GIT_REPO"/cvmfs-client >> "$CVMFS_LOG_DIR"/build.log
echo "done"
;;
squid)
echo -n "Building frontier-squid image with name cvmfs-frontier-squid... "
docker build -t "$CVMFS_CONTAINER_BASE_IMAGE_NAME"-frontier-squid "$CVMFS_SERVER_LOCAL_GIT_REPO"/proxy-frontier >> "$CVMFS_LOG_DIR"/build.log
echo "done"
;;
*)
echo "FATAL: provided option $2 not recognized. Please select [0/1/pub/client]."
return
;;
esac
unset REQ
ln -sf "$CVMFS_LOG_DIR"/build.log "$CVMFS_LOG_DIR"/last-operation.log
;;
# Option to execute the base image
run)
rm -f "$CVMFS_LOG_DIR"/run.log
REQ=""
TEST="false"
HOST_CVMFS_DATA_DIR=""
if [[ "$2" == "--test" ]]; then
REQ="$3"
STRATUM="$REQ"
CVMFS_STRATUM_CONTAINER=cvmfs-stratum"$STRATUM"
HOST_CVMFS_DATA_DIR=${4:-"$DEFAULT_HOST_CVMFS_ROOT_DIR"/"$CVMFS_STRATUM_CONTAINER"}
TEST="true"
else
REQ="$2"
STRATUM="$REQ"
CVMFS_STRATUM_CONTAINER=cvmfs-stratum"$STRATUM"
HOST_CVMFS_DATA_DIR=${3:-"$DEFAULT_HOST_CVMFS_ROOT_DIR"/"$CVMFS_STRATUM_CONTAINER"}
TEST="false"
fi
case "$REQ" in
0|1)
IMAGE_NAME="$CVMFS_CONTAINER_BASE_IMAGE_NAME"-stratum"$STRATUM"-base
echo "Running cvmfs stratum$STRATUM docker container as $CVMFS_STRATUM_CONTAINER with:"
echo -e "\t- Host cvmfs dir = $HOST_CVMFS_DATA_DIR"
sh "$CVMFS_SERVER_LOCAL_GIT_REPO"/cvmfs-stratum"$STRATUM"/Dockerrun-args.sh "$HOST_CVMFS_DATA_DIR" "$IMAGE_NAME" "$TEST" >> "$CVMFS_LOG_DIR"/run.log
echo "done"
unset CVMFS_STRATUM_CONTAINER
unset IMAGE_NAME
unset HOST_CVMFS_DATA_DIR
unset STRATUM
;;
pub)
unset STRATUM
unset CVMFS_STRATUM_CONTAINER
CVMFS_STRATUM_CONTAINER=cvmfs-publisher
IMAGE_NAME="$CVMFS_CONTAINER_BASE_IMAGE_NAME"-publisher
echo "Running cvmfs publisher docker container as $CVMFS_STRATUM_CONTAINER with:"
echo -e "\t- Host cvmfs dir = $HOST_CVMFS_DATA_DIR"
sh "$CVMFS_SERVER_LOCAL_GIT_REPO"/cvmfs-publisher/Dockerrun-args.sh "$HOST_CVMFS_DATA_DIR" "$IMAGE_NAME" >> "$CVMFS_LOG_DIR"/run.log
echo "done"
unset CVMFS_STRATUM_CONTAINER
unset IMAGE_NAME
unset HOST_CVMFS_DATA_DIR
;;
client)
unset STRATUM
unset CVMFS_STRATUM_CONTAINER
echo "Running cvmfs client docker container as cvmfs-client... "
sh "$CVMFS_SERVER_LOCAL_GIT_REPO"/cvmfs-client/Dockerrun.sh >> "$CVMFS_LOG_DIR"/run.log
echo "done"
;;
squid)
unset STRATUM
unset CVMFS_STRATUM_CONTAINER
echo "Running frontier-squid docker container as cvmfs-squid... "
sh "$CVMFS_SERVER_LOCAL_GIT_REPO"/proxy-frontier/Dockerrun.sh >> "$CVMFS_LOG_DIR"/run.log
echo "done"
;;
*)
echo "FATAL: provided option $2 not recognized. Please select [0/1/pub/client]."
return
;;
esac
unset REQ
ln -sf "$CVMFS_LOG_DIR"/run.log "$CVMFS_LOG_DIR"/last-operation.log
;;
# Option to switch the endpoint of CVMFS commands if both containers running locally on the same host
switch-str)
if [[ ! "$CVMFS_STRATUM_CONTAINER" == "dummy" ]]; then
if [[ "$CVMFS_STRATUM_CONTAINER" == "cvmfs-stratum0" ]]; then
echo "Switching to stratum1... done"
CVMFS_STRATUM_CONTAINER="cvmfs-stratum1"
elif [[ "$CVMFS_STRATUM_CONTAINER" == "cvmfs-stratum1" ]]; then
echo "Switching to stratum0... done"
CVMFS_STRATUM_CONTAINER="cvmfs-stratum0"
else
echo "FATAL: env variable CVMFS_STRATUM_CONTAINER=$CVMFS_STRATUM_CONTAINER does not refer to any runnign container."
echo " Please manually export the name of the CVMFS cotnainer via 'export CVMFS_STRATUM_CONTAINER=<container-name>'."
return
fi
else
prompt_stratum_selection
return
fi
;;
# Option to initialize the required repo[s] using the internal script and committing the new image on top of the existing
mkfs-list)
rm -f "$CVMFS_LOG_DIR"/initrepo.log
prompt_stratum_selection
REQUIRED_REPOS="${@: -1}"
OPTIONS="${@:2:$#-3}"
echo "Required repos: $REQUIRED_REPOS"
if [[ -z "$OPTIONS" ]] || [[ ! $(echo "$OPTIONS" | grep -q "\-o root") ]]; then
OPTIONS="-o root ""$OPTIONS"
fi
echo "Required options: $OPTIONS"
if [[ -z "$REQUIRED_REPOS" ]]; then
echo "FATAL: no repository name provided."
return
else
REPO_NAME_ARRAY=$(echo $REQUIRED_REPOS | tr "," "\n")
REQUIRED_REPOS_SUFFIX=$(echo $REQUIRED_REPOS | sed 's/\,/-/')
for REPO_NAME in $REPO_NAME_ARRAY
do
echo -n "Initializing $REPO_NAME repository in $CVMFS_STRATUM_CONTAINER container with options: $OPTIONS ... "
# docker exec -ti "$CVMFS_STRATUM_CONTAINER" cvmfs_server mkfs -o root "$REPO_NAME" >> "$CVMFS_LOG_DIR"/initrepo.log
# docker exec -ti "$CVMFS_STRATUM_CONTAINER" cvmfs_server check "$REPO_NAME" >> "$CVMFS_LOG_DIR"/initrepo.log
echo "done"
done
unset REPO_NAME_ARRAY
unset REQUIRED_REPOS_SUFFIX
unset REPO_NAME
ln -sf "$CVMFS_LOG_DIR"/initrepo.log "$CVMFS_LOG_DIR"/last-operation.log
fi
unset OPTIONS
unset REQUIRED_REPOS
;;
get-keys-rpm)
prompt_stratum_selection
INPUT_CVMFS_REPO_NAME="$3"
REPO_LIST=($(docker exec -ti "$CVMFS_STRATUM_CONTAINER" cvmfs_server list | awk '{print $1}'))
REPO_LIST_LENGTH="${#REPO_LIST[@]}"
IS_REPO_VALID=$(containsElement "$INPUT_CVMFS_REPO_NAME" "${REPO_LIST[@]}")
if [[ "$IS_REPO_VALID" == "found" ]]; then
CVMFS_REPO_NAME="$INPUT_CVMFS_REPO_NAME"
elif [[ "$REPO_LIST_LENGTH" == 1 ]]; then
CVMFS_REPO_NAME="${REPO_LIST[0]}"
else
echo "FATAL: multiple repos available, but no one specified as argument. Aborting."
unset REPO_LIST
unset REPO_LIST_LENGTH
unset IS_REPO_VALID
unset CVMFS_REPO_NAME
return
fi
unset REPO_LIST
unset REPO_LIST_LENGTH
unset IS_REPO_VALID
OPTION="$2"
RPM_STUFF_PATH="/root/RPM-stuff"
if [[ -z "$4" && "$OPTION"=="--conf" ]]; then
echo "FATAL: please provide stratum 1 FQN or IP! Aborting... "
return
fi
echo "$RPM_STUFF_PATH"/build-keys-rpms.sh "$OPTION" "$CVMFS_REPO_NAME" "$4" "$5"
docker exec -ti "$CVMFS_STRATUM_CONTAINER" bash -c "$RPM_STUFF_PATH/build-keys-rpms.sh $OPTION $CVMFS_REPO_NAME $4 $5"
unset RPM_STUFF_PATH
unset RPM_STUFF_PATH
unset CVMFS_REPO_NAME
;;
# Option to recover the required repo[s] using the internal script
mount)
rm -f "$CVMFS_LOG_DIR"/recover.log
prompt_stratum_selection
if [[ "$2" == "-a" || -z "$2" ]]; then
HOST_CVMFS_DATA_DIR=${3:-"$DEFAULT_HOST_CVMFS_ROOT_DIR"/"$CVMFS_STRATUM_CONTAINER"}
REPO_NAME_ARRAY=$(ls $HOST_CVMFS_DATA_DIR/srv-cvmfs/ | tr " " "\n" | sed "/info/d")
for REPO_NAME in $REPO_NAME_ARRAY
do
echo -n "Recovering $REPO_NAME repository in $CVMFS_STRATUM_CONTAINER container... "
docker exec -ti "$CVMFS_STRATUM_CONTAINER" sh /etc/cvmfs-scripts/restore-repo.sh "$REPO_NAME" >> "$CVMFS_LOG_DIR"/recover.log
echo "done"
done
unset HOST_CVMFS_DATA_DIR
unset REPO_NAME_ARRAY
else
REQUIRED_REPOS="$2"
REPO_NAME_ARRAY=$(echo $REQUIRED_REPOS | tr "," "\n")
REQUIRED_REPOS_SUFFIX=$(echo $REQUIRED_REPOS | sed 's/\,/-/')
for REPO_NAME in $REPO_NAME_ARRAY
do
echo -n "Recovering $REPO_NAME repository in $CVMFS_STRATUM_CONTAINER container... "
docker exec -ti "$CVMFS_STRATUM_CONTAINER" sh /etc/cvmfs-scripts/restore-repo.sh "$REPO_NAME" >> "$CVMFS_LOG_DIR"/recover.log
echo "done"
done
unset REPO_NAME_ARRAY
unset REQUIRED_REPOS_SUFFIX
unset REPO_NAME
unset REQUIRED_REPOS
fi
ln -sf "$CVMFS_LOG_DIR"/recover.log "$CVMFS_LOG_DIR"/last-operation.log
;;
# Help option
help)
prompt_stratum_selection
echo "CernVM-FS Container Server Tool\n"
echo
echo "Usage: cvmfs_server_container COMMAND [options] <parameters>\n"
echo
echo "Supported commands:\n"
echo
echo " get Clone the git repo locally and source again this script"
echo " build [0/1]"
echo " Build the stratum[0/1] container image"
echo " run [0/1]"
echo " Runs the stratum[0/1] container as cvmfs-stratum[0/1]."
echo " switch-str Allows to switch between redirecting commands to "
echo " stratum0 or stratum1, if deployed on the same host."
echo " mkfs [-o owner=root] [-w stratum0 url] [-u upstream storage]"
echo " [-m replicable] [-f union filesystem type] [-s S3 config file]"
echo " [-g disable auto tags] [-G Set timespan for auto tags]"
echo " [-a hash algorithm (default: SHA-1)]"
echo " [-z enable garbage collection] [-v volatile content]"
echo " [-Z compression algorithm (default: zlib)]"
echo " [-k path to existing keychain] [-p no apache config]"
echo " [-R require masterkeycard key ]"
echo " [-V VOMS authorization] [-X (external data)]"
echo " <fully qualified repository name>,"
echo " [fully qualified repository name],..."
echo " Configures the running container"
echo " to host the provided comma-separed repo or list"
echo " of repos always setting root as owner."
echo " mount [-a]"
echo " Mounts all the repositories found in"
echo " the host root path, automatically recovering"
echo " from crashes and shutdowns."
echo " mount <fully qualified repository name>,"
echo " [fully qualified repository name],..."
echo " Mounts the specified repo or list of repos,"
echo " automatically recovering them from crashes,"
echo " container prunes and shutdowns."
echo " get-keys-rpm <--pub|--relman|--conf> [fully qualified repository name]"
echo " Generates a RPM package to distribute the repository keys."
echo " The --pub option packs the public key for the specified repository."
echo " The --relman option packs the .pub, .crt and .gw keys of the repository."
echo " The --conf option creates an .rpm with the .conf file to be installed at the clients."
echo " The RPMs are made available in the host location of the cotnainer /etc/cvmfs/keys."
docker exec -ti "$CVMFS_STRATUM_CONTAINER" cvmfs_server help | awk 'NR>5'| awk '!/^NOTE:/' | awk '!/^ mount/' | awk '!/^ Mount/' | sed '/add-replica/,$!d'
;;
# Option to forward commands to cvmfs_server software running inside the container
*)
prompt_stratum_selection
CVMFS_REPO_NAME="${@: -1}"
case "$1" in
"transaction"|"publish")
REPO_LIST=($(docker exec -ti "$CVMFS_STRATUM_CONTAINER" cvmfs_server list | awk '{print $1}'))
REPO_LIST_LENGTH="${#REPO_LIST[@]}"
IS_REPO_VALID=$(containsElement "$CVMFS_REPO_NAME" "${REPO_LIST[@]}")
if [[ "$IS_REPO_VALID" == "found" ]]; then
docker exec -ti "$CVMFS_STRATUM_CONTAINER" cvmfs_server "$@"
elif [[ "$REPO_LIST_LENGTH" == 1 ]]; then
CVMFS_REPO_NAME="${REPO_LIST[0]}"
docker exec -ti "$CVMFS_STRATUM_CONTAINER" cvmfs_server "$@"
else
echo "FATAL: multiple repos available, but no one specified as argument. Aborting."
unset REPO_LIST
unset REPO_LIST_LENGTH
return
fi
unset REPO_LIST
unset REPO_LIST_LENGTH
if [[ "$1" == "transaction" ]]; then
mount -o remount,rw overlay_"$CVMFS_REPO_NAME"
fi
if [[ "$1" == "publish" ]]; then
mount -o remount,ro overlay_"$CVMFS_REPO_NAME"
fi
;;
*)
docker exec -ti "$CVMFS_STRATUM_CONTAINER" cvmfs_server "$@"
;;
esac
unset CVMFS_REPO_NAME
;;
esac
unset MODE
}