From e1bc348254daeea55b47f650a5657343a77c3440 Mon Sep 17 00:00:00 2001 From: yairgott Date: Sat, 25 Jan 2025 00:25:45 +0000 Subject: [PATCH] adding dev container setup script --- .devcontainer/Dockerfile | 19 +++++++--- ...vcontainer.json => devcontainer_base.json} | 11 ++++-- .devcontainer/setup.sh | 35 +++++++++++++++++++ .gitignore | 1 + DEVELOPER.md | 11 ++++-- ci/entrypoint.sh | 4 +-- 6 files changed, 69 insertions(+), 12 deletions(-) rename .devcontainer/{devcontainer.json => devcontainer_base.json} (75%) create mode 100755 .devcontainer/setup.sh diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index a7089b4..a674734 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -56,19 +56,28 @@ ENV PATH="/usr/local/bin:$PATH" # Set working directory WORKDIR /workspace -# Add non-root user for development -RUN useradd -m vscode && echo "vscode ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers +ARG USERNAME=ubuntu +ENV USERNAME=${USERNAME} +ARG USER_UID=1000 +ENV USER_UID=${USER_UID} +ARG USER_GID=1000 +ENV USER_GID=${USER_GID} -USER vscode +RUN groupadd -g $USER_GID $USERNAME || true; \ + useradd -ms /bin/bash -u $USER_UID -g $USER_GID $USERNAME || true; \ + echo "$USERNAME ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers || true; + +USER $USERNAME # Add an alias for 'vi' to point to 'vim' if 'vi' doesn't exist -RUN echo 'if ! command -v vi &> /dev/null; then alias vi="vim"; fi' >> /home/vscode/.bashrc +RUN echo 'if ! command -v vi &> /dev/null; then alias vi="vim"; fi' >> /home/$USERNAME/.bashrc ARG ENABLE_COMP_DB_REFRESH=true ENV ENABLE_COMP_DB_REFRESH=${ENABLE_COMP_DB_REFRESH} # Ensure the history file exists and configure permissions -RUN touch /home/vscode/.bash_history && chmod 600 /home/vscode/.bash_history +RUN touch /home/$USERNAME/.bash_history && \ + chmod 600 /home/$USERNAME/.bash_history ENV CC=clang ENV CXX=clang++ diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer_base.json similarity index 75% rename from .devcontainer/devcontainer.json rename to .devcontainer/devcontainer_base.json index eebaccf..66abc57 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer_base.json @@ -2,7 +2,12 @@ "name": "ValkeySearch Development Environment", "build": { "dockerfile": "Dockerfile", - "context": "." + "context": ".", + "args": { + "USERNAME": "ubuntu", + "USER_UID": "1000", + "USER_GID": "1000" + } }, "customizations": { "vscode": { @@ -24,6 +29,6 @@ ] } }, - "remoteUser": "vscode", - "postCreateCommand": "./ci/entrypoint.sh && /bin/bash" + "remoteUser": "ubuntu", + "postCreateCommand": "export USER=ubuntu && ./ci/entrypoint.sh && /bin/bash" } \ No newline at end of file diff --git a/.devcontainer/setup.sh b/.devcontainer/setup.sh new file mode 100755 index 0000000..9179b08 --- /dev/null +++ b/.devcontainer/setup.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +# Get the current user's ID, group ID, and username +user_id=$(id -u) +group_id=$(id -g) +user_name=$(whoami) + +# Define source and destination file paths +source_file=".devcontainer/devcontainer_base.json" +destination_file=".devcontainer/devcontainer.json" + +# Check if the source file exists +if [[ ! -f $source_file ]]; then + echo "Source file $source_file does not exist. Exiting." + exit 1 +fi + +# Create the destination file with the updated content +sed -e "s/\"USER_UID\": \"1000\"/\"USER_UID\": \"$user_id\"/g" \ + -e "s/\"USER_GID\": \"1000\"/\"USER_GID\": \"$group_id\"/g" \ + -e "s/\"remoteUser\": \"ubuntu\"/\"remoteUser\": \"$user_name\"/g" \ + -e "s/\"USERNAME\": \"ubuntu\"/\"USERNAME\": \"$user_name\"/g" \ + -e "s/USER=ubuntu/USER=$user_name/g" \ + "$source_file" > "$destination_file" + +# Verify the operation and provide feedback +if [[ $? -eq 0 ]]; then + echo "File successfully created at $destination_file with updated user information:" + echo " USER_UID: $user_id" + echo " USER_GID: $group_id" + echo " remoteUser: $user_name" +else + echo "Failed to create the file $destination_file." + exit 2 +fi diff --git a/.gitignore b/.gitignore index 5ce202b..f41f421 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ MODULE.bazel.lock resolved_deps.py *.swp compile_commands.json +.devcontainer/devcontainer.json diff --git a/DEVELOPER.md b/DEVELOPER.md index 6176080..38babb6 100644 --- a/DEVELOPER.md +++ b/DEVELOPER.md @@ -10,13 +10,20 @@ For development purposes, it is recommended to use VSCode, which is alrea - Note: Building the code may take some time, and it is important to use a host with decent CPU capacity. If you prefer, you can use a remote host. In that case, also install the following extensions: - `Remote - SSH` by Microsoft - `Remote Explorer` by Microsoft -2. Open the Repository in VSCode: + +2. Run the dev container setup script + - Issue the following command from the cloned repo root directory: + ```bash + .devcontainer/setup.sh + ``` + +3. Open the Repository in VSCode: - On your local machine, open the root directory of the cloned valkey-search repository in VSCode. - If the repository is located on a remote host: 1. Press Ctrl+Shift+P (Windows/Linux) or Cmd+Shift+P (macOS) to open the Command Palette. 2. Type Remote-SSH: Connect to Host and select it. 3. Choose the appropriate host and provide any necessary authentication details. - + Once connected, VSCode will open the repository in the context of the remote host. diff --git a/ci/entrypoint.sh b/ci/entrypoint.sh index 9e0cf9a..ec92ba0 100755 --- a/ci/entrypoint.sh +++ b/ci/entrypoint.sh @@ -1,9 +1,9 @@ #!/bin/bash set -e export MOUNTED_DIR=$(pwd) -echo "export MOUNTED_DIR=$MOUNTED_DIR" >> /home/vscode/.bashrc +echo "export MOUNTED_DIR=$MOUNTED_DIR" >> /home/$USER/.bashrc if [ "$ENABLE_COMP_DB_REFRESH" = "true" ]; then - echo "$(pwd)/ci/refresh_comp_db.sh &> /dev/null &" >> /home/vscode/.bashrc + echo "$(pwd)/ci/refresh_comp_db.sh &> /dev/null &" >> /home/$USER/.bashrc fi bazel build //... bazel run @hedron_compile_commands//:refresh_all \ No newline at end of file