You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
564 B

#!/usr/bin/env bash
# CONSTANTS
CONTAINER_NAME=$1
# FUNCTIONS
function usage {
echo "$(basename "$0") <CONTAINER_NAME>|--all"
}
function lookup {
cmd="pvesh get /cluster/resources --type vm --output=json | jq --raw-output '.[] | \"\(.name) \(.vmid)\"'"
if [[ $CONTAINER_NAME == "--all" ]]; then
eval "$cmd"
else
cmd="$cmd | egrep '^$CONTAINER_NAME '"
found=$(eval "$cmd") || ( >&2 echo "ERROR: container <$CONTAINER_NAME> not found!" && exit 10 )
echo "$found" | cut -d' ' -f2
fi
}
# MAIN
set -Eue
[[ "$#" -lt 1 ]] && usage && exit 1
lookup