Skip to content

Commit

Permalink
Use .runonce for rust, dotnet, mysql, postgres, vnc, yugabyte to avoi…
Browse files Browse the repository at this point in the history
…d race condition
  • Loading branch information
axonasif committed Apr 8, 2023
1 parent d9ec66a commit 7907f57
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 45 deletions.
7 changes: 3 additions & 4 deletions chunks/lang-rust/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ ENV PATH=$HOME/.cargo/bin:$PATH
RUN curl -fsSL https://sh.rustup.rs | sh -s -- -y --profile minimal --no-modify-path --default-toolchain stable \
-c rls rust-analysis rust-src rustfmt clippy \
&& for cmp in rustup cargo; do rustup completions bash "$cmp" > "$HOME/.local/share/bash-completion/completions/$cmp"; done \
&& printf '%s\n' 'export CARGO_HOME=/workspace/.cargo' \
'mkdir -m 0755 -p "$CARGO_HOME/bin" 2>/dev/null' \
'export PATH=$CARGO_HOME/bin:$PATH' \
'test ! -e "$CARGO_HOME/bin/rustup" && mv "$(command -v rustup)" "$CARGO_HOME/bin"' > $HOME/.bashrc.d/80-rust \
&& cargo install cargo-watch cargo-edit cargo-workspaces \
&& rm -rf "$HOME/.cargo/registry" # This registry cache is now useless as we change the CARGO_HOME path to `/workspace`

COPY --chown=gitpod:gitpod rust_bashrc.sh $HOME/.bashrc.d/80-rust
COPY --chown=gitpod:gitpod rust_runonce.sh $HOME/.runonce/80-rust
6 changes: 6 additions & 0 deletions chunks/lang-rust/rust_bashrc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

if test -e "${GITPOD_REPO_ROOT:-}"; then {
export CARGO_HOME=/workspace/.cargo
export PATH=$CARGO_HOME/bin:$PATH
}; fi
10 changes: 10 additions & 0 deletions chunks/lang-rust/rust_runonce.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

if test -e "${GITPOD_REPO_ROOT:-}"; then {
CARGO_HOME="/workspace/.cargo"
mkdir -p "$CARGO_HOME/bin" 2>/dev/null

if test ! -e "$CARGO_HOME/bin/rustup" && rustup="$(command -v rustup)"; then {
mv "${rustup}" "$CARGO_HOME/bin"
}; fi
}; fi
9 changes: 1 addition & 8 deletions chunks/tool-dotnet/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,4 @@ ENV PATH=/home/gitpod/dotnet:$PATH

# TODO(toru): Remove this hack when the kernel bug is resolved.
# ref. https://github.com/gitpod-io/gitpod/issues/8901
RUN bash \
&& { echo 'if [ ! -z $GITPOD_REPO_ROOT ]; then'; \
echo '\tCONTAINER_DIR=$(awk '\''{ print $6 }'\'' /proc/self/maps | grep ^\/run\/containerd | head -n 1 | cut -d '\''/'\'' -f 1-6)'; \
echo '\tif [ ! -z $CONTAINER_DIR ]; then'; \
echo '\t\t[[ ! -d $CONTAINER_DIR ]] && sudo mkdir -p $CONTAINER_DIR && sudo ln -s / $CONTAINER_DIR/rootfs'; \
echo '\tfi'; \
echo 'fi'; } >> /home/gitpod/.bashrc.d/110-dotnet
RUN chmod +x /home/gitpod/.bashrc.d/110-dotnet
COPY --chown=gitpod:gitpod dotnet_runonce.sh $HOME/.runonce/110-dotnet
10 changes: 10 additions & 0 deletions chunks/tool-dotnet/dotnet_runonce.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

if [ -n "${GITPOD_REPO_ROOT:-}" ]; then
CONTAINER_DIR=$(awk '{ print $6 }' /proc/self/maps | grep '^/run/containerd' | head -n 1 | cut -d '/' -f 1-6)
if [ -n "${CONTAINER_DIR}" ] && [ ! -d "${CONTAINER_DIR}" ]; then
sudo sh <<-CMD
mkdir -p "${CONTAINER_DIR}" && ln -s / "${CONTAINER_DIR}/rootfs"
CMD
fi
fi
2 changes: 1 addition & 1 deletion chunks/tool-mysql/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ COPY mysql-bashrc-launch.sh /etc/mysql/mysql-bashrc-launch.sh

USER gitpod

RUN echo "/etc/mysql/mysql-bashrc-launch.sh" >> /home/gitpod/.bashrc.d/100-mysql-launch
RUN echo "/etc/mysql/mysql-bashrc-launch.sh" >> $HOME/.runonce/100-mysql-launch
2 changes: 1 addition & 1 deletion chunks/tool-postgresql/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ ENV PATH="$HOME/.pg_ctl/bin:$PATH"
ENV DATABASE_URL="postgresql://gitpod@localhost"
ENV PGHOSTADDR="127.0.0.1"
ENV PGDATABASE="postgres"
COPY --chown=gitpod:gitpod postgresql-hook.bash $HOME/.bashrc.d/200-postgresql-launch
COPY --chown=gitpod:gitpod postgresql-hook.bash $HOME/.runonce/200-postgresql-launch

USER gitpod
41 changes: 20 additions & 21 deletions chunks/tool-postgresql/postgresql-hook.bash
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
#!/usr/bin/env bash
# Auto-start PostgreSQL server
(
if mkdir /tmp/.pgsql_lock 2>/dev/null; then {
target="${PGWORKSPACE}"
source="${target//\/workspace/$HOME}"
target="${PGWORKSPACE}"
source="${target//\/workspace/$HOME}"

if test -e "$source"; then {
if test -e "$source"; then {

if test ! -e "$target"; then {
mv "$source" "$target"
}; fi
if test ! -e "$target"; then {
mv "$source" "$target"
}; fi

if ! [[ "$(pg_ctl status)" =~ PID ]]; then {
printf 'INFO: %s\n' "Executing command: pg_start"
pg_start
trap "pg_stop" TERM EXIT
exec {sfd}<> <(:)
printf 'INFO: %s\n' \
"Please create another terminal" \
"this one is monitoring postgres server for gracefully shutting down when needed"
until read -r -t 3600 -u $sfd; do continue; done
}; fi
if ! [[ "$(pg_ctl status)" =~ PID ]]; then {
printf 'INFO: %s\n' "Executing command: pg_start"
pg_start
(
trap "pg_stop" TERM EXIT
exec {sfd}<> <(:)
# printf 'INFO: %s\n' \
# "Please create another terminal" \
# "this one is monitoring postgres server for gracefully shutting down when needed"
until read -r -t 3600 -u $sfd; do continue; done
) &
disown
}; fi

}; fi
}; fi &
)
}; fi
5 changes: 2 additions & 3 deletions chunks/tool-vnc/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ COPY novnc-index.html /opt/novnc/index.html

# Add VNC startup script
COPY gp-vncsession /usr/bin/
RUN chmod 0755 "$(which gp-vncsession)" \
&& printf '%s\n' 'export DISPLAY=:0' \
'test -e "$GITPOD_REPO_ROOT" && gp-vncsession' >> "$HOME/.bashrc"
RUN printf 'export DISPLAY=:%s\n' '0' > "$HOME/.bashrc.d/1000-vnc" \
&& printf '%s\n' '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
7 changes: 3 additions & 4 deletions chunks/tool-yugabytedb/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ ENV YSQL_API_PORT=13000
ENV YCQL_API_PORT=12000

# re-initialization is automatically handled
RUN echo "\n# yugabytedb start command" >> /home/gitpod/.bashrc.d/100-yugabyedb-launch
RUN echo "[[ -f \${GITPOD_REPO_ROOT}/.nopreload ]] || yugabyted start --base_dir=$STORE --listen=$HOST > /dev/null" >> /home/gitpod/.bashrc.d/100-yugabyedb-launch

RUN chmod +x /home/gitpod/.bashrc.d/100-yugabyedb-launch
RUN printf '%s\n' \
"\n# yugabytedb start command" \
"[[ -f \${GITPOD_REPO_ROOT}/.nopreload ]] || yugabyted start --base_dir=$STORE --listen=$HOST > /dev/null" > $HOME/.runonce/100-yugabyedb-launch

EXPOSE ${YSQL_PORT} ${YCQL_PORT} ${WEB_PORT} ${TSERVER_WEB_PORT} ${YSQL_API_PORT} ${YCQL_API_PORT}

0 comments on commit 7907f57

Please sign in to comment.