Skip to content

Commit

Permalink
Skip container launch config and logging when simply exec'ing a shell
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuru committed Jan 24, 2025
1 parent 6e637c2 commit 9e3715b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 32 deletions.
7 changes: 7 additions & 0 deletions ReleaseNotes-v4.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ Now, you can create a `launch-options.sh` file in the Geodesic configuration dir
to customize the launch of the Geodesic container. The directory search path and the
priority of the files are the same as for the other Geodesic customization files.

Note that most of the launch options configure the launching of the Docker container,
and therefore have no effect when you run `geodesic` to start a new shell
inside an already running container. However, you can use the `--solo` option to
force Geodesic to start a new container to pick up the new launch options. You
can also add `ONE_SHELL=true` to the `launch-options.sh` file to force Geodesic to
start a new container each time you run it.

#### Better File System Layout

Geodesic no longer mounts the entire host user's home directory into the container.
Expand Down
15 changes: 10 additions & 5 deletions rootfs/etc/profile.d/_20-mounts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ function _map_mounts() {
if [[ "$MAP_FILE_OWNERSHIP" == "true" ]]; then
local src="/.FS_HOST"
local dest="/.FS_CONT"
if
if [[ -d "${src}" ]]; then
map="${dest}"
[[ -d "${src}" ]]
then
mkdir -p "${dest}"
else
red "# ERROR: Supposed host mount directory ${src} does not exist. Fatal error."
Expand Down Expand Up @@ -183,9 +181,13 @@ function _add_symlinks() {
done
}

if [[ $SHLVL == 1 ]]; then
# Ensure we only run this once per container
if [[ -f "${GEODESIC_HOST_PATHS_CACHE:=/tmp/geodesic-host-paths}" ]]; then
# Source the cached paths
. "${GEODESIC_HOST_PATHS_CACHE}"
else
_map_mounts
_add_symlinks
_add_symlinks

# Ensure we do not have paths that match everything
paths=("${GEODESIC_HOST_PATHS[@]}")
Expand All @@ -196,6 +198,9 @@ if [[ $SHLVL == 1 ]]; then
GEODESIC_HOST_PATHS+=("$p")
fi
done
declare -p GEODESIC_HOST_PATHS >"${GEODESIC_HOST_PATHS_CACHE}"
chmod 644 "${GEODESIC_HOST_PATHS_CACHE}"
fi

unset GEODESIC_HOST_PATHS_CACHE
unset -f _map_mounts _map_owner _map_host _ensure_dest paths _add_symlinks
61 changes: 34 additions & 27 deletions rootfs/templates/wrapper-body.sh
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ function parse_args() {
--solo)
export ONE_SHELL=true
;;
--no-solo|--no-one-shell)
export ONE_SHELL=false
;;
--trace)
export GEODESIC_TRACE=custom
;;
Expand Down Expand Up @@ -163,13 +166,11 @@ function help() {
echo " help Show this help"
echo " stop [container-name] Stop a running Geodesic container"
echo ""
echo " Options when starting a container:"
echo " --workspace Set which host directory is used as the working directory in the container "
echo ""
echo " Options when starting a shell:"
echo " Options:"
echo " --no-custom Disable loading of custom configuration"
echo " --no-motd Disable the MOTD"
echo " --solo Launch a new container exclusively for this shell"
echo " --no-solo Override the 'solo/ONE_SHELL' setting in your configuration"
echo " --trace Enable tracing of shell customization within Geodesic"
echo " --trace=<options> Enable tracing of specific parts of shell configuration"
echo " -v --verbose Enable verbose output of launch configuration"
Expand All @@ -180,6 +181,9 @@ function help() {
echo " terminal Trace the terminal color mode detection"
echo " You can specify multiple modes, separated by commas, e.g. --trace=custom,hist"
echo ""
echo " Options that only take effect when starting a container:"
echo " --workspace Set which host directory is used as the working directory in the container "
echo ""
}

function options_to_env() {
Expand All @@ -189,17 +193,17 @@ function options_to_env() {

for option in "${options[@]}"; do
# Safely split on '='
IFS='=' read -r -a kv <<< "$option"
IFS='=' read -r -a kv <<<"$option"
k=${kv[0]} # Take first element as key
k=${k#--} # Strip leading --
k=${k//-/_} # Convert dashes to underscores
k=$(echo "$k" | tr '[:lower:]' '[:upper:]') # Convert to uppercase (bash3 compat)
# Treat remaining elements as value, restoring the '=' separator
# This preserves multiple consecutive whitespace characters
v="$(IFS='='; echo "${kv[*]:1}")"
v="$(IFS='=' echo "${kv[*]:1}")"
v="${v:-true}" # Set it to true for boolean flags

export $k="$v"
export "$k"="$v"
done
}

Expand Down Expand Up @@ -286,6 +290,22 @@ function run_exit_hooks() {

function use() {
[ "$1" = "use" ] && shift
trap run_exit_hooks EXIT

if [ "$ONE_SHELL" != "true" ]; then
CONTAINER_ID=$(docker ps --filter name="^/${DOCKER_NAME}\$" --format '{{ .ID }}')
if [ -n "$CONTAINER_ID" ]; then
echo "# Starting shell in already running ${DOCKER_NAME} container ($CONTAINER_ID)"
if [ $# -eq 0 ]; then
set -- "/bin/bash" "-l" "$@"
fi
# We set unusual detach keys because (a) the default first char is ctrl-p, which is used for command history,
# and (b) if you detach from the shell, there is no way to reattach to it, so we want to effectively disable detach.
docker exec -it --detach-keys "ctrl-^,ctrl-[,ctrl-@" --env GEODESIC_HOST_CWD="${GEODESIC_HOST_CWD}" "${DOCKER_NAME}" $*
return 0
fi
fi

DOCKER_ARGS=()
if [ -t 1 ]; then
# Running in terminal
Expand Down Expand Up @@ -491,33 +511,20 @@ function use() {
--env GEODESIC_HOST_CWD="${GEODESIC_HOST_CWD}"
)

trap run_exit_hooks EXIT
if [ "$ONE_SHELL" = "true" ]; then
DOCKER_NAME="${DOCKER_NAME}-$(date +%d%H%M%S)"
echo "# Starting single shell ${DOCKER_NAME} session from ${DOCKER_IMAGE}"
echo "# Exposing port ${GEODESIC_PORT}"
[ -z "${GEODESIC_DOCKER_EXTRA_ARGS}" ] || echo "# Launching with extra Docker args: ${GEODESIC_DOCKER_EXTRA_ARGS}"
docker run --name "${DOCKER_NAME}" "${DOCKER_ARGS[@]}" ${GEODESIC_DOCKER_EXTRA_ARGS} ${DOCKER_IMAGE} -l $*
else
# the extra curly braces around .ID are because this file goes through go template substitution locally before being installed as a shell script
CONTAINER_ID=$(docker ps --filter name="^/${DOCKER_NAME}\$" --format '{{ .ID }}')
if [ -n "$CONTAINER_ID" ]; then
echo "# Starting shell in already running ${DOCKER_NAME} container ($CONTAINER_ID)"
if [ $# -eq 0 ]; then
set -- "/bin/bash" "-l" "$@"
fi
# We set unusual detach keys because (a) the default first char is ctrl-p, which is used for command history,
# and (b) if you detach from the shell, there is no way to reattach to it, so we want to effectively disable detach.
docker exec -it --detach-keys "ctrl-^,ctrl-[,ctrl-@" --env GEODESIC_HOST_CWD="${GEODESIC_HOST_CWD}" "${DOCKER_NAME}" $*
else
echo "# Running new ${DOCKER_NAME} container from ${DOCKER_IMAGE}"
echo "# Exposing port ${GEODESIC_PORT}"
[ -z "${GEODESIC_DOCKER_EXTRA_ARGS}" ] || echo "# Launching with extra Docker args: ${GEODESIC_DOCKER_EXTRA_ARGS}"
# docker run "${DOCKER_ARGS[@]}" ${GEODESIC_DOCKER_EXTRA_ARGS} ${DOCKER_IMAGE} -l $*
CONTAINER_ID=$(docker run --detach --init --name "${DOCKER_NAME}" "${DOCKER_ARGS[@]}" ${GEODESIC_DOCKER_EXTRA_ARGS} ${DOCKER_IMAGE} /usr/local/sbin/shell-monitor)
echo "# Started session ${CONTAINER_ID:0:12}. Starting shell via \`docker exec\`..."
docker exec -it --detach-keys "ctrl-^,ctrl-[,ctrl-@" --env GEODESIC_HOST_CWD="${GEODESIC_HOST_CWD}" "${DOCKER_NAME}" /bin/bash -l $*
fi
echo "# Running new ${DOCKER_NAME} container from ${DOCKER_IMAGE}"
echo "# Exposing port ${GEODESIC_PORT}"
[ -z "${GEODESIC_DOCKER_EXTRA_ARGS}" ] || echo "# Launching with extra Docker args: ${GEODESIC_DOCKER_EXTRA_ARGS}"
# docker run "${DOCKER_ARGS[@]}" ${GEODESIC_DOCKER_EXTRA_ARGS} ${DOCKER_IMAGE} -l $*
CONTAINER_ID=$(docker run --detach --init --name "${DOCKER_NAME}" "${DOCKER_ARGS[@]}" ${GEODESIC_DOCKER_EXTRA_ARGS} ${DOCKER_IMAGE} /usr/local/sbin/shell-monitor)
echo "# Started session ${CONTAINER_ID:0:12}. Starting shell via \`docker exec\`..."
docker exec -it --detach-keys "ctrl-^,ctrl-[,ctrl-@" --env GEODESIC_HOST_CWD="${GEODESIC_HOST_CWD}" "${DOCKER_NAME}" /bin/bash -l $*
fi
true
}
Expand Down

0 comments on commit 9e3715b

Please sign in to comment.