From 911e65a49f48426f1117170e450bf443e12efee3 Mon Sep 17 00:00:00 2001 From: Aminur Rahman Date: Fri, 23 Jun 2023 09:12:31 +0000 Subject: [PATCH] Run VNC startup script once --- base/Dockerfile | 48 +++++++++++++++++++++++++++-------- chunks/tool-vnc/Dockerfile | 8 ++---- chunks/tool-vnc/gp-vncsession | 6 ++--- tests/tool-vnc.yaml | 1 + 4 files changed, 44 insertions(+), 19 deletions(-) diff --git a/base/Dockerfile b/base/Dockerfile index e56e7058e..6301af437 100644 --- a/base/Dockerfile +++ b/base/Dockerfile @@ -50,24 +50,52 @@ RUN useradd -l -u 33333 -G sudo -md /home/gitpod -s /bin/bash -p gitpod gitpod \ ENV HOME=/home/gitpod WORKDIR $HOME -# custom Bash prompt -RUN { echo && echo "PS1='\[\033[01;32m\]\u\[\033[00m\] \[\033[01;34m\]\w\[\033[00m\]\$(__git_ps1 \" (%s)\") $ '" ; } >> .bashrc +# Configure Git COPY default.gitconfig /etc/gitconfig -COPY --chown=gitpod:gitpod default.gitconfig /home/gitpod/.gitconfig - -# configure git-lfs +COPY --chown=gitpod:gitpod default.gitconfig $HOME/.gitconfig RUN git lfs install --system --skip-repo ### Gitpod user (2) ### USER gitpod # use sudo so that user does not get sudo usage info on (the first) login -RUN sudo echo "Running 'sudo' for Gitpod: success" && \ - # create .bashrc.d folder and source it in the bashrc - mkdir -p /home/gitpod/.bashrc.d && \ - (echo; echo "for i in \$(ls -A \$HOME/.bashrc.d/); do source \$HOME/.bashrc.d/\$i; done"; echo) >> /home/gitpod/.bashrc && \ +RUN sudo echo "Running 'sudo' for Gitpod: success" \ + # create .bashrc.d and .runonce dirs + && mkdir -p $HOME/.bashrc.d $HOME/.runonce \ # create a completions dir for gitpod user - mkdir -p /home/gitpod/.local/share/bash-completion/completions + && mkdir -p $HOME/.local/share/bash-completion/completions + +RUN <<'EOF' +cat >> "${HOME}/.bashrc" <<'SCRIPT' + +# custom Bash prompt +PS1="\033[1;32m\u\033[0m \033[1;34m\w\033[0m$(__git_ps1 " (%s)") $ " + +# runonce startup scripts loader +# This should not modify the shell environment, hence subshell. +( + runonce_dir="$HOME/.runonce" + lock_dir="${runonce_dir}/.lock" + lock_done="${lock_dir}/done" + + if mkdir "${lock_dir}" 2>/dev/null; then { + # First terminal holds the Atomic lock and others jump into the `else` block + shopt -s nullglob + for script in "${runonce_dir}/"*; do { + # shellcheck source=/dev/null + source "${script}" + }; done + touch "${lock_done}" # Unlock + }; else { + # Other terminals awaits for unlock + until test -e "${lock_done}"; do sleep 0.3; done + }; fi +) + +# shell environment scripts loader +for i in $(ls -A $HOME/.bashrc.d/); do source $HOME/.bashrc.d/$i; done +SCRIPT +EOF # Custom PATH additions ENV PATH=$HOME/.local/bin:/usr/games:$PATH diff --git a/chunks/tool-vnc/Dockerfile b/chunks/tool-vnc/Dockerfile index e8e2cf539..390e98fbb 100644 --- a/chunks/tool-vnc/Dockerfile +++ b/chunks/tool-vnc/Dockerfile @@ -21,12 +21,8 @@ COPY novnc-index.html /opt/novnc/index.html COPY gp-vncsession /usr/bin/ # Add VNC startup script -COPY <<-"EOF" /home/gitpod/.bashrc.d/500-vnc -export DISPLAY=:0 -test -e "$GITPOD_REPO_ROOT" && gp-vncsession -EOF - -RUN chmod 0755 "$(which gp-vncsession)" +RUN printf 'export DISPLAY=:%s\n' '0' > "$HOME/.bashrc.d/1000-vnc" \ + && printf '%s\n' 'test -e "${GITPOD_REPO_ROOT:-}" && gp-vncsession' > "$HOME/.runonce/1000-vnc" # Add X11 dotfiles COPY --chown=gitpod:gitpod .xinitrc $HOME/ diff --git a/chunks/tool-vnc/gp-vncsession b/chunks/tool-vnc/gp-vncsession index fc1679417..e5e46ffb8 100755 --- a/chunks/tool-vnc/gp-vncsession +++ b/chunks/tool-vnc/gp-vncsession @@ -13,7 +13,7 @@ function log::info() { printf 'info[%s]: %s\n' "${0##*/}" "$@" } -if test ! -e /tmp/.X0-lock; then { +if test ! -d /tmp/.X11-unix; then { : "${DISPLAY:=":0"}" export DISPLAY VNC_PORT=5900 @@ -47,12 +47,12 @@ if test ! -e /tmp/.X0-lock; then { ## Create .xinit # log::info 'Creating .xinit script' # printf '%s\n' '#!/bin/sh' 'exec dbus-launch --exit-with-session xfce4-session;' > $HOME/.xinitrc; - chmod 755 "$HOME/.xinitrc" + # chmod 755 "$HOME/.xinitrc" # Start vncserver log::info "Starting tigerVNC server on port $VNC_PORT" # vncserver -kill "${DISPLAY}" - start_service "$(command -v vncserver)" -geometry "${TIGERVNC_GEOMETRY:-1920x1080}" -SecurityTypes None $DISPLAY + start_service "$(command -v vncserver)" -geometry "${TIGERVNC_GEOMETRY:-1920x1080}" -SecurityTypes None "${DISPLAY}" # Wait log::info "Waiting for the desktop to be fully loaded ..." diff --git a/tests/tool-vnc.yaml b/tests/tool-vnc.yaml index 35635937d..4e4df47d2 100644 --- a/tests/tool-vnc.yaml +++ b/tests/tool-vnc.yaml @@ -5,6 +5,7 @@ - stdout.indexOf("/bin/vncserver") != -1 - desc: "the vnc session should start" + entrypoint: [env, GITPOD_REPO_ROOT=/workspace, bash, -c] command: [gp-vncsession] assert: - status == 0