From dddfe2a9ed6fe6d28bc5e80422b3eec033d59f10 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Fri, 4 Apr 2025 19:28:32 +0200 Subject: [PATCH] Fixes. --- bookworm/scripts/lib/abort_if_command_unknown | 7 +++ bookworm/scripts/lib/constants_installer | 1 - bookworm/scripts/lib/expect_n_args | 21 ++++++++ .../scripts/lib/get_mountable_device_path | 12 ++--- bookworm/scripts/make_writable_installer.sh | 18 +++---- bookworm/scripts/update_preseed.sh | 20 +++---- testing/home/desktop/.local/bin/borgplom | 53 +++---------------- .../scripts/init_server_access.sh | 0 testing/scripts/lib/expect_n_args | 1 + 9 files changed, 60 insertions(+), 73 deletions(-) create mode 100644 bookworm/scripts/lib/abort_if_command_unknown create mode 100644 bookworm/scripts/lib/expect_n_args rename {bookworm => testing}/scripts/init_server_access.sh (100%) create mode 120000 testing/scripts/lib/expect_n_args diff --git a/bookworm/scripts/lib/abort_if_command_unknown b/bookworm/scripts/lib/abort_if_command_unknown new file mode 100644 index 0000000..81a3365 --- /dev/null +++ b/bookworm/scripts/lib/abort_if_command_unknown @@ -0,0 +1,7 @@ +. lib/abort + +abort_if_command_unknown() { + if ! which "$1" > /dev/null; then + abort "Expected command unavailable: ${1}." + fi +} diff --git a/bookworm/scripts/lib/constants_installer b/bookworm/scripts/lib/constants_installer index b710116..92e60df 100644 --- a/bookworm/scripts/lib/constants_installer +++ b/bookworm/scripts/lib/constants_installer @@ -1,3 +1,2 @@ FILENAME_PRESEED_CFG=preseed.cfg PATH_PRESEED_CFG="../${FILENAME_PRESEED_CFG}" -PATH_MNT_STICK=/mnt/stick diff --git a/bookworm/scripts/lib/expect_n_args b/bookworm/scripts/lib/expect_n_args new file mode 100644 index 0000000..950671a --- /dev/null +++ b/bookworm/scripts/lib/expect_n_args @@ -0,0 +1,21 @@ +. lib/abort + +expect_n_args() { + _N_MIN_ARGS="$1" + _N_MAX_ARGS="$2" + _USAGE="$3" + shift 3 + _ABORT_MSG= + if [ "$#" -lt "${_N_MIN_ARGS}" ]; then + _ABORT_MSG="missing arguments (got $# instead of ${_N_MAX_ARGS})." + elif [ "$#" -gt "${_N_MAX_ARGS}" ]; then + shift "${_N_MAX_ARGS}" + _ABORT_MSG="unexpected arguments beyond expected number (${_N_MAX_ARGS}): $@" + fi + if [ ! -z "${_ABORT_MSG}" ]; then + if [ ! -z "${_USAGE}" ]; then + _ABORT_MSG="${_ABORT_MSG}\nExpected arguments: ${_USAGE}" + fi + abort "Aborting due to ${_ABORT_MSG}" + fi +} diff --git a/bookworm/scripts/lib/get_mountable_device_path b/bookworm/scripts/lib/get_mountable_device_path index 7090a46..b0a1cd5 100644 --- a/bookworm/scripts/lib/get_mountable_device_path +++ b/bookworm/scripts/lib/get_mountable_device_path @@ -1,11 +1,11 @@ . lib/abort get_mountable_device_path() { - PATH_DEV="/dev/$1" - if [ ! -b "${PATH_DEV}" ]; then - abort "No block device at ${PATH_DEV}." - elif [ $(mount | grep -E "^${PATH_DEV}" | wc -l) -gt 0 ]; then - abort "${PATH_DEV} already mounted." + _PATH_DEV="/dev/$1" + if [ ! -b "${_PATH_DEV}" ]; then + abort "No block device at ${_PATH_DEV}." + elif [ $(mount | grep -E "^${_PATH_DEV}" | wc -l) -gt 0 ]; then + abort "${_PATH_DEV} already mounted." fi - printf "${PATH_DEV}" + printf "${_PATH_DEV}" } diff --git a/bookworm/scripts/make_writable_installer.sh b/bookworm/scripts/make_writable_installer.sh index d828344..1cf871d 100755 --- a/bookworm/scripts/make_writable_installer.sh +++ b/bookworm/scripts/make_writable_installer.sh @@ -3,28 +3,24 @@ set -e cd $(dirname "$0") . lib/abort +. lib/abort_if_command_unknown . lib/abort_if_not_user . lib/abort_if_offline . lib/constants_installer -. lib/expect_min_n_args +. lib/expect_n_args . lib/get_mountable_device_path INSTALLER_VERSION=12.10.0 PATH_MNT_ISO=/mnt/iso +expect_n_args 1 1 'DEVICE (e.g. "sdb")' $@ abort_if_not_user root -abort_if_offline root -abort_if_command_unknown() { - if ! which "$1" > /dev/null; then - abort "expected command unavailable: ${1}." - fi -} -abort_if_command_unknown wget -abort_if_command_unknown rsync -abort_if_command_unknown parted +abort_if_offline abort_if_command_unknown mkfs.vfat +abort_if_command_unknown parted +abort_if_command_unknown rsync +abort_if_command_unknown wget -expect_min_n_args 1 '(device name, e.g. "sdb")' $@ PATH_STICK="$(get_mountable_device_path $1)" FILENAME_ISO="debian-${INSTALLER_VERSION}-amd64-netinst.iso" diff --git a/bookworm/scripts/update_preseed.sh b/bookworm/scripts/update_preseed.sh index b0572bc..ffd0938 100755 --- a/bookworm/scripts/update_preseed.sh +++ b/bookworm/scripts/update_preseed.sh @@ -1,17 +1,19 @@ #!/bin/sh set -e cd $(dirname "$0") -. lib/abort_if_not_user +. lib/abort_if_command_unknown . lib/constants_installer -. lib/expect_min_n_args +. lib/expect_n_args . lib/get_mountable_device_path -abort_if_not_user root -expect_min_n_args 1 '(e.g. "sdb1")' $@ -PATH_STICK="$(get_mountable_device_path $1)" +expect_n_args 1 1 'DEVICE (e.g. "sdb")' $@ +abort_if_command_unknown pmount -echo "Mounting ${PATH_STICK} …" -mount "${PATH_STICK}" "${PATH_MNT_STICK}" -trap "echo 'Unmounting'; umount ${PATH_MNT_STICK}" EXIT INT TERM +DEVICE_NAME="$1" +PATH_DEVICE="$(get_mountable_device_path ${DEVICE_NAME})" +PATH_MOUNTED="/media/$1" +echo "Mounting ${PATH_DEVICE} …" +pmount "${DEVICE_NAME}" +trap "echo 'Unmounting '${PATH_DEVICE}; pumount ${DEVICE_NAME}" EXIT INT TERM echo "Copying ${PATH_PRESEED_CFG} …" -cp "${PATH_PRESEED_CFG}" "${PATH_MNT_STICK}/" +cp "${PATH_PRESEED_CFG}" "${PATH_MOUNTED}/" diff --git a/testing/home/desktop/.local/bin/borgplom b/testing/home/desktop/.local/bin/borgplom index 1f1eccd..58969e0 100755 --- a/testing/home/desktop/.local/bin/borgplom +++ b/testing/home/desktop/.local/bin/borgplom @@ -3,6 +3,7 @@ set -e cd $(dirname "$0") . lib/abort . lib/constants_borg # PATH_BORG_CONF +. lib/expect_n_args . lib/get_passphrase . lib/path_tmp_timestamped . lib/print_usage @@ -22,19 +23,7 @@ error_exit() { abort "Aborting due to $1"; } error_exit_with_usage() { error_exit "${1}\n\n$(print_usage)"; } # commands -USAGE_INDICES='backup_keys claim help keys orgpull info' - -_check_args_n() { - _N_MIN_ARGS="$1" - _N_MAX_ARGS="$2" - shift 2 - if [ "$#" -lt "${_N_MIN_ARGS}" ]; then - error_exit_with_usage "missing arguments ($# instead of ${_N_MAX_ARGS})." - elif [ "$#" -gt "${_N_MAX_ARGS}" ]; then - shift "${_N_MAX_ARGS}" - error_exit_with_usage "unexpected arguments beyond expected number: $@" - fi -} +USAGE_INDICES='backup_keys claim help info keys orgpull' _run_borg_with_passphrase() { while true; do @@ -61,7 +50,7 @@ _id_from_file() { head -1 "$1" | cut -d' ' -f2; } USAGE_ARGS_backup_keys='PATH' USAGE_DESC_backup_keys="copy known keys to PATH/, with their repos' server names as filenames" CMDFNC_backup_keys() { - _check_args_n 1 1 "$@" + expect_n_args 1 1 "${USAGE_ARGS_backup_keys}" "$@" _PATH_TARGET_DIR=$(realpath "$1") if [ -e "${_PATH_TARGET_DIR}" ] && [ ! -d "${_PATH_TARGET_DIR}" ]; then error_exit "non-directory at ${_PATH_TARGET_DIR}" @@ -86,7 +75,7 @@ CMDFNC_backup_keys() { USAGE_ARGS_claim='PATH' USAGE_DESC_claim="register file of PATH as key to repo at \"$(location_from_servername SERVER_NAME)\", with SERVER_NAME the filename portion of PATH" CMDFNC_claim() { - _check_args_n 1 1 "$@" + expect_n_args 1 1 "${USAGE_ARGS_claim}" "$@" _PATH_SOURCE="$1" _ensure_no_overwrite_at() { if [ -f "$1" ]; then @@ -116,14 +105,14 @@ CMDFNC_claim() { USAGE_DESC_help='print this help and exit' CMDFNC_help() { - _check_args_n 0 0 "$@" + expect_n_args 0 0 '' "$@" print_usage } USAGE_ARGS_info='SERVER_NAME [ARCHIVE]' USAGE_DESC_info="run 'borg info' against repo at \"$(location_from_servername SERVER_NAME)\", optionally only against ARCHIVE; if latter not provided, list most recent archives of repo" CMDFNC_info() { - _check_args_n 1 2 "$@" + expect_n_args 1 2 "${USAGE_ARGS_info}" $@ _TARGET_REPO="$(location_from_servername ${1})" if [ ! -z "$2" ]; then borg info "${_TARGET_REPO}::$2" @@ -136,35 +125,7 @@ CMDFNC_info() { USAGE_DESC_keys='list known repos in ID, key filename, and alleged location' CMDFNC_keys() { - _check_args_n 0 0 "$@" - _exit_ok() { - echo "(none, since directory ${1})" - exit 0 - } - - echo "Known keys, as per ${PATH_BORG_CONF_KEYS}:" - if [ ! -d "${PATH_BORG_CONF_KEYS}" ]; then - _exit_ok "non-existant" - fi - _KEYFILES="$(ls -1 ${PATH_BORG_CONF_KEYS})" - if [ -z "${_KEYFILES}" ]; then - _exit_ok "empty" - fi - echo "${_KEYFILES}" | while read _FILENAME; do - _KEY_ID=$(_id_from_file "${PATH_BORG_CONF_KEYS}/${_FILENAME}") - _PATH_LOC="$(path_repo_location ${_KEY_ID})" - printf "${_KEY_ID} ${_FILENAME} " - if [ -f "${_PATH_LOC}" ]; then - printf '%s\n' "$(cat ${_PATH_LOC})" - else - printf '?\n' - fi - done -} - -USAGE_DESC_orgpull='pull most recent org directory available in repos' -CMDFNC_orgpull() { - _check_args_n 0 0 "$@" + expect_n_args 0 0 '' "$@" _NAME_ARCHIVE=orgdir # determine server and repo diff --git a/bookworm/scripts/init_server_access.sh b/testing/scripts/init_server_access.sh similarity index 100% rename from bookworm/scripts/init_server_access.sh rename to testing/scripts/init_server_access.sh diff --git a/testing/scripts/lib/expect_n_args b/testing/scripts/lib/expect_n_args new file mode 120000 index 0000000..5c37e82 --- /dev/null +++ b/testing/scripts/lib/expect_n_args @@ -0,0 +1 @@ +../../../bookworm/scripts/lib/expect_n_args \ No newline at end of file -- 2.30.2