Skip to content

feat(mqtt-bridge): add systemd service configuration for production deployments#157

Open
groupsky wants to merge 7 commits intomainfrom
feature/systemd-mqtt-bridge
Open

feat(mqtt-bridge): add systemd service configuration for production deployments#157
groupsky wants to merge 7 commits intomainfrom
feature/systemd-mqtt-bridge

Conversation

@groupsky
Copy link
Owner

@groupsky groupsky commented Jan 4, 2026

Summary

Adds comprehensive systemd service configuration for running ya-modbus-bridge as a production service on Linux systems:

  • Systemd unit file with extensive security hardening (filesystem protection, capability dropping, syscall filtering, resource limits)
  • Installation guide (437 lines) covering installation, configuration, troubleshooting, updates, multiple instances, and uninstallation
  • Configuration examples for MQTT settings and environment variables
  • Package integration to include systemd files in npm distribution

Security Features

  • ProtectSystem=strict, PrivateTmp, ProtectHome, PrivateDevices
  • All kernel protection directives enabled
  • Capabilities dropped, NoNewPrivileges=yes
  • SystemCallFilter with @system-service allowlist
  • Resource limits (512MB memory, 100% CPU, 128 tasks)
  • Supports systemd v235+ (v247+ recommended for full features)

Test plan

  • Verify systemd files are included in npm package (npm pack)
  • Test installation on systemd v235+ system
  • Test installation on systemd v247+ system
  • Verify service starts and connects to MQTT broker
  • Verify systemd-analyze security ya-modbus-bridge.service produces good score
  • Test graceful shutdown with systemctl stop

groupsky and others added 7 commits January 4, 2026 17:59
…eployments

Provides production-ready systemd service configuration following security best practices including sandboxing, resource limits, and proper logging integration. Includes comprehensive installation guide covering setup, management, troubleshooting, and security analysis.
- Include systemd directory in published npm package
- Document npm global installation paths for systemd files
- Add binary path verification and symlink instructions
- Remove unsupported LOG_LEVEL environment variable
- Add restart loop prevention (StartLimitBurst, StartLimitIntervalSec)

Addresses Claude PR review feedback items #1, #4, #6, #7 and nice-to-have restart prevention.
Fixes three critical issues identified in comprehensive review:

1. ExecPaths compatibility with npm global installations
   - Changed from /usr/lib/node_modules to /usr/lib /usr/local/lib
   - Allows Node.js to execute code from standard npm installation paths
   - Fixes "Permission denied" errors on most Linux distributions

2. NPM path resolution for nvm/workspace environments
   - Replaced npm config get prefix with npm root -g
   - Added fallback to find via binary location for nvm users
   - Prevents ENOWORKSPACES errors in monorepo environments

3. Network address family restrictions
   - Added AF_UNIX to RestrictAddressFamilies
   - Allows Node.js IPC via Unix domain sockets
   - Prevents potential runtime failures in dependencies

These fixes ensure compatibility with:
- Standard npm installations (/usr/local)
- nvm/n/fnm version managers
- npm workspace environments
- Node.js dependencies requiring Unix sockets
Replace ReadWritePaths with StateDirectory directive which automatically
creates /var/lib/ya-modbus-bridge with correct ownership when the service
starts. This simplifies installation by removing manual directory creation
and ownership steps.
- Document minimum systemd version requirements (v235 min, v247+ recommended)
- Fix useradd command to let StateDirectory handle directory creation
- Add serial port permissions step for Modbus RTU/RS-485 users
- Add explicit GitHub repository URL for manual file download
- Renumber installation steps after adding new serial port section
- Add PrivateDevices=yes for additional device isolation
- Extend ExecPaths to include /lib and /lib64 for broader distro compatibility
- Add robust error handling to path detection with clear error messages
- Add Node.js alternative for jq in troubleshooting section
- Add configuration backup step before package updates
- Standardize boolean values to 'yes' (was mixing 'true' and 'yes')
- Fix multiple instances section: use proper naming convention (-instance2
  instead of @instance2) and add complete setup commands
- Add symlink removal step to uninstallation section
- Add MemoryLimit as cgroup v1 fallback for older systems
@codecov
Copy link

codecov bot commented Jan 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.65%. Comparing base (c77d151) to head (808a3c6).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #157   +/-   ##
=======================================
  Coverage   97.65%   97.65%           
=======================================
  Files          63       63           
  Lines        2348     2348           
  Branches      564      564           
=======================================
  Hits         2293     2293           
  Misses         13       13           
  Partials       42       42           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

1 participant