mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 10:25:42 +03:00
When creating a dynamic host volumes, set up an optional monitor that waits for the node to fingerprint the volume as healthy. Ref: https://github.com/hashicorp/nomad/pull/24479
105 lines
2.5 KiB
Bash
Executable File
105 lines
2.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Copyright (c) HashiCorp, Inc.
|
|
# SPDX-License-Identifier: BUSL-1.1
|
|
|
|
# db TODO(1.10.0): where does PATH come from here? somewhere implicit? /sbin/ and /bin/ and ...?
|
|
|
|
set -euo pipefail
|
|
|
|
help() {
|
|
cat <<EOF
|
|
Dynamic Host Volume plugin which creates an ext4 loopback drive of
|
|
the minimum requested size, and mounts it at the provided path argument.
|
|
|
|
Note: Requires superuser access to mount.
|
|
|
|
Usage:
|
|
$(basename "$0") [options] <create|delete|version> [path]
|
|
|
|
Options:
|
|
-v|--verbose: Show shell commands (set -x)
|
|
-h|--help: Print this help text and exit
|
|
|
|
Operations:
|
|
create: Creates and mounts the device at path (required)
|
|
required environment:
|
|
CAPACITY_MIN_BYTES
|
|
delete: Unmounts and deletes the device at path (required)
|
|
version: Outputs this plugin's version
|
|
|
|
EOF
|
|
}
|
|
|
|
version() {
|
|
echo "0.0.1"
|
|
}
|
|
|
|
# parse args
|
|
[ $# -eq 0 ] && { help; exit 1; }
|
|
for arg in "$@"; do
|
|
case $arg in
|
|
-h|-help|--help) help; exit 0 ;;
|
|
version|--version) version; exit 0 ;;
|
|
-v|--verbose) set -x; shift; ;;
|
|
esac
|
|
done
|
|
|
|
# path is required for everything else
|
|
[ $# -lt 2 ] && { echo 'path required; seek --help' 1>&2; exit 1; }
|
|
host_path="$2"
|
|
|
|
validate_path() {
|
|
local path="$1"
|
|
if [[ ! "$path" =~ [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} ]]; then
|
|
1>&2 echo "expected uuid-lookin ID in the HOST_PATH; got: '$path'"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
is_mounted() {
|
|
awk '{print $2}' /proc/mounts | grep -q "^$1$"
|
|
}
|
|
|
|
create_volume() {
|
|
local path="$1"
|
|
validate_path "$path"
|
|
local bytes="$2"
|
|
|
|
# translate to mb for dd block size
|
|
local megs=$((bytes / 1024 / 1024)) # lazy, approximate
|
|
|
|
# the extra conditionals are for idempotency
|
|
if [ ! -f "$path.ext4" ]; then
|
|
dd if=/dev/zero of="$path.ext4" bs=1M count="$megs"
|
|
# mkfs is noisy on stdout, so we send it to stderr
|
|
# to avoid breaking the JSON parsing on the client
|
|
mkfs.ext4 "$path.ext4" 1>&2
|
|
fi
|
|
if ! is_mounted "$path"; then
|
|
mkdir -p "$path"
|
|
mount "$path.ext4" "$path"
|
|
fi
|
|
}
|
|
|
|
delete_volume() {
|
|
local path="$1"
|
|
validate_path "$path"
|
|
is_mounted "$path" && umount "$path"
|
|
rm -rf "$path"
|
|
rm -f "$path.ext4"
|
|
}
|
|
|
|
case "$1" in
|
|
"create")
|
|
create_volume "$host_path" "$CAPACITY_MIN_BYTES"
|
|
# output what Nomad expects
|
|
bytes="$(stat --format='%s' "$host_path.ext4")"
|
|
printf '{"path": "%s", "bytes": %s}' "$host_path" "$bytes"
|
|
;;
|
|
"delete")
|
|
delete_volume "$host_path" ;;
|
|
*)
|
|
echo "unknown operation: $1" 1>&2
|
|
exit 1 ;;
|
|
esac
|