Skip to content

Commit

Permalink
Updated shell history changes for PR#1157
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaniska244 committed Dec 6, 2024
1 parent d6ba404 commit 8406068
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 17 deletions.
19 changes: 16 additions & 3 deletions src/common-utils/devcontainer-feature.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
13 changes: 7 additions & 6 deletions src/common-utils/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 **
Expand Down
20 changes: 13 additions & 7 deletions src/common-utils/scripts/setup_history.sh
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"

Expand All @@ -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
echo "Shell history setup for history persistence amongst active containers is complete."
fi
104 changes: 103 additions & 1 deletion test/common-utils/allow_shell_history.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<EOL
FROM $BASE_IMAGE
RUN apt-get update && apt-get install -y curl git sudo
RUN useradd -m vscode
CMD ["bash"]
EOL
# Build and tag the image
docker build -t $IMAGE_NAME .
# Run the container in the background
CONTAINER_ID=$(docker run -it -d $IMAGE_NAME)
echo "Started container: $CONTAINER_ID"
# 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
# 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
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
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
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
reportResults
5 changes: 5 additions & 0 deletions test/common-utils/scenarios.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down

0 comments on commit 8406068

Please sign in to comment.