Skip to content

Commit

Permalink
Clean-up stdout and keep all misc messages on stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
travier committed Jul 22, 2022
1 parent 68a86ef commit 189c778
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions rhcos-toolbox
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ AUTHFILE=/var/lib/kubelet/config.json
TOOLBOX_NAME=toolbox-"$(whoami)"
TOOLBOXRC="${HOME}"/.toolboxrc

errecho() {
>&2 echo "${@}"
}

setup() {
# Allow user overrides
if [ -f "${TOOLBOXRC}" ]; then
echo ".toolboxrc file detected, overriding defaults..."
errecho ".toolboxrc file detected, overriding defaults..."
source "${TOOLBOXRC}"
fi
TOOLBOX_IMAGE="${REGISTRY}"/"${IMAGE}"
Expand All @@ -26,42 +30,41 @@ run() {
sudo podman rm "${TOOLBOX_NAME}"
fi
elif ! image_fresh; then
read -r -p "There is a newer version of ${TOOLBOX_IMAGE} available. Would you like to pull it? [y/N] "

>&2 read -r -p "There is a newer version of ${TOOLBOX_IMAGE} available. Would you like to pull it? [y/N] "
if [[ ${REPLY} =~ ^([Yy][Ee][Ss]|[Yy])+$ ]]; then
image_pull
if container_exists; then
sudo podman rm "${TOOLBOX_NAME}"
fi
else
echo "Skipping retrieval of new image.."
errecho "Skipping retrieval of new image.."
fi
fi

local runlabel=$(image_runlabel)
if ! container_exists; then
echo "Spawning a container '${TOOLBOX_NAME}' with image '${TOOLBOX_IMAGE}'"
errecho "Spawning a container '${TOOLBOX_NAME}' with image '${TOOLBOX_IMAGE}'"
if [[ -z "${runlabel}" ]] || [[ "${runlabel}" == "<no value>" ]]; then
container_create
else
echo "Detected RUN label in the container image. Using that as the default..."
errecho "Detected RUN label in the container image. Using that as the default..."
container_create_runlabel
fi
else
echo "Container '${TOOLBOX_NAME}' already exists. Trying to start..."
echo "(To remove the container and start with a fresh toolbox, run: sudo podman rm '${TOOLBOX_NAME}')"
errecho "Container '${TOOLBOX_NAME}' already exists. Trying to start..."
errecho "(To remove the container and start with a fresh toolbox, run: sudo podman rm '${TOOLBOX_NAME}')"
fi

local state=$(container_state)
if [[ "${state}" == configured ]] || [[ "${state}" == created ]] || [[ "${state}" == exited ]] || [[ "${state}" == stopped ]]; then
container_start
elif [[ "${state}" != running ]]; then
echo "Container '${TOOLBOX_NAME}' in unknown state: '$state'"
errecho "Container '${TOOLBOX_NAME}' in unknown state: '$state'"
return 1
fi

if [[ "$#" -eq "0" ]]; then
echo "Container started successfully. To exit, type 'exit'."
errecho "Container started successfully. To exit, type 'exit'."
fi
container_exec "$@"
}
Expand All @@ -85,11 +88,11 @@ image_exists() {
# returns 0 if the image on disk is "fresh", i.e. no newer image on remote
# registry. (or if we couldn't inspect the registry successfully)
image_fresh() {
echo "Checking if there is a newer version of ${TOOLBOX_IMAGE} available..."
errecho "Checking if there is a newer version of ${TOOLBOX_IMAGE} available..."
local_date=$(sudo podman image inspect "${TOOLBOX_IMAGE}" --format '{{.Created}}')

if ! remote_date=$(sudo skopeo inspect --authfile "${AUTHFILE}" docker://"${TOOLBOX_IMAGE}" --format '{{.Created}}'); then
echo "Error inspecting ${TOOLBOX_IMAGE} via skopeo"
errecho "Error inspecting ${TOOLBOX_IMAGE} via skopeo"
return
fi
# if the date on the registry is *NOT* newer than the local image date
Expand All @@ -105,13 +108,13 @@ image_runlabel() {

image_pull() {
if ! sudo --preserve-env podman pull --authfile "${AUTHFILE}" "${TOOLBOX_IMAGE}"; then
read -r -p "Would you like to manually authenticate to registry: '${REGISTRY}' and try again? [y/N] "
>&2 read -r -p "Would you like to manually authenticate to registry: '${REGISTRY}' and try again? [y/N] "

if [[ ${REPLY} =~ ^([Yy][Ee][Ss]|[Yy])+$ ]]; then
sudo --preserve-env podman login --authfile "${AUTHFILE}" "${REGISTRY}"
sudo --preserve-env podman pull --authfile "${AUTHFILE}" "${TOOLBOX_IMAGE}"
else
echo "Exiting..."
errecho "Exiting..."
exit 1
fi
fi
Expand All @@ -137,15 +140,15 @@ container_create() {
--volume /etc/machine-id:/etc/machine-id \
--volume /etc/localtime:/etc/localtime \
--volume /:/host \
"${TOOLBOX_IMAGE}" "${cmd}" 2>&1; then
echo "$0: failed to create container '${TOOLBOX_NAME}'"
"${TOOLBOX_IMAGE}" "${cmd}"; then
errecho "$0: failed to create container '${TOOLBOX_NAME}'"
exit 1
fi
}

container_start() {
if ! sudo podman start "${TOOLBOX_NAME}" 2>&1; then
echo "$0: failed to start container '${TOOLBOX_NAME}'"
if ! sudo podman start "${TOOLBOX_NAME}"; then
errecho "$0: failed to start container '${TOOLBOX_NAME}'"
exit 1
fi
}
Expand All @@ -161,7 +164,7 @@ container_create_runlabel() {
| sed 's/IMAGE=IMAGE/IMAGE=${TOOLBOX_IMAGE}/' \
| sed 's/host IMAGE/host ${TOOLBOX_IMAGE}/')"
if ! eval "${pod}" ; then
echo "$0: failed to create container from runlabel '${TOOLBOX_NAME}'"
errecho "$0: failed to create container from runlabel '${TOOLBOX_NAME}'"
exit 1
fi
}
Expand All @@ -175,7 +178,7 @@ container_exec() {
}

show_help() {
echo "USAGE: toolbox [-h/--help] [command]
errecho "USAGE: toolbox [-h/--help] [command]
toolbox is a small script that launches a container to let you bring in your favorite debugging or admin tools.
The toolbox container is a pet container and will be restarted on following runs.
To remove the container and start fresh, do sudo podman rm ${TOOLBOX_NAME}.
Expand Down

0 comments on commit 189c778

Please sign in to comment.