Skip to content

Commit 289c09e

Browse files
authored
Split extra args into launch and exec (#976)
1 parent 6dc06cd commit 289c09e

File tree

6 files changed

+100
-41
lines changed

6 files changed

+100
-41
lines changed

ReleaseNotes-v4.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,3 +722,11 @@ V4 changes:
722722
a "monorepo" containing multiple projects. It must be an absolute path either equal to or a subdirectory of `WORKSPACE_FOLDER_HOST_DIR`.
723723
- `GEODESIC_TERM_THEME` is normally unset. Set it to "light" or "dark" _before launching Geodesic_ to disable the initial attempt at automatic terminal light/dark theme detection, and use the set value instead.
724724
- `GEODESIC_TERM_THEME_AUTO` is normally unset. Set it to "enabled" to enable automatic attempts to detect changes in terminal light/dark theme. This feature should be considered experimental and may not work as expected.
725+
726+
Starting with Geodesic version 4.4.0:
727+
728+
- `GEODESIC_DOCKER_EXTRA_ARGS` is deprecated. Geodesic distinguishes between options for launching a container
729+
and options for exec’ing into a running one. Use `GEODESIC_DOCKER_EXTRA_LAUNCH_ARGS` for container launch and
730+
`GEODESIC_DOCKER_EXTRA_EXEC_ARGS` for exec’ing into a running container. As with `GEODESIC_DOCKER_EXTRA_ARGS`,
731+
the new variables are placed unquoted on the command line, so they cannot contain arguments with
732+
spaces or other special characters, and quoting will not help.

docs/environment.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ Geodesic version 4 additions and changes:
4040
- `MAP_FILE_OWNERSHIP` replaces `GEODESIC_HOST_BINDFS_ENABLED`. If set to true, Geodesic will use `bindfs` to map file ownership
4141
between the host and container. This not normally needed, as it should be handled automatically by Docker.
4242

43+
Starting with Geodesic version 4.4.0:
44+
45+
- `GEODESIC_DOCKER_EXTRA_ARGS` is deprecated. Geodesic distinguishes between options for launching a container
46+
and options for exec’ing into a running one. Use `GEODESIC_DOCKER_EXTRA_LAUNCH_ARGS` for container launch and
47+
`GEODESIC_DOCKER_EXTRA_EXEC_ARGS` for exec’ing into a running container. As with `GEODESIC_DOCKER_EXTRA_ARGS`,
48+
the new variables are placed unquoted on the command line, so they cannot contain arguments with
49+
spaces or other special characters, and quoting will not help.
50+
4351
### Geodesic Version 3 Environment Variables
4452

4553
Below is a list of environment variables that may be visible in the shell and were present in Geodesic v3.

rootfs/etc/profile.d/_01-launch-warning.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
# Specifically, this guards against:
1212
# docker run -it cloudposse/geodesic:latest-debian | bash
1313

14+
# It works by outputting a command that will print a warning message to stderr and then exit,
15+
# which will be evaluated by the shell receiving the output, which will print the message.
16+
# It also outputs backspaces to erase the message, so the message does not appear
17+
# on the screen during the normal startup processing if the output is a terminal.
18+
1419
# If the warning message is longer than the terminal width,
1520
# on some terminals the message may be truncated. In that case,
1621
# all the erasure characters will not be printed, and the message will not be erased.
@@ -59,5 +64,5 @@ function warn_if_piped() {
5964
trap - EXIT
6065
}
6166

62-
warn_if_piped
67+
[[ -t 0 ]] && warn_if_piped
6368
unset -f warn_if_piped

rootfs/etc/profile.d/_60-register-stray-shells.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,13 @@
1919

2020
if [[ $$ != 1 ]] && [[ $PPID == 0 ]] && [[ -z $G_HOST_PID ]]; then
2121
export G_HOST_PID=0
22-
[[ -t 0 ]] && yellow '# Detected shell launched by `docker exec` without wrapper info, tracking as stray shell.'
22+
[[ -t 0 ]] && yellow '# Detected shell launched by `docker exec` without wrapper info, tracking as stray shell.' >&2
23+
fi
24+
25+
if [[ $$ == 1 ]] && ! [[ -t 0 ]] && [[ $# -eq 0 ]]; then
26+
echo >&2
27+
echo "# You have launched a login shell without a terminal and without a command." >&2
28+
echo "# This is not supported. Please run a command or attach a terminal." >&2
29+
echo >&2
30+
exit 1
2331
fi

rootfs/templates/wrapper-body.sh

Lines changed: 67 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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
1616
fi
@@ -239,17 +239,33 @@ options_to_env
239239
[ "$VERBOSE" = "true" ] && [ -n "$verbose_buffer" ] && printf "%s\n" "${verbose_buffer[@]}"
240240

241241
function 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+
247263
function _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

251267
function _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

255271
function _running_shell_count() {
@@ -258,14 +274,14 @@ function _running_shell_count() {
258274
}
259275

260276
function _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

264280
function _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+
362391
function 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
}

rootfs/usr/local/bin/boot

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ else
1919
function color() { echo "$*" >&2; }
2020
color "########################################################################################"
2121
color "# Attach a terminal (docker run --rm --it ...) if you want to run a shell."
22-
color "# Run the following to install the script that runs ${APP_NAME:-$(basename ${DOCKER_IMAGE:-Geodesic})} "
22+
color "#"
23+
color "# Run the following to install the script that runs ${APP_NAME:-$(basename ${DOCKER_IMAGE:-Geodesic})}"
2324
color "# with all its features (the recommended way to use Geodesic):"
2425
fi
2526

0 commit comments

Comments
 (0)