Skip to content

Complete rewrite of the ubuntu24 installer - #1198

Closed
ingoratsdorf wants to merge 11 commits into
netalertx:mainfrom
ingoratsdorf:ubuntu24
Closed

Complete rewrite of the ubuntu24 installer#1198
ingoratsdorf wants to merge 11 commits into
netalertx:mainfrom
ingoratsdorf:ubuntu24

Conversation

@ingoratsdorf

@ingoratsdorf ingoratsdorf commented Sep 25, 2025

Copy link
Copy Markdown
Contributor

setup.sh and start.sh combined into a single script
netalertx now starts and runs via systemd unit, can be started, stopped and restarted
Amalgamated chmods
tuned chmods based on earlier feedback and discussion

Please have a look, comments welcome :-)

Summary by CodeRabbit

  • New Features

    • Unified Ubuntu 24 installer with guided install/update/start workflow, interactive prompts, safe install/update options, and verbose status messages.
    • System service support for automatic startup and restart-on-failure; runtime environment exported for configured listen address and port.
    • Automated dependency setup, Python virtual environment provisioning, optional vendor updates, and tmpfs-backed log/API handling.
  • Chores

    • Consolidated legacy installer/startup flows and updated runtime dependency list.

Start script deleted and all packed into install script
systemd service added for start and stop
adding quotes, simplifying steps, changes to chmods, unifying start/stop of services
pipefail failed ;-)
@coderabbitai

coderabbitai Bot commented Sep 25, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

Adds a unified Ubuntu 24 installer install/ubuntu24/install.sh, removes legacy split installers, adds requirements.txt, and installs/updates a systemd unit install/ubuntu24/netalertx.service adjusted to run the NetAlertX server via the Python venv.

Changes

Cohort / File(s) Summary
Unified Ubuntu24 installer
install/ubuntu24/install.sh
Adds a single installer script that enforces root, updates apt, installs packages, clones/updates /app, creates a Python venv and installs requirements.txt, configures NGINX/PHP‑FPM, prepares tmpfs-backed logs/API mounts, writes /app/.env, optionally updates vendors, and registers/enables a systemd service.
Removed legacy installers
install/ubuntu24/install.ubuntu24.sh, install/ubuntu24/start.ubuntu24.sh
Deletes prior split install/start scripts; their installation and startup logic is consolidated into the new install.sh.
Systemd unit
install/ubuntu24/netalertx.service
Adds/updates a systemd service unit that loads /app/.env, sets WorkingDirectory=/opt/netalertx-python/bin, and starts the server with ExecStart=/opt/netalertx-python/bin/python3 /app/server, Restart=on-failure, and WantedBy=multi-user.target.
Python dependency manifest
install/ubuntu24/requirements.txt
Adds pinned Python dependencies (network device clients, aio/http libs, utilities) including a git-sourced package for aiofreepybox.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    actor User
    participant Installer as install.sh
    participant OS as Apt/OS
    participant Git as Git Repo
    participant Venv as Python venv (/opt/netalertx-python)
    participant Nginx as NGINX
    participant PHP as PHP‑FPM
    participant Systemd as systemd
    participant Server as NetAlertX

    User->>Installer: run (must be root)
    Installer->>OS: apt update & install packages
    Installer->>Git: clone or git‑pull into /app
    Installer->>Venv: create venv & pip install requirements.txt
    Installer->>Installer: write /app/.env, create buildtimestamp, adjust TZ/PORT/LISTEN_ADDR
    Installer->>Nginx: write config & symlink web UI
    Installer->>PHP: adjust PHP‑FPM socket/settings
    Installer->>Installer: prepare tmpfs mounts, create log files
    Installer->>Systemd: install & enable netalertx.service
    Installer->>PHP: start PHP‑FPM
    Installer->>Nginx: start NGINX
    Systemd->>Server: ExecStart -> /opt/netalertx-python/bin/python3 /app/server
    Server-->>Systemd: runs (Restart on failure)
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

Poem

I’m a rabbit with a bashful hat,
One script now hops where two once sat.
Venv and NGINX hum in tune,
systemd wakes us by the moon.
Hop—deploy—logs snug and flat. 🐇✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly and accurately communicates the primary change—fully rewriting the Ubuntu 24 installer—which aligns with merging and replacing the old scripts, adding systemd support, and updating installation logic.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3dd5c4b and 244ba76.

📒 Files selected for processing (5)
  • install/ubuntu24/install.sh (1 hunks)
  • install/ubuntu24/install.ubuntu24.sh (0 hunks)
  • install/ubuntu24/netalertx.service (1 hunks)
  • install/ubuntu24/requirements.txt (1 hunks)
  • install/ubuntu24/start.ubuntu24.sh (0 hunks)
