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