Enhanced azure-cli with list command, and more robust volume handling arguments.

This commit is contained in:
2025-12-15 08:01:11 +01:00
parent 183a27c8e1
commit a97dbbc285

View File

@@ -3,9 +3,15 @@
# Set default values # Set default values
ACCOUNT_NAME="$(id -un)" ACCOUNT_NAME="$(id -un)"
USER_NAME="ubuntu" USER_NAME="ubuntu"
EXTRA_ARGS=()
LIST=""
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
case $1 in case $1 in
--list|-l)
LIST=true
break
;;
--account|-a) --account|-a)
ACCOUNT_NAME="$2" ACCOUNT_NAME="$2"
shift 2 shift 2
@@ -14,6 +20,26 @@ while [ $# -gt 0 ]; do
USER_NAME="$2" USER_NAME="$2"
shift 2 shift 2
;; ;;
--volume|-v)
read VOL_SRC VOL_DST < <(echo "$2" | tr ':' ' ')
EXTRA_ARGS+=("--mount" "type=bind,source=${VOL_SRC},target=${VOL_DST}")
shift 2
;;
--)
# Stop parsing arguments
shift
break
;;
--help|-h)
cat <<EOF
Usage: $(basename "$0")
[--account|-a ACCOUNT_NAME]
[--user|-u USER_NAME]
[--volume|-v HOST_PATH:CONTAINER_PATH]
[additional docker/podman/container args]
EOF
exit 0
;;
*) *)
break break
;; ;;
@@ -23,43 +49,48 @@ done
IMAGE_NAME="skoszewski/azure-cli:latest" IMAGE_NAME="skoszewski/azure-cli:latest"
# Normalize account name for use in volume name # Normalize account name for use in volume name
ACCOUNT_NAME="${ACCOUNT_NAME/@/_at_}" SANITIZED_ACCOUNT_NAME="${ACCOUNT_NAME/@/_at_}"
ACCOUNT_NAME="${ACCOUNT_NAME//[-.]/_}" SANITIZED_ACCOUNT_NAME="${SANITIZED_ACCOUNT_NAME//[-.]/_}"
# Find container runtime # Find container runtime
if command -v podman &> /dev/null; then if command -v podman &> /dev/null; then
CMD="podman" CMD="podman"
HOSTNAME_ARG="--hostname $(hostname -s)" EXTRA_ARGS+=("--hostname $(hostname -s)")
elif command -v docker &> /dev/null; then elif command -v docker &> /dev/null; then
CMD="docker" CMD="docker"
HOSTNAME_ARG="--hostname $(hostname -s)" EXTRA_ARGS+=("--hostname $(hostname -s)")
elif command -v container &> /dev/null; then elif command -v container &> /dev/null; then
# Apple container command line tool # Apple container command line tool
CMD="container" CMD="container"
HOSTNAME_ARG=""
else else
echo "Error: No usable container runtime was found." >&2 echo "Error: No usable container runtime was found." >&2
exit 1 exit 1
fi fi
if [ -n "$LIST" ]; then
echo "Available accounts:"
$CMD volume ls --format json | jq -r '.[] | select(.labels | has("account")) | .labels.account'
exit 0
fi
# Check, if the account volume exists, if not create it # Check, if the account volume exists, if not create it
# This ensures persistent storage for the Azure CLI configuration # This ensures persistent storage for the Azure CLI configuration
if ! $CMD volume inspect "account_$ACCOUNT_NAME" &> /dev/null; then if ! $CMD volume inspect "account_$SANITIZED_ACCOUNT_NAME" &> /dev/null; then
echo "Creating volume account_$ACCOUNT_NAME for Azure CLI configuration." echo "Creating volume account_$SANITIZED_ACCOUNT_NAME for Azure CLI configuration."
if ! $CMD volume create "account_$ACCOUNT_NAME" &> /dev/null; then if ! $CMD volume create "account_$SANITIZED_ACCOUNT_NAME" --label "account=$ACCOUNT_NAME" &> /dev/null; then
echo "Error: Failed to create volume account_$ACCOUNT_NAME." >&2 echo "Error: Failed to create volume account_$SANITIZED_ACCOUNT_NAME." >&2
exit 1 exit 1
fi fi
fi fi
EXTRA_ARGS+=(
"--mount" "type=volume,source=account_$SANITIZED_ACCOUNT_NAME,target=/home/${USER_NAME}"
"--mount" "type=bind,source=$(pwd),target=/workdir"
)
# Run the container # Run the container
$CMD run \ $CMD run --rm -it \
--rm \ ${EXTRA_ARGS[@]} \
-it \ --name "azure-cli-$SANITIZED_ACCOUNT_NAME" \
--mount type=volume,source="account_$ACCOUNT_NAME",target=/home/${USER_NAME} \
--mount type=bind,source=$(pwd),target=/workdir \
--name "azure-cli" \
$HOSTNAME_ARG \
--workdir /workdir \ --workdir /workdir \
"$@" \ $IMAGE_NAME --user "$USER_NAME" "$@"
$IMAGE_NAME --user "$USER_NAME"