💤 Files with no reviewable changes (2)
  • install/ubuntu24/install.ubuntu24.sh
  • install/ubuntu24/start.ubuntu24.sh
🧰 Additional context used
🪛 Shellcheck (0.11.0)
install/ubuntu24/install.sh

[warning] 125-125: Use "${var:?}" to ensure this never expands to /* .

(SC2115)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: docker_dev

Comment thread install/ubuntu24/install.sh
Comment thread install/ubuntu24/netalertx.service Outdated

[Service]
EnvironmentFile=/app/.env
ExecStart=/opt/netalertx-python/bin/python /app/server/

@coderabbitai coderabbitai Bot Sep 25, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Fix ExecStart to launch the real application entry point

ExecStart is pointing Python at /app/server/, but Python cannot execute a directory, so the unit will exit immediately with “Is a directory.” Please call the actual script (or python -m …) and add WorkingDirectory if the code expects to run from /app/server.

🤖 Prompt for AI Agents
In install/ubuntu24/netalertx.service around line 7, ExecStart currently points
to the directory "/app/server/" which causes systemd to fail with “Is a
directory.” Update ExecStart to call the real entrypoint (for example the
absolute path to your script like /app/server/main.py or use the module form -m
your_package) and add a WorkingDirectory=/app/server if the app expects to run
from that directory; ensure the referenced script is executable and use the full
python virtualenv interpreter path (/opt/netalertx-python/bin/python) in
ExecStart.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ingoratsdorf Thank you for acknowledging the feedback! If you need any help implementing the fix for the systemd service file - such as identifying the correct entry point script or setting up the WorkingDirectory - feel free to let me know. I'm here to assist with getting the ExecStart directive properly configured.

Comment thread install/ubuntu24/requirements.txt Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 244ba76 and 09e0a45.

📒 Files selected for processing (3)
  • install/ubuntu24/install.sh (1 hunks)
  • install/ubuntu24/netalertx.service (1 hunks)
  • install/ubuntu24/requirements.txt (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • install/ubuntu24/netalertx.service
  • install/ubuntu24/requirements.txt
🧰 Additional context used
🪛 Shellcheck (0.11.0)
install/ubuntu24/install.sh

[warning] 126-126: Use "${var:?}" to ensure this never expands to /* .

(SC2115)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: docker_dev

Comment thread install/ubuntu24/install.sh

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 09e0a45 and e0687a0.

📒 Files selected for processing (2)
  • install/ubuntu24/install.sh (1 hunks)
  • install/ubuntu24/netalertx.service (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • install/ubuntu24/netalertx.service
🧰 Additional context used
🪛 Shellcheck (0.11.0)
install/ubuntu24/install.sh

[warning] 129-129: Use "${var:?}" to ensure this never expands to /* .

(SC2115)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: docker_dev

Comment on lines +1 to +28
#!/usr/bin/env bash

# 🛑 Important: This is only used for the bare-metal install 🛑

echo "---------------------------------------------------------"
echo "[INSTALL] Starting NetAlertX installation for Ubuntu"
echo "---------------------------------------------------------"
echo
echo "This script will install NetAlertX on your Ubuntu system."
echo "It will clone the repository, set up necessary files, and start the application."
echo "Please ensure you have a stable internet connection."
echo "---------------------------------------------------------"

# DO NOT CHANGE ANYTHING BELOW THIS LINE!
INSTALL_DIR=/app
INSTALL_SYSTEM_NAME=ubuntu24
INSTALLER_DIR=${INSTALL_DIR}/install/$INSTALL_SYSTEM_NAME
CONF_FILE=app.conf
DB_FILE=app.db
NGINX_CONF_FILE=netalertx.conf
WEB_UI_DIR=/var/www/html/netalertx
NGINX_CONFIG_FILE=/etc/nginx/conf.d/$NGINX_CONF_FILE
OUI_FILE="/usr/share/arp-scan/ieee-oui.txt" # Define the path to ieee-oui.txt and ieee-iab.txt
SCRIPT_DIR="$(cd -- "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
FILEDB=${INSTALL_DIR}/db/${DB_FILE}
PHPVERSION="8.3"
VENV_DIR="/opt/netalertx-python"
# DO NOT CHANGE ANYTHING ABOVE THIS LINE!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Add fail-fast guards so broken installs don’t slip through

Right now the script keeps marching even if apt-get, git clone, python3 -m venv, etc. fail. That leaves a half-baked install and the systemd unit dies later with missing deps. Please switch the script into strict mode near the top and explicitly tolerate only the handful of commands you expect to fail (e.g. systemctl stop ... || true, umount ... || true).

 #!/usr/bin/env bash
+set -euo pipefail
🤖 Prompt for AI Agents
In install/ubuntu24/install.sh around lines 1 to 28, the installer currently
continues on errors; enable strict fail-fast behavior by turning on shell strict
mode (e.g. set -euo pipefail) near the top of the script so any unexpected
command failure causes immediate exit, and then explicitly allow known-possible
failures by appending "|| true" (or checking exit codes) only to those commands
that should be tolerated (for example systemctl stop ..., umount ..., or rm of
non-existent files); ensure critical operations (apt-get, git clone, python3 -m
venv, package installs, service starts) are left to fail the script so the
installer stops on broken installs and add minimal, specific comments by each
tolerated command explaining why it is allowed to fail.

Comment on lines +138 to +145
echo "[INSTALL] Updating the existing installation..."
cd "${INSTALL_DIR}" || { echo "[INSTALL] Failed to change directory to ${INSTALL_DIR}"; exit 1; }
# In case there were changes, stash them
git stash -q
git pull
echo "[INSTALL] If there were any local changes, they have been >>STASHED<<"
echo "[INSTALL] You can recover them with 'git stash pop' in ${INSTALL_DIR}"
echo

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Make the git stash step robust for root installs

On real boxes we run this as root, and root usually has no git identity configured. In that case git stash -q errors out with “please tell me who you are”, the script pretends everything was stashed, and the subsequent git pull still fails because the tree is dirty. Please detect pending changes first and either perform the stash with a temporary identity or abort with a clear message when stashing isn’t possible.

-    git stash -q
-    git pull
-    echo "[INSTALL] If there were any local changes, they have been >>STASHED<<"
+    if ! git diff --quiet; then
+      git -c user.name="NetAlertX Installer" -c user.email="installer@netalertx.local" stash push -q || {
+        echo "[INSTALL] Failed to stash local changes (configure git user.name/user.email or commit them before rerunning)."
+        exit 1
+      }
+      stashed=1
+    fi
+    git pull
+    if [ "${stashed:-0}" -eq 1 ]; then
+      echo "[INSTALL] Local changes were stashed. Recover them with 'git stash pop' in ${INSTALL_DIR}"
+    else
+      echo "[INSTALL] No local changes detected before updating."
+    fi

Committable suggestion skipped: line range outside the PR's diff.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e0687a0 and 30e301f.

📒 Files selected for processing (1)
  • install/ubuntu24/netalertx.service (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: docker_dev

Comment on lines +7 to +8
WorkingDirectory=/opt/netalertx-python/bin
ExecStart=/opt/netalertx-python/bin/python3 /app/server

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

ExecStart still invokes a directory instead of the app entry point

python3 /app/server still points at the directory, so systemd will exit with “Is a directory” exactly as before. Update ExecStart to call the real script or module (python3 /app/server/<entry>.py or python3 -m netalertx) and set WorkingDirectory to the application path (e.g. /app/server) if the app relies on relative paths.

🤖 Prompt for AI Agents
In install/ubuntu24/netalertx.service around lines 7-8, ExecStart currently runs
"python3 /app/server" which is a directory and causes "Is a directory" errors;
change ExecStart to invoke the actual entry point (for example use the real
script path like /app/server/<entry>.py or run the package as a module with
"python3 -m netalertx") and set WorkingDirectory to the application path (e.g.
/app/server) if the app uses relative paths so systemd starts the correct file.

@ingoratsdorf

Copy link
Copy Markdown
Contributor Author

The plugin manager calls python xyz.... and that does not seem to be working in a systemd environment. It cannot find the virtualenv, even when the system unit has the working directory set to the virtualenv location.
Damn.

@jokob-sk

Copy link
Copy Markdown
Collaborator

oh, sorry to hear that :/ - I'm not sure at all if relevant, there is a Home Assistant container which is doing some magic, but I didn't implement it myself: https://github.com/alexbelgium/hassio-addons/tree/master/netalertx - again, maybe I'm completely off topic

we could also replace the calls in peoples config files if really necessary with a regex expression

@ingoratsdorf

Copy link
Copy Markdown
Contributor Author

I managed to get it sorted.
Env file via the good old /etc/default/.

Geez, the documentation for systemd is not really great. Had to google so many sites to get answers. Not even AI was able to help. Testing ATM. Looking good.

If all satisfied, I'll open a new PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants