From 7cbb7de4764d8670d85047ddc80e66c2cf2521bb Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Tue, 22 Oct 2024 08:23:06 +0000 Subject: [PATCH 01/34] [common-utils] - append shell history functionality - #1026 --- src/common-utils/devcontainer-feature.json | 16 ++++++- src/common-utils/main.sh | 50 ++++++++++++++++++++++ test/common-utils/allow_shell_history.sh | 17 ++++++++ test/common-utils/scenarios.json | 10 +++++ 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 test/common-utils/allow_shell_history.sh diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index 3fa6487df..14fea55fb 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -64,6 +64,18 @@ "type": "boolean", "default": false, "description": "Add packages from non-free Debian repository? (Debian only)" + }, + "allowShellHistory": { + "type": "boolean", + "default": false, + "description": "Preserve shell history across dev container instances? (Currently supports bash, zsh, and fish)" + } + }, + "mounts": [ + { + "source": "${devcontainerId}-shellhistory", + "target": "/dc/shellhistory", + "type": "volume" } - } -} + ] +} \ No newline at end of file diff --git a/src/common-utils/main.sh b/src/common-utils/main.sh index b5fe11f57..9dd8dcf98 100644 --- a/src/common-utils/main.sh +++ b/src/common-utils/main.sh @@ -18,6 +18,7 @@ USERNAME="${USERNAME:-"automatic"}" USER_UID="${USERUID:-"automatic"}" USER_GID="${USERGID:-"automatic"}" ADD_NON_FREE_PACKAGES="${NONFREEPACKAGES:-"false"}" +ALLOW_SHELL_HISTORY="${ALLOWSHELLHISTORY:-"false"}" MARKER_FILE="/usr/local/etc/vscode-dev-containers/common" @@ -567,6 +568,55 @@ if [ "${INSTALL_ZSH}" = "true" ]; then fi 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 + +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 + +# 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/ +fi + # ********************************* # ** Ensure config directory ** # ********************************* diff --git a/test/common-utils/allow_shell_history.sh b/test/common-utils/allow_shell_history.sh new file mode 100644 index 000000000..fa502b3d4 --- /dev/null +++ b/test/common-utils/allow_shell_history.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +set -e + +# Optional: Import test library +source dev-container-features-test-lib + +# Definition specific tests +check "default-shell-is-zsh" bash -c "getent passwd $(whoami) | awk -F: '{ print $7 }' | grep '/bin/zsh'" +# check it overrides the ~/.zshrc with default dev containers template +check "default-zshrc-is-dev-container-template" bash -c "cat ~/.zshrc | grep ZSH_THEME | grep devcontainers" +check "zsh-path-contains-local-bin" zsh -l -c "echo $PATH | grep '/home/devcontainer/.local/bin'" + +check "Ensure .zprofile is owned by remoteUser" bash -c "stat -c '%U' /home/devcontainer/.zprofile | grep devcontainer" + +# Report result +reportResults \ No newline at end of file diff --git a/test/common-utils/scenarios.json b/test/common-utils/scenarios.json index a929f534a..701f8b762 100644 --- a/test/common-utils/scenarios.json +++ b/test/common-utils/scenarios.json @@ -292,5 +292,15 @@ "features": { "common-utils": {} } + }, + "allow_shell_history": { + "image": "debian:bookworm", + "features": { + "common-utils": { + "installZsh": true, + "configureZshAsDefaultShell": true, + "allowShellHistory": true + } + } } } \ No newline at end of file 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 02/34] [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 From 76d048b0b89ebcfc751784bafda0e4ab70bf7e24 Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Sun, 3 Nov 2024 00:29:44 +0000 Subject: [PATCH 03/34] change to mounts formatting --- src/common-utils/devcontainer-feature.json | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index 891f2ec4e..3af06cc10 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -71,7 +71,9 @@ "description": "Preserve shell history across dev container instances? (Currently supports bash, zsh, and fish)" } }, - "mounts": [ - "source=devcontainers,target=/devcontainers,type=volume" - ] + "mounts": [{ + "source": "devcontainers", + "target": "/devcontainers", + "type": "volume" + }] } \ No newline at end of file From 192e4f0b1296e31fb3efc81465815d82231dfa2d Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Tue, 5 Nov 2024 01:27:41 +0000 Subject: [PATCH 04/34] changes as suggested --- src/common-utils/devcontainer-feature.json | 6 +++ src/common-utils/main.sh | 45 ++-------------------- src/common-utils/setup_history.sh | 36 +++++++++++++++++ 3 files changed, 46 insertions(+), 41 deletions(-) create mode 100755 src/common-utils/setup_history.sh diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index 3af06cc10..502345f7d 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -71,9 +71,15 @@ "description": "Preserve shell history across dev container instances? (Currently supports bash, zsh, and fish)" } }, + "postCreateCommand": "export DEVCONTAINER_ID=${devcontainerId} && chmod +x /tmp/setup_history.sh && . /tmp/setup_history.sh", "mounts": [{ "source": "devcontainers", "target": "/devcontainers", "type": "volume" + }, + { + "source": "/workspaces/features/src/common-utils/setup_history.sh", + "target": "/tmp/setup_history.sh", + "type": "bind" }] } \ No newline at end of file diff --git a/src/common-utils/main.sh b/src/common-utils/main.sh index 760f92eba..1f1a0eab0 100644 --- a/src/common-utils/main.sh +++ b/src/common-utils/main.sh @@ -572,48 +572,11 @@ fi # ** Enable shell history ** # ********************************* -if [ "${ALLOW_SHELL_HISTORY}" = "true" ]; then - echo "Activating feature 'shell-history'" - echo "User: ${USERNAME} User home: ${user_home}" +echo export ALLOW_SHELL_HISTORY="${ALLOW_SHELL_HISTORY}" > /tmp/env.sh +echo export user_home="${user_home}" >> /tmp/env.sh +echo export USERNAME="${USERNAME}" >> /tmp/env.sh - 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 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 +sudo chmod +x /tmp/env.sh # ********************************* # ** Ensure config directory ** diff --git a/src/common-utils/setup_history.sh b/src/common-utils/setup_history.sh new file mode 100755 index 000000000..94d92666e --- /dev/null +++ b/src/common-utils/setup_history.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +# Source the environment variables from env.sh +if [ -f /tmp/env.sh ]; then + . /tmp/env.sh +else + echo "env.sh not found!" +fi + +if [ "${ALLOW_SHELL_HISTORY}" = "true" ]; then + echo "Activating feature 'shell-history'" + echo "User: ${USERNAME} User home: ${user_home}" + + # Create the shell history directory in the mounted volume + 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 + sudo touch "${VOLUME_HISTORY_FILE}" + sudo chown -R "${USERNAME}" "${VOLUME_HISTORY_FILE}" + sudo chmod -R u+rwx "${VOLUME_HISTORY_FILE}" + + # 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 -r;"' >> "${user_home}/.bashrc" + + echo "Shell history setup for persistent appending is complete." +fi \ No newline at end of file From 930050dab43fca556f6049a8c582a42bf4cb6862 Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Tue, 5 Nov 2024 01:31:30 +0000 Subject: [PATCH 05/34] changed path of source for bind mount --- src/common-utils/devcontainer-feature.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index 502345f7d..4f2f0a4f7 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -78,7 +78,7 @@ "type": "volume" }, { - "source": "/workspaces/features/src/common-utils/setup_history.sh", + "source": "/src/common-utils/setup_history.sh", "target": "/tmp/setup_history.sh", "type": "bind" }] From 69709233414cda37aa1511a795ea313ae0d356d0 Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Tue, 5 Nov 2024 01:33:27 +0000 Subject: [PATCH 06/34] changes to path yet again for source of bind mount --- src/common-utils/devcontainer-feature.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index 4f2f0a4f7..953c8a5f3 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -78,7 +78,7 @@ "type": "volume" }, { - "source": "/src/common-utils/setup_history.sh", + "source": "./setup_history.sh", "target": "/tmp/setup_history.sh", "type": "bind" }] From 5c7b51313765b78763426754bf556831d5537811 Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Tue, 5 Nov 2024 01:36:14 +0000 Subject: [PATCH 07/34] change --- src/common-utils/devcontainer-feature.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index 953c8a5f3..502345f7d 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -78,7 +78,7 @@ "type": "volume" }, { - "source": "./setup_history.sh", + "source": "/workspaces/features/src/common-utils/setup_history.sh", "target": "/tmp/setup_history.sh", "type": "bind" }] From 753ab15c97c8a6887a224ac3e76aaf8dafce007d Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Tue, 5 Nov 2024 05:21:02 +0000 Subject: [PATCH 08/34] point to correct source dir --- src/common-utils/devcontainer-feature.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index 502345f7d..766a07481 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -78,7 +78,7 @@ "type": "volume" }, { - "source": "/workspaces/features/src/common-utils/setup_history.sh", + "source": "${containerWorkspaceFolder}/setup_history.sh", "target": "/tmp/setup_history.sh", "type": "bind" }] From d21cc089300ac90237fbd53f0877ccfd2228fbf0 Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Tue, 5 Nov 2024 05:24:21 +0000 Subject: [PATCH 09/34] small change to path of source file --- src/common-utils/devcontainer-feature.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index 766a07481..3c9392f88 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -78,7 +78,7 @@ "type": "volume" }, { - "source": "${containerWorkspaceFolder}/setup_history.sh", + "source": "${containerWorkspaceFolder}/src/common-utils/scripts/setup_history.sh", "target": "/tmp/setup_history.sh", "type": "bind" }] From e48c1c0a164e6480b04ede655a607e9d9a5c0561 Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Tue, 5 Nov 2024 05:29:08 +0000 Subject: [PATCH 10/34] change --- src/common-utils/devcontainer-feature.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index 3c9392f88..d5e1bb890 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -71,14 +71,14 @@ "description": "Preserve shell history across dev container instances? (Currently supports bash, zsh, and fish)" } }, - "postCreateCommand": "export DEVCONTAINER_ID=${devcontainerId} && chmod +x /tmp/setup_history.sh && . /tmp/setup_history.sh", + "postCreateCommand": "ls -la ${containerWorkspaceFolder} && export DEVCONTAINER_ID=${devcontainerId} && chmod +x /tmp/setup_history.sh && . /tmp/setup_history.sh", "mounts": [{ "source": "devcontainers", "target": "/devcontainers", "type": "volume" }, { - "source": "${containerWorkspaceFolder}/src/common-utils/scripts/setup_history.sh", + "source": "${containerWorkspaceFolder}/scripts/setup_history.sh", "target": "/tmp/setup_history.sh", "type": "bind" }] From 10747ffae6d2362b00be23d41bc27a2bfb5454f4 Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Tue, 5 Nov 2024 05:32:36 +0000 Subject: [PATCH 11/34] check root folder --- src/common-utils/devcontainer-feature.json | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index d5e1bb890..a690bfff4 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -71,15 +71,17 @@ "description": "Preserve shell history across dev container instances? (Currently supports bash, zsh, and fish)" } }, - "postCreateCommand": "ls -la ${containerWorkspaceFolder} && export DEVCONTAINER_ID=${devcontainerId} && chmod +x /tmp/setup_history.sh && . /tmp/setup_history.sh", + "postCreateCommand": "set -x && ls -la ${containerWorkspaceFolder} && export DEVCONTAINER_ID=${devcontainerId} && chmod +x /tmp/setup_history.sh && . /tmp/setup_history.sh", "mounts": [{ "source": "devcontainers", "target": "/devcontainers", "type": "volume" - }, - { - "source": "${containerWorkspaceFolder}/scripts/setup_history.sh", - "target": "/tmp/setup_history.sh", - "type": "bind" - }] + } + // , + // { + // "source": "${containerWorkspaceFolder}/scripts/setup_history.sh", + // "target": "/tmp/setup_history.sh", + // "type": "bind" + // } +] } \ No newline at end of file From 581e5eb130d739d18a2c7dc3adeb9dd6de43f04d Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Tue, 5 Nov 2024 05:33:04 +0000 Subject: [PATCH 12/34] change --- src/common-utils/{ => scripts}/setup_history.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/common-utils/{ => scripts}/setup_history.sh (100%) diff --git a/src/common-utils/setup_history.sh b/src/common-utils/scripts/setup_history.sh similarity index 100% rename from src/common-utils/setup_history.sh rename to src/common-utils/scripts/setup_history.sh From 54119e36802f4b5b1caed1849593d52c52ea93f1 Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Tue, 5 Nov 2024 05:36:07 +0000 Subject: [PATCH 13/34] misc --- src/common-utils/devcontainer-feature.json | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index a690bfff4..2ab29af62 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -71,17 +71,15 @@ "description": "Preserve shell history across dev container instances? (Currently supports bash, zsh, and fish)" } }, - "postCreateCommand": "set -x && ls -la ${containerWorkspaceFolder} && export DEVCONTAINER_ID=${devcontainerId} && chmod +x /tmp/setup_history.sh && . /tmp/setup_history.sh", + "postCreateCommand": "export DEVCONTAINER_ID=${devcontainerId} && chmod +x /tmp/setup_history.sh && . /tmp/setup_history.sh", "mounts": [{ "source": "devcontainers", "target": "/devcontainers", "type": "volume" - } - // , - // { - // "source": "${containerWorkspaceFolder}/scripts/setup_history.sh", - // "target": "/tmp/setup_history.sh", - // "type": "bind" - // } -] + }, + { + "source": "${containerWorkspaceFolder}/scripts/setup_history.sh", + "target": "/tmp/setup_history.sh", + "type": "bind" + }] } \ No newline at end of file From dd3410374b7ef6edd2bdaf60a9a9f7fb2cf74bd2 Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Tue, 5 Nov 2024 05:42:08 +0000 Subject: [PATCH 14/34] path correction final --- src/common-utils/devcontainer-feature.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index 2ab29af62..3c9392f88 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -78,7 +78,7 @@ "type": "volume" }, { - "source": "${containerWorkspaceFolder}/scripts/setup_history.sh", + "source": "${containerWorkspaceFolder}/src/common-utils/scripts/setup_history.sh", "target": "/tmp/setup_history.sh", "type": "bind" }] From ba649cf3bca32d528eb007b50866d018842f6086 Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Tue, 5 Nov 2024 05:48:05 +0000 Subject: [PATCH 15/34] change --- src/common-utils/devcontainer-feature.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index 3c9392f88..c90cc424e 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -71,14 +71,14 @@ "description": "Preserve shell history across dev container instances? (Currently supports bash, zsh, and fish)" } }, - "postCreateCommand": "export DEVCONTAINER_ID=${devcontainerId} && chmod +x /tmp/setup_history.sh && . /tmp/setup_history.sh", + "postCreateCommand": "ls -la ${localWorkspaceFolder} && export DEVCONTAINER_ID=${devcontainerId} && chmod +x /tmp/setup_history.sh && . /tmp/setup_history.sh", "mounts": [{ "source": "devcontainers", "target": "/devcontainers", "type": "volume" }, { - "source": "${containerWorkspaceFolder}/src/common-utils/scripts/setup_history.sh", + "source": "${localWorkspaceFolder}/src/common-utils/scripts/setup_history.sh", "target": "/tmp/setup_history.sh", "type": "bind" }] From c1b37fadcea4ef0a6056fc62e52fa6fd165f4aec Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Tue, 5 Nov 2024 05:50:12 +0000 Subject: [PATCH 16/34] try --- src/common-utils/devcontainer-feature.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index c90cc424e..82475b798 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -78,7 +78,7 @@ "type": "volume" }, { - "source": "${localWorkspaceFolder}/src/common-utils/scripts/setup_history.sh", + "source": "${localWorkspaceFolder}/scripts/setup_history.sh", "target": "/tmp/setup_history.sh", "type": "bind" }] From c227dc820a834bae49fc387b7517f6961b136068 Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Tue, 5 Nov 2024 05:51:57 +0000 Subject: [PATCH 17/34] c --- src/common-utils/devcontainer-feature.json | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index 82475b798..0502e394e 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -76,10 +76,12 @@ "source": "devcontainers", "target": "/devcontainers", "type": "volume" - }, - { - "source": "${localWorkspaceFolder}/scripts/setup_history.sh", - "target": "/tmp/setup_history.sh", - "type": "bind" - }] + } + // }, + // { + // "source": "${localWorkspaceFolder}/scripts/setup_history.sh", + // "target": "/tmp/setup_history.sh", + // "type": "bind" + // }] + ] } \ No newline at end of file From 1de5f38581c0e290a93620a96777bfa34a71528a Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Tue, 5 Nov 2024 05:54:30 +0000 Subject: [PATCH 18/34] s --- src/common-utils/devcontainer-feature.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index 0502e394e..8cdcc7fc7 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -71,7 +71,7 @@ "description": "Preserve shell history across dev container instances? (Currently supports bash, zsh, and fish)" } }, - "postCreateCommand": "ls -la ${localWorkspaceFolder} && export DEVCONTAINER_ID=${devcontainerId} && chmod +x /tmp/setup_history.sh && . /tmp/setup_history.sh", + "postCreateCommand": "ls -la ${containerWorkspaceFolder} && export DEVCONTAINER_ID=${devcontainerId} && chmod +x /tmp/setup_history.sh && . /tmp/setup_history.sh", "mounts": [{ "source": "devcontainers", "target": "/devcontainers", From 50ff6d21a290da5837cd96ec64f3549843607b14 Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Tue, 5 Nov 2024 05:56:26 +0000 Subject: [PATCH 19/34] hi --- src/common-utils/devcontainer-feature.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index 8cdcc7fc7..eb6e2b34a 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -71,7 +71,7 @@ "description": "Preserve shell history across dev container instances? (Currently supports bash, zsh, and fish)" } }, - "postCreateCommand": "ls -la ${containerWorkspaceFolder} && export DEVCONTAINER_ID=${devcontainerId} && chmod +x /tmp/setup_history.sh && . /tmp/setup_history.sh", + "postCreateCommand": "ls -la ${containerWorkspaceFolder}/.devcontainer && export DEVCONTAINER_ID=${devcontainerId} && chmod +x /tmp/setup_history.sh && . /tmp/setup_history.sh", "mounts": [{ "source": "devcontainers", "target": "/devcontainers", From 129f6bff796977e1fcd58ee59950d229384041a0 Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Tue, 5 Nov 2024 05:59:18 +0000 Subject: [PATCH 20/34] fin --- src/common-utils/devcontainer-feature.json | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index eb6e2b34a..6c9ab5623 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -71,17 +71,15 @@ "description": "Preserve shell history across dev container instances? (Currently supports bash, zsh, and fish)" } }, - "postCreateCommand": "ls -la ${containerWorkspaceFolder}/.devcontainer && export DEVCONTAINER_ID=${devcontainerId} && chmod +x /tmp/setup_history.sh && . /tmp/setup_history.sh", + "postCreateCommand": "export DEVCONTAINER_ID=${devcontainerId} && chmod +x /tmp/setup_history.sh && . /tmp/setup_history.sh", "mounts": [{ "source": "devcontainers", "target": "/devcontainers", "type": "volume" - } - // }, - // { - // "source": "${localWorkspaceFolder}/scripts/setup_history.sh", - // "target": "/tmp/setup_history.sh", - // "type": "bind" - // }] - ] + }, + { + "source": "${containerWorkspaceFolder}/.devcontainer/common-utils/scripts/setup_history.sh", + "target": "/tmp/setup_history.sh", + "type": "bind" + }] } \ No newline at end of file From c7c5aa84b4f9c068354e7d9caba07a4dffb49896 Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Tue, 5 Nov 2024 06:11:57 +0000 Subject: [PATCH 21/34] c --- src/common-utils/devcontainer-feature.json | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index 6c9ab5623..8f74c4819 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -71,15 +71,17 @@ "description": "Preserve shell history across dev container instances? (Currently supports bash, zsh, and fish)" } }, - "postCreateCommand": "export DEVCONTAINER_ID=${devcontainerId} && chmod +x /tmp/setup_history.sh && . /tmp/setup_history.sh", + "postCreateCommand": "ls -la ${containerWorkspaceFolder}/.devcontainer/common-utils && ls -la ${containerWorkspaceFolder}/.devcontainer/common-utils/scripts && export DEVCONTAINER_ID=${devcontainerId} && chmod +x /tmp/setup_history.sh && . /tmp/setup_history.sh", "mounts": [{ "source": "devcontainers", "target": "/devcontainers", "type": "volume" - }, - { - "source": "${containerWorkspaceFolder}/.devcontainer/common-utils/scripts/setup_history.sh", - "target": "/tmp/setup_history.sh", - "type": "bind" - }] + } + // , + // { + // "source": "${containerWorkspaceFolder}/.devcontainer/common-utils/scripts/setup_history.sh", + // "target": "/tmp/setup_history.sh", + // "type": "bind" + // } +] } \ No newline at end of file From 3f1e7c99bd08623a0b5a2a0baae68a57dd7566cc Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Wed, 6 Nov 2024 18:39:00 +0530 Subject: [PATCH 22/34] Update devcontainer-feature.json --- src/common-utils/devcontainer-feature.json | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index 8f74c4819..a27af3e90 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -77,11 +77,11 @@ "target": "/devcontainers", "type": "volume" } - // , - // { - // "source": "${containerWorkspaceFolder}/.devcontainer/common-utils/scripts/setup_history.sh", - // "target": "/tmp/setup_history.sh", - // "type": "bind" - // } + , + { + "source": "${containerWorkspaceFolder}/.devcontainer/common-utils/scripts/setup_history.sh", + "target": "/tmp/setup_history.sh", + "type": "bind" + } ] -} \ No newline at end of file +} From be92e63ca96789601b298f900fc9a9ae6db7a1ac Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Fri, 8 Nov 2024 03:58:31 +0000 Subject: [PATCH 23/34] giving due permissions before mounts is called for file move --- src/common-utils/devcontainer-feature.json | 1 + 1 file changed, 1 insertion(+) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index a27af3e90..d4d76029c 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -71,6 +71,7 @@ "description": "Preserve shell history across dev container instances? (Currently supports bash, zsh, and fish)" } }, + "initializeCommand": "chmod -R 755 ${containerWorkspaceFolder}/.devcontainer/common-utils/scripts", "postCreateCommand": "ls -la ${containerWorkspaceFolder}/.devcontainer/common-utils && ls -la ${containerWorkspaceFolder}/.devcontainer/common-utils/scripts && export DEVCONTAINER_ID=${devcontainerId} && chmod +x /tmp/setup_history.sh && . /tmp/setup_history.sh", "mounts": [{ "source": "devcontainers", From dec0296ace9849f6de9fe910ec362cd8b538b6d0 Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Thu, 14 Nov 2024 03:40:03 +0000 Subject: [PATCH 24/34] few changes --- src/common-utils/devcontainer-feature.json | 9 +-------- src/common-utils/main.sh | 5 ++++- src/common-utils/scripts/setup_history.sh | 2 ++ test/common-utils/scenarios.json | 3 ++- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index d4d76029c..baa417e54 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -71,18 +71,11 @@ "description": "Preserve shell history across dev container instances? (Currently supports bash, zsh, and fish)" } }, - "initializeCommand": "chmod -R 755 ${containerWorkspaceFolder}/.devcontainer/common-utils/scripts", - "postCreateCommand": "ls -la ${containerWorkspaceFolder}/.devcontainer/common-utils && ls -la ${containerWorkspaceFolder}/.devcontainer/common-utils/scripts && export DEVCONTAINER_ID=${devcontainerId} && chmod +x /tmp/setup_history.sh && . /tmp/setup_history.sh", + "postCreateCommand": "export DEVCONTAINER_ID=${devcontainerId} && . /tmp/setup_history.sh", "mounts": [{ "source": "devcontainers", "target": "/devcontainers", "type": "volume" } - , - { - "source": "${containerWorkspaceFolder}/.devcontainer/common-utils/scripts/setup_history.sh", - "target": "/tmp/setup_history.sh", - "type": "bind" - } ] } diff --git a/src/common-utils/main.sh b/src/common-utils/main.sh index 1f1a0eab0..e39291fdb 100644 --- a/src/common-utils/main.sh +++ b/src/common-utils/main.sh @@ -576,7 +576,10 @@ echo export ALLOW_SHELL_HISTORY="${ALLOW_SHELL_HISTORY}" > /tmp/env.sh echo export user_home="${user_home}" >> /tmp/env.sh echo export USERNAME="${USERNAME}" >> /tmp/env.sh -sudo chmod +x /tmp/env.sh +chmod +x /tmp/env.sh + +cp -f "${FEATURE_DIR}/scripts/setup_history.sh" /tmp/setup_history.sh +chmod +x /tmp/setup_history.sh # ********************************* # ** Ensure config directory ** diff --git a/src/common-utils/scripts/setup_history.sh b/src/common-utils/scripts/setup_history.sh index 94d92666e..b00c192be 100755 --- a/src/common-utils/scripts/setup_history.sh +++ b/src/common-utils/scripts/setup_history.sh @@ -1,5 +1,7 @@ #!/bin/bash +set -e + # Source the environment variables from env.sh if [ -f /tmp/env.sh ]; then . /tmp/env.sh diff --git a/test/common-utils/scenarios.json b/test/common-utils/scenarios.json index 114c72ff6..60a8a5613 100644 --- a/test/common-utils/scenarios.json +++ b/test/common-utils/scenarios.json @@ -301,6 +301,7 @@ "allowShellHistory": true } }, - "remoteUser": "vscode" + "remoteUser": "vscode", + "containerUser": "vscode" } } \ No newline at end of file From 261428f113347af988f68959bc75d63fdaedcec5 Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Thu, 14 Nov 2024 04:56:55 +0000 Subject: [PATCH 25/34] change --- test/common-utils/allow_shell_history.sh | 8 -------- 1 file changed, 8 deletions(-) diff --git a/test/common-utils/allow_shell_history.sh b/test/common-utils/allow_shell_history.sh index fa502b3d4..f1f42a1cf 100644 --- a/test/common-utils/allow_shell_history.sh +++ b/test/common-utils/allow_shell_history.sh @@ -5,13 +5,5 @@ set -e # Optional: Import test library source dev-container-features-test-lib -# Definition specific tests -check "default-shell-is-zsh" bash -c "getent passwd $(whoami) | awk -F: '{ print $7 }' | grep '/bin/zsh'" -# check it overrides the ~/.zshrc with default dev containers template -check "default-zshrc-is-dev-container-template" bash -c "cat ~/.zshrc | grep ZSH_THEME | grep devcontainers" -check "zsh-path-contains-local-bin" zsh -l -c "echo $PATH | grep '/home/devcontainer/.local/bin'" - -check "Ensure .zprofile is owned by remoteUser" bash -c "stat -c '%U' /home/devcontainer/.zprofile | grep devcontainer" - # Report result reportResults \ No newline at end of file From 840606838831a9b1a595f3f203367f9987435f8b Mon Sep 17 00:00:00 2001 From: Kaniska244 Date: Fri, 6 Dec 2024 09:18:41 +0000 Subject: [PATCH 26/34] Updated shell history changes for PR#1157 --- src/common-utils/devcontainer-feature.json | 19 +++- src/common-utils/main.sh | 13 +-- src/common-utils/scripts/setup_history.sh | 20 ++-- test/common-utils/allow_shell_history.sh | 104 ++++++++++++++++++++- test/common-utils/scenarios.json | 5 + 5 files changed, 144 insertions(+), 17 deletions(-) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index baa417e54..cb70e338e 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -71,11 +71,24 @@ "description": "Preserve shell history across dev container instances? (Currently supports bash, zsh, and fish)" } }, - "postCreateCommand": "export DEVCONTAINER_ID=${devcontainerId} && . /tmp/setup_history.sh", + "containerEnv": { + "DOCKER_BUILDKIT": "1" + }, + "postCreateCommand": "export DEVCONTAINER_ID=${devcontainerId} && . /etc/setup_history.sh", "mounts": [{ "source": "devcontainers", "target": "/devcontainers", "type": "volume" - } -] + }, + { + "source": "dind-var-lib-docker-${devcontainerId}", + "target": "/var/lib/docker", + "type": "volume" + }, + { + "source": "/var/run/docker.sock", + "target": "/var/run/docker.sock", + "type": "bind" + }], + "entrypoint": ["/usr/local/share/docker-init.sh", "/usr/local/share/ssh-init.sh"] } diff --git a/src/common-utils/main.sh b/src/common-utils/main.sh index e39291fdb..19f28bade 100644 --- a/src/common-utils/main.sh +++ b/src/common-utils/main.sh @@ -572,14 +572,15 @@ fi # ** Enable shell history ** # ********************************* -echo export ALLOW_SHELL_HISTORY="${ALLOW_SHELL_HISTORY}" > /tmp/env.sh -echo export user_home="${user_home}" >> /tmp/env.sh -echo export USERNAME="${USERNAME}" >> /tmp/env.sh +echo export ALLOW_SHELL_HISTORY="${ALLOW_SHELL_HISTORY}" > /etc/env.sh +echo export user_home="${user_home}" >> /etc/env.sh +echo export USERNAME="${USERNAME}" >> /etc/env.sh -chmod +x /tmp/env.sh +chown "$USERNAME":"$USERNAME" "/etc/env.sh" +chmod u+rx "/etc/env.sh" -cp -f "${FEATURE_DIR}/scripts/setup_history.sh" /tmp/setup_history.sh -chmod +x /tmp/setup_history.sh +cp -f "${FEATURE_DIR}/scripts/setup_history.sh" /etc/setup_history.sh +chmod +x /etc/setup_history.sh # ********************************* # ** Ensure config directory ** diff --git a/src/common-utils/scripts/setup_history.sh b/src/common-utils/scripts/setup_history.sh index b00c192be..8c8c9bf78 100755 --- a/src/common-utils/scripts/setup_history.sh +++ b/src/common-utils/scripts/setup_history.sh @@ -1,10 +1,11 @@ -#!/bin/bash +#!/bin/sh set -e # Source the environment variables from env.sh -if [ -f /tmp/env.sh ]; then - . /tmp/env.sh +if [ -f /etc/env.sh ]; then + echo "importing values from env.sh.." + . /etc/env.sh else echo "env.sh not found!" fi @@ -13,8 +14,11 @@ if [ "${ALLOW_SHELL_HISTORY}" = "true" ]; then echo "Activating feature 'shell-history'" echo "User: ${USERNAME} User home: ${user_home}" + echo "Creating sub-folder with ${DEVCONTAINER_ID}.." + # Create the shell history directory in the mounted volume - HISTORY_DIR="/devcontainers/${DEVCONTAINER_ID}/shellHistory" + BASE_HISTORY_DIR="/devcontainers" + HISTORY_DIR="${BASE_HISTORY_DIR}/${DEVCONTAINER_ID}/shellHistory" USER_HISTORY_FILE="${user_home}/.bash_history" VOLUME_HISTORY_FILE="${HISTORY_DIR}/.bash_history" @@ -32,7 +36,9 @@ if [ "${ALLOW_SHELL_HISTORY}" = "true" ]; then sudo ln -sf ${USER_HISTORY_FILE} ${VOLUME_HISTORY_FILE} # Configure immediate history saving to the volume - echo 'PROMPT_COMMAND="history -a; history -r;"' >> "${user_home}/.bashrc" + if ! grep -q "PROMPT_COMMAND" "${user_home}/.bashrc"; then + echo 'PROMPT_COMMAND="history -a; history -r;"' >> "${user_home}/.bashrc" + fi - echo "Shell history setup for persistent appending is complete." -fi \ No newline at end of file + echo "Shell history setup for history persistence amongst active containers is complete." +fi diff --git a/test/common-utils/allow_shell_history.sh b/test/common-utils/allow_shell_history.sh index f1f42a1cf..933ed7171 100644 --- a/test/common-utils/allow_shell_history.sh +++ b/test/common-utils/allow_shell_history.sh @@ -5,5 +5,107 @@ set -e # Optional: Import test library source dev-container-features-test-lib +# Initialize an array to store container IDs +declare -a CONTAINER_IDS + +# Name of the generated script file +SCRIPT_NAME="build_and_run.sh" + +# Dynamically write the script to a file +cat > $SCRIPT_NAME <<'EOF' +#!/bin/bash + +# Parameters +BASE_IMAGE=${1:-"ubuntu:latest"} +IMAGE_NAME=${2:-"custom-image"} + +# Create Dockerfile +cat > Dockerfile <> ~/.bash_history" +} + +# Function to check shell history +check_shell_history() { + local container_id=$1 + docker exec -it $container_id /bin/bash -c "cat ~/.bash_history" +} + +# Start the first container and add shell history +docker start $container1 +add_shell_history $container1 "First container shell history" +docker stop $container1 + +# Start the second container and add shell history +docker start $container2 +add_shell_history $container2 "Second container shell history" +docker stop $container2 + +# Start both containers and check shell history persistence +docker start $container1 +echo "Shell history for container 1:" +check_shell_history $container1 +docker stop $container1 + +docker start $container2 +echo "Shell history for container 2:" +check_shell_history $container2 +docker stop $container2 + # Report result -reportResults \ No newline at end of file +reportResults diff --git a/test/common-utils/scenarios.json b/test/common-utils/scenarios.json index 60a8a5613..dc6fde589 100644 --- a/test/common-utils/scenarios.json +++ b/test/common-utils/scenarios.json @@ -296,11 +296,16 @@ "allow_shell_history": { "image": "debian:bookworm", "features": { + "docker-in-docker": {}, "common-utils": { "installZsh": true, "allowShellHistory": true } }, + "overrideFeatureInstallOrder": [ + "./common-utils", + "./docker-in-docker" + ], "remoteUser": "vscode", "containerUser": "vscode" } From f8a5099d0a7cadce22e52fedc51501811640d955 Mon Sep 17 00:00:00 2001 From: Kaniska244 Date: Tue, 10 Dec 2024 06:37:04 +0000 Subject: [PATCH 27/34] Updated shell history test scenarios in allow_shell_history.sh file --- test/common-utils/allow_shell_history.sh | 131 +++++++++++++---------- 1 file changed, 73 insertions(+), 58 deletions(-) diff --git a/test/common-utils/allow_shell_history.sh b/test/common-utils/allow_shell_history.sh index 933ed7171..7335ef4d4 100644 --- a/test/common-utils/allow_shell_history.sh +++ b/test/common-utils/allow_shell_history.sh @@ -5,9 +5,6 @@ set -e # Optional: Import test library source dev-container-features-test-lib -# Initialize an array to store container IDs -declare -a CONTAINER_IDS - # Name of the generated script file SCRIPT_NAME="build_and_run.sh" @@ -18,94 +15,112 @@ cat > $SCRIPT_NAME <<'EOF' # Parameters BASE_IMAGE=${1:-"ubuntu:latest"} IMAGE_NAME=${2:-"custom-image"} +CONTAINER_NAME=${3:-"${IMAGE_NAME}-container"} + +cat > setup_history.sh <> "/home/vscode/.bashrc" +fi + +echo "Shell history setup for history persistence amongst active containers is complete." +EOL + +# Create entrypoint script +cat > entrypoint.sh < /var/log/entrypoint.log + +# Execute setup history script +chmod +x /usr/local/bin/setup_history.sh >> /var/log/entrypoint.log 2>&1 +/usr/local/bin/setup_history.sh >> /var/log/entrypoint.log 2>&1 + +# Keep the container running +tail -f /dev/null +EOL # Create Dockerfile cat > Dockerfile < /dev/null -# Copy the setup_history.sh file to the running container -sudo docker cp /etc/env.sh $CONTAINER_ID:/etc/env.sh -sudo docker cp /etc/setup_history.sh $CONTAINER_ID:/etc/setup_history.sh +# Output the container ID to a file +echo "CONTAINER_ID=$CONTAINER_ID" > /tmp/container_id.txt - -# Execute the command inside the container to set the environment variable and run the script -docker exec -it $CONTAINER_ID bash -c "chmod +x /etc/setup_history.sh && export DEVCONTAINER_ID=${IMAGE_NAME} && . /etc/setup_history.sh" - -docker logs $CONTAINER_ID +echo "Started container: $CONTAINER_ID" EOF # Make the generated script executable chmod +x $SCRIPT_NAME - -echo "The script '$SCRIPT_NAME' has been created. You can execute it with parameters like:" -echo "./$SCRIPT_NAME python python-app" - ./$SCRIPT_NAME python python-app -./$SCRIPT_NAME node node-app - -# Run the first container (python-app) -CONTAINER_ID_PYTHON=$(docker run -it -d python-app) -CONTAINER_IDS+=("$CONTAINER_ID_PYTHON") -echo "Started python-app container: $CONTAINER_ID_PYTHON" - -# Run the second container (node-app) -CONTAINER_ID_NODE=$(docker run -it -d node-app) -CONTAINER_IDS+=("$CONTAINER_ID_NODE") -echo "Started node-app container: $CONTAINER_ID_NODE" - -# Export the container ID array to a file for use outside -export CONTAINER_IDS_STR="${CONTAINER_IDS[*]}" - -echo "Container IDs: $CONTAINER_IDS_STR" - -container1=${CONTAINER_IDS[0]} -container2=${CONTAINER_IDS[1]} # Function to add shell history add_shell_history() { local container_id=$1 local history_message=$2 + echo -e "\nWriting shell history: $history_message"; docker exec -it $container_id /bin/bash -c "echo \"$history_message\" >> ~/.bash_history" } # Function to check shell history check_shell_history() { local container_id=$1 + echo -e "\nChecking shell history from container: "; docker exec -it $container_id /bin/bash -c "cat ~/.bash_history" } -# Start the first container and add shell history -docker start $container1 -add_shell_history $container1 "First container shell history" -docker stop $container1 - -# Start the second container and add shell history -docker start $container2 -add_shell_history $container2 "Second container shell history" -docker stop $container2 - -# Start both containers and check shell history persistence -docker start $container1 -echo "Shell history for container 1:" -check_shell_history $container1 -docker stop $container1 - -docker start $container2 -echo "Shell history for container 2:" -check_shell_history $container2 -docker stop $container2 +source /tmp/container_id.txt + +# Start the container and add shell history +docker start $CONTAINER_ID > /dev/null +add_shell_history $CONTAINER_ID "Shell History for First Container Created." +docker stop $CONTAINER_ID > /dev/null + +# Start the container and check shell history persistence +docker start $CONTAINER_ID > /dev/null +check_shell_history $CONTAINER_ID # Report result reportResults From ddcf3ae7cc0842a8a8e83db59e610aa1813f9aae Mon Sep 17 00:00:00 2001 From: Kaniska244 Date: Tue, 10 Dec 2024 06:52:02 +0000 Subject: [PATCH 28/34] Correcting the devcontainer-feature.json file --- src/common-utils/devcontainer-feature.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index cb70e338e..715f5a55c 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -90,5 +90,5 @@ "target": "/var/run/docker.sock", "type": "bind" }], - "entrypoint": ["/usr/local/share/docker-init.sh", "/usr/local/share/ssh-init.sh"] + "entrypoint": "/usr/local/share/docker-init.sh" } From f95f1f69db813b28abe11e35a4251a91f999a9b3 Mon Sep 17 00:00:00 2001 From: Kaniska244 Date: Tue, 10 Dec 2024 07:45:23 +0000 Subject: [PATCH 29/34] Fixing allow_shell_history issue --- src/common-utils/devcontainer-feature.json | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index 715f5a55c..6b74eb9ca 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -84,11 +84,6 @@ "source": "dind-var-lib-docker-${devcontainerId}", "target": "/var/lib/docker", "type": "volume" - }, - { - "source": "/var/run/docker.sock", - "target": "/var/run/docker.sock", - "type": "bind" }], "entrypoint": "/usr/local/share/docker-init.sh" } From eaa03c2aff3f228a878cef4019ffdfa6e41413cd Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Thu, 12 Dec 2024 05:16:33 +0000 Subject: [PATCH 30/34] few changes for check correction --- test/common-utils/allow_shell_history.sh | 3 +-- test/common-utils/scenarios.json | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/test/common-utils/allow_shell_history.sh b/test/common-utils/allow_shell_history.sh index 7335ef4d4..ac84e6e8f 100644 --- a/test/common-utils/allow_shell_history.sh +++ b/test/common-utils/allow_shell_history.sh @@ -72,7 +72,6 @@ EOL cat > Dockerfile < Date: Fri, 13 Dec 2024 07:17:45 +0000 Subject: [PATCH 31/34] misc change --- test/common-utils/allow_shell_history.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/common-utils/allow_shell_history.sh b/test/common-utils/allow_shell_history.sh index ac84e6e8f..d60a2e5c1 100644 --- a/test/common-utils/allow_shell_history.sh +++ b/test/common-utils/allow_shell_history.sh @@ -15,7 +15,6 @@ cat > $SCRIPT_NAME <<'EOF' # Parameters BASE_IMAGE=${1:-"ubuntu:latest"} IMAGE_NAME=${2:-"custom-image"} -CONTAINER_NAME=${3:-"${IMAGE_NAME}-container"} cat > setup_history.sh < Date: Fri, 13 Dec 2024 07:41:25 +0000 Subject: [PATCH 32/34] misc change --- test/common-utils/allow_shell_history.sh | 14 +++++------ test/common-utils/scenarios.json | 32 ++++++++++++------------ 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/test/common-utils/allow_shell_history.sh b/test/common-utils/allow_shell_history.sh index d60a2e5c1..2c8a15408 100644 --- a/test/common-utils/allow_shell_history.sh +++ b/test/common-utils/allow_shell_history.sh @@ -79,10 +79,10 @@ CMD ["bash"] EOL # Build and tag the image -docker build -t $IMAGE_NAME . +sudo docker build -t $IMAGE_NAME . # Run the container again for attaching volume -CONTAINER_ID=$(docker run -v devcontainers:/devcontainers -itd $IMAGE_NAME) > /dev/null +CONTAINER_ID=$(sudo docker run -v devcontainers:/devcontainers -itd $IMAGE_NAME) > /dev/null # Output the container ID to a file echo "CONTAINER_ID=$CONTAINER_ID" > /tmp/container_id.txt @@ -99,25 +99,25 @@ add_shell_history() { local container_id=$1 local history_message=$2 echo -e "\nWriting shell history: $history_message"; - docker exec -it $container_id /bin/bash -c "echo \"$history_message\" >> ~/.bash_history" + sudo docker exec -it $container_id /bin/bash -c "echo \"$history_message\" >> ~/.bash_history" } # Function to check shell history check_shell_history() { local container_id=$1 echo -e "\nChecking shell history from container: "; - docker exec -it $container_id /bin/bash -c "cat ~/.bash_history" + sudo docker exec -it $container_id /bin/bash -c "cat ~/.bash_history" } source /tmp/container_id.txt # Start the container and add shell history -docker start $CONTAINER_ID > /dev/null +sudo docker start $CONTAINER_ID > /dev/null add_shell_history $CONTAINER_ID "Shell History for First Container Created." -docker stop $CONTAINER_ID > /dev/null +sudo docker stop $CONTAINER_ID > /dev/null # Start the container and check shell history persistence -docker start $CONTAINER_ID > /dev/null +sudo docker start $CONTAINER_ID > /dev/null check_shell_history $CONTAINER_ID # Report result diff --git a/test/common-utils/scenarios.json b/test/common-utils/scenarios.json index 2602ccc56..b500675a5 100644 --- a/test/common-utils/scenarios.json +++ b/test/common-utils/scenarios.json @@ -1,4 +1,20 @@ { + "allow_shell_history": { + "image": "mcr.microsoft.com/devcontainers/base:ubuntu", + "features": { + "docker-in-docker": {}, + "common-utils": { + "installZsh": true, + "allowShellHistory": true + } + }, + "overrideFeatureInstallOrder": [ + "./common-utils", + "./docker-in-docker" + ], + "remoteUser": "vscode", + "containerUser": "vscode" + }, "bionic": { "image": "ubuntu:bionic", "remoteUser": "devcontainer", @@ -292,21 +308,5 @@ "features": { "common-utils": {} } - }, - "allow_shell_history": { - "image": "mcr.microsoft.com/devcontainers/base:ubuntu", - "features": { - "docker-in-docker": {}, - "common-utils": { - "installZsh": true, - "allowShellHistory": true - } - }, - "overrideFeatureInstallOrder": [ - "./common-utils", - "./docker-in-docker" - ], - "remoteUser": "vscode", - "containerUser": "vscode" } } \ No newline at end of file From da0ae0e9061bc182a45fb7177174dde8f3ce6ffa Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Fri, 13 Dec 2024 08:05:22 +0000 Subject: [PATCH 33/34] misc change for docker commands to work in test case --- src/common-utils/devcontainer-feature.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index 6b74eb9ca..715f5a55c 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -84,6 +84,11 @@ "source": "dind-var-lib-docker-${devcontainerId}", "target": "/var/lib/docker", "type": "volume" + }, + { + "source": "/var/run/docker.sock", + "target": "/var/run/docker.sock", + "type": "bind" }], "entrypoint": "/usr/local/share/docker-init.sh" } From abac4e4cc09b01a18d06d31d7aa693a73c14dfb6 Mon Sep 17 00:00:00 2001 From: Gaurav Saini <147703805+gauravsaini04@users.noreply.github.com> Date: Fri, 13 Dec 2024 09:12:59 +0000 Subject: [PATCH 34/34] misc changes --- test/common-utils/allow_shell_history.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/common-utils/allow_shell_history.sh b/test/common-utils/allow_shell_history.sh index 2c8a15408..efe4449e6 100644 --- a/test/common-utils/allow_shell_history.sh +++ b/test/common-utils/allow_shell_history.sh @@ -99,14 +99,14 @@ add_shell_history() { local container_id=$1 local history_message=$2 echo -e "\nWriting shell history: $history_message"; - sudo docker exec -it $container_id /bin/bash -c "echo \"$history_message\" >> ~/.bash_history" + sudo docker exec -i $container_id /bin/bash -c "echo \"$history_message\" >> ~/.bash_history" } # Function to check shell history check_shell_history() { local container_id=$1 echo -e "\nChecking shell history from container: "; - sudo docker exec -it $container_id /bin/bash -c "cat ~/.bash_history" + sudo docker exec -i $container_id /bin/bash -c "cat ~/.bash_history" } source /tmp/container_id.txt