Enhanced azure-cli with list command, and more robust volume handling arguments.
This commit is contained in:
67
azure-cli
67
azure-cli
@@ -3,9 +3,15 @@
|
||||
# Set default values
|
||||
ACCOUNT_NAME="$(id -un)"
|
||||
USER_NAME="ubuntu"
|
||||
EXTRA_ARGS=()
|
||||
LIST=""
|
||||
|
||||
while [ $# -gt 0 ]; do
|
||||
case $1 in
|
||||
--list|-l)
|
||||
LIST=true
|
||||
break
|
||||
;;
|
||||
--account|-a)
|
||||
ACCOUNT_NAME="$2"
|
||||
shift 2
|
||||
@@ -14,6 +20,26 @@ while [ $# -gt 0 ]; do
|
||||
USER_NAME="$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
|
||||
;;
|
||||
@@ -23,43 +49,48 @@ done
|
||||
IMAGE_NAME="skoszewski/azure-cli:latest"
|
||||
|
||||
# Normalize account name for use in volume name
|
||||
ACCOUNT_NAME="${ACCOUNT_NAME/@/_at_}"
|
||||
ACCOUNT_NAME="${ACCOUNT_NAME//[-.]/_}"
|
||||
SANITIZED_ACCOUNT_NAME="${ACCOUNT_NAME/@/_at_}"
|
||||
SANITIZED_ACCOUNT_NAME="${SANITIZED_ACCOUNT_NAME//[-.]/_}"
|
||||
|
||||
# Find container runtime
|
||||
if command -v podman &> /dev/null; then
|
||||
CMD="podman"
|
||||
HOSTNAME_ARG="--hostname $(hostname -s)"
|
||||
EXTRA_ARGS+=("--hostname $(hostname -s)")
|
||||
elif command -v docker &> /dev/null; then
|
||||
CMD="docker"
|
||||
HOSTNAME_ARG="--hostname $(hostname -s)"
|
||||
EXTRA_ARGS+=("--hostname $(hostname -s)")
|
||||
elif command -v container &> /dev/null; then
|
||||
# Apple container command line tool
|
||||
CMD="container"
|
||||
HOSTNAME_ARG=""
|
||||
else
|
||||
echo "Error: No usable container runtime was found." >&2
|
||||
exit 1
|
||||
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
|
||||
# This ensures persistent storage for the Azure CLI configuration
|
||||
if ! $CMD volume inspect "account_$ACCOUNT_NAME" &> /dev/null; then
|
||||
echo "Creating volume account_$ACCOUNT_NAME for Azure CLI configuration."
|
||||
if ! $CMD volume create "account_$ACCOUNT_NAME" &> /dev/null; then
|
||||
echo "Error: Failed to create volume account_$ACCOUNT_NAME." >&2
|
||||
if ! $CMD volume inspect "account_$SANITIZED_ACCOUNT_NAME" &> /dev/null; then
|
||||
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
|
||||
exit 1
|
||||
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
|
||||
$CMD run \
|
||||
--rm \
|
||||
-it \
|
||||
--mount type=volume,source="account_$ACCOUNT_NAME",target=/home/${USER_NAME} \
|
||||
--mount type=bind,source=$(pwd),target=/workdir \
|
||||
--name "azure-cli" \
|
||||
$HOSTNAME_ARG \
|
||||
$CMD run --rm -it \
|
||||
${EXTRA_ARGS[@]} \
|
||||
--name "azure-cli-$SANITIZED_ACCOUNT_NAME" \
|
||||
--workdir /workdir \
|
||||
"$@" \
|
||||
$IMAGE_NAME --user "$USER_NAME"
|
||||
$IMAGE_NAME --user "$USER_NAME" "$@"
|
||||
|
||||
Reference in New Issue
Block a user