Skip to content

[deb/rpm] copy run folder from old agent installation#7999

Merged
pkoutsovasilis merged 6 commits intoelastic:mainfrom
pkoutsovasilis:fix/migrate_run_for_deb_rpm
May 2, 2025
Merged

[deb/rpm] copy run folder from old agent installation#7999
pkoutsovasilis merged 6 commits intoelastic:mainfrom
pkoutsovasilis:fix/migrate_run_for_deb_rpm

Conversation

@pkoutsovasilis
Copy link
Contributor

@pkoutsovasilis pkoutsovasilis commented Apr 28, 2025

What does this PR do?

This PR improves the upgrade experience of Elastic Agent installed via DEB and RPM packages. It updates the pre-installation script (preinstall.sh.tmpl) to copy the run directory from the previous agent installation into the new version's folder during an upgrade, preserving agent state that would otherwise be lost.

Additionally, it extends integration tests to:

  • Introduce validation to ensure the run directory and migration marker are properly copied across an upgrade.
  • Add Debian OS support in DEB-related upgrade tests.

A new helper function FindRunDir is also introduced in the testing framework to locate the agent's run directory based on versioned data paths.

Based on my investigation:

  1. Fleet-related configuration is at /etc/elastic-agent/fleet.enc which is preserved across upgrades
  2. Elastic-agent config file is at /etc/elastic-agent/elastic-agent.yml which is preserved across upgrades
  3. Legacy state.yml and state.enc are getting copied during upgrades
  4. With this PR, run folder which contains components registry is also copied during ugprades

Why is it important?

Currently, when an Elastic Agent installed via a DEB or RPM package is upgraded using a package manager (apt/rpm), it does not preserve the previous state saved under run folder resulting in a poor upgrade experience. This PR ensures that the run directory is correctly migrated, matching the behaviour expected during upgrades.

Checklist

  • I have read and understood the pull request guidelines of this project.
  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in ./changelog/fragments using the changelog tool
  • I have added an integration test or an E2E test

Disruptive User Impact

No disruptive impact expected. This change improves the upgrade process without altering current workflows for users. Upgrades will now correctly migrate the agent’s runtime state.

How to test this PR locally

  • Install an Elastic Agent via a DEB package on Ubuntu or Debian.
  • Enroll and start the agent.
  • Place a marker file inside the agent's run directory.
  • Upgrade the agent using apt install or dpkg -i.
  • Verify that the marker file is preserved in the new run directory after upgrade.
  • Run the updated integration tests under testing/integration/.

Related issues

@pkoutsovasilis pkoutsovasilis added Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team skip-changelog backport-active-all Automated backport with mergify to all the active branches labels Apr 28, 2025
@pkoutsovasilis pkoutsovasilis self-assigned this Apr 28, 2025
@pkoutsovasilis pkoutsovasilis force-pushed the fix/migrate_run_for_deb_rpm branch 4 times, most recently from 7598b06 to abac95d Compare April 28, 2025 20:27
@pkoutsovasilis pkoutsovasilis force-pushed the fix/migrate_run_for_deb_rpm branch from abac95d to 7272d04 Compare April 28, 2025 20:34
@pkoutsovasilis pkoutsovasilis force-pushed the fix/migrate_run_for_deb_rpm branch from 7272d04 to 9d04234 Compare April 29, 2025 07:48
@pkoutsovasilis pkoutsovasilis marked this pull request as ready for review April 29, 2025 10:49
@pkoutsovasilis pkoutsovasilis requested a review from a team as a code owner April 29, 2025 10:49
@elasticmachine
Copy link
Contributor

Pinging @elastic/elastic-agent-control-plane (Team:Elastic-Agent-Control-Plane)

@pkoutsovasilis pkoutsovasilis changed the title [wip] [deb/rpm] copy run folder from old agent installation Apr 29, 2025
@pkoutsovasilis pkoutsovasilis added the bug Something isn't working label Apr 29, 2025
Copy link
Contributor

@ycombinator ycombinator left a comment

Choose a reason for hiding this comment

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

Tested this PR locally and it works as expected:

$ dpkg -i elastic-agent-9.0.0-arm64.deb
Selecting previously unselected package elastic-agent.
...
systemd enable/restart elastic-agent
$ touch /var/lib/elastic-agent/data/elastic-agent-9.0.0-9786ac/run/panos_was_here
$ ls -l /var/lib/elastic-agent/data/elastic-agent-9.0.0-9786ac/run/
total 4
drwxr-x--- 1 root root 36 Apr 29 13:56 beat
drwxrwx--- 1 root root 34 Apr 29 13:56 filestream-monitoring
drwxr-x--- 1 root root 36 Apr 29 13:56 http
-rw-r--r-- 1 root root  0 Apr 29 13:56 panos_was_here
drwxr-x--- 1 root root 30 Apr 29 13:56 system
$ dpkg -i elastic-agent-9.1.0-SNAPSHOT-arm64.deb
(Reading database ... 32803 files and directories currently installed.)
...
found /var/lib/elastic-agent/data/elastic-agent-9.0.0-9786ac/run, copy to /var/lib/elastic-agent/data/elastic-agent-9.1.0-SNAPSHOT-5a8714
...
systemd enable/restart elastic-agent
$ ls -l /var/lib/elastic-agent/data/elastic-agent-9.1.0-SNAPSHOT-5a8714/run/
total 0
drwxr-x--- 1 root root 36 Apr 29 13:57 beat
drwxrwx--- 1 root root 34 Apr 29 13:57 filestream-monitoring
drwxr-x--- 1 root root 36 Apr 29 13:57 http
-rw-r--r-- 1 root root  0 Apr 29 13:57 panos_was_here
drwxr-x--- 1 root root 30 Apr 29 13:57 system

Left some minor comments on the code.

@elasticmachine
Copy link
Contributor

💛 Build succeeded, but was flaky

Failed CI Steps

History

cc @pkoutsovasilis

@pkoutsovasilis pkoutsovasilis merged commit 815c264 into elastic:main May 2, 2025
12 checks passed
@github-actions
Copy link
Contributor

github-actions bot commented May 2, 2025

@Mergifyio backport 8.17 8.18 8.19 9.0

@mergify
Copy link
Contributor

mergify bot commented May 2, 2025

backport 8.17 8.18 8.19 9.0

✅ Backports have been created

Details
mergify bot pushed a commit that referenced this pull request May 2, 2025
* fix: copy run folder from old agent installation

* fix: add debian OS support in deb related integration tests

* doc: add changelog fragment

* fix: deduplicate similar code and improve code readability

* fix: typo

* fix: preserve mode,ownership,timestamps during cp

(cherry picked from commit 815c264)

# Conflicts:
#	dev-tools/packaging/templates/linux/preinstall.sh.tmpl
#	pkg/testing/fixture.go
#	testing/integration/linux_deb_test.go
#	testing/integration/restrict_upgrade_deb_test.go
mergify bot pushed a commit that referenced this pull request May 2, 2025
* fix: copy run folder from old agent installation

* fix: add debian OS support in deb related integration tests

* doc: add changelog fragment

* fix: deduplicate similar code and improve code readability

* fix: typo

* fix: preserve mode,ownership,timestamps during cp

(cherry picked from commit 815c264)

# Conflicts:
#	dev-tools/packaging/templates/linux/preinstall.sh.tmpl
#	pkg/testing/fixture.go
#	testing/integration/linux_deb_test.go
mergify bot pushed a commit that referenced this pull request May 2, 2025
* fix: copy run folder from old agent installation

* fix: add debian OS support in deb related integration tests

* doc: add changelog fragment

* fix: deduplicate similar code and improve code readability

* fix: typo

* fix: preserve mode,ownership,timestamps during cp

(cherry picked from commit 815c264)

# Conflicts:
#	dev-tools/packaging/templates/linux/preinstall.sh.tmpl
#	pkg/testing/fixture.go
#	testing/integration/linux_deb_test.go
mergify bot pushed a commit that referenced this pull request May 2, 2025
* fix: copy run folder from old agent installation

* fix: add debian OS support in deb related integration tests

* doc: add changelog fragment

* fix: deduplicate similar code and improve code readability

* fix: typo

* fix: preserve mode,ownership,timestamps during cp

(cherry picked from commit 815c264)
pkoutsovasilis added a commit that referenced this pull request May 5, 2025
* fix: copy run folder from old agent installation

* fix: add debian OS support in deb related integration tests

* doc: add changelog fragment

* fix: deduplicate similar code and improve code readability

* fix: typo

* fix: preserve mode,ownership,timestamps during cp

(cherry picked from commit 815c264)

Co-authored-by: Panos Koutsovasilis <panos.koutsovasilis@elastic.co>
pkoutsovasilis added a commit that referenced this pull request May 5, 2025
…allation (#8060)

* [deb/rpm] copy run folder from old agent installation (#7999)

* fix: copy run folder from old agent installation

* fix: add debian OS support in deb related integration tests

* doc: add changelog fragment

* fix: deduplicate similar code and improve code readability

* fix: typo

* fix: preserve mode,ownership,timestamps during cp

(cherry picked from commit 815c264)

# Conflicts:
#	dev-tools/packaging/templates/linux/preinstall.sh.tmpl
#	pkg/testing/fixture.go
#	testing/integration/linux_deb_test.go

* fix: resolve conflicts

---------

Co-authored-by: Panos Koutsovasilis <panos.koutsovasilis@elastic.co>
pkoutsovasilis added a commit that referenced this pull request May 5, 2025
…allation (#8059)

* [deb/rpm] copy run folder from old agent installation (#7999)

* fix: copy run folder from old agent installation

* fix: add debian OS support in deb related integration tests

* doc: add changelog fragment

* fix: deduplicate similar code and improve code readability

* fix: typo

* fix: preserve mode,ownership,timestamps during cp

(cherry picked from commit 815c264)

# Conflicts:
#	dev-tools/packaging/templates/linux/preinstall.sh.tmpl
#	pkg/testing/fixture.go
#	testing/integration/linux_deb_test.go

* fix: resolve conflicts

* fix: resolve conflicts

---------

Co-authored-by: Panos Koutsovasilis <panos.koutsovasilis@elastic.co>
pkoutsovasilis added a commit that referenced this pull request May 5, 2025
…allation (#8058)

* [deb/rpm] copy run folder from old agent installation (#7999)

* fix: copy run folder from old agent installation

* fix: add debian OS support in deb related integration tests

* doc: add changelog fragment

* fix: deduplicate similar code and improve code readability

* fix: typo

* fix: preserve mode,ownership,timestamps during cp

(cherry picked from commit 815c264)

# Conflicts:
#	dev-tools/packaging/templates/linux/preinstall.sh.tmpl
#	pkg/testing/fixture.go
#	testing/integration/linux_deb_test.go
#	testing/integration/restrict_upgrade_deb_test.go

* fix: resolve conflicts

* fix: linter issues

---------

Co-authored-by: Panos Koutsovasilis <panos.koutsovasilis@elastic.co>
@pkoutsovasilis pkoutsovasilis deleted the fix/migrate_run_for_deb_rpm branch June 3, 2025 04:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-active-all Automated backport with mergify to all the active branches bug Something isn't working Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team

4 participants