Minimize differences between devcontainer and production

This commit is contained in:
Adam Outler
2025-10-06 23:31:20 +00:00
parent 290b6c6f3b
commit 558ab44d3f
28 changed files with 477 additions and 730 deletions

View File

@@ -10,13 +10,15 @@
# but remove the COPY . ${INSTALL_DIR}/ command from it. This avoids
# overwriting /app (which uses symlinks to the workspace) and preserves
# debugging capabilities inside the devcontainer.
echo "Generating .devcontainer/Dockerfile"
SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
DEVCONTAINER_DIR="${SCRIPT_DIR%/scripts}"
ROOT_DIR="${DEVCONTAINER_DIR%/.devcontainer}"
OUT_FILE="${DEVCONTAINER_DIR}/Dockerfile"
echo "Adding base Dockerfile from $ROOT_DIR..."
echo "# DO NOT MODIFY THIS FILE DIRECTLY. IT IS AUTO-GENERATED BY .devcontainer/scripts/generate-dockerfile.sh" > "$OUT_FILE"
echo "" >> "$OUT_FILE"
echo "# ---/Dockerfile---" >> "$OUT_FILE"
@@ -27,6 +29,36 @@ echo "" >> "$OUT_FILE"
echo "# ---/resources/devcontainer-Dockerfile---" >> "$OUT_FILE"
echo "" >> "$OUT_FILE"
echo "Adding devcontainer-Dockerfile from $DEVCONTAINER_DIR/resources..."
cat "${DEVCONTAINER_DIR}/resources/devcontainer-Dockerfile" >> "$OUT_FILE"
echo "Generated $OUT_FILE using root dir $ROOT_DIR" >&2
# Generate devcontainer nginx config from production template
echo "Generating devcontainer nginx config"
NGINX_TEMPLATE="${ROOT_DIR}/install/production-filesystem/services/config/nginx/netalertx.conf.template"
NGINX_OUT="${DEVCONTAINER_DIR}/resources/devcontainer-overlay/services/config/nginx/netalertx.conf.template"
# Create output directory if it doesn't exist
mkdir -p "$(dirname "$NGINX_OUT")"
# Start with header comment
cat > "$NGINX_OUT" << 'EOF'
# DO NOT MODIFY THIS FILE DIRECTLY. IT IS AUTO-GENERATED BY .devcontainer/scripts/generate-configs.sh
# Generated from: install/production-filesystem/services/config/nginx/netalertx.conf.template
EOF
# Process the template: replace listen directive and inject Xdebug params
sed 's/${LISTEN_ADDR}:${PORT}/0.0.0.0:20211/g' "$NGINX_TEMPLATE" | \
awk '
/fastcgi_param SCRIPT_NAME \$fastcgi_script_name;/ {
print $0
print ""
print " fastcgi_param PHP_VALUE \"xdebug.remote_enable=1\";"
next
}
{ print }
' >> "$NGINX_OUT"
echo "Generated $NGINX_OUT from $NGINX_TEMPLATE" >&2

View File

@@ -1,26 +0,0 @@
#!/bin/sh
# Start (or restart) the NetAlertX Python backend under debugpy in background.
# This script is invoked by the VS Code task "Restart GraphQL".
# It exists to avoid complex inline command chains that were being mangled by the task runner.
set -e
LOG_DIR=/app/log
APP_DIR=/app/server
PY=python3
PORT_DEBUG=5678
# Kill any prior debug/run instances
sudo killall python3 2>/dev/null || true
sleep 2
echo ''|tee $LOG_DIR/stdout.log $LOG_DIR/stderr.log $LOG_DIR/app.log
cd "$APP_DIR"
# Launch using absolute module path for clarity; rely on cwd for local imports
setsid nohup "${PY}" -m debugpy --listen "0.0.0.0:${PORT_DEBUG}" /app/server/__main__.py \
1>>"$LOG_DIR/stdout.log" \
2>>"$LOG_DIR/stderr.log" &
PID=$!
sleep 2

View File

