From 6e637c22679bfe152e7735a85d580d1fc3bfe4a2 Mon Sep 17 00:00:00 2001 From: Nuru Date: Mon, 20 Jan 2025 00:30:56 -0800 Subject: [PATCH] Handle workspace path has spaces, is symlink --- ReleaseNotes-v4.md | 2 +- rootfs/etc/profile.d/_20-mounts.sh | 23 ++++++++++++++++++++++- rootfs/templates/wrapper-body.sh | 28 ++++++++++++++++------------ 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/ReleaseNotes-v4.md b/ReleaseNotes-v4.md index 941c490d..c7df6bc8 100644 --- a/ReleaseNotes-v4.md +++ b/ReleaseNotes-v4.md @@ -1,4 +1,4 @@ -## Geodesic v4.0.0 Release Notes +# Geodesic v4.0.0 Release Notes ### Highlights diff --git a/rootfs/etc/profile.d/_20-mounts.sh b/rootfs/etc/profile.d/_20-mounts.sh index 24c82bd2..1b99e229 100644 --- a/rootfs/etc/profile.d/_20-mounts.sh +++ b/rootfs/etc/profile.d/_20-mounts.sh @@ -163,8 +163,29 @@ function _map_mounts() { done } +function _add_symlinks() { + local links dest src + IFS='|' read -ra links <<<"${GEODESIC_HOST_SYMLINK}" + for l in "${links[@]}"; do + [[ -z "$l" ]] && continue + local src dest + IFS='>' read -r dest src <<<"$l" + if [[ -z $src ]] || [[ -z $dest ]]; then + red "# ERROR: Invalid symlink definition: $l" + continue + fi + mkdir -p "$(dirname "$dest")" + if [[ -e $dest ]]; then + red "# ERROR: Symlink destination already exists: '$dest'" + continue + fi + ln -sT "$src" "$dest" && yellow symlinking "'$src' -> '$dest'" || red "# ERROR: Failed to create symlink: '$src' -> '$dest'" + done +} + if [[ $SHLVL == 1 ]]; then _map_mounts + _add_symlinks # Ensure we do not have paths that match everything paths=("${GEODESIC_HOST_PATHS[@]}") @@ -177,4 +198,4 @@ if [[ $SHLVL == 1 ]]; then done fi -unset -f _map_mounts _map_owner _map_host _ensure_dest paths +unset -f _map_mounts _map_owner _map_host _ensure_dest paths _add_symlinks diff --git a/rootfs/templates/wrapper-body.sh b/rootfs/templates/wrapper-body.sh index 17c2caf3..878e656b 100755 --- a/rootfs/templates/wrapper-body.sh +++ b/rootfs/templates/wrapper-body.sh @@ -2,8 +2,8 @@ homedir_default_mounts=".aws,.config,.emacs.d,.geodesic,.kube,.ssh,.terraform.d" function require_installed() { - if ! command -v $1 >/dev/null 2>&1; then - echo "Cannot find $1 installed on this system. Please install and try again." + if ! command -v "$1" >/dev/null 2>&1; then + echo "Cannot find '$1' installed on this system. Please install and try again." exit 1 fi } @@ -188,13 +188,15 @@ function options_to_env() { local v for option in "${options[@]}"; do - 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) - - v="${kv[@]:1}" # Treat remaining elements as value + # Safely split on '=' + 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="${v:-true}" # Set it to true for boolean flags export $k="$v" @@ -437,6 +439,7 @@ function use() { fi if [ "$configured_wfhd" != "$WORKSPACE_FOLDER_HOST_DIR" ]; then echo "# Resolved ${configured_wfhd} to '${WORKSPACE_FOLDER_HOST_DIR}'" + export GEODESIC_HOST_SYMLINK+="${configured_wfhd}>${WORKSPACE_FOLDER_HOST_DIR}|" fi echo "# Mounting '${WORKSPACE_MOUNT_HOST_DIR}' into container at '${WORKSPACE_MOUNT}'" @@ -448,6 +451,7 @@ function use() { --env WORKSPACE_MOUNT="${WORKSPACE_MOUNT}" --env WORKSPACE_FOLDER="${WORKSPACE_FOLDER}" ) + [ -n "${GEODESIC_HOST_SYMLINK}" ] && DOCKER_ARGS+=(--env GEODESIC_HOST_SYMLINK) # Mount the host mounts wherever the users asks for them to be mounted. # However, if file ownership mapping is enabled, @@ -526,17 +530,17 @@ _polite_stop() { return fi - printf "# Signalling ${name} to stop..." + printf "# Signalling '%s' to stop..." "${name}" docker kill -s TERM "${name}" >/dev/null for i in {1..9}; do if [ $i -eq 9 ] || [ $(docker ps -q --filter "name=${name}" | wc -l | tr -d " ") -eq 0 ]; then - printf " ${name} stopped gracefully.\n\n" + printf " '%s' stopped gracefully.\n\n" "${name}" return 0 fi [ $i -lt 8 ] && sleep 1 done - printf " ${name} did not stop gracefully. Killing it.\n\n" + printf " '%s' did not stop gracefully. Killing it.\n\n" "${name}" docker kill -s TERM "${name}" >/dev/null return 138 }