From dc8e1bf3fa79d472dde4a54ca3d938af85dddda5 Mon Sep 17 00:00:00 2001 From: Michal Czyz Date: Mon, 25 Mar 2024 12:01:02 +0100 Subject: [PATCH] Test devcontainer action Signed-off-by: Michal Czyz --- .devcontainer/prebuilt/devcontainer.json | 3 + .devcontainer/ubuntu-gui/devcontainer.json | 1 + .github/workflows/ci.yml | 47 ++++++++++++++ common.sh | 72 ++++++++++++++++++++++ desktop-update.sh | 38 ++++++++++++ update-config.sh | 59 ++++++++++++++++++ 6 files changed, 220 insertions(+) create mode 100644 .devcontainer/prebuilt/devcontainer.json create mode 100644 .github/workflows/ci.yml create mode 100644 common.sh create mode 100644 desktop-update.sh create mode 100644 update-config.sh diff --git a/.devcontainer/prebuilt/devcontainer.json b/.devcontainer/prebuilt/devcontainer.json new file mode 100644 index 0000000..d8d8543 --- /dev/null +++ b/.devcontainer/prebuilt/devcontainer.json @@ -0,0 +1,3 @@ +{ + "image": "ghcr.io/openroad-codespace/test:latest" +} diff --git a/.devcontainer/ubuntu-gui/devcontainer.json b/.devcontainer/ubuntu-gui/devcontainer.json index 919cbce..fc98873 100644 --- a/.devcontainer/ubuntu-gui/devcontainer.json +++ b/.devcontainer/ubuntu-gui/devcontainer.json @@ -9,4 +9,5 @@ "label": "desktop" } } + // "onCreateCommand": "bash /workspaces/openroad-codespace/update-config.sh" } diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..9a73e36 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,47 @@ +name: Prebuild docker image +on: + push: + pull_request: + branches: + - main + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} + +jobs: + build: + name: BUILD + runs-on: ubuntu-22.04 + timeout-minutes: 600 + steps: + - uses: actions/checkout@v4 + with: + ref: "mczyz/dev" + + # - name: Pre-build dev container image + # uses: devcontainers/ci@v0.3 + # with: + # imageName: ghcr.io/openroad-codespace/test + # cacheFrom: ghcr.io/openroad-codespace/test + # push: always + # configFile: ./.devcontainer/ubuntu-gui/devcontainer.json + + - name: Devcontainer build + run: | + npm install -g @devcontainers/cli + devcontainer build --config .devcontainer/ubuntu-gui/devcontainer.json --workspace-folder . --push false --image-name test:latest + docker image tag test:latest ghcr.io/antmicro/openroad-codespace:test + + - name: Login to GitHub Container Registry (GHCR) + if: github.event_name != 'pull_request' && github.repository == 'antmicro/openroad-codespace' + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: gha + password: ${{ github.token }} + + - name: Push container image to GitHub Container Registry (GHCR) + if: github.event_name != 'pull_request' && github.repository == 'antmicro/openroad-codespace' + run: docker push ghcr.io/antmicro/openroad-codespace:test diff --git a/common.sh b/common.sh new file mode 100644 index 0000000..c2ddb23 --- /dev/null +++ b/common.sh @@ -0,0 +1,72 @@ +# Base file is from devcontainers/features/desktop-lite/install.sh +# Original file is licensed as MIT +# Applied modifications to the script 2024 + +user_name="root" +group_name="root" +LOG=/tmp/container-init.log + +export DBUS_SESSION_BUS_ADDRESS="autolaunch:" +export DISPLAY=":1" +export VNC_RESOLUTION="1920x1080x32" +export LANG="en_US.UTF-8" +export LANGUAGE="en_US.UTF-8" + +# Try to kill pid based on name +kill_pid() +{ +PIDS_TO_KILL=$(ps axjf | grep $1 | awk '{print $2}') +for PID in ${PIDS_TO_KILL[@]}; do + echo "Attempting to kill $PID" + kill -9 $PID +done +} + +# Execute the command it not already running +startInBackgroundIfNotRunning() +{ + log "Starting $1." + echo -e "\n** $(date) **" | sudoIf tee -a /tmp/$1.log > /dev/null + if ! pgrep -x $1 > /dev/null; then + keepRunningInBackground "$@" + while ! pgrep -x $1 > /dev/null; do + sleep 1 + done + log "$1 started." + else + echo "$1 is already running." | sudoIf tee -a /tmp/$1.log > /dev/null + log "$1 is already running." + fi +} + +# Keep command running in background +keepRunningInBackground() +{ + ($2 bash -c "while :; do echo [\$(date)] Process started.; $3; echo [\$(date)] Process exited!; sleep 5; done 2>&1" | sudoIf tee -a /tmp/$1.log > /dev/null & echo "$!" | sudoIf tee /tmp/$1.pid > /dev/null) +} + +# Use sudo to run as root when required +sudoIf() +{ + if [ "$(id -u)" -ne 0 ]; then + sudo "$@" + else + "$@" + fi +} + +# Use sudo to run as non-root user if not already running +sudoUserIf() +{ + if [ "$(id -u)" -eq 0 ] && [ "${user_name}" != "root" ]; then + sudo -u ${user_name} "$@" + else + "$@" + fi +} + +# Log messages +log() +{ + echo -e "[$(date)] $@" | sudoIf tee -a $LOG > /dev/null +} diff --git a/desktop-update.sh b/desktop-update.sh new file mode 100644 index 0000000..3fa05ca --- /dev/null +++ b/desktop-update.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +. ./common.sh + +log "** SCRIPT START **" + +# Start dbus. +log 'Running "/etc/init.d/dbus start".' +if [ -f "/var/run/dbus/pid" ] && ! pgrep -x dbus-daemon > /dev/null; then + sudoIf rm -f /var/run/dbus/pid +fi +sudoIf /etc/init.d/dbus start 2>&1 | sudoIf tee -a /tmp/dbus-daemon-system.log > /dev/null +while ! pgrep -x dbus-daemon > /dev/null; do + sleep 1 +done + +# Startup tigervnc server and fluxbox +sudoIf rm -rf /tmp/.X11-unix /tmp/.X*-lock +mkdir -p /tmp/.X11-unix +sudoIf chmod 1777 /tmp/.X11-unix +sudoIf chown root:${group_name} /tmp/.X11-unix +if [ "$(echo "${VNC_RESOLUTION}" | tr -cd 'x' | wc -c)" = "1" ]; then VNC_RESOLUTION=${VNC_RESOLUTION}x16; fi +screen_geometry="${VNC_RESOLUTION%*x*}" +screen_depth="${VNC_RESOLUTION##*x}" +startInBackgroundIfNotRunning "Xtigervnc" sudoUserIf "tigervncserver ${DISPLAY} -geometry ${screen_geometry} -depth ${screen_depth} -rfbport 5901 -dpi ${VNC_DPI:-96} -localhost -desktop fluxbox -fg" -passwd /usr/local/etc/vscode-dev-containers/vnc-passwd + +# Spin up noVNC if installed and not running. +if [ -d "/usr/local/novnc" ] && [ "$(ps -ef | grep /usr/local/novnc/noVNC*/utils/launch.sh | grep -v grep)" = "" ]; then + keepRunningInBackground "noVNC" sudoIf "/usr/local/novnc/noVNC*/utils/launch.sh --listen 6080 --vnc localhost:5901" + log "noVNC started." +else + log "noVNC is already running or not installed." +fi + +# Run whatever was passed in +log "Executing \"$@\"." +exec "$@" +log "** SCRIPT EXIT **" diff --git a/update-config.sh b/update-config.sh new file mode 100644 index 0000000..4909149 --- /dev/null +++ b/update-config.sh @@ -0,0 +1,59 @@ +#!/bin/bash + +. ./common.sh + +# Attempt to change default shell +chsh -s /bin/bash + +# Install Konsole + +apt-get update +sudoIf apt-get -y install konsole + +# Update fluxbox config + +fluxbox_apps="$(cat \ +<< 'EOF' + [transient] (role=GtkFileChooserDialog) + [Dimensions] {100% 100%} + [Position] (CENTER) {0 0} +[end] +EOF +)" + +echo "${fluxbox_apps}" > ~/.fluxbox/apps + +fluxbox_menu="$(cat \ +<< 'EOF' +[begin] ( Application Menu ) + [exec] (File Manager) { nautilus ~ } <> + [exec] (Text Editor) { mousepad } <> + [exec] (Terminal) { konsole -e /bin/bash } <> + [exec] (Web Browser) { x-www-browser --disable-dev-shm-usage } <> + [submenu] (System) {} + [exec] (Set Resolution) { tilix -t "Set Resolution" -e bash /usr/local/bin/set-resolution } <> + [exec] (Edit Application Menu) { mousepad ~/.fluxbox/menu } <> + [exec] (Passwords and Keys) { seahorse } <> + [exec] (Top Processes) { tilix -t "Top" -e htop } <> + [exec] (Disk Utilization) { tilix -t "Disk Utilization" -e ncdu / } <> + [exec] (Editres) {editres} <> + [exec] (Xfontsel) {xfontsel} <> + [exec] (Xkill) {xkill} <> + [exec] (Xrefresh) {xrefresh} <> + [end] + [config] (Configuration) + [workspaces] (Workspaces) +[end] +EOF +)" + +echo "${fluxbox_menu}" > ~/.fluxbox/menu + +kill_pid tigervncserver +kill_pid novnc + +ps axjf | grep tigervncserver +ps axjf | grep novnc + +bash desktop-update.sh +# rm -f /usr/local/etc/vscode-dev-containers/vnc-passwd