Skip to content

Commit

Permalink
simplify the entry-point and cmd.
Browse files Browse the repository at this point in the history
  • Loading branch information
mazgi committed Sep 27, 2022
1 parent cab0f61 commit 9e6d2b8
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 7 deletions.
31 changes: 26 additions & 5 deletions Dockerfile.d/provisioning/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
# See https://github.com/mazgi/dockerfiles/blob/main/Dockerfile.d/provisioning/with-user.Dockerfile
# See https://github.com/mazgi/dockerfiles/blob/main/Dockerfile.d/provisioning/customize-example.Dockerfile
FROM ghcr.io/mazgi/provisioning

# Set in non-interactive mode.
ENV DEBIAN_FRONTEND=noninteractive

# https://www.terraform.io/downloads.html
ARG TERRAFORM_VERSIONS=1.2.8
ENV TERRAFORM_VERSIONS=${TERRAFORM_VERSIONS}

ARG DOCKER_GID
ARG GID=0
ARG UID=0
ENV DOCKER_GID=${DOCKER_GID}
ENV GID=${GID:-0}
ENV UID=${UID:-0}

COPY rootfs /
RUN :\
&& cd /usr/local/bin\
&& ln -fs echo-with-color.zsh echoDebug\
&& ln -fs echo-with-color.zsh echoInfo\
&& ln -fs echo-with-color.zsh echoWarn\
&& ln -fs echo-with-color.zsh echoErr\
&& :

HEALTHCHECK --interval=2s --timeout=1s --retries=2 --start-period=5s\
CMD jq -e ". | select(.succeeded)" $(docker-util.keep-running.zsh --print-status-file-path)

RUN : Terraform\
# Install Terraform via tfenv
&& echo $TERRAFORM_VERSIONS | tr ',' '\n' | xargs -IV tfenv install V\
# use the beginning version on the list
&& tfenv use ${TERRAFORM_VERSIONS%%,*}\
&& :

RUN :\
# Create a user for development who has the same UID and GID as you.
&& useradd --comment '' --create-home --gid users --uid ${UID} developer\
Expand All @@ -21,12 +44,10 @@ RUN :\
&& usermod --append --groups docker developer 2> /dev/null || true\
# It will be duplicate UID or GID with "node" user when your UID==1000 or GID==100.
&& echo '%users ALL=(ALL) NOPASSWD: ALL' > /etc/sudoers.d/grant-all-without-password-to-users\
&& echo '%developer ALL=(ALL) NOPASSWD: ALL' > /etc/sudoers.d/grant-all-without-password-to-developer
&& echo '%developer ALL=(ALL) NOPASSWD: ALL' > /etc/sudoers.d/grant-all-without-password-to-developer\
&& :

# Reset DEBIAN_FRONTEND to default(`dialog`).
# If you no need `dialog`, you can set `DEBIAN_FRONTEND=readline`.
# see also: man 7 debconf
ENV DEBIAN_FRONTEND=

COPY rootfs /
ENTRYPOINT [ "/usr/local/bin/docker-entrypoint.start-services.zsh" ]

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env -S zsh -eu

readonly local SCRIPT_NAME=$(basename $0)
readonly local STATUS_FILE=/var/run/container-stat-myself/status.json
getStatusFilePath () {
echo ${STATUS_FILE:?}
}
resetStatus () {
echoDebug "Reset the status."
readonly local _dir=$(dirname ${STATUS_FILE:?})
sudo mkdir -p ${_dir:?}/
sudo chmod 777 ${_dir:?}/
echo '{}' > ${STATUS_FILE:?}
}
updateStatusToSucceeded () {
echoDebug "Update the status to succeeded."
readonly local _tmpfile=${STATUS_FILE:?}.tmp
cp ${STATUS_FILE:?} ${_tmpfile:?}
jq '. + {succeeded:true}' ${_tmpfile:?} | tee ${STATUS_FILE:?}
rm -rf ${_tmpfile:?}
}
updateStatusToFailed () {
echoDebug "Update the status to succeeded."
readonly local _tmpfile=${STATUS_FILE:?}.tmp
cp ${STATUS_FILE:?} ${_tmpfile:?}
jq '. + {succeeded:false}' ${_tmpfile:?} | tee ${STATUS_FILE:?}
rm -rf ${_tmpfile:?}
}

showHelp() {
echo "Usage: ${SCRIPT_NAME} [OPTIONS]"
echo "Options:"
echo "\t-h, --help\t\tShow this help message."
echo "\t-p, --print-status-file-path\t\t(TBD)."
echo "\t-f, --record-failure\t\t(TBD)."
echo "\t-r, --reset-status\t\t(TBD)."
echo "\t-s, --record-success\t\t(TBD)."
echo "\t-w, --wait-signals\t\t(TBD)."
}

args=$(getopt -o hpfrsw -l help,print-status-file-path,record-failure,reset-status,record-success,wait-signals -- "$@") || exit 1
eval "set -- $args"
while [ $# -gt 0 ]; do
case $1 in
-h | --help)
showHelp
shift; exit 1;;
-p | --print-status-file-path)
getStatusFilePath
shift; exit 0;;
-f | --record-failure)
updateStatusToFailed
shift; exit 0;;
-r | --reset-status)
resetStatus
shift; exit 0;;
-s | --record-success)
updateStatusToSucceeded
shift; exit 0;;
-w | --wait-signals)
# Nothing todo.
shift; break;;
--)
shift; break;;
esac
done

trapSigTerm () {
echoDebug "Exit by SIGTERM."
trap SIGTERM
exit 0
}
trap 'trapSigTerm' SIGTERM

echoInfo '✅ You are all set!'
# http:https://patorjk.com/software/taag/#p=display&f=Calvin%20S&t=you're%20all%20set
echoDebug '┬ ┬┌─┐┬ ┬┬─┐┌─┐ ┌─┐┬ ┬ ┌─┐┌─┐┌┬┐'
echoDebug '└┬┘│ ││ │├┬┘├┤ ├─┤│ │ └─┐├┤ │ '
echoDebug ' ┴ └─┘└─┘┴└─└─┘ ┴ ┴┴─┘┴─┘ └─┘└─┘ ┴ '
echoDebug "Turn in infinity sleep..."
sleep infinity &; readonly local SLEEP_PID=$!
wait ${SLEEP_PID}
exit $?
28 changes: 28 additions & 0 deletions Dockerfile.d/provisioning/rootfs/usr/local/bin/echo-with-color.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env -S zsh -eu

readonly local SCRIPT_NAME=$(basename $0)
readonly local termColorClear='\033[0m'
echoDebug() {
readonly local termColorDebug='\033[1;34m'
echo -e "${termColorDebug}$@${termColorClear}"
}
echoInfo() {
readonly local termColorInfo='\033[1;32m'
echo -e "${termColorInfo}$@${termColorClear}"
}
echoWarn() {
readonly local termColorWarn='\033[1;33m'
echo -e "${termColorWarn}$@${termColorClear}"
}
echoErr() {
readonly local termColorErr='\033[1;31m'
echo -e "${termColorErr}$@${termColorClear}"
}

# Export functions by names
case ${SCRIPT_NAME} in
echoDebug | echoInfo |echoWarn | echoErr)
${SCRIPT_NAME} "$@"
exit 0
;;
esac

0 comments on commit 9e6d2b8

Please sign in to comment.