From d6e354e15a6726866afbec04eb93e5ab7bd64358 Mon Sep 17 00:00:00 2001 From: spwoodcock Date: Fri, 10 Nov 2023 08:48:56 +0000 Subject: [PATCH] fix(install-script): add DOCKER_HOST to top of bashrc --- scripts/setup/docker.sh | 24 +++++++++++++++++++----- src/frontend/public/install.sh | 33 +++++++++++++++++++++++++++++---- 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/scripts/setup/docker.sh b/scripts/setup/docker.sh index c4da462f07..d95afb7cac 100644 --- a/scripts/setup/docker.sh +++ b/scripts/setup/docker.sh @@ -167,16 +167,22 @@ EOF } add_vars_to_bashrc() { + # DOCKER_HOST must be added to the top of bashrc, as running non-interactively + # Most distros exit .bashrc execution is non-interactive + heading_echo "Adding rootless DOCKER_HOST to bashrc" user_id=$(id -u) - docker_host_var="export DOCKER_HOST=unix:///run/user/$user_id//docker.sock" + docker_host_var="export DOCKER_HOST=unix:///run/user/$user_id/docker.sock" dc_alias_cmd="alias dc='docker compose'" - # Check if DOCKER_HOST is already defined + # Create a temporary file + tmpfile=$(mktemp) + + # Check if DOCKER_HOST is already defined in user's .bashrc if ! grep -q "$docker_host_var" ~/.bashrc; then echo "Adding rootless DOCKER_HOST var to ~/.bashrc." - echo "$docker_host_var" >> ~/.bashrc + echo "$docker_host_var" >> "$tmpfile" fi echo "Done" @@ -184,12 +190,20 @@ add_vars_to_bashrc() { heading_echo "Adding dc='docker compose' alias" - # Check if the alias already exists + # Check if the alias already exists in user's .bashrc if ! grep -q "$dc_alias_cmd" ~/.bashrc; then echo "Adding 'dc' alias to ~/.bashrc." - echo "$dc_alias_cmd" >> ~/.bashrc + echo "$dc_alias_cmd" >> "$tmpfile" + fi + + # Append the rest of the original .bashrc to the temporary file + if [ -e ~/.bashrc ]; then + grep -v -e "$docker_host_var" -e "$dc_alias_cmd" ~/.bashrc >> "$tmpfile" fi + # Replace the original .bashrc with the modified file + mv "$tmpfile" ~/.bashrc + echo "Done" } diff --git a/src/frontend/public/install.sh b/src/frontend/public/install.sh index b049d7ba70..29fd22abfc 100644 --- a/src/frontend/public/install.sh +++ b/src/frontend/public/install.sh @@ -299,24 +299,31 @@ echo "Done" add_vars_to_bashrc() { + # DOCKER_HOST must be added to the top of bashrc, as running non-interactively + # Most distros exit .bashrc execution is non-interactive + heading_echo "Adding DOCKER_HOST and 'dc' alias to bashrc" user_id=$(id -u) docker_host_var="export DOCKER_HOST=unix:///run/user/$user_id/docker.sock" dc_alias_cmd="alias dc='docker compose'" + # Create temporary files for root and user bashrc + tmpfile_root=$(mktemp) + tmpfile_user=$(mktemp) + if [ "$RUN_AS_ROOT" = true ]; then # Check if DOCKER_HOST is already defined in /root/.bashrc if ! sudo grep -q "$docker_host_var" /root/.bashrc; then echo "Adding DOCKER_HOST var to /root/.bashrc." - echo "$docker_host_var" | sudo tee -a /root/.bashrc > /dev/null + echo "$docker_host_var" | sudo tee -a "$tmpfile_root" > /dev/null echo fi # Check if the 'dc' alias already exists in /root/.bashrc if ! sudo grep -q "$dc_alias_cmd" /root/.bashrc; then echo "Adding 'dc' alias to /root/.bashrc." - echo "$dc_alias_cmd" | sudo tee -a /root/.bashrc > /dev/null + echo "$dc_alias_cmd" | sudo tee -a "$tmpfile_root" > /dev/null echo fi fi @@ -324,17 +331,35 @@ add_vars_to_bashrc() { # Check if DOCKER_HOST is already defined in ~/.bashrc if ! grep -q "$docker_host_var" ~/.bashrc; then echo "Adding DOCKER_HOST var to ~/.bashrc." - echo "$docker_host_var" >> ~/.bashrc + echo "$docker_host_var" | tee -a "$tmpfile_user" > /dev/null echo fi # Check if the 'dc' alias already exists in ~/.bashrc if ! grep -q "$dc_alias_cmd" ~/.bashrc; then echo "Adding 'dc' alias to ~/.bashrc." - echo "$dc_alias_cmd" >> ~/.bashrc + echo "$dc_alias_cmd" | tee -a "$tmpfile_user" > /dev/null echo fi + # Append the rest of the original .bashrc to the temporary file + if [ -e ~/.bashrc ]; then + grep -v -e "$docker_host_var" -e "$dc_alias_cmd" ~/.bashrc >> "$tmpfile_user" + fi + # Replace the original .bashrc with the modified file + mv "$tmpfile_user" ~/.bashrc + + # If RUN_AS_ROOT is true, replace /root/.bashrc with the modified file + if [ "$RUN_AS_ROOT" = true ]; then + # Append the rest of the original /root/.bashrc to the temporary file + if [ -e /root/.bashrc ]; then + grep -v -e "$docker_host_var" -e "$dc_alias_cmd" /root/.bashrc >> "$tmpfile_root" + fi + + # Replace the original /root/.bashrc with the modified file + sudo mv "$tmpfile_root" /root/.bashrc + fi + echo "Setting DOCKER_HOST for the current session." export DOCKER_HOST=unix:///run/user/$(id -u)/docker.sock