Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(proposal): runonce scripts, eqivalent to .bashrc.d but runs only once without any race conditions between several terminals #1104

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions base/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 2 additions & 6 deletions chunks/tool-vnc/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
6 changes: 3 additions & 3 deletions chunks/tool-vnc/gp-vncsession
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ..."
Expand Down
1 change: 1 addition & 0 deletions tests/tool-vnc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading