From 3076345af87a53985db4da6291a81f26a859d02a Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Sun, 3 Nov 2024 00:22:53 +0000 Subject: [PATCH] [common-utils] - added working shell history preserving logic for only bash right now --- src/common-utils/devcontainer-feature.json | 8 +-- src/common-utils/main.sh | 78 +++++++++++----------- test/common-utils/scenarios.json | 4 +- 3 files changed, 42 insertions(+), 48 deletions(-) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index 14fea55fb..891f2ec4e 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -72,10 +72,6 @@ } }, "mounts": [ - { - "source": "${devcontainerId}-shellhistory", - "target": "/dc/shellhistory", - "type": "volume" - } + "source=devcontainers,target=/devcontainers,type=volume" ] -} \ No newline at end of file +} \ No newline at end of file diff --git a/src/common-utils/main.sh b/src/common-utils/main.sh index 9dd8dcf98..760f92eba 100644 --- a/src/common-utils/main.sh +++ b/src/common-utils/main.sh @@ -571,50 +571,48 @@ fi # ********************************* # ** Enable shell history ** # ********************************* -# -# Generally, the installation favours configuring a shell to use the mounted volume -# for storing history rather than symlinking as any operation to recreate files -# could delete the symlink. -# On shells where this isn't possible, we symlink as a fallback. -# if [ "${ALLOW_SHELL_HISTORY}" = "true" ]; then -# Set HISTFILE for bash -cat << EOF >> "${user_home}/.bashrc" -if [[ -z "\$HISTFILE_OLD" ]]; then - export HISTFILE_OLD=\$HISTFILE -fi -export HISTFILE=/dc/shellhistory/.bash_history -export PROMPT_COMMAND='history -a' -sudo chown -R $USERNAME /dc/shellhistory -EOF -chown -R $USERNAME ${user_home}/.bashrc + echo "Activating feature 'shell-history'" + echo "User: ${USERNAME} User home: ${user_home}" -if [ "${INSTALL_ZSH}" = "true" ]; then -# Set HISTFILE for zsh -cat << EOF >> "${user_home}/.zshrc" -export HISTFILE=/dc/shellhistory/.zsh_history -export PROMPT_COMMAND='history -a' -sudo chown -R $USERNAME /dc/shellhistory -EOF -chown -R $USERNAME ${user_home}/.zshrc -fi + if ! command -v uuidgen &> /dev/null; then + sudo apt-get update + sudo apt-get install -y uuid-runtime + fi + # Create the shell history directory in the mounted volume + DEVCONTAINER_ID=$(uuidgen) + HISTORY_DIR="/devcontainers/${DEVCONTAINER_ID}/shellHistory" + USER_HISTORY_FILE="${user_home}/.bash_history" + VOLUME_HISTORY_FILE="${HISTORY_DIR}/.bash_history" + + # Create the history directory in the volume, if it doesn’t already exist + sudo mkdir -p "${HISTORY_DIR}" + sudo chown -R "${USERNAME}" "${HISTORY_DIR}" + sudo chmod -R u+rwx "${HISTORY_DIR}" + + # Ensure the volume's history file exists and set permissions + if [[ ! -f "${VOLUME_HISTORY_FILE}" ]]; then + # Create an empty history file if it doesn’t already exist + sudo touch "${VOLUME_HISTORY_FILE}" + sudo chown -R "${USERNAME}" "${VOLUME_HISTORY_FILE}" + sudo chmod -R u+rwx "${VOLUME_HISTORY_FILE}" + fi -# Create symlink for fish -mkdir -p ${user_home}/.config/fish -cat << EOF >> "${user_home}/.config/fish/config.fish" -if test -z "\$XDG_DATA_HOME" - set history_location ~/.local/share/fish/fish_history -else - set history_location \$XDG_DATA_HOME/fish/fish_history -end -if test -f \$history_location - mv \$history_location "\$history_location-old" -end -ln -s /dc/shellhistory/fish_history \$history_location -sudo chown -R $USERNAME \$history_location -EOF -chown -R $USERNAME ${user_home}/.config/ + # Create or update the user’s .bash_history to append to the volume’s history + if [[ ! -f "${USER_HISTORY_FILE}" ]]; then + sudo touch "${USER_HISTORY_FILE}" + sudo chown -R "${USERNAME}" "${USER_HISTORY_FILE}" + sudo chmod -R u+rwx "${USER_HISTORY_FILE}" + fi + + # Symlink for Bash history + sudo ln -sf ${USER_HISTORY_FILE} ${VOLUME_HISTORY_FILE} + + # Configure immediate history saving to the volume + echo 'PROMPT_COMMAND="history -a; history -c; history -r"' >> "${user_home}/.bashrc" + + echo "Shell history setup for persistent appending is complete." fi # ********************************* diff --git a/test/common-utils/scenarios.json b/test/common-utils/scenarios.json index 701f8b762..114c72ff6 100644 --- a/test/common-utils/scenarios.json +++ b/test/common-utils/scenarios.json @@ -298,9 +298,9 @@ "features": { "common-utils": { "installZsh": true, - "configureZshAsDefaultShell": true, "allowShellHistory": true } - } + }, + "remoteUser": "vscode" } } \ No newline at end of file