feat(devcontainer): multi node

This commit is contained in:
Jacky 2025-02-03 21:00:37 +08:00
parent c85a570396
commit b090564a34
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
14 changed files with 150 additions and 49 deletions

View file

@ -13,15 +13,7 @@ RUN apt-get update && \
\
# Update package information and install Nginx
apt-get update && \
apt-get install -y --no-install-recommends nginx && \
\
# Install the latest Node.js via NodeSource setup script
curl -fsSL https://deb.nodesource.com/setup_current.x | bash - && \
apt-get update && \
apt-get install -y nodejs && \
\
# Install pnpm globally using npm
npm install -g pnpm && \
apt-get install -y --no-install-recommends nginx inotify-tools file && \
\
# Automatically retrieve the latest stable Go version and install it,
# download the appropriate binary based on system architecture (amd64 or arm64)

View file

@ -10,7 +10,8 @@
"features": {
"ghcr.io/devcontainers/features/common-utils:2": {
"installOhMyZsh": true
}
},
"ghcr.io/devcontainers/features/node:1": {}
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
@ -27,7 +28,8 @@
"antfu.unocss",
"github.copilot",
"golang.go",
"vue.volar"
"vue.volar",
"ms-azuretools.vscode-docker"
]
}
},

View file

@ -1,15 +1,31 @@
services:
nginx-ui:
build: .
image: nginx-ui-dev
container_name: nginx-ui
volumes:
- ../..:/workspaces:cached
- ./go-path:/root/go
- ./nginx:/etc/nginx
- ./data/nginx:/etc/nginx
command: sleep infinity
environment:
- NGINX_UI_CERT_CA_DIR=https://pebble:14000/dir
networks:
nginxui:
nginx-ui-2:
image: nginx-ui-dev
container_name: nginx-ui-2
volumes:
- ../..:/workspaces:cached
- ./data/nginx-ui-2/nginx:/etc/nginx
- ./data/nginx-ui-2/nginx-ui:/etc/nginx-ui
working_dir: /workspaces/nginx-ui
command: ./.devcontainer/node-supervisor.sh
depends_on:
- nginx-ui
networks:
nginxui:
pebble:
image: ghcr.io/letsencrypt/pebble:latest
volumes:

6
.devcontainer/init-nginx.sh Executable file
View file

@ -0,0 +1,6 @@
# init nginx config dir
if [ "$(ls -A /etc/nginx)" = "" ]; then
echo "Initialing Nginx config dir"
cp -rp /etc/nginx.orig/* /etc/nginx/
echo "Initialed Nginx config dir"
fi

View file

@ -0,0 +1,87 @@
#!/bin/bash
# Configurable variables
SOURCE_FILE=/workspaces/nginx-ui/tmp/main
TARGET_PATH=/usr/local/bin/nginx-ui
CONFIG_FILE=/etc/nginx-ui/app.ini
# init nginx
./.devcontainer/init-nginx.sh
LOG_PREFIX="[Supervisor]"
# Debug initial state
echo "$LOG_PREFIX Starting supervisor with:"
echo "$LOG_PREFIX SOURCE_FILE: $SOURCE_FILE"
echo "$LOG_PREFIX TARGET_PATH: $TARGET_PATH"
echo "$LOG_PREFIX CONFIG_FILE: $CONFIG_FILE"
# Wait for initial file creation
while [[ ! -f "$SOURCE_FILE" ]]; do
echo "$LOG_PREFIX Waiting for $SOURCE_FILE to be created..."
sleep 1
done
# Initial copy and start
echo "$LOG_PREFIX Initial file detected, starting service..."
cp -fv "$SOURCE_FILE" "$TARGET_PATH"
chmod +x "$TARGET_PATH"
pkill -x nginx-ui || echo "$LOG_PREFIX No existing process to kill"
nohup "$TARGET_PATH" -config "$CONFIG_FILE" > /proc/1/fd/1 2>&1 &
# Use proper field separation for inotify output
inotifywait -m -e close_write,moved_to,create,delete \
--format "%T|%w%f|%e" \
--timefmt "%F-%H:%M:%S" \
"$(dirname "$SOURCE_FILE")" |
while IFS='|' read -r TIME FILE EVENT; do
echo "$LOG_PREFIX [${TIME}] Event: ${EVENT} - ${FILE}"
# Handle atomic save operations
if [[ "$FILE" =~ .*-tmp-umask$ ]] || [[ "$EVENT" == "DELETE" ]]; then
echo "$LOG_PREFIX Detected build intermediate file, checking main..."
sleep 0.3 # Allow atomic replace completion
if [[ -f "$SOURCE_FILE" ]]; then
echo "$LOG_PREFIX Valid main file detected after build"
FILE="$SOURCE_FILE"
else
echo "$LOG_PREFIX Main file missing after build operation"
continue
fi
fi
if [[ "$FILE" == "$SOURCE_FILE" ]]; then
# Stability checks
echo "$LOG_PREFIX File metadata:"
ls -l "$FILE"
file "$FILE"
# Wait for file stability with retries
retries=5
while ((retries-- > 0)); do
if file "$FILE" | grep -q "executable"; then
break
fi
echo "$LOG_PREFIX Waiting for valid executable (${retries} retries left)..."
sleep 1
done
if ((retries <= 0)); then
echo "$LOG_PREFIX ERROR: File validation failed after 5 retries"
continue
fi
# Copy and restart service
echo "$LOG_PREFIX Updating service..."
cp -fv "$FILE" "$TARGET_PATH"
chmod +x "$TARGET_PATH"
echo "$LOG_PREFIX Killing existing process..."
pkill -x nginx-ui || echo "$LOG_PREFIX No process to kill"
echo "$LOG_PREFIX Starting new process..."
nohup "$TARGET_PATH" -config "$CONFIG_FILE" > /proc/1/fd/1 2>&1 &
echo "$LOG_PREFIX Restart complete. New PID: $(pgrep nginx-ui)"
fi
done

View file

@ -9,11 +9,7 @@ if ! grep -q "zsh-autosuggestions" ~/.zshrc; then
fi
# init nginx config dir
if [ "$(ls -A /etc/nginx)" = "" ]; then
echo "Initialing Nginx config dir"
cp -rp /etc/nginx.orig/* /etc/nginx/
echo "Initialed Nginx config dir"
fi
./.devcontainer/init-nginx.sh
# install app dependencies
echo "Installing app dependencies"