The script longhorn-engine-exec outputs usage info only if it's wrongly used
#!/usr/bin/env bash
+function usage() {
cat << EOF
+
USAGE:
# Must have Longhorn installed in "longhorn-system" namespace and have access to "kubectl" and the namespace
+ # The volume should be state "attached"
./longhorn-engine-exec [volume-name] [longhorn-command-args]
EXAMPLE:
@@ -11,17 +14,19 @@ EXAMPLE:
EOF
+}
if [[ $# -lt 2 ]]
then
- echo "Require at least 2 parameters: [volume-name] and [longhorn-command-args]"
+ echo "Error: Require at least 2 parameters: [volume-name] and [longhorn-command-args]"
+ usage
exit 1
fi
kubectl -n longhorn-system get lhv $1 > /dev/null
if [[ $? -ne 0 ]]
then
- echo "Volume $1 is not found in Longhorn system"
+ echo "Error: Volume $1 is not found in Longhorn system"
exit 1
fi
@@ -40,24 +45,24 @@ do
instanceManager=$(echo "${engineInfo}" | grep "instanceManagerName:" | sed "s/ *instanceManagerName: *//" | sed "s/\"//g")
if [[ "${enginePort}" == "" ]]
then
- echo "Cannot find related port"
+ echo "Error: Cannot find related port"
exit 1
fi
if [[ "${instanceManager}" == "" ]]
then
- echo "Cannot find related instance manager. Or the volume is not running and the instance manager is empty."
+ echo "Error: Cannot find related instance manager, or the volume is not running and the instance manager is empty."
+ usage
exit 1
fi
array=( $@ )
args=( ${array[@]:1} )
- echo -e "\"longhorn\" command output: \n"
kubectl -n longhorn-system exec -it ${instanceManager} -- bash -c "longhorn --url localhost:${enginePort} ${args[@]}"
exit 0
fi
done
-echo "Cannot find related engine for volume $1, exit"
+echo "Error: Cannot find related engine for volume $1"
exit 1
