From 9e9308beaa8c291877c29261ba3be7b337bc0ba5 Mon Sep 17 00:00:00 2001 From: geebytes Date: Fri, 14 Mar 2025 05:59:12 +0000 Subject: [PATCH 1/3] feat(build):add vscode devcontainer config --- .devcontainer.json | 76 ++++++++++++++++++++++++++++++++++++ .devcontainer/Dockerfile.dev | 40 +++++++++++++++++++ .devcontainer/dbgpt.pth | 8 ++++ .devcontainer/post-create.sh | 54 +++++++++++++++++++++++++ Makefile | 7 ++-- pyproject.toml | 2 +- 6 files changed, 183 insertions(+), 4 deletions(-) create mode 100755 .devcontainer.json create mode 100644 .devcontainer/Dockerfile.dev create mode 100644 .devcontainer/dbgpt.pth create mode 100755 .devcontainer/post-create.sh diff --git a/.devcontainer.json b/.devcontainer.json new file mode 100755 index 000000000..db8bfafff --- /dev/null +++ b/.devcontainer.json @@ -0,0 +1,76 @@ +{ + // Set container runtime user + "remoteUser": "work", + "build": { + "dockerfile": ".devcontainer/Dockerfile.dev", + "context": "./", + "args": { + "USER_UID": "${localEnv:UID:1001}", + "USER_GID": "${localEnv:GID:1001}", + "USER_NAME":"${localEnv:USER:work}" + }, + "options": ["--no-cache"] + + }, + "name": "dbgpt", + "workspaceFolder": "/app", + "workspaceMount": "source=${localWorkspaceFolder},target=/app,type=bind", + "runArgs": [ + "--network", + "host", + "--runtime=nvidia", + "--gpus", + "all", + "-e", + "LOCAL_DB_HOST=${localEnv:LOCAL_DB_HOST}", + "-e", + "LOCAL_DB_PASSWORD=${localEnv:LOCAL_DB_PASSWORD}", + "-e", + "MYSQL_ROOT_PASSWORD=${localEnv:MYSQL_ROOT_PASSWORD}", + "-e", + "LLM_MODEL=${localEnv:LLM_MODEL}", + "-e", + "LANGUAGE=${localEnv:LANGUAGE}", + "-e", + "PROXY_SERVER_URL=${localEnv:PROXY_SERVER_URL}", + "-e", + "HF_HOME=/app/models" + ], + "mounts": [ + // sharing-git-credentials see https://code.visualstudio.com/remote/advancedcontainers/sharing-git-credentials + "source=${localEnv:SSH_AUTH_SOCK},target=/run/host-services/ssh-auth.sock,type=bind", + // mount to local models + "source=${localWorkspaceFolder}/models/text2vec-large-chinese,target=/app/models/text2vec-large-chinese,type=bind" + ], + "containerEnv": { + "SSH_AUTH_SOCK": "/run/host-services/ssh-auth.sock" + }, + "postCreateCommand": "chmod +x /app/.devcontainer/post-create.sh && /app/.devcontainer/post-create.sh", + "customizations": { + "vscode": { + "settings": { + "extensions.verifySignature": false, + "http.proxyStrictSSL": false, + "python.linting.flake8Enabled": true, + "python.languageServer": "Pylance", + "python.linting.enabled": true, + "terminal.integrated.defaultProfile.linux": "zsh", + "terminal.integrated.shell.linux": "/bin/zsh", + "python.linting.mypyEnabled": true, + "python.linting.provider": "ruff", + "python.formatting.provider": "ruff" + }, + "extensions": [ + "ms-python.python", + "ms-python.isort", + "ms-python.vscode-pylance", + "ms-python.autopep8", + "ms-vscode.makefile-tools", + "ms-python.flake8", + "ms-azuretools.vscode-docker", + "ms-python.mypy-type-checker", + "charliermarsh.ruff" + ] + } + } +} diff --git a/.devcontainer/Dockerfile.dev b/.devcontainer/Dockerfile.dev new file mode 100644 index 000000000..486490797 --- /dev/null +++ b/.devcontainer/Dockerfile.dev @@ -0,0 +1,40 @@ +FROM eosphorosai/dbgpt:latest +ARG EXTRAS="proxy_openai,rag,storage_chromadb,quant_bnb,graph_rag" +ARG PYTHON_VERSION=3.10 +ARG USER_UID=1001 +ARG USER_GID=1001 +ARG USER=work +ARG PIP_INDEX_URL="https://pypi.tuna.tsinghua.edu.cn/simple" + +WORKDIR /app +COPY . . +USER root +# Set the GID and UID of the container and +# add a user to prevent permission mismatches +# between the container user (root) and the host user, +# and to resolve the issue of the host user lacking write permissions. +RUN groupadd -g $USER_GID $USER && \ + useradd -u $USER_UID -g $USER_GID -m $USER && \ + chown -R $USER_UID:$USER_GID /app + +RUN apt-get update && apt-get install -y \ + git \ + curl \ + wget \ + ssh zsh autojump curl git-flow vim sudo \ + && python${PYTHON_VERSION} -m pip install --upgrade pip \ + && python${PYTHON_VERSION} -m pip install --upgrade pipx \ + && pipx install uv --global \ + && chown -R $USER:$USER /opt/.uv.venv \ + && echo "$USER ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/$USER \ + && chmod 0440 /etc/sudoers.d/$USER +USER $USER +ENV UV_LINK_MODE=copy +RUN /opt/.uv.venv/bin/python3 -m pip install uv -i https://mirrors.aliyun.com/pypi/simple/ && \ + extras=$(echo $EXTRAS | tr ',' '\n' | while read extra; do echo "--extra $extra"; done | tr '\n' ' ') && \ + echo $extras && \ + /opt/.uv.venv/bin/uv pip install -r pyproject.toml --all-extras && \ + /opt/.uv.venv/bin/uv pip install -r requirements/dev-requirements.txt && \ + /opt/.uv.venv/bin/uv pip install -r requirements/lint-requirements.txt && \ + cp .devcontainer/dbgpt.pth /opt/.uv.venv/lib/python3.10/site-packages/dbgpt.pth && \ + python -c "import dbgpt; print(dbgpt.__version__)" \ No newline at end of file diff --git a/.devcontainer/dbgpt.pth b/.devcontainer/dbgpt.pth new file mode 100644 index 000000000..b4f79cbee --- /dev/null +++ b/.devcontainer/dbgpt.pth @@ -0,0 +1,8 @@ +/app/packages/dbgpt-app/src +/app/packages/dbgpt-accelerator +/app/packages/dbgpt-accelerator/src +/app/packages/dbgpt-core/src +/app/packages/dbgpt-client/src +/app/packages/dbgpt-ext/src +/app/packages/dbgpt-serve/src +/app/packages/dbgpt-app/src \ No newline at end of file diff --git a/.devcontainer/post-create.sh b/.devcontainer/post-create.sh new file mode 100755 index 000000000..d85b414f8 --- /dev/null +++ b/.devcontainer/post-create.sh @@ -0,0 +1,54 @@ +#!/bin/bash +set -e +cd /app +# Install Oh My Zsh +if [ ! -d ~/.oh-my-zsh ]; then + sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended +fi + +# Install plugins +plugins=( + "zsh-users/zsh-autosuggestions" + "zsh-users/zsh-syntax-highlighting" +) + +for plugin in "${plugins[@]}"; do + repo_name=$(basename $plugin) + if [ ! -d ~/.oh-my-zsh/custom/plugins/$repo_name ]; then + git clone --depth=1 https://github.com/$plugin.git ~/.oh-my-zsh/custom/plugins/$repo_name + fi +done + +# Install theme +if [ ! -d ~/.oh-my-zsh/custom/themes/powerlevel10k ]; then + git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ~/.oh-my-zsh/custom/themes/powerlevel10k +fi + +# Apply custom configuration +if [ -f /workspace/.devcontainer/zshrc-config ]; then + cp /workspace/.devcontainer/zshrc-config ~/.zshrc +else + # Generate basic .zshrc if no custom configuration exists + cat << EOF > ~/.zshrc +export ZSH="\$HOME/.oh-my-zsh" +ZSH_THEME="robbyrussell" +plugins=(git zsh-autosuggestions zsh-syntax-highlighting autojump) +source \$ZSH/oh-my-zsh.sh + +# Enable autojump +[[ -s /usr/share/autojump/autojump.sh ]] && source /usr/share/autojump/autojump.sh +EOF +fi + +# Ensure autojump configuration is applied (even if custom configuration exists) +if ! grep -q "autojump.sh" ~/.zshrc; then + echo '[[ -s /usr/share/autojump/autojump.sh ]] && source /usr/share/autojump/autojump.sh' >> ~/.zshrc +fi +cat << EOF >> ~/.zshrc +# Add the following to ~/.zshrc +if [ -f /app/.env ]; then + export $(grep -vE '^#|^$' /app/.env | xargs) +fi +EOF +rm -rf .venv.make +echo "Post-create setup completed!" \ No newline at end of file diff --git a/Makefile b/Makefile index 51399cf26..184b743c8 100644 --- a/Makefile +++ b/Makefile @@ -24,13 +24,14 @@ $(VENV)/.venv-timestamp: uv.lock testenv: $(VENV)/.testenv $(VENV)/.testenv: $(VENV)/bin/activate - uv sync --all-packages \ + . $(VENV_BIN)/activate && uv sync --active --all-packages \ --extra "base" \ --extra "proxy_openai" \ --extra "rag" \ --extra "storage_chromadb" \ --extra "dbgpts" \ --link-mode=copy + cp .devcontainer/dbgpt.pth $(VENV)/lib/python3.11/site-packages touch $(VENV)/.testenv @@ -69,7 +70,7 @@ fmt-check: setup ## Check Python code formatting and style without making change pre-commit: fmt-check test test-doc mypy ## Run formatting and unit tests before committing test: $(VENV)/.testenv ## Run unit tests - $(VENV_BIN)/pytest dbgpt + $(VENV_BIN)/pytest --pyargs dbgpt .PHONY: test-doc test-doc: $(VENV)/.testenv ## Run doctests @@ -88,7 +89,7 @@ mypy: $(VENV)/.testenv ## Run mypy checks .PHONY: coverage coverage: setup ## Run tests and report coverage - $(VENV_BIN)/pytest dbgpt --cov=dbgpt + $(VENV_BIN)/pytest --pyargs dbgpt --cov=dbgpt .PHONY: clean clean: ## Clean up the environment diff --git a/pyproject.toml b/pyproject.toml index 74878e886..6bfa5bf49 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,7 +28,7 @@ members = [ "packages/dbgpt-core", "packages/dbgpt-ext", "packages/dbgpt-serve", - "packages/dbgpt-accelerator/*" + "packages/dbgpt-accelerator/dbgpt-acc*" ] [tool.uv] From 69b71a8cac66c189cddf165f37039acb3c0ece95 Mon Sep 17 00:00:00 2001 From: geebytes Date: Sun, 16 Mar 2025 09:43:19 +0000 Subject: [PATCH 2/3] feat:get user info by get_env.sh chore(build):Optimize the zsh setup fix(dev):Load .env on demand fix(dev):install oh-my-zsh from mirror --- .devcontainer.json | 17 ++++++------- .devcontainer/Dockerfile.dev | 46 ++++++++++++++++++------------------ .devcontainer/get_env.sh | 22 +++++++++++++++++ .devcontainer/post-create.sh | 40 +++++++++++++++++++++---------- 4 files changed, 82 insertions(+), 43 deletions(-) create mode 100755 .devcontainer/get_env.sh diff --git a/.devcontainer.json b/.devcontainer.json index db8bfafff..f01b2c963 100755 --- a/.devcontainer.json +++ b/.devcontainer.json @@ -1,17 +1,16 @@ { // Set container runtime user - "remoteUser": "work", "build": { "dockerfile": ".devcontainer/Dockerfile.dev", "context": "./", "args": { - "USER_UID": "${localEnv:UID:1001}", - "USER_GID": "${localEnv:GID:1001}", - "USER_NAME":"${localEnv:USER:work}" + "USERNAME": "${localEnv:USER}" }, - "options": ["--no-cache"] - + "options": [ + "--no-cache" + ] }, + "initializeCommand": ".devcontainer/get_env.sh", "name": "dbgpt", "workspaceFolder": "/app", "workspaceMount": "source=${localWorkspaceFolder},target=/app,type=bind", @@ -38,8 +37,10 @@ ], "mounts": [ // sharing-git-credentials see https://code.visualstudio.com/remote/advancedcontainers/sharing-git-credentials + // This will enable you to work with the repository code using Git inside the Dev container. "source=${localEnv:SSH_AUTH_SOCK},target=/run/host-services/ssh-auth.sock,type=bind", // mount to local models + // Persist the model to avoid redundant downloads. "source=${localWorkspaceFolder}/models/text2vec-large-chinese,target=/app/models/text2vec-large-chinese,type=bind" ], "containerEnv": { @@ -61,7 +62,7 @@ "python.formatting.provider": "ruff" }, "extensions": [ - "ms-python.python", + "ms-python.python", "ms-python.isort", "ms-python.vscode-pylance", "ms-python.autopep8", @@ -73,4 +74,4 @@ ] } } -} +} \ No newline at end of file diff --git a/.devcontainer/Dockerfile.dev b/.devcontainer/Dockerfile.dev index 486490797..4528cc193 100644 --- a/.devcontainer/Dockerfile.dev +++ b/.devcontainer/Dockerfile.dev @@ -1,11 +1,8 @@ FROM eosphorosai/dbgpt:latest -ARG EXTRAS="proxy_openai,rag,storage_chromadb,quant_bnb,graph_rag" -ARG PYTHON_VERSION=3.10 -ARG USER_UID=1001 -ARG USER_GID=1001 -ARG USER=work -ARG PIP_INDEX_URL="https://pypi.tuna.tsinghua.edu.cn/simple" - +ARG PYTHON_VERSION=3.11 +ARG PIP_INDEX_URL="https://mirrors.aliyun.com/pypi/simple" +ARG USERNAME +ARG DEFAULT_VEN=/opt/.uv.venv WORKDIR /app COPY . . USER root @@ -13,10 +10,10 @@ USER root # add a user to prevent permission mismatches # between the container user (root) and the host user, # and to resolve the issue of the host user lacking write permissions. -RUN groupadd -g $USER_GID $USER && \ - useradd -u $USER_UID -g $USER_GID -m $USER && \ +RUN . .devcontainer/.env && \ + groupadd -g $USER_GID $USERNAME && \ + useradd -u $USER_UID -g $USER_GID -m $USERNAME && \ chown -R $USER_UID:$USER_GID /app - RUN apt-get update && apt-get install -y \ git \ curl \ @@ -24,17 +21,20 @@ RUN apt-get update && apt-get install -y \ ssh zsh autojump curl git-flow vim sudo \ && python${PYTHON_VERSION} -m pip install --upgrade pip \ && python${PYTHON_VERSION} -m pip install --upgrade pipx \ - && pipx install uv --global \ - && chown -R $USER:$USER /opt/.uv.venv \ - && echo "$USER ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/$USER \ - && chmod 0440 /etc/sudoers.d/$USER -USER $USER -ENV UV_LINK_MODE=copy -RUN /opt/.uv.venv/bin/python3 -m pip install uv -i https://mirrors.aliyun.com/pypi/simple/ && \ - extras=$(echo $EXTRAS | tr ',' '\n' | while read extra; do echo "--extra $extra"; done | tr '\n' ' ') && \ - echo $extras && \ - /opt/.uv.venv/bin/uv pip install -r pyproject.toml --all-extras && \ - /opt/.uv.venv/bin/uv pip install -r requirements/dev-requirements.txt && \ - /opt/.uv.venv/bin/uv pip install -r requirements/lint-requirements.txt && \ - cp .devcontainer/dbgpt.pth /opt/.uv.venv/lib/python3.10/site-packages/dbgpt.pth && \ + && pipx install -i $PIP_INDEX_URL uv --global \ + && chown -R $USERNAME:$USERNAME $DEFAULT_VEN \ + && echo "$USERNAME ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/$USERNAME \ + && chmod 0440 /etc/sudoers.d/$USERNAME +USER $USERNAME +ENV UV_LINK_MODE=copy \ + PIP_INDEX_URL=$PIP_INDEX_URL \ + VIRTUAL_ENV=$DEFAULT_VEN \ + UV_PROJECT_ENVIRONMENT=$DEFAULT_VEN \ + UV_PYTHON=$DEFAULT_VEN/bin/python3 + +RUN . $DEFAULT_VEN/bin/activate && \ + uv pip install --prefix $VIRTUAL_ENV -r pyproject.toml --all-extras --index-url=$PIP_INDEX_URL && \ + uv pip install --prefix $VIRTUAL_ENV -r requirements/dev-requirements.txt --index-url=$PIP_INDEX_URL && \ + uv pip install --prefix $VIRTUAL_ENV -r requirements/lint-requirements.txt --index-url=$PIP_INDEX_URL && \ + cp .devcontainer/dbgpt.pth /opt/.uv.venv/lib/python${PYTHON_VERSION}/site-packages/dbgpt.pth && \ python -c "import dbgpt; print(dbgpt.__version__)" \ No newline at end of file diff --git a/.devcontainer/get_env.sh b/.devcontainer/get_env.sh new file mode 100755 index 000000000..3ba41ae0a --- /dev/null +++ b/.devcontainer/get_env.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +set -euo pipefail + +OS=$(uname -s) +USERNAME="$USER" +USER_UID=$(id -u "$USER") + +if [ "$OS" = "Linux" ]; then + GROUPNAME=$(id -gn "$USER") + USER_GID=$(id -g "$USER") +else + GROUPNAME="root" + USER_GID="0" +fi + + +printf "OS=%s\nUSERNAME=%s\nUSER_UID=%s\nGROUPNAME=%s\nUSER_GID=%s\n" \ + "$OS" \ + "$USERNAME" \ + "$USER_UID" \ + "$GROUPNAME" \ + "$USER_GID" > .devcontainer/.env diff --git a/.devcontainer/post-create.sh b/.devcontainer/post-create.sh index d85b414f8..902edd2a1 100755 --- a/.devcontainer/post-create.sh +++ b/.devcontainer/post-create.sh @@ -1,12 +1,14 @@ #!/bin/bash set -e cd /app -# Install Oh My Zsh -if [ ! -d ~/.oh-my-zsh ]; then - sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended + +# Install Oh My Zsh with mirror fallback +if [ ! -f ~/.oh-my-zsh/oh-my-zsh.sh ]; then + echo "Installing Oh My Zsh..." + REPO=mirrors/oh-my-zsh REMOTE=https://gitee.com/mirrors/oh-my-zsh.git sh -c "$(curl -fsSL https://gitee.com/mirrors/oh-my-zsh/raw/master/tools/install.sh)" "" --unattended fi -# Install plugins +# Install plugins with mirror switching plugins=( "zsh-users/zsh-autosuggestions" "zsh-users/zsh-syntax-highlighting" @@ -15,21 +17,29 @@ plugins=( for plugin in "${plugins[@]}"; do repo_name=$(basename $plugin) if [ ! -d ~/.oh-my-zsh/custom/plugins/$repo_name ]; then - git clone --depth=1 https://github.com/$plugin.git ~/.oh-my-zsh/custom/plugins/$repo_name + echo "Installing plugin: $plugin" + # Clone from GitHub with Gitee mirror fallback + git clone --depth=1 https://github.com/$plugin.git ~/.oh-my-zsh/custom/plugins/$repo_name || \ + git clone --depth=1 https://gitee.com/zsh-users/$repo_name.git ~/.oh-my-zsh/custom/plugins/$repo_name fi -done +done -# Install theme + +# Install theme with mirror fallback if [ ! -d ~/.oh-my-zsh/custom/themes/powerlevel10k ]; then - git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ~/.oh-my-zsh/custom/themes/powerlevel10k + echo "Installing powerlevel10k theme..." + # Clone from GitHub with Gitee mirror fallback + git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ~/.oh-my-zsh/custom/themes/powerlevel10k || \ + git clone --depth=1 https://gitee.com/romkatv/powerlevel10k.git ~/.oh-my-zsh/custom/themes/powerlevel10k fi +# Configuration section remains the same... # Apply custom configuration if [ -f /workspace/.devcontainer/zshrc-config ]; then cp /workspace/.devcontainer/zshrc-config ~/.zshrc else # Generate basic .zshrc if no custom configuration exists - cat << EOF > ~/.zshrc + cat << EOF >> ~/.zshrc export ZSH="\$HOME/.oh-my-zsh" ZSH_THEME="robbyrussell" plugins=(git zsh-autosuggestions zsh-syntax-highlighting autojump) @@ -46,9 +56,15 @@ if ! grep -q "autojump.sh" ~/.zshrc; then fi cat << EOF >> ~/.zshrc # Add the following to ~/.zshrc -if [ -f /app/.env ]; then - export $(grep -vE '^#|^$' /app/.env | xargs) -fi +load_env() { + if [ -f /app/.env ]; then + ENV_CONTENT=$(grep -vE '^#|^$' /app/.env | xargs) + if [ -n "$ENV_CONTENT" ]; then + export $ENV_CONTENT + fi + fi +} +load_env EOF rm -rf .venv.make echo "Post-create setup completed!" \ No newline at end of file From 599ebff27865349cc2c4669001962acf388d2dd1 Mon Sep 17 00:00:00 2001 From: geebytes Date: Mon, 17 Mar 2025 15:24:37 +0000 Subject: [PATCH 3/3] feat(dev):add init env script --- .devcontainer.json | 4 +-- .devcontainer/get_env.sh | 22 ---------------- .devcontainer/init_env.sh | 54 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 24 deletions(-) delete mode 100755 .devcontainer/get_env.sh create mode 100755 .devcontainer/init_env.sh diff --git a/.devcontainer.json b/.devcontainer.json index f01b2c963..d416d61d5 100755 --- a/.devcontainer.json +++ b/.devcontainer.json @@ -10,7 +10,7 @@ "--no-cache" ] }, - "initializeCommand": ".devcontainer/get_env.sh", + "initializeCommand": ".devcontainer/init_env.sh", "name": "dbgpt", "workspaceFolder": "/app", "workspaceMount": "source=${localWorkspaceFolder},target=/app,type=bind", @@ -41,7 +41,7 @@ "source=${localEnv:SSH_AUTH_SOCK},target=/run/host-services/ssh-auth.sock,type=bind", // mount to local models // Persist the model to avoid redundant downloads. - "source=${localWorkspaceFolder}/models/text2vec-large-chinese,target=/app/models/text2vec-large-chinese,type=bind" + "source=${localWorkspaceFolder}/models,target=/app/models,type=bind" ], "containerEnv": { "SSH_AUTH_SOCK": "/run/host-services/ssh-auth.sock" diff --git a/.devcontainer/get_env.sh b/.devcontainer/get_env.sh deleted file mode 100755 index 3ba41ae0a..000000000 --- a/.devcontainer/get_env.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail - -OS=$(uname -s) -USERNAME="$USER" -USER_UID=$(id -u "$USER") - -if [ "$OS" = "Linux" ]; then - GROUPNAME=$(id -gn "$USER") - USER_GID=$(id -g "$USER") -else - GROUPNAME="root" - USER_GID="0" -fi - - -printf "OS=%s\nUSERNAME=%s\nUSER_UID=%s\nGROUPNAME=%s\nUSER_GID=%s\n" \ - "$OS" \ - "$USERNAME" \ - "$USER_UID" \ - "$GROUPNAME" \ - "$USER_GID" > .devcontainer/.env diff --git a/.devcontainer/init_env.sh b/.devcontainer/init_env.sh new file mode 100755 index 000000000..221ee6c82 --- /dev/null +++ b/.devcontainer/init_env.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash +OS=$(uname -s) +USERNAME="$USER" +USER_UID=$(id -u "$USER") + +if [ "$OS" = "Linux" ]; then + GROUPNAME=$(id -gn "$USER") + USER_GID=$(id -g "$USER") +else + GROUPNAME="root" + USER_GID="0" +fi + +printf "OS=%s\nUSERNAME=%s\nUSER_UID=%s\nGROUPNAME=%s\nUSER_GID=%s\n" \ + "$OS" \ + "$USERNAME" \ + "$USER_UID" \ + "$GROUPNAME" \ + "$USER_GID" > .devcontainer/.env + +# sharing-git-credentials see https://code.visualstudio.com/remote/advancedcontainers/sharing-git-credentials +init_ssh_agent(){ +# Define code block to insert (with unique identifier comment) +SSH_AGENT_CODE='# SSH Agent Auto Management[ID:ssh_agent_v1] +if [ -z "$SSH_AUTH_SOCK" ]; then + RUNNING_AGENT="$(ps -ax | grep '\''ssh-agent -s'\'' | grep -v grep | wc -l | tr -d '\''[:space:]'\'')" + if [ "$RUNNING_AGENT" = "0" ]; then + ssh-agent -s &> $HOME/.ssh/ssh-agent + fi + eval $(cat $HOME/.ssh/ssh-agent) > /dev/null + ssh-add 2> /dev/null +fi +# END_SSH_AGENT_CODE' + +# Auto detect shell type +TARGET_FILE="$HOME/.bashrc" +if [[ "$SHELL" == *"zsh"* ]]; then + TARGET_FILE="$HOME/.zshrc" +fi + +# Create .ssh directory if not exists +mkdir -p "$HOME/.ssh" + +# Check for existing code block +if ! grep -q 'END_SSH_AGENT_CODE' "$TARGET_FILE"; then + echo "Adding SSH agent management code to ${TARGET_FILE}..." + echo "$SSH_AGENT_CODE" >> "$TARGET_FILE" + echo "Code added successfully. Please run source ${TARGET_FILE} to apply changes immediately" +else + echo "Existing SSH agent code detected, no need to add again" +fi +} +init_ssh_agent +mkdir -p models \ No newline at end of file