Fixed issues with setting up colored Bash prompt supporting displaying Git repository information.
All checks were successful
Build Docker Image / build (push) Successful in 38s
All checks were successful
Build Docker Image / build (push) Successful in 38s
This commit is contained in:
27
Dockerfile
27
Dockerfile
@@ -22,33 +22,18 @@ RUN apt-get update && apt-get install -y azure-cli
|
|||||||
RUN apt-get install -y zip unzip tree wget nano neovim \
|
RUN apt-get install -y zip unzip tree wget nano neovim \
|
||||||
python3 python3-venv python3-pip
|
python3 python3-venv python3-pip
|
||||||
|
|
||||||
# Configure Bash
|
COPY enable-git-bash-prompt /usr/local/bin/enable-git-bash-prompt
|
||||||
RUN cat <<'EOF' >> /etc/profile.d/git-prompt-helper
|
|
||||||
# Include Git prompt helpers if available
|
|
||||||
if [ -f /usr/lib/git-core/git-sh-prompt ]; then
|
|
||||||
source /usr/lib/git-core/git-sh-prompt
|
|
||||||
|
|
||||||
# Then PS1 can include __git_ps1 which is optimized and shows branch + state:
|
|
||||||
PS1='\[\e[32m\]AzureCLI (\[\e[35m\]${ACCOUNT_NAME}\[\e[0m\])\n\[\e[0m\]\[\e[34m\]\w\[\e[0m\]\[\e[33m\]$(__git_ps1 " (%s)")\[\e[0m\]\$ '
|
|
||||||
fi
|
|
||||||
|
|
||||||
export PATH=$HOME/bin:$PATH
|
|
||||||
EOF
|
|
||||||
|
|
||||||
|
# Patch the default .bashrc to enable git bash prompt
|
||||||
RUN cat <<'EOF' >> /etc/skel/.bashrc
|
RUN cat <<'EOF' >> /etc/skel/.bashrc
|
||||||
|
|
||||||
if ! command -v __git_ps1 > /dev/null; then
|
# Enable git bash prompt
|
||||||
source /etc/profile.d/git-prompt-helper
|
if [ -f /usr/local/bin/enable-git-bash-prompt ]; then
|
||||||
fi
|
source /usr/local/bin/enable-git-bash-prompt
|
||||||
EOF
|
|
||||||
|
|
||||||
RUN cat <<'EOF' >> /home/ubuntu/.bashrc
|
|
||||||
if ! command -v __git_ps1 > /dev/null; then
|
|
||||||
source /etc/profile.d/git-prompt-helper
|
|
||||||
fi
|
fi
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
COPY entrypoint.sh /entrypoint.sh
|
COPY entrypoint.sh /entrypoint.sh
|
||||||
RUN chmod +x /entrypoint.sh
|
RUN chmod +x /entrypoint.sh
|
||||||
ENTRYPOINT ["/entrypoint.sh"]
|
|
||||||
|
|
||||||
|
ENTRYPOINT ["/entrypoint.sh"]
|
||||||
|
|||||||
13
azure-cli
13
azure-cli
@@ -111,6 +111,16 @@ if [ ! "FOUND" = "$_FOUND" ]; then
|
|||||||
echo "Error: Failed to create volume $VOLUME_NAME." >&2
|
echo "Error: Failed to create volume $VOLUME_NAME." >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Volume created, initialize it
|
||||||
|
echo "Initializing volume $VOLUME_NAME."
|
||||||
|
if ! $CMD run --rm -it \
|
||||||
|
--mount "type=volume,source=$VOLUME_NAME,target=/home/${USER_NAME}" \
|
||||||
|
skoszewski/azure-cli:latest --user "$USER_NAME"; then
|
||||||
|
echo "Error: Failed to initialize volume $VOLUME_NAME." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "Volume $VOLUME_NAME initialized successfully."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
EXTRA_ARGS+=(
|
EXTRA_ARGS+=(
|
||||||
@@ -118,8 +128,9 @@ EXTRA_ARGS+=(
|
|||||||
"--mount" "type=bind,source=$(pwd),target=/workdir"
|
"--mount" "type=bind,source=$(pwd),target=/workdir"
|
||||||
"--env" "ACCOUNT_NAME=$ACCOUNT_NAME"
|
"--env" "ACCOUNT_NAME=$ACCOUNT_NAME"
|
||||||
"--name" "azure-cli-$VOLUME_NAME"
|
"--name" "azure-cli-$VOLUME_NAME"
|
||||||
|
"--user" "$USER_NAME"
|
||||||
"--workdir" "/workdir"
|
"--workdir" "/workdir"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Run the container
|
# Run the container as the specified user
|
||||||
$CMD run --rm -it ${EXTRA_ARGS[@]} $IMAGE_NAME --user "$USER_NAME" "$@"
|
$CMD run --rm -it ${EXTRA_ARGS[@]} $IMAGE_NAME --user "$USER_NAME" "$@"
|
||||||
|
|||||||
12
build
12
build
@@ -1,17 +1,13 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
# Find container runtime
|
# Find container runtime
|
||||||
if command -v podman &> /dev/null; then
|
if command -v docker &> /dev/null; then
|
||||||
CMD="podman"
|
echo "Using Dokcer as container runtime."
|
||||||
elif command -v docker &> /dev/null; then
|
docker buildx build -t skoszewski/azure-cli:latest .
|
||||||
CMD="docker"
|
|
||||||
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"
|
container build -t skoszewski/azure-cli:latest .
|
||||||
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
|
||||||
|
|
||||||
echo "Using container runtime: $CMD"
|
|
||||||
$CMD build -t skoszewski/azure-cli:latest .
|
|
||||||
|
|||||||
7
enable-git-bash-prompt
Normal file
7
enable-git-bash-prompt
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# Include Git prompt helpers if available
|
||||||
|
if [ -f /usr/lib/git-core/git-sh-prompt ]; then
|
||||||
|
source /usr/lib/git-core/git-sh-prompt
|
||||||
|
|
||||||
|
# Then PS1 can include __git_ps1 which is optimized and shows branch + state:
|
||||||
|
PS1='\[\e[32m\]AzureCLI (\[\e[35m\]${ACCOUNT_NAME}\[\e[0m\])\n\[\e[0m\]\[\e[34m\]\w\[\e[0m\]\[\e[33m\]$(__git_ps1 " (%s)")\[\e[0m\]\$ '
|
||||||
|
fi
|
||||||
@@ -2,42 +2,55 @@
|
|||||||
|
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
# Let's set some defaults
|
# Setup default values
|
||||||
SU_USER="ubuntu"
|
USER_NAME="ubuntu"
|
||||||
|
|
||||||
# Parse command line arguments
|
# Parse arguments
|
||||||
while [ $# -gt 0 ]; do
|
while [ $# -gt 0 ]; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
-u|--user)
|
--user|-u)
|
||||||
SU_USER="$2"
|
USER_NAME="$2"
|
||||||
shift 2
|
shift 2
|
||||||
;;
|
;;
|
||||||
|
--)
|
||||||
|
# Stop parsing arguments
|
||||||
|
shift
|
||||||
|
break
|
||||||
|
;;
|
||||||
*)
|
*)
|
||||||
break
|
break
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
# Check if the user exists
|
# Check, if we are running as root
|
||||||
if ! id -u "$SU_USER" >/dev/null 2>&1; then
|
if [ "$(id -u)" -eq 0 ]; then
|
||||||
useradd -m -s /usr/bin/bash "$SU_USER"
|
# Check, if the home directory exists for the specified user
|
||||||
|
if [ ! -d "/home/${USER_NAME}" ]; then
|
||||||
|
echo "Error: Home directory for user '${USER_NAME}' does not exist." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check, ownership of the home directory
|
||||||
|
OWNER_UID=$(stat -c '%u' "/home/${USER_NAME}")
|
||||||
|
OWNER_GID=$(stat -c '%g' "/home/${USER_NAME}")
|
||||||
|
if [ "${OWNER_UID}" -ne 0 ] || [ "${OWNER_GID}" -ne 0 ]; then
|
||||||
|
# The home directory is not owned by the specfied user, correct it
|
||||||
|
chown "${USER_NAME}:${USER_NAME}" "/home/${USER_NAME}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Re-initialize the contents of the home directory
|
||||||
|
su - "${USER_NAME}" -c "cp -a /etc/skel/. /home/${USER_NAME}/"
|
||||||
|
|
||||||
|
# We are done as root, quit. The container will be re-run as the specified user.
|
||||||
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check if the home directory exists
|
# Verify that we are running as the user owning the home directory
|
||||||
if [ ! -d "/home/$SU_USER" ]; then
|
if [ "$(id -un)" != "${USER_NAME}" ]; then
|
||||||
echo "A home directory for $SU_USER does not exist. Exiting."
|
echo "Error: The script must be run as user '${USER_NAME}'." >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check user ownership of the home directory
|
# Run shell
|
||||||
if [ "$(stat -c '%u' /home/$SU_USER)" -ne "$(id -u $SU_USER)" ]; then
|
exec /usr/bin/bash "$@"
|
||||||
# Change ownership of the home directory
|
|
||||||
chown $SU_USER:$SU_USER /home/$SU_USER
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Switch to the specified user
|
|
||||||
if [ $# -gt 0 ]; then
|
|
||||||
exec su $SU_USER -c "$@"
|
|
||||||
else
|
|
||||||
exec su $SU_USER
|
|
||||||
fi
|
|
||||||
|
|||||||
Reference in New Issue
Block a user