Skip to content

Add systemd service support to installer - #59

Merged
thiagoralves merged 1 commit into
developmentfrom
devin/1767164370-add-systemd-service
Dec 31, 2025
Merged

Add systemd service support to installer#59
thiagoralves merged 1 commit into
developmentfrom
devin/1767164370-add-systemd-service

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Dec 31, 2025

Copy link
Copy Markdown
Contributor

Summary

Adds automatic systemd service installation to the OpenPLC Runtime v4 installer for native Linux systems. The installer now detects if systemd is available and functional, and if so, creates and starts an openplc-runtime.service that automatically starts the runtime on boot.

Key changes:

  • has_systemd_support() function with multi-layer detection to avoid false positives in Docker/GitHub Actions
  • install_systemd_service() function that creates the service file at /etc/systemd/system/openplc-runtime.service
  • Service is enabled and started immediately using systemctl enable --now
  • Final installation message now shows systemctl commands when service is installed, or falls back to manual start instructions

The detection logic checks: systemctl command exists, /run/systemd/system directory exists, PID 1 is actually systemd, and systemd is reachable via systemctl show-environment.

Updates since last revision

Fixed user data persistence across reboots - User credentials were being lost after reboot because /var/run/runtime is a tmpfs that gets cleared. The fix:

  • Added is_running_in_container() function to detect Docker/Podman environments (checks /.dockerenv, env vars, /proc/1/cgroup, /proc/1/environ)
  • Added get_persistent_data_dir() function to determine where to store .env and restapi.db
  • Native Linux: now uses /var/lib/openplc-runtime for persistent data (survives reboot)
  • Containers: continues using /var/run/runtime (expected to be a mounted volume)
  • Installer creates /var/lib/openplc-runtime when systemd is available

Review & Testing Checklist for Human

  • Test user persistence across reboot: Create a user, reboot, verify the user still exists and can log in
  • Test on native Linux with systemd: Run sudo ./install.sh and verify service is created, enabled, and started
  • Verify persistent data location: After install on native Linux, check that .env and restapi.db are in /var/lib/openplc-runtime/ (not /var/run/runtime/)
  • Test in Docker: Verify container detection works and data is stored in /var/run/runtime (mounted volume)
  • Test container detection edge cases: Verify detection works correctly in Podman, Kubernetes pods, LXC containers

Recommended Test Plan

  1. Run installer on a fresh Ubuntu/Debian VM with systemd
  2. Verify systemctl status openplc-runtime shows the service running
  3. Access the runtime at https://localhost:8443, create a user
  4. Reboot the system
  5. Verify the service starts automatically and the user still exists
  6. Run the Docker image build to ensure CI still passes

Notes

  • CI will not exercise the native Linux code path since Docker containers don't have systemd as PID 1
  • Container detection uses multiple methods for robustness but may have edge cases in unusual container runtimes
  • No uninstall mechanism is provided - users would need to manually run systemctl disable --now openplc-runtime && rm /etc/systemd/system/openplc-runtime.service and optionally remove /var/lib/openplc-runtime/

Requested by: Thiago Alves (@thiagoralves)
Link to Devin run: https://app.devin.ai/sessions/ebc278ce265842bba5abf29df61e1201

- Add has_systemd_support() function to detect if systemd is available
  - Checks for systemctl command
  - Verifies /run/systemd/system directory exists
  - Confirms PID 1 is systemd (handles Docker/GitHub Actions)
  - Tests communication with systemd via show-environment

- Add install_systemd_service() function to create and enable service
  - Creates /etc/systemd/system/openplc-runtime.service
  - Runs daemon-reload and enables service with --now flag
  - Service runs start_openplc.sh as root

- Update installation completion message
  - Shows systemctl commands when service is installed
  - Falls back to manual start instructions when systemd unavailable

This allows native Linux installations to automatically start the
runtime on boot while gracefully skipping service installation in
Docker containers and GitHub Actions environments.

Co-Authored-By: Thiago Alves <thiagoralves@gmail.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@thiagoralves
thiagoralves requested a review from Copilot December 31, 2025 20:38

Copilot AI 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.

Pull request overview

This PR adds systemd service support to the OpenPLC Runtime v4 installer, enabling automatic startup on boot for native Linux systems. The installer now detects systemd availability through multiple validation checks and creates a service configuration when appropriate.

Key changes:

  • Added robust systemd detection with multiple fallback checks to avoid false positives in containerized environments
  • Automatic service installation, enablement, and startup via systemctl
  • Context-aware installation messages showing either systemd management commands or manual start instructions

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread install.sh
Comment on lines +117 to +118
User=root
Group=root

Copilot AI Dec 31, 2025

Copy link

Choose a reason for hiding this comment

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

Running the OpenPLC Runtime service as root introduces unnecessary security risks. Consider creating a dedicated system user (e.g., 'openplc') with minimal required permissions and updating the service to run under that user account instead.

Copilot uses AI. Check for mistakes.
Comment thread install.sh

# Create installation marker
# Create installation marker (must be done before starting the service)
touch "$OPENPLC_DIR/.installed"

Copilot AI Dec 31, 2025

Copy link

Choose a reason for hiding this comment

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

The installation marker file is created twice with different content. Line 383 creates an empty file, then line 384 overwrites it. Remove the redundant touch command on line 383.

Suggested change
touch "$OPENPLC_DIR/.installed"

Copilot uses AI. Check for mistakes.
@thiagoralves
thiagoralves merged commit ec0ccf1 into development Dec 31, 2025
1 check passed
@thiagoralves
thiagoralves deleted the devin/1767164370-add-systemd-service branch December 31, 2025 20:40
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