From d0747918e79ab743a1cde307f3fc5a46f3a31fe3 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Wed, 30 Mar 2022 22:46:34 -0400 Subject: [PATCH] Cleanup code in the bootstrap function I came here to do something else, but could not help cleaning up the bootstrap code. * add die() helper * pull the container ID into an environment variable right away instead of interpolating it every time * we don't need to write out to a file prior to piping it into docker * use $() instead of backticks * tidied up messaging from failed docker commits --- launcher | 63 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/launcher b/launcher index 1154ba1..4c175d0 100755 --- a/launcher +++ b/launcher @@ -140,6 +140,10 @@ compare_version() { return 1 } +die () { + echo -e "\n$1\n" >&2 + exit "${2:-1}" +} install_docker() { echo "Docker is not installed, you will need to install Docker in order to run Launcher" @@ -660,61 +664,56 @@ run_bootstrap() { set_volumes set_links - rm -f $cidbootstrap - - unset ERR - - $docker_path run $user_args --rm -i $image gem which pups || ERR=$? - if [[ "$ERR" ]]; then + if $docker_path run $user_args --rm -i $image gem which pups; then + pups_command="/usr/local/bin/pups --stdin" + else # Fallback to git pull method here if `pups` was not installed by gem in base image pups_command="cd /pups &&" if [[ ! "false" = $update_pups ]]; then pups_command="$pups_command git pull && git checkout $pups_version &&" fi pups_command="$pups_command /pups/bin/pups --stdin" - else - pups_command="/usr/local/bin/pups --stdin" fi echo $pups_command - unset ERR - - tmp_input_file=$(mktemp) + declare -i BOOTSTRAP_EXITCODE + rm -f $cidbootstrap - echo "$input" > "$tmp_input_file" - (exec cat "$tmp_input_file" | $docker_path run --shm-size=512m $user_args $links "${env[@]}" -e DOCKER_HOST_IP="$docker_ip" --cidfile $cidbootstrap -i -a stdin -a stdout -a stderr $volumes $image \ - /bin/bash -c "$pups_command") || ERR=$? + echo "$input" | $docker_path run --shm-size=512m $user_args $links "${env[@]}" -e DOCKER_HOST_IP="$docker_ip" --cidfile "$cidbootstrap" -i -a stdin -a stdout -a stderr $volumes $image \ + /bin/bash -c "$pups_command" + BOOTSTRAP_EXITCODE=$? - rm -f "$tmp_input_file" + CONTAINER_ID=$(cat "$cidbootstrap") + rm -f "$cidbootstrap" - unset FAILED # magic exit code that indicates a retry - if [[ "$ERR" == 77 ]]; then - $docker_path rm `cat $cidbootstrap` - rm $cidbootstrap + if [[ $BOOTSTRAP_EXITCODE -eq 77 ]]; then + $docker_path rm "$CONTAINER_ID" exit 77 - elif [[ "$ERR" > 0 ]]; then - FAILED=TRUE - fi + elif [[ $BOOTSTRAP_EXITCODE -gt 0 ]]; then + echo "bootstrap failed with exit code $BOOTSTRAP_EXITCODE" + echo "** FAILED TO BOOTSTRAP ** please scroll up and look for earlier error messages, there may be more than one." + echo "./discourse-doctor may help diagnose the problem." - if [[ $FAILED = "TRUE" ]]; then - if [[ ! -z "$DEBUG" ]]; then - $docker_path commit `cat $cidbootstrap` $local_discourse/$config-debug || echo 'FAILED TO COMMIT' - echo "** DEBUG ** Maintaining image for diagnostics $local_discourse/$config-debug" + if [[ -n "$DEBUG" ]]; then + if $docker_path commit "$CONTAINER_ID" $local_discourse/$config-debug; then + echo "** DEBUG ** Maintaining image for diagnostics $local_discourse/$config-debug" + else + echo "** DEBUG ** Failed to commit container $CONTAINER_ID for diagnostics" + fi fi - $docker_path rm `cat $cidbootstrap` - rm $cidbootstrap - echo "** FAILED TO BOOTSTRAP ** please scroll up and look for earlier error messages, there may be more than one." - echo "./discourse-doctor may help diagnose the problem." + $docker_path rm "$CONTAINER_ID" exit 1 fi sleep 5 - $docker_path commit `cat $cidbootstrap` $local_discourse/$config || echo 'FAILED TO COMMIT' - $docker_path rm `cat $cidbootstrap` && rm $cidbootstrap + $docker_path commit \ + "$CONTAINER_ID" \ + $local_discourse/$config || die "FAILED TO COMMIT $CONTAINER_ID" + $docker_path rm "$CONTAINER_ID" } case "$command" in -- 2.25.1