@@ -60,20 +60,20 @@ isRamDisk() {
# Setup source directory
configure_source() {
echo "[1/4] Configuring System..."
echo " -> Setting up /services permissions"
echo " -> Setting up /services permissions"
sudo chown -R netalertx /services
echo "[2/4] Configuring Source..."
echo " -> Cleaning up previous instances"
echo " -> Cleaning up previous instances"
test -e ${NETALERTX_LOG} && sudo umount "${NETALERTX_LOG}" 2>/dev/null || true
test -e ${NETALERTX_API} && sudo umount "${NETALERTX_API}" 2>/dev/null || true
test -e ${NETALERTX_APP} && sudo rm -Rf ${NETALERTX_APP}/
echo " -> Linking source to ${NETALERTX_APP}"
echo " -> Linking source to ${NETALERTX_APP}"
sudo ln -s ${SOURCE_DIR}/ ${NETALERTX_APP}
echo " -> Mounting ramdisks for /log and /api"
echo " -> Mounting ramdisks for /log and /api"
mkdir -p ${NETALERTX_LOG} ${NETALERTX_API}
sudo mount -o uid=$(id -u netalertx),gid=$(id -g netalertx),mode=775 -t tmpfs -o size=256M tmpfs "${NETALERTX_LOG}"
sudo mount -o uid=$(id -u netalertx),gid=$(id -g netalertx),mode=775 -t tmpfs -o size=256M tmpfs "${NETALERTX_API}"
@@ -82,19 +82,27 @@ configure_source() {
# tmpfs mounts configured with netalertx ownership and 775 permissions above
touch /app/log/nginx_error.log
echo " -> Empty log"|tee ${INSTALL_DIR}/log/app.log \
echo " -> Empty log"|tee ${INSTALL_DIR}/log/app.log \
${INSTALL_DIR}/log/app_front.log \
${INSTALL_DIR}/log/stdout.log
touch ${INSTALL_DIR}/log/stderr.log \
${INSTALL_DIR}/log/execution_queue.log
echo 0>${INSTALL_DIR}/log/db_is_locked.log
for f in ${INSTALL_DIR}/log/*.log; do
sudo chown netalertx:www-data $f
sudo chmod 664 $f
echo "" > $f
done
mkdir -p /app/log/plugins
sudo chown -R netalertx:www-data ${INSTALL_DIR}
killall python &>/dev/null
sleep 1
while `ps ax | grep -v grep | grep python3 > /dev/null`; do
killall python3 &>/dev/null
sleep 0.2
done
}
# configure_php: configure PHP-FPM and enable dev debug options
@@ -106,34 +114,24 @@ configure_php() {
# start_services: start crond, PHP-FPM, nginx and the application
start_services() {
echo "[4/4] Starting services..."
echo "[4/4] Starting services"
echo " -> Starting CronD (${SYSTEM_SERVICES_CROND}...)"
setsid nohup /services/start-crond.sh &>/dev/null &
echo " -> Starting PHP-FPM (${SYSTEM_SERVICES_PHP_FOLDER}...)"
setsid nohup /services/start-php-fpm.sh &>/dev/null &
# Wait for the previous nginx processes to exit and for the port to free up
tries=0
while ss -ltn | grep -q ":${PORT}[[:space:]]" && [ $tries -lt 10 ]; do
echo " -> Waiting for port ${PORT} to free..."
sleep 0.2
tries=$((tries+1))
done
chmod +x /entrypoint.sh
setsid bash /entrypoint.sh&
sleep 1
echo " -> Starting Nginx (${NETALERTX_FRONT}...)"
setsid nohup /services/start-nginx.sh &>/dev/null &
echo " -> Starting Backend (${NETALERTX_SERVER}...)"
setsid nohup /services/start-backend.sh >/dev/null 2>&1 &
sleep 2
}
echo "$(git rev-parse --short=8 HEAD)">/app/.VERSION
sudo chmod 755 /app/
echo "Development $(git rev-parse --short=8 HEAD)"| sudo tee /app/.VERSION
# Run the main function
main
# create a services readme file
echo "This folder is auto-generated by the container and devcontainer setup.sh script." > /services/README.md
echo "Any changes here will be lost on rebuild. To make permanent changes, edit files in .devcontainer or production filesystem and rebuild the container." >> /services/README.md
echo "Only make temporary/test changes in this folder, then perform a rebuild to reset." >> /services/README.md

View File

@@ -1,5 +0,0 @@
#!/bin/bash
#Logging handled in nginx.conf
nginx -c "/services/nginx/nginx.conf" -g "daemon off;" 2>&1 >/dev/null

View File

@@ -1,40 +0,0 @@
#!/bin/sh
# Stream NetAlertX logs to stdout so the Dev Containers output channel shows them.
# This script waits briefly for the files to appear and then tails them with -F.
LOG_FILES="/app/log/app.log /app/log/db_is_locked.log /app/log/execution_queue.log /app/log/app_front.log /app/log/app.php_errors.log /app/log/IP_changes.log /app/stderr.log /app/stdout.log"
wait_for_files() {
# Wait up to ~10s for at least one of the files to exist
attempts=0
while [ $attempts -lt 20 ]; do
for f in $LOG_FILES; do
if [ -f "$f" ]; then
return 0
fi
done
attempts=$((attempts+1))
sleep 0.5
done
return 1
}
if wait_for_files; then
echo "Starting log stream for:"
for f in $LOG_FILES; do
[ -f "$f" ] && echo " $f"
done
# Use tail -F where available. If tail -F isn't supported, tail -f is used as fallback.
# Some minimal images may have busybox tail without -F; this handles both.
if tail --version >/dev/null 2>&1; then
# GNU tail supports -F
tail -n +1 -F $LOG_FILES
else
# Fallback to -f for busybox; will exit if files rotate or do not exist initially
tail -n +1 -f $LOG_FILES
fi
else
echo "No log files appeared after wait; exiting stream script."
exit 0
fi