Skip to content

Commit d074791

Browse files
committed
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
1 parent 62b009b commit d074791

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

launcher

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ compare_version() {
140140
return 1
141141
}
142142

143+
die () {
144+
echo -e "\n$1\n" >&2
145+
exit "${2:-1}"
146+
}
143147

144148
install_docker() {
145149
echo "Docker is not installed, you will need to install Docker in order to run Launcher"
@@ -660,61 +664,56 @@ run_bootstrap() {
660664
set_volumes
661665
set_links
662666

663-
rm -f $cidbootstrap
664-
665-
unset ERR
666-
667-
$docker_path run $user_args --rm -i $image gem which pups || ERR=$?
668-
if [[ "$ERR" ]]; then
667+
if $docker_path run $user_args --rm -i $image gem which pups; then
668+
pups_command="/usr/local/bin/pups --stdin"
669+
else
669670
# Fallback to git pull method here if `pups` was not installed by gem in base image
670671
pups_command="cd /pups &&"
671672
if [[ ! "false" = $update_pups ]]; then
672673
pups_command="$pups_command git pull && git checkout $pups_version &&"
673674
fi
674675
pups_command="$pups_command /pups/bin/pups --stdin"
675-
else
676-
pups_command="/usr/local/bin/pups --stdin"
677676
fi
678677

679678
echo $pups_command
680679

681-
unset ERR
682-
683-
tmp_input_file=$(mktemp)
680+
declare -i BOOTSTRAP_EXITCODE
681+
rm -f $cidbootstrap
684682

685-
echo "$input" > "$tmp_input_file"
686-
(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 \
687-
/bin/bash -c "$pups_command") || ERR=$?
683+
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 \
684+
/bin/bash -c "$pups_command"
685+
BOOTSTRAP_EXITCODE=$?
688686

689-
rm -f "$tmp_input_file"
687+
CONTAINER_ID=$(cat "$cidbootstrap")
688+
rm -f "$cidbootstrap"
690689

691-
unset FAILED
692690
# magic exit code that indicates a retry
693-
if [[ "$ERR" == 77 ]]; then
694-
$docker_path rm `cat $cidbootstrap`
695-
rm $cidbootstrap
691+
if [[ $BOOTSTRAP_EXITCODE -eq 77 ]]; then
692+
$docker_path rm "$CONTAINER_ID"
696693
exit 77
697-
elif [[ "$ERR" > 0 ]]; then
698-
FAILED=TRUE
699-
fi
694+
elif [[ $BOOTSTRAP_EXITCODE -gt 0 ]]; then
695+
echo "bootstrap failed with exit code $BOOTSTRAP_EXITCODE"
696+
echo "** FAILED TO BOOTSTRAP ** please scroll up and look for earlier error messages, there may be more than one."
697+
echo "./discourse-doctor may help diagnose the problem."
700698

701-
if [[ $FAILED = "TRUE" ]]; then
702-
if [[ ! -z "$DEBUG" ]]; then
703-
$docker_path commit `cat $cidbootstrap` $local_discourse/$config-debug || echo 'FAILED TO COMMIT'
704-
echo "** DEBUG ** Maintaining image for diagnostics $local_discourse/$config-debug"
699+
if [[ -n "$DEBUG" ]]; then
700+
if $docker_path commit "$CONTAINER_ID" $local_discourse/$config-debug; then
701+
echo "** DEBUG ** Maintaining image for diagnostics $local_discourse/$config-debug"
702+
else
703+
echo "** DEBUG ** Failed to commit container $CONTAINER_ID for diagnostics"
704+
fi
705705
fi
706706

707-
$docker_path rm `cat $cidbootstrap`
708-
rm $cidbootstrap
709-
echo "** FAILED TO BOOTSTRAP ** please scroll up and look for earlier error messages, there may be more than one."
710-
echo "./discourse-doctor may help diagnose the problem."
707+
$docker_path rm "$CONTAINER_ID"
711708
exit 1
712709
fi
713710

714711
sleep 5
715712

716-
$docker_path commit `cat $cidbootstrap` $local_discourse/$config || echo 'FAILED TO COMMIT'
717-
$docker_path rm `cat $cidbootstrap` && rm $cidbootstrap
713+
$docker_path commit \
714+
"$CONTAINER_ID" \
715+
$local_discourse/$config || die "FAILED TO COMMIT $CONTAINER_ID"
716+
$docker_path rm "$CONTAINER_ID"
718717
}
719718

720719
case "$command" in

0 commit comments

Comments
 (0)