From 109515a20812b5f1758fdfbf4b1936eafc129465 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20R=C4=85czy?= Date: Fri, 15 Sep 2023 14:00:24 -0700 Subject: [PATCH] devcontainer: expose host config, fix mac screen issues (#29932) * export host config via .host/.env file. fix mac display issues * append instead of replace for bashrc * Log when mac display override happens * Update xauthority path in json old-commit-hash: dd26a1faadb4cb6a468b77d5be7803cf1dad4beb --- .devcontainer/.gitignore | 4 +++- .devcontainer/container_post_create.sh | 14 ++++++++++++++ .devcontainer/container_post_start.sh | 7 +++++++ .devcontainer/devcontainer.json | 8 ++++---- .devcontainer/host_setup.sh | 24 ++++++++++++++++++++++++ .devcontainer/setup_host.sh | 16 ---------------- 6 files changed, 52 insertions(+), 21 deletions(-) create mode 100755 .devcontainer/container_post_create.sh create mode 100755 .devcontainer/container_post_start.sh create mode 100755 .devcontainer/host_setup.sh delete mode 100755 .devcontainer/setup_host.sh diff --git a/.devcontainer/.gitignore b/.devcontainer/.gitignore index 1f8ec242b8..5682fe1190 100644 --- a/.devcontainer/.gitignore +++ b/.devcontainer/.gitignore @@ -1 +1,3 @@ -.Xauthority \ No newline at end of file +.Xauthority +.env +.host/ \ No newline at end of file diff --git a/.devcontainer/container_post_create.sh b/.devcontainer/container_post_create.sh new file mode 100755 index 0000000000..52a4b5f858 --- /dev/null +++ b/.devcontainer/container_post_create.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +source .devcontainer/.host/.env + +# override display flag for mac +if [[ $HOST_OS == darwin ]]; then + echo "Setting up DISPLAY override for macOS..." + cat <> /root/.bashrc +if [ -n "\$DISPLAY" ]; then + DISPLAY_NUM=\$(echo "\$DISPLAY" | awk -F: '{print \$NF}') + export DISPLAY=host.docker.internal:\$DISPLAY_NUM +fi +EOF +fi diff --git a/.devcontainer/container_post_start.sh b/.devcontainer/container_post_start.sh new file mode 100755 index 0000000000..4404f6a9a9 --- /dev/null +++ b/.devcontainer/container_post_start.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +# setup safe directories for submodules +SUBMODULE_DIRS=$(git config --file .gitmodules --get-regexp path | awk '{ print $2 }') +for DIR in $SUBMODULE_DIRS; do + git config --global --add safe.directory "$PWD/$DIR" +done diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 2dfe46b4f0..7224f251a7 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -3,9 +3,9 @@ "build": { "dockerfile": "Dockerfile" }, - "postCreateCommand": "bash -c 'if [[ $DISPLAY == *xquartz* ]]; then echo \"export DISPLAY=host.docker.internal:0\" >> /root/.bashrc; fi'", - "postStartCommand": "git config --file .gitmodules --get-regexp path | awk '{ print $2 }' | xargs -I{} git config --global --add safe.directory \"$PWD/{}\"", - "initializeCommand": ".devcontainer/setup_host.sh", + "postCreateCommand": ".devcontainer/container_post_create.sh", + "postStartCommand": ".devcontainer/container_post_start.sh", + "initializeCommand": ".devcontainer/host_setup.sh", "privileged": true, "containerEnv": { "DISPLAY": "${localEnv:DISPLAY}", @@ -14,7 +14,7 @@ }, "runArgs": [ "--volume=/tmp/.X11-unix:/tmp/.X11-unix", - "--volume=${localWorkspaceFolder}/.devcontainer/.Xauthority:/root/.Xauthority", + "--volume=${localWorkspaceFolder}/.devcontainer/.host/.Xauthority:/root/.Xauthority", "--volume=${localEnv:HOME}/.comma:/root/.comma", "--volume=/tmp/comma_download_cache:/tmp/comma_download_cache", "--volume=/tmp/devcontainer_scons_cache:/tmp/scons_cache", diff --git a/.devcontainer/host_setup.sh b/.devcontainer/host_setup.sh new file mode 100755 index 0000000000..597d0a1112 --- /dev/null +++ b/.devcontainer/host_setup.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +# setup .host dir +mkdir -p .devcontainer/.host + +# setup links to Xauthority +XAUTHORITY_LINK=".devcontainer/.host/.Xauthority" +rm -f $XAUTHORITY_LINK +if [[ -z $XAUTHORITY ]]; then + echo "XAUTHORITY not set. Fallback to ~/.Xauthority ..." + if ! [[ -f $HOME/.Xauthority ]]; then + echo "~/.XAuthority file does not exist. GUI tools may not work properly." + touch $XAUTHORITY_LINK # dummy file to satisfy container volume mount + else + ln -sf $HOME/.Xauthority $XAUTHORITY_LINK + fi +else + ln -sf $XAUTHORITY $XAUTHORITY_LINK +fi + +# setup host env file +HOST_INFO_FILE=".devcontainer/.host/.env" +SYSTEM=$(uname -s | tr '[:upper:]' '[:lower:]') +echo "HOST_OS=\"$SYSTEM\"" > $HOST_INFO_FILE diff --git a/.devcontainer/setup_host.sh b/.devcontainer/setup_host.sh deleted file mode 100755 index 5c3c5e900c..0000000000 --- a/.devcontainer/setup_host.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env bash - -# setup links to Xauthority -XAUTHORITY_LINK=".devcontainer/.Xauthority" -rm -f $XAUTHORITY_LINK -if [[ -z $XAUTHORITY ]]; then - echo "XAUTHORITY not set. Fallback to ~/.Xauthority ..." - if ! [[ -f $HOME/.Xauthority ]]; then - echo "~/.XAuthority file does not exist. GUI tools may not work properly." - touch $XAUTHORITY_LINK # dummy file to satisfy container volume mount - else - ln -sf $HOME/.Xauthority $XAUTHORITY_LINK - fi -else - ln -sf $XAUTHORITY $XAUTHORITY_LINK -fi