architectural change 1

This commit is contained in:
Adam Outler
2025-09-24 16:29:15 -04:00
parent 3dd5c4bfcc
commit 29aa884836
13 changed files with 301 additions and 109 deletions

24
dockerfiles/entrypoint.sh Normal file
View File

@@ -0,0 +1,24 @@
#!/bin/bash
echo "---------------------------------------------------------"
echo "[ENTRYPOINT] Initializing container..."
echo "---------------------------------------------------------"
# Run the main initialization script
/app/dockerfiles/init.sh
echo "---------------------------------------------------------"
echo "[ENTRYPOINT] Starting services..."
echo "---------------------------------------------------------"
# Start all services in the background
/app/dockerfiles/start-crond.sh &
/app/dockerfiles/start-php-fpm.sh &
/app/dockerfiles/start-nginx.sh &
/app/dockerfiles/start-backend.sh &
# Wait for any process to exit
wait -n
# Exit with status of process that exited first
exit $?

View File

@@ -0,0 +1,73 @@
#!/bin/bash
# NetAlertX Comprehensive Health Check Script
# This script verifies all critical services and endpoints are functioning
set -e
EXIT_CODE=0
echo "[HEALTHCHECK] Starting comprehensive health check..."
# Function to log errors and set exit code
log_error() {
echo "[HEALTHCHECK] ERROR: $1" >&2
EXIT_CODE=1
}
# Function to log success
log_success() {
echo "[HEALTHCHECK] ✓ $1"
}
# 1. Check if crond is running
if pgrep -f "crond" > /dev/null; then
log_success "crond is running"
else
log_error "crond is not running"
fi
# 2. Check if php-fpm is running
if pgrep -f "php-fpm" > /dev/null; then
log_success "php-fpm is running"
else
log_error "php-fpm is not running"
fi
# 3. Check if nginx is running
if pgrep -f "nginx" > /dev/null; then
log_success "nginx is running"
else
log_error "nginx is not running"
fi
# 4. Check if python /app/server is running
if pgrep -f "python.*server" > /dev/null; then
log_success "python /app/server is running"
else
log_error "python /app/server is not running"
fi
# 5. Check port 20211 is open and contains "netalertx"
if curl -sf --max-time 10 "http://localhost:${PORT:-20211}" | grep -i "netalertx" > /dev/null; then
log_success "Port ${PORT:-20211} is responding and contains 'netalertx'"
else
log_error "Port ${PORT:-20211} is not responding or doesn't contain 'netalertx'"
fi
# 6. Check port 20212/graphql returns "graphql" in first lines
GRAPHQL_PORT=${GRAPHQL_PORT:-20212}
if curl -sf --max-time 10 "http://localhost:${GRAPHQL_PORT}/graphql" | head -10 | grep -i "graphql" > /dev/null; then
log_success "Port ${GRAPHQL_PORT}/graphql is responding with GraphQL content"
else
log_error "Port ${GRAPHQL_PORT}/graphql is not responding or doesn't contain 'graphql'"
fi
# Summary
if [ $EXIT_CODE -eq 0 ]; then
echo "[HEALTHCHECK] ✅ All health checks passed"
else
echo "[HEALTHCHECK] ❌ One or more health checks failed"
fi
exit $EXIT_CODE

View File

@@ -0,0 +1,6 @@
#!/bin/bash
echo "Initializing backend..."
# Future backend initialization steps can go here.
# For now, we'll just ensure permissions are correct.
chown -R nginx:www-data "${NETALERTX_APP}"
echo "Backend initialized."

View File

@@ -0,0 +1,7 @@
#!/bin/bash
echo "Initializing crond..."
# Add crontab file
cp -f ${NETALERTX_APP}/install/crontab /etc/crontabs/root
chmod 600 /etc/crontabs/root
chown root:root /etc/crontabs/root
echo "crond initialized."

View File

@@ -0,0 +1,7 @@
#!/bin/bash
echo "Initializing nginx..."
# Setup NGINX
echo "Setting webserver to address ($LISTEN_ADDR) and port ($PORT)"
envsubst '$NETALERTX_APP $LISTEN_ADDR $PORT' < "${NETALERTX_APP}/install/netalertx.template.conf" > "${NGINX_CONFIG_FILE}"
rm -f /etc/nginx/http.d/default.conf
echo "nginx initialized."

View File

@@ -0,0 +1,22 @@
#!/bin/bash
echo "Initializing php-fpm..."
# Set up PHP-FPM directories and socket configuration
install -d -o nginx -g www-data /run/php/
sed -i "/^;pid/c\pid = /run/php/php8.3-fpm.pid" /etc/php83/php-fpm.conf
sed -i "/^listen/c\listen = /run/php/php8.3-fpm.sock" /etc/php83/php-fpm.d/www.conf
sed -i "/^;listen.owner/c\listen.owner = nginx" /etc/php83/php-fpm.d/www.conf
sed -i "/^;listen.group/c\listen.group = www-data" /etc/php83/php-fpm.d/www.conf
sed -i "/^user/c\user = nginx" /etc/php83/php-fpm.d/www.conf
sed -i "/^group/c\group = www-data" /etc/php83/php-fpm.d/www.conf
# Increase max child process count
sed -i -e 's/pm.max_children = 5/pm.max_children = 10/' /etc/php83/php-fpm.d/www.conf
# Set error log path
sed -i "/^;*error_log\s*=/c\error_log = ${LOG_APP_PHP_ERRORS}" /etc/php83/php-fpm.conf
# If the line was not found, append it to the end of the file
if ! grep -q '^error_log\s*=' /etc/php83/php-fpm.conf; then
echo "error_log = ${LOG_APP_PHP_ERRORS}" >> /etc/php83/php-fpm.conf
fi
echo "php-fpm initialized."

View File

@@ -12,14 +12,8 @@ PGID=${PGID:-${DEFAULT_GID}}
echo "[INSTALL] Setting up user UID and GID"
if ! groupmod -o -g "$PGID" www-data && [ "$PGID" != "$DEFAULT_GID" ] ; then
echo "Failed to set user GID to ${PGID}, trying with default GID ${DEFAULT_GID}"
groupmod -o -g "$DEFAULT_GID" www-data
fi
if ! usermod -o -u "$PUID" nginx && [ "$PUID" != "$DEFAULT_PUID" ] ; then
echo "Failed to set user UID to ${PUID}, trying with default PUID ${DEFAULT_PUID}"
usermod -o -u "$DEFAULT_PUID" nginx
fi
groupmod -o -g "$PGID" www-data 2>/dev/null || echo "Failed to set user GID to ${PGID}, using existing GID"
usermod -o -u "$PUID" nginx 2>/dev/null || echo "Failed to set user UID to ${PUID}, using existing UID"
echo "
---------------------------------------------------------
@@ -29,18 +23,11 @@ User UID: $(id -u nginx)
User GID: $(getent group www-data | cut -d: -f3)
---------------------------------------------------------"
chown nginx:nginx /run/nginx/ /var/log/nginx/ /var/lib/nginx/ /var/lib/nginx/tmp/
chgrp www-data /var/www/localhost/htdocs/
export INSTALL_DIR=/app # Specify the installation directory here
# DO NOT CHANGE ANYTHING BELOW THIS LINE!
CONF_FILE="app.conf"
NGINX_CONF_FILE=netalertx.conf
DB_FILE="app.db"
FULL_FILEDB_PATH="${INSTALL_DIR}/db/${DB_FILE}"
NGINX_CONFIG_FILE="/etc/nginx/http.d/${NGINX_CONF_FILE}"
FULL_FILEDB_PATH="${NETALERTX_DB}/${DB_FILE}"
OUI_FILE="/usr/share/arp-scan/ieee-oui.txt" # Define the path to ieee-oui.txt and ieee-iab.txt
INSTALL_DIR_OLD=/home/pi/pialert
@@ -58,51 +45,49 @@ fi
if [ "$ALWAYS_FRESH_INSTALL" = true ]; then
echo "[INSTALL] ❗ ALERT /db and /config folders are cleared because the ALWAYS_FRESH_INSTALL is set to: $ALWAYS_FRESH_INSTALL"
# Delete content of "$INSTALL_DIR/config/"
rm -rf "$INSTALL_DIR/config/"*
rm -rf "$INSTALL_DIR_OLD/config/"*
# Delete content of "$NETALERTX_CONFIG/"
rm -rf ${NETALERTX_CONFIG}/*
# Delete content of "$INSTALL_DIR/db/"
rm -rf "$INSTALL_DIR/db/"*
rm -rf "$INSTALL_DIR_OLD/db/"*
# Delete content of "$NETALERTX_DB/"
rm -rf ${NETALERTX_DB}/*
fi
# OVERRIDE settings: Handling APP_CONF_OVERRIDE
# Check if APP_CONF_OVERRIDE is set
# remove old
rm "${INSTALL_DIR}/config/app_conf_override.json"
rm -f "${NETALERTX_CONFIG}/app_conf_override.json"
if [ -z "$APP_CONF_OVERRIDE" ]; then
echo "APP_CONF_OVERRIDE is not set. Skipping config file creation."
else
# Save the APP_CONF_OVERRIDE env variable as a JSON file
echo "$APP_CONF_OVERRIDE" > "${INSTALL_DIR}/config/app_conf_override.json"
echo "Config file saved to ${INSTALL_DIR}/config/app_conf_override.json"
echo "$APP_CONF_OVERRIDE" > "${NETALERTX_CONFIG}/app_conf_override.json"
echo "Config file saved to ${NETALERTX_CONFIG}/app_conf_override.json"
fi
# 🔻 FOR BACKWARD COMPATIBILITY - REMOVE AFTER 12/12/2025
# Check if pialert.db exists, then create a symbolic link to app.db
if [ -f "${INSTALL_DIR_OLD}/db/${OLD_APP_NAME}.db" ]; then
ln -s "${INSTALL_DIR_OLD}/db/${OLD_APP_NAME}.db" "${FULL_FILEDB_PATH}"
ln -sf "${INSTALL_DIR_OLD}/db/${OLD_APP_NAME}.db" "${FULL_FILEDB_PATH}"
fi
# Check if ${OLD_APP_NAME}.conf exists, then create a symbolic link to app.conf
if [ -f "${INSTALL_DIR_OLD}/config/${OLD_APP_NAME}.conf" ]; then
ln -s "${INSTALL_DIR_OLD}/config/${OLD_APP_NAME}.conf" "${INSTALL_DIR}/config/${CONF_FILE}"
ln -sf "${INSTALL_DIR_OLD}/config/${OLD_APP_NAME}.conf" "${NETALERTX_CONFIG}/${CONF_FILE}"
fi
# 🔺 FOR BACKWARD COMPATIBILITY - REMOVE AFTER 12/12/2025
echo "[INSTALL] Copy starter ${DB_FILE} and ${CONF_FILE} if they don't exist"
# Copy starter app.db, app.conf if they don't exist
cp -na "${INSTALL_DIR}/back/${CONF_FILE}" "${INSTALL_DIR}/config/${CONF_FILE}"
cp -na "${INSTALL_DIR}/back/${DB_FILE}" "${FULL_FILEDB_PATH}"
cp -n "${NETALERTX_BACK}/${CONF_FILE}" "${NETALERTX_CONFIG}/${CONF_FILE}" 2>/dev/null || true
cp -n "${NETALERTX_BACK}/${DB_FILE}" "${FULL_FILEDB_PATH}" 2>/dev/null || true
# if custom variables not set we do not need to do anything
if [ -n "${TZ}" ]; then
FILECONF="${INSTALL_DIR}/config/${CONF_FILE}"
FILECONF="${NETALERTX_CONFIG}/${CONF_FILE}"
echo "[INSTALL] Setup timezone"
sed -i "\#^TIMEZONE=#c\TIMEZONE='${TZ}'" "${FILECONF}"
@@ -113,14 +98,14 @@ fi
# if custom variables not set we do not need to do anything
if [ -n "${LOADED_PLUGINS}" ]; then
FILECONF="${INSTALL_DIR}/config/${CONF_FILE}"
FILECONF="${NETALERTX_CONFIG}/${CONF_FILE}"
echo "[INSTALL] Setup custom LOADED_PLUGINS variable"
sed -i "\#^LOADED_PLUGINS=#c\LOADED_PLUGINS=${LOADED_PLUGINS}" "${FILECONF}"
fi
echo "[INSTALL] Setup NGINX"
echo "Setting webserver to address ($LISTEN_ADDR) and port ($PORT)"
envsubst '$INSTALL_DIR $LISTEN_ADDR $PORT' < "${INSTALL_DIR}/install/netalertx.template.conf" > "${NGINX_CONFIG_FILE}"
envsubst '$NETALERTX_APP $LISTEN_ADDR $PORT' < "${NETALERTX_APP}/install/netalertx.template.conf" > "${NGINX_CONFIG_FILE}"
# Run the hardware vendors update at least once
echo "[INSTALL] Run the hardware vendors update"
@@ -132,33 +117,15 @@ else
echo "The file ieee-oui.txt does not exist. Running update_vendors..."
# Run the update_vendors.sh script
if [ -f "${INSTALL_DIR}/back/update_vendors.sh" ]; then
"${INSTALL_DIR}/back/update_vendors.sh"
if [ -f "${NETALERTX_BACK}/update_vendors.sh" ]; then
"${NETALERTX_BACK}/update_vendors.sh"
else
echo "update_vendors.sh script not found in ${INSTALL_DIR}."
echo "update_vendors.sh script not found in ${NETALERTX_APP}."
fi
fi
# Create an empty log files
# Create the execution_queue.log and app_front.log files if they don't exist
touch "${INSTALL_DIR}"/log/{app.log,execution_queue.log,app_front.log,app.php_errors.log,stderr.log,stdout.log,db_is_locked.log}
touch "${INSTALL_DIR}"/api/user_notifications.json
# Create plugins sub-directory if it doesn't exist in case a custom log folder is used
mkdir -p "${INSTALL_DIR}"/log/plugins
echo "[INSTALL] Fixing permissions after copied starter config & DB"
chown -R nginx:www-data "${INSTALL_DIR}"
chmod 750 "${INSTALL_DIR}"/{config,log,db}
find "${INSTALL_DIR}"/{config,log,db} -type f -exec chmod 640 {} \;
# Check if buildtimestamp.txt doesn't exist
if [ ! -f "${INSTALL_DIR}/front/buildtimestamp.txt" ]; then
# Create buildtimestamp.txt
date +%s > "${INSTALL_DIR}/front/buildtimestamp.txt"
chown nginx:www-data "${INSTALL_DIR}/front/buildtimestamp.txt"
fi
echo "[INSTALL] Fixing permissions after runtime initialization"
chown -R nginx:www-data "${NETALERTX_CONFIG}" "${NETALERTX_DB}" "${NETALERTX_LOG}"
echo -e "
[ENV] PATH is ${PATH}

View File

@@ -0,0 +1,8 @@
#!/bin/bash
echo "Starting backend..."
cd "${NETALERTX_APP}" || exit
# Clear previous logs
echo '' > "${LOG_STDOUT}"
echo '' > "${LOG_STDERR}"
# Start the backend and redirect output
exec python3 -m server >> "${LOG_STDOUT}" 2>> "${LOG_STDERR}"

View File

@@ -0,0 +1,3 @@
#!/bin/bash
echo "Starting crond..."
exec /usr/sbin/crond -f -L /dev/stdout

View File

@@ -0,0 +1,3 @@
#!/bin/bash
echo "Starting nginx..."
exec nginx -g "daemon off;"

View File

@@ -0,0 +1,3 @@
#!/bin/bash
echo "Starting php-fpm..."
exec /usr/sbin/php-fpm83 -F

View File

@@ -1,49 +1,7 @@
#!/bin/bash
export INSTALL_DIR=/app
export APP_NAME=netalertx
# This script is now mostly handled by the Dockerfile
# Only runtime cleanup remains
# php-fpm setup
install -d -o nginx -g www-data /run/php/
sed -i "/^;pid/c\pid = /run/php/php8.3-fpm.pid" /etc/php83/php-fpm.conf
sed -i "/^listen/c\listen = /run/php/php8.3-fpm.sock" /etc/php83/php-fpm.d/www.conf
sed -i "/^;listen.owner/c\listen.owner = nginx" /etc/php83/php-fpm.d/www.conf
sed -i "/^;listen.group/c\listen.group = www-data" /etc/php83/php-fpm.d/www.conf
sed -i "/^user/c\user = nginx" /etc/php83/php-fpm.d/www.conf
sed -i "/^group/c\group = www-data" /etc/php83/php-fpm.d/www.conf
# s6 overlay setup
mkdir -p /etc/s6-overlay/s6-rc.d/{SetupOneshot,crond/dependencies.d,php-fpm/dependencies.d,nginx/dependencies.d,$APP_NAME/dependencies.d}
echo "oneshot" > /etc/s6-overlay/s6-rc.d/SetupOneshot/type
echo "longrun" > /etc/s6-overlay/s6-rc.d/crond/type
echo "longrun" > /etc/s6-overlay/s6-rc.d/php-fpm/type
echo "longrun" > /etc/s6-overlay/s6-rc.d/nginx/type
echo "longrun" > /etc/s6-overlay/s6-rc.d/$APP_NAME/type
echo -e "${INSTALL_DIR}/dockerfiles/init.sh" > /etc/s6-overlay/s6-rc.d/SetupOneshot/up
echo -e '#!/bin/execlineb -P
if { echo
"
[INSTALL] Starting crond service...
" }' > /etc/s6-overlay/s6-rc.d/crond/run
echo -e "/usr/sbin/crond -f" >> /etc/s6-overlay/s6-rc.d/crond/run
echo -e "#!/bin/execlineb -P\n/usr/sbin/php-fpm83 -F" > /etc/s6-overlay/s6-rc.d/php-fpm/run
echo -e '#!/bin/execlineb -P\nnginx -g "daemon off;"' > /etc/s6-overlay/s6-rc.d/nginx/run
echo -e '#!/bin/execlineb -P
with-contenv
importas -u PORT PORT
if { echo
"
[INSTALL] 🚀 Starting app (:${PORT})
" }' > /etc/s6-overlay/s6-rc.d/$APP_NAME/run
echo -e "python ${INSTALL_DIR}/server" >> /etc/s6-overlay/s6-rc.d/$APP_NAME/run
touch /etc/s6-overlay/s6-rc.d/user/contents.d/{SetupOneshot,crond,php-fpm,nginx,$APP_NAME} /etc/s6-overlay/s6-rc.d/{crond,php-fpm,nginx,$APP_NAME}/dependencies.d/SetupOneshot
touch /etc/s6-overlay/s6-rc.d/nginx/dependencies.d/php-fpm
touch /etc/s6-overlay/s6-rc.d/$APP_NAME/dependencies.d/nginx
# this removes the current file
# Remove this script since all setup is now done at build time
rm -f $0