Added an option to specify a custom volume name.

This commit is contained in:
2025-12-15 09:02:33 +01:00
parent a40a96e662
commit 374b4e48bf

View File

@@ -5,6 +5,7 @@ ACCOUNT_NAME="$(id -un)"
USER_NAME="ubuntu"
EXTRA_ARGS=()
LIST=""
VOLUME_NAME=""
while [ $# -gt 0 ]; do
case $1 in
@@ -12,6 +13,10 @@ while [ $# -gt 0 ]; do
LIST=true
break
;;
--name|-n)
VOLUME_NAME="$2"
shift 2
;;
--account|-a)
ACCOUNT_NAME="$2"
shift 2
@@ -33,10 +38,16 @@ while [ $# -gt 0 ]; do
--help|-h)
cat <<EOF
Usage: $(basename "$0")
[--list|-l]
or
[--account|-a ACCOUNT_NAME]
[--name|-n VOLUME_NAME]
[--user|-u USER_NAME]
[--volume|-v HOST_PATH:CONTAINER_PATH]
[additional docker/podman/container args]
[--]
[additional args to pass to container]
EOF
exit 0
;;
@@ -48,9 +59,12 @@ done
IMAGE_NAME="skoszewski/azure-cli:latest"
# Check if a custom volume name is provided
if [ -z "$VOLUME_NAME" ]; then
# Normalize account name for use in volume name
SANITIZED_ACCOUNT_NAME="${ACCOUNT_NAME/@/_at_}"
SANITIZED_ACCOUNT_NAME="${SANITIZED_ACCOUNT_NAME//[-.]/_}"
VOLUME_NAME="account_${ACCOUNT_NAME/@/_at_}"
VOLUME_NAME="${VOLUME_NAME//[-.]/_}"
fi
# Find container runtime
if command -v podman &> /dev/null; then
@@ -75,28 +89,29 @@ fi
# Check, if the account volume exists, if not create it
# This ensures persistent storage for the Azure CLI configuration
if ! $CMD volume inspect "account_$SANITIZED_ACCOUNT_NAME" &> /dev/null; then
if [ ! "FOUND" = "$($CMD volume ls --format json | jq -r --arg name "$VOLUME_NAME" --arg account "$ACCOUNT_NAME" '.[] | select(.name == $name or .labels.account == $account) | "FOUND"')" ]; then
echo "A volume for account '$ACCOUNT_NAME' does not exist."
read -p "Would you like to create one? (y/n) " -r RESPONSE
if [[ ! "$RESPONSE" =~ ^[Yy]$ ]]; then
echo "Aborting."
exit 1
fi
echo "Creating volume account_$SANITIZED_ACCOUNT_NAME for Azure CLI configuration."
if ! $CMD volume create "account_$SANITIZED_ACCOUNT_NAME" --label "account=$ACCOUNT_NAME" &> /dev/null; then
echo "Error: Failed to create volume account_$SANITIZED_ACCOUNT_NAME." >&2
echo "Creating volume $VOLUME_NAME for Azure CLI configuration."
if ! $CMD volume create "$VOLUME_NAME" --label "account=$ACCOUNT_NAME" &> /dev/null; then
echo "Error: Failed to create volume $VOLUME_NAME." >&2
exit 1
fi
fi
EXTRA_ARGS+=(
"--mount" "type=volume,source=account_$SANITIZED_ACCOUNT_NAME,target=/home/${USER_NAME}"
"--mount" "type=volume,source=$VOLUME_NAME,target=/home/${USER_NAME}"
"--mount" "type=bind,source=$(pwd),target=/workdir"
)
# Run the container
$CMD run --rm -it \
${EXTRA_ARGS[@]} \
--name "azure-cli-$SANITIZED_ACCOUNT_NAME" \
--name "azure-cli-$VOLUME_NAME" \
--workdir /workdir \
$IMAGE_NAME --user "$USER_NAME" "$@"