@@ -10,7 +10,7 @@ function require_installed() {
1010
1111# # Verify we have the foundations in place
1212
13- if [ " ${GEODESIC_SHELL} " == " true" ]; then
13+ if [ " ${GEODESIC_SHELL} " = " true" ]; then
1414 echo " Cannot run while in a geodesic shell"
1515 exit 1
1616fi
@@ -239,17 +239,33 @@ options_to_env
239239[ " $VERBOSE " = " true" ] && [ -n " $verbose_buffer " ] && printf " %s\n" " ${verbose_buffer[@]} "
240240
241241function debug() {
242- if [ " ${VERBOSE} " == " true" ]; then
243- echo " [DEBUG] $* "
242+ if [ " ${VERBOSE} " = " true" ]; then
243+ printf " [DEBUG] %s\n " " $* " >&2
244244 fi
245245}
246246
247+ function debug_and_run() {
248+ local noerr
249+ [ " $1 " = " --noerr" ] && noerr=true && shift
250+ debug ' >>>'
251+ debug " Running: $* "
252+ if [ " $noerr " = true ]; then
253+ " $@ " 2> /dev/null
254+ else
255+ " $@ "
256+ fi
257+ local status=$?
258+ debug " Exit status: $status "
259+ debug ' <<<'
260+ return $status
261+ }
262+
247263function _running_shell_pids() {
248- docker exec " ${DOCKER_NAME} " list-wrapper-shells 2> /dev/null
264+ debug_and_run --noerr docker exec " ${DOCKER_NAME} " list-wrapper-shells
249265}
250266
251267function _our_shell_pid() {
252- docker exec " ${DOCKER_NAME} " list-wrapper-shells " $WRAPPER_PID " 2> /dev/null || true
268+ debug_and_run --noerr docker exec " ${DOCKER_NAME} " list-wrapper-shells " $WRAPPER_PID " || true
253269}
254270
255271function _running_shell_count() {
@@ -258,14 +274,14 @@ function _running_shell_count() {
258274}
259275
260276function _on_shell_exit() {
261- command -v " ${ON_SHELL_EXIT:= geodesic_on_exit} " > /dev/null && " ${ON_SHELL_EXIT} "
277+ command -v " ${ON_SHELL_EXIT:= geodesic_on_exit} " > /dev/null && debug_and_run " ${ON_SHELL_EXIT} "
262278}
263279
264280function _on_container_exit() {
265281 export GEODESIC_EXITING_CONTAINER_ID=" ${CONTAINER_ID: 0: 12} "
266282 export GEODESIC_EXITING_CONTAINER_NAME=" ${DOCKER_NAME} "
267283 _on_shell_exit
268- [ -n " ${ON_CONTAINER_EXIT} " ] && command -v " ${ON_CONTAINER_EXIT} " > /dev/null && " ${ON_CONTAINER_EXIT} "
284+ [ -n " ${ON_CONTAINER_EXIT} " ] && command -v " ${ON_CONTAINER_EXIT} " > /dev/null && debug_and_run " ${ON_CONTAINER_EXIT} "
269285}
270286
271287# Call this function to wait for the container to exit, after all other shells have exited.
@@ -309,7 +325,7 @@ function run_exit_hooks() {
309325 # There can then be a further delay before the container exits.
310326 # So we need to build in some delays to allow for these events to occur.
311327
312- if [[ ${ONE_SHELL} == " true" ]]; then
328+ if [[ ${ONE_SHELL} = " true" ]]; then
313329 # We can expect the Docker container to exit quickly, and do not need to report on it.
314330 _on_container_exit
315331 return 0
@@ -320,7 +336,7 @@ function run_exit_hooks() {
320336
321337 # Best case scenario: no shells running
322338 if [ " ${# shell_pids[@]} " -eq 0 ]; then
323- wait_for_container_exit
339+ debug_and_run wait_for_container_exit
324340 return $?
325341 fi
326342
@@ -355,16 +371,42 @@ function run_exit_hooks() {
355371 [ $i -lt $n ] && echo " Finished." >&2 || printf " \nTimeout waiting for container shell to exit.\n" >&2
356372 fi
357373
358- wait_for_container_exit
374+ debug_and_run wait_for_container_exit
359375 return $?
360376}
361377
378+ function _exec_existing {
379+ if [ $# -eq 0 ]; then
380+ set -- " /bin/bash" " -l"
381+ fi
382+ [ -t 0 ] && DOCKER_EXEC_ARGS+=(-it)
383+ [ -z " ${GEODESIC_DOCKER_EXTRA_EXEC_ARGS} " ] || echo " # Exec'ing shell with extra Docker args: ${GEODESIC_DOCKER_EXTRA_EXEC_ARGS} " >&2
384+ # GEODESIC_DOCKER_EXTRA_EXEC_ARGS is not quoted because it is expected to be a list of arguments
385+
386+ # We set unusual detach keys because (a) the default first char is ctrl-p, which is used for command history,
387+ # and (b) if you detach from the shell, there is no way to reattach to it, so we want to effectively disable detach.
388+ debug_and_run docker exec --env G_HOST_PID=" ${WRAPPER_PID} " --detach-keys " ctrl-^,ctrl-[,ctrl-@" " ${DOCKER_EXEC_ARGS[@]} " ${GEODESIC_DOCKER_EXTRA_EXEC_ARGS} " ${DOCKER_NAME} " " $@ "
389+ }
390+
362391function use() {
363392 [ " $1 " = " use" ] && shift
364393 trap run_exit_hooks EXIT
365394
366395 export WRAPPER_PID=$$
367396
397+ if [ -n " ${GEODESIC_DOCKER_EXTRA_ARGS+x} " ]; then
398+ echo ' # WARNING: $GEODESIC_DOCKER_EXTRA_ARGS is deprecated. ' >&2
399+ echo ' # Use GEODESIC_DOCKER_EXTRA_LAUNCH_ARGS to configure container launch (`docker run`)' >&2
400+ echo ' # and GEODESIC_DOCKER_EXTRA_EXEC_ARGS to configure starting a new shell' >&2
401+ echo ' # in a running container (`docker exec`).' >&2
402+ if [ -n " ${GEODESIC_DOCKER_EXTRA_LAUNCH_ARGS+x} " ]; then
403+ echo ' # WARNING: Both $GEODESIC_DOCKER_EXTRA_ARGS and $GEODESIC_DOCKER_EXTRA_LAUNCH_ARGS are set. ' >&2
404+ echo ' # $GEODESIC_DOCKER_EXTRA_ARGS will be ignored.' >&2
405+ else
406+ export GEODESIC_DOCKER_EXTRA_LAUNCH_ARGS=" ${GEODESIC_DOCKER_EXTRA_ARGS} "
407+ fi
408+ fi
409+
368410 DOCKER_EXEC_ARGS=(--env LS_COLORS --env TERM --env TERM_COLOR --env TERM_PROGRAM)
369411 # Some settings from the host environment need to propagate into the container
370412 # Set them explicitly so they do not have to be exported in `launch-options.sh`
@@ -375,7 +417,7 @@ function use() {
375417 fi
376418 done
377419
378- if [[ ${GEODESIC_CUSTOMIZATION_DISABLED-false} == false ]]; then
420+ if [[ ${GEODESIC_CUSTOMIZATION_DISABLED-false} = false ]]; then
379421 if [ -n " ${GEODESIC_TRACE} " ]; then
380422 DOCKER_EXEC_ARGS+=(--env GEODESIC_TRACE)
381423 fi
@@ -394,13 +436,7 @@ function use() {
394436 CONTAINER_ID=$( docker ps --filter name=" ^/${DOCKER_NAME} \$ " --format ' {{ .ID }}' )
395437 if [ -n " $CONTAINER_ID " ]; then
396438 echo " # Starting shell in already running ${DOCKER_NAME} container ($CONTAINER_ID )"
397- if [ $# -eq 0 ]; then
398- set -- " /bin/bash" " -l"
399- fi
400- [ -t 0 ] && DOCKER_EXEC_ARGS+=(-it)
401- # We set unusual detach keys because (a) the default first char is ctrl-p, which is used for command history,
402- # and (b) if you detach from the shell, there is no way to reattach to it, so we want to effectively disable detach.
403- docker exec --env G_HOST_PID=$WRAPPER_PID --detach-keys " ctrl-^,ctrl-[,ctrl-@" " ${DOCKER_EXEC_ARGS[@]} " " ${DOCKER_NAME} " " $@ "
439+ _exec_existing " $@ "
404440 return 0
405441 fi
406442 fi
@@ -412,7 +448,7 @@ function use() {
412448 -e SSH_AUTH_SOCK=/run/host-services/ssh-auth.sock)
413449 fi
414450
415- if [ " ${WITH_DOCKER} " == " true" ]; then
451+ if [ " ${WITH_DOCKER} " = " true" ]; then
416452 # Bind-mount docker socket into container
417453 # Should work on Linux and Mac.
418454 # Note that the mounted /var/run/docker.sock is not a file or
@@ -590,26 +626,19 @@ function use() {
590626 if [ " $ONE_SHELL " = " true" ]; then
591627 [ -t 0 ] && DOCKER_EXEC_ARGS+=(-it)
592628 DOCKER_NAME=" ${DOCKER_NAME} -$( date +%d%H%M%S) "
593- echo " # Starting single shell ${DOCKER_NAME} session from ${DOCKER_IMAGE} "
594- echo " # Exposing port ${GEODESIC_PORT} "
595- [ -z " ${GEODESIC_DOCKER_EXTRA_ARGS } " ] || echo " # Launching with extra Docker args: ${GEODESIC_DOCKER_EXTRA_ARGS} "
596- # GEODESIC_DOCKER_EXTRA_ARGS is not quoted because it is expected to be a list of arguments
597- docker run --name " ${DOCKER_NAME} " " ${DOCKER_LAUNCH_ARGS[@]} " " ${DOCKER_EXEC_ARGS[@]} " ${GEODESIC_DOCKER_EXTRA_ARGS } " ${DOCKER_IMAGE} " -l " $@ "
629+ echo " # Starting single shell ${DOCKER_NAME} session from ${DOCKER_IMAGE} " >&2
630+ echo " # Exposing port ${GEODESIC_PORT} " >&2
631+ [ -z " ${GEODESIC_DOCKER_EXTRA_LAUNCH_ARGS } " ] || echo " # Launching with extra Docker args: ${GEODESIC_DOCKER_EXTRA_LAUNCH_ARGS} " >&2
632+ # GEODESIC_DOCKER_EXTRA_LAUNCH_ARGS is not quoted because it is expected to be a list of arguments
633+ debug_and_run docker run --name " ${DOCKER_NAME} " " ${DOCKER_LAUNCH_ARGS[@]} " " ${DOCKER_EXEC_ARGS[@]} " ${GEODESIC_DOCKER_EXTRA_LAUNCH_ARGS } " ${DOCKER_IMAGE} " -l " $@ "
598634 else
599- echo " # Running new ${DOCKER_NAME} container from ${DOCKER_IMAGE} "
600- echo " # Exposing port ${GEODESIC_PORT} "
601- [ -z " ${GEODESIC_DOCKER_EXTRA_ARGS} " ] || echo " # Launching with extra Docker args: ${GEODESIC_DOCKER_EXTRA_ARGS} "
602- # GEODESIC_DOCKER_EXTRA_ARGS is not quoted because it is expected to be a list of arguments
603- CONTAINER_ID=$( docker run --detach --init --name " ${DOCKER_NAME} " " ${DOCKER_LAUNCH_ARGS[@]} " " ${DOCKER_EXEC_ARGS[@]} " ${GEODESIC_DOCKER_EXTRA_ARGS} " ${DOCKER_IMAGE} " /usr/local/sbin/shell-monitor)
604- echo " # Started session ${CONTAINER_ID: 0: 12} . Starting shell via \` docker exec\` ..."
605- if [ $# -eq 0 ]; then
606- set -- " /bin/bash" " -l"
607- fi
608-
609- [ -t 0 ] && DOCKER_EXEC_ARGS+=(-it)
610- # We set unusual detach keys because (a) the default first char is ctrl-p, which is used for command history,
611- # and (b) if you detach from the shell, there is no way to reattach to it, so we want to effectively disable detach.
612- docker exec --env G_HOST_PID=$$ --detach-keys " ctrl-^,ctrl-[,ctrl-@" " ${DOCKER_EXEC_ARGS[@]} " " ${DOCKER_NAME} " " $@ "
635+ echo " # Running new ${DOCKER_NAME} container from ${DOCKER_IMAGE} " >&2
636+ echo " # Exposing port ${GEODESIC_PORT} " >&2
637+ [ -z " ${GEODESIC_DOCKER_EXTRA_LAUNCH_ARGS} " ] || echo " # Launching with extra Docker args: ${GEODESIC_DOCKER_EXTRA_LAUNCH_ARGS} " >&2
638+ # GEODESIC_DOCKER_EXTRA_LAUNCH_ARGS is not quoted because it is expected to be a list of arguments
639+ CONTAINER_ID=$( debug_and_run docker run --detach --init --name " ${DOCKER_NAME} " " ${DOCKER_LAUNCH_ARGS[@]} " " ${DOCKER_EXEC_ARGS[@]} " ${GEODESIC_DOCKER_EXTRA_LAUNCH_ARGS} " ${DOCKER_IMAGE} " /usr/local/sbin/shell-monitor)
640+ echo " # Started session ${CONTAINER_ID: 0: 12} . Starting shell via \` docker exec\` ..." >&2
641+ _exec_existing " $@ "
613642 fi
614643 true
615644}
0 commit comments