Skip to content

Conversation

kaanyalti
Copy link
Contributor

@kaanyalti kaanyalti commented Aug 15, 2025

PR 5/6

  • Enhancement

What does this PR do?

  • Updates the download step to return path after download succeeds even in error cases
  • Adds deferred cleanup process in the upgrade function to cleanup download and extracted agent versioned home paths
  • Updates error handling sequence in the upgrade function to add download and versioned home paths in the cleanup list
  • Adds relevant tests

Why is it important?

  • Currently http and fs downloaders cleanup if they fail however if there is any other error (verifier errors) in the download process, the agent does not cleanup the downloaded artifacts
  • The upgrade process reverts the symlink changes and cleans up the new versioned home only after the symlink switch happens. If anything in between the downloaders and symlink switch fails, the agent does not clean up anything.

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

none

How to test this PR locally

  • Run the upgrade_test tests
  • Build the agent for windows, mac and linux
  • Try to upgrade from one version to another without sha512 and force verification failure and validate that the downloads directory is cleaned up.
  • The way I triggered the second cleanup scenario was by filling up the disk space just enough so that the agent can partially extract the downloaded archive but fails midway. Then I validated that the versioned home for the new agent is removed along with the downloads in the initial agent's versioned home directory.

Related issues

@kaanyalti kaanyalti requested a review from a team as a code owner August 15, 2025 14:20
@kaanyalti kaanyalti added Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team backport-active-all Automated backport with mergify to all the active branches labels Aug 15, 2025
@elasticmachine
Copy link
Collaborator

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

@kaanyalti
Copy link
Contributor Author

@pkoutsovasilis @michel-laterman the pr is ready for review

@kaanyalti kaanyalti force-pushed the enhancement/5235_upgrade_cleanup_downloads_and_extracted_agent branch from 0174246 to 8186da8 Compare August 20, 2025 12:52
@kaanyalti kaanyalti marked this pull request as draft September 2, 2025 13:06
@kaanyalti kaanyalti force-pushed the enhancement/5235_upgrade_cleanup_downloads_and_extracted_agent branch from 8186da8 to 033782f Compare September 4, 2025 15:13
@kaanyalti
Copy link
Contributor Author

Last commit has issues, looking into it

@kaanyalti kaanyalti force-pushed the enhancement/5235_upgrade_cleanup_downloads_and_extracted_agent branch from 033782f to bbc909a Compare September 4, 2025 15:16
@kaanyalti kaanyalti changed the title Enhancement/5235 upgrade cleanup downloads and extracted agent Enhancement/5235 upgrade cleans up downloads and extracted agent Sep 8, 2025
Copy link
Contributor

mergify bot commented Sep 8, 2025

This pull request is now in conflicts. Could you fix it? 🙏
To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b enhancement/5235_upgrade_cleanup_downloads_and_extracted_agent upstream/enhancement/5235_upgrade_cleanup_downloads_and_extracted_agent
git merge upstream/main
git push upstream enhancement/5235_upgrade_cleanup_downloads_and_extracted_agent

@kaanyalti kaanyalti force-pushed the enhancement/5235_upgrade_cleanup_downloads_and_extracted_agent branch 2 times, most recently from ae00198 to 66935ec Compare September 8, 2025 18:34
Copy link
Contributor

mergify bot commented Sep 9, 2025

This pull request is now in conflicts. Could you fix it? 🙏
To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b enhancement/5235_upgrade_cleanup_downloads_and_extracted_agent upstream/enhancement/5235_upgrade_cleanup_downloads_and_extracted_agent
git merge upstream/main
git push upstream enhancement/5235_upgrade_cleanup_downloads_and_extracted_agent

@kaanyalti kaanyalti force-pushed the enhancement/5235_upgrade_cleanup_downloads_and_extracted_agent branch from 66935ec to 9c67e8b Compare September 12, 2025 17:25
Copy link
Contributor

mergify bot commented Sep 14, 2025

This pull request is now in conflicts. Could you fix it? 🙏
To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b enhancement/5235_upgrade_cleanup_downloads_and_extracted_agent upstream/enhancement/5235_upgrade_cleanup_downloads_and_extracted_agent
git merge upstream/main
git push upstream enhancement/5235_upgrade_cleanup_downloads_and_extracted_agent

@kaanyalti
Copy link
Contributor Author

@ycombinator @michel-laterman I will merge changes from target to address ci failures, and will request reviews again, sorry for the inconvenience. I put a post about this in the team channel

@kaanyalti kaanyalti enabled auto-merge (squash) September 18, 2025 15:05
Copy link

@elasticmachine
Copy link
Collaborator

💚 Build Succeeded

History

cc @kaanyalti

@kaanyalti kaanyalti merged commit ab23962 into elastic:main Sep 18, 2025
23 checks passed
Copy link
Contributor

@Mergifyio backport 8.18 8.19 9.0 9.1

Copy link
Contributor

mergify bot commented Sep 18, 2025

backport 8.18 8.19 9.0 9.1

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Sep 18, 2025
* enhancement(5235): added comments describing updates in error handling. returning path in error cases if the download is completed

* enhancement(5235): added wrapper var for verifier
constructor in download step to improve testability

* enhancement(5235): added test for downloadArtifact
verifier errors

* enhancement(5235): addded cleanupPaths slice. added deferred call to remove paths in the cleanupPaths slice

* enhancement(5235): add archive and hash path to cleanup paths after artifact download

* enhancement(5235): update error handling after unpack. add new versioned home to cleanup paths

enhancement(5235): move newHome declaration

* enhancement(5235): added upgrade cleanup test

* enhancement(5235): added changelog fragment

enhancement(5235): updated changelog fragment

* enhancement(5235): fix errors in step_download after rebase

* enhancement(5235): fix error in step_download_test after rebase

* enhancement(5235): refactor disk space error check and path cleanup into one deferred call

* enhancement(5235): refactored verifier factory and updated relevant download tests

* enhancement(5235): removed separate test for cleanup, added cleanup checks in error handling test

* enhancement(5235): changed function name in verifier

* enhancement(5235): using AddHashExtension instead of hard coded hash extension string in upgrader

(cherry picked from commit ab23962)

# Conflicts:
#	internal/pkg/agent/application/upgrade/upgrade.go
#	internal/pkg/agent/application/upgrade/upgrade_test.go
mergify bot pushed a commit that referenced this pull request Sep 18, 2025
* enhancement(5235): added comments describing updates in error handling. returning path in error cases if the download is completed

* enhancement(5235): added wrapper var for verifier
constructor in download step to improve testability

* enhancement(5235): added test for downloadArtifact
verifier errors

* enhancement(5235): addded cleanupPaths slice. added deferred call to remove paths in the cleanupPaths slice

* enhancement(5235): add archive and hash path to cleanup paths after artifact download

* enhancement(5235): update error handling after unpack. add new versioned home to cleanup paths

enhancement(5235): move newHome declaration

* enhancement(5235): added upgrade cleanup test

* enhancement(5235): added changelog fragment

enhancement(5235): updated changelog fragment

* enhancement(5235): fix errors in step_download after rebase

* enhancement(5235): fix error in step_download_test after rebase

* enhancement(5235): refactor disk space error check and path cleanup into one deferred call

* enhancement(5235): refactored verifier factory and updated relevant download tests

* enhancement(5235): removed separate test for cleanup, added cleanup checks in error handling test

* enhancement(5235): changed function name in verifier

* enhancement(5235): using AddHashExtension instead of hard coded hash extension string in upgrader

(cherry picked from commit ab23962)

# Conflicts:
#	internal/pkg/agent/application/upgrade/upgrade.go
#	internal/pkg/agent/application/upgrade/upgrade_test.go
mergify bot pushed a commit that referenced this pull request Sep 18, 2025
* enhancement(5235): added comments describing updates in error handling. returning path in error cases if the download is completed

* enhancement(5235): added wrapper var for verifier
constructor in download step to improve testability

* enhancement(5235): added test for downloadArtifact
verifier errors

* enhancement(5235): addded cleanupPaths slice. added deferred call to remove paths in the cleanupPaths slice

* enhancement(5235): add archive and hash path to cleanup paths after artifact download

* enhancement(5235): update error handling after unpack. add new versioned home to cleanup paths

enhancement(5235): move newHome declaration

* enhancement(5235): added upgrade cleanup test

* enhancement(5235): added changelog fragment

enhancement(5235): updated changelog fragment

* enhancement(5235): fix errors in step_download after rebase

* enhancement(5235): fix error in step_download_test after rebase

* enhancement(5235): refactor disk space error check and path cleanup into one deferred call

* enhancement(5235): refactored verifier factory and updated relevant download tests

* enhancement(5235): removed separate test for cleanup, added cleanup checks in error handling test

* enhancement(5235): changed function name in verifier

* enhancement(5235): using AddHashExtension instead of hard coded hash extension string in upgrader

(cherry picked from commit ab23962)

# Conflicts:
#	internal/pkg/agent/application/upgrade/upgrade_test.go
mergify bot pushed a commit that referenced this pull request Sep 18, 2025
* enhancement(5235): added comments describing updates in error handling. returning path in error cases if the download is completed

* enhancement(5235): added wrapper var for verifier
constructor in download step to improve testability

* enhancement(5235): added test for downloadArtifact
verifier errors

* enhancement(5235): addded cleanupPaths slice. added deferred call to remove paths in the cleanupPaths slice

* enhancement(5235): add archive and hash path to cleanup paths after artifact download

* enhancement(5235): update error handling after unpack. add new versioned home to cleanup paths

enhancement(5235): move newHome declaration

* enhancement(5235): added upgrade cleanup test

* enhancement(5235): added changelog fragment

enhancement(5235): updated changelog fragment

* enhancement(5235): fix errors in step_download after rebase

* enhancement(5235): fix error in step_download_test after rebase

* enhancement(5235): refactor disk space error check and path cleanup into one deferred call

* enhancement(5235): refactored verifier factory and updated relevant download tests

* enhancement(5235): removed separate test for cleanup, added cleanup checks in error handling test

* enhancement(5235): changed function name in verifier

* enhancement(5235): using AddHashExtension instead of hard coded hash extension string in upgrader

(cherry picked from commit ab23962)

# Conflicts:
#	internal/pkg/agent/application/upgrade/upgrade_test.go
intxgo pushed a commit to intxgo/elastic-agent that referenced this pull request Sep 24, 2025
…stic#9386)

* enhancement(5235): added comments describing updates in error handling. returning path in error cases if the download is completed

* enhancement(5235): added wrapper var for verifier
constructor in download step to improve testability

* enhancement(5235): added test for downloadArtifact
verifier errors

* enhancement(5235): addded cleanupPaths slice. added deferred call to remove paths in the cleanupPaths slice

* enhancement(5235): add archive and hash path to cleanup paths after artifact download

* enhancement(5235): update error handling after unpack. add new versioned home to cleanup paths

enhancement(5235): move newHome declaration

* enhancement(5235): added upgrade cleanup test

* enhancement(5235): added changelog fragment

enhancement(5235): updated changelog fragment

* enhancement(5235): fix errors in step_download after rebase

* enhancement(5235): fix error in step_download_test after rebase

* enhancement(5235): refactor disk space error check and path cleanup into one deferred call

* enhancement(5235): refactored verifier factory and updated relevant download tests

* enhancement(5235): removed separate test for cleanup, added cleanup checks in error handling test

* enhancement(5235): changed function name in verifier

* enhancement(5235): using AddHashExtension instead of hard coded hash extension string in upgrader
kaanyalti added a commit that referenced this pull request Sep 24, 2025
…and extracted agent (#10036)

* Enhancement/5235 upgrade cleans up downloads and extracted agent (#9386)

* enhancement(5235): added comments describing updates in error handling. returning path in error cases if the download is completed

* enhancement(5235): added wrapper var for verifier
constructor in download step to improve testability

* enhancement(5235): added test for downloadArtifact
verifier errors

* enhancement(5235): addded cleanupPaths slice. added deferred call to remove paths in the cleanupPaths slice

* enhancement(5235): add archive and hash path to cleanup paths after artifact download

* enhancement(5235): update error handling after unpack. add new versioned home to cleanup paths

enhancement(5235): move newHome declaration

* enhancement(5235): added upgrade cleanup test

* enhancement(5235): added changelog fragment

enhancement(5235): updated changelog fragment

* enhancement(5235): fix errors in step_download after rebase

* enhancement(5235): fix error in step_download_test after rebase

* enhancement(5235): refactor disk space error check and path cleanup into one deferred call

* enhancement(5235): refactored verifier factory and updated relevant download tests

* enhancement(5235): removed separate test for cleanup, added cleanup checks in error handling test

* enhancement(5235): changed function name in verifier

* enhancement(5235): using AddHashExtension instead of hard coded hash extension string in upgrader

(cherry picked from commit ab23962)

# Conflicts:
#	internal/pkg/agent/application/upgrade/upgrade.go
#	internal/pkg/agent/application/upgrade/upgrade_test.go

* resolved merge conflicts

---------

Co-authored-by: Kaan Yalti <[email protected]>
kaanyalti added a commit that referenced this pull request Sep 24, 2025
…and extracted agent (#10037)

* Enhancement/5235 upgrade cleans up downloads and extracted agent (#9386)

* enhancement(5235): added comments describing updates in error handling. returning path in error cases if the download is completed

* enhancement(5235): added wrapper var for verifier
constructor in download step to improve testability

* enhancement(5235): added test for downloadArtifact
verifier errors

* enhancement(5235): addded cleanupPaths slice. added deferred call to remove paths in the cleanupPaths slice

* enhancement(5235): add archive and hash path to cleanup paths after artifact download

* enhancement(5235): update error handling after unpack. add new versioned home to cleanup paths

enhancement(5235): move newHome declaration

* enhancement(5235): added upgrade cleanup test

* enhancement(5235): added changelog fragment

enhancement(5235): updated changelog fragment

* enhancement(5235): fix errors in step_download after rebase

* enhancement(5235): fix error in step_download_test after rebase

* enhancement(5235): refactor disk space error check and path cleanup into one deferred call

* enhancement(5235): refactored verifier factory and updated relevant download tests

* enhancement(5235): removed separate test for cleanup, added cleanup checks in error handling test

* enhancement(5235): changed function name in verifier

* enhancement(5235): using AddHashExtension instead of hard coded hash extension string in upgrader

(cherry picked from commit ab23962)

# Conflicts:
#	internal/pkg/agent/application/upgrade/upgrade.go
#	internal/pkg/agent/application/upgrade/upgrade_test.go

* resolved merge conflicts

---------

Co-authored-by: Kaan Yalti <[email protected]>
kaanyalti added a commit that referenced this pull request Sep 24, 2025
…nd extracted agent (#10038)

* Enhancement/5235 upgrade cleans up downloads and extracted agent (#9386)

* enhancement(5235): added comments describing updates in error handling. returning path in error cases if the download is completed

* enhancement(5235): added wrapper var for verifier
constructor in download step to improve testability

* enhancement(5235): added test for downloadArtifact
verifier errors

* enhancement(5235): addded cleanupPaths slice. added deferred call to remove paths in the cleanupPaths slice

* enhancement(5235): add archive and hash path to cleanup paths after artifact download

* enhancement(5235): update error handling after unpack. add new versioned home to cleanup paths

enhancement(5235): move newHome declaration

* enhancement(5235): added upgrade cleanup test

* enhancement(5235): added changelog fragment

enhancement(5235): updated changelog fragment

* enhancement(5235): fix errors in step_download after rebase

* enhancement(5235): fix error in step_download_test after rebase

* enhancement(5235): refactor disk space error check and path cleanup into one deferred call

* enhancement(5235): refactored verifier factory and updated relevant download tests

* enhancement(5235): removed separate test for cleanup, added cleanup checks in error handling test

* enhancement(5235): changed function name in verifier

* enhancement(5235): using AddHashExtension instead of hard coded hash extension string in upgrader

(cherry picked from commit ab23962)

# Conflicts:
#	internal/pkg/agent/application/upgrade/upgrade_test.go

* resolved merge conflicts

---------

Co-authored-by: Kaan Yalti <[email protected]>
kaanyalti added a commit that referenced this pull request Sep 24, 2025
…nd extracted agent (#10039)

* Enhancement/5235 upgrade cleans up downloads and extracted agent (#9386)

* enhancement(5235): added comments describing updates in error handling. returning path in error cases if the download is completed

* enhancement(5235): added wrapper var for verifier
constructor in download step to improve testability

* enhancement(5235): added test for downloadArtifact
verifier errors

* enhancement(5235): addded cleanupPaths slice. added deferred call to remove paths in the cleanupPaths slice

* enhancement(5235): add archive and hash path to cleanup paths after artifact download

* enhancement(5235): update error handling after unpack. add new versioned home to cleanup paths

enhancement(5235): move newHome declaration

* enhancement(5235): added upgrade cleanup test

* enhancement(5235): added changelog fragment

enhancement(5235): updated changelog fragment

* enhancement(5235): fix errors in step_download after rebase

* enhancement(5235): fix error in step_download_test after rebase

* enhancement(5235): refactor disk space error check and path cleanup into one deferred call

* enhancement(5235): refactored verifier factory and updated relevant download tests

* enhancement(5235): removed separate test for cleanup, added cleanup checks in error handling test

* enhancement(5235): changed function name in verifier

* enhancement(5235): using AddHashExtension instead of hard coded hash extension string in upgrader

(cherry picked from commit ab23962)

# Conflicts:
#	internal/pkg/agent/application/upgrade/upgrade_test.go

* resolved merge conflicts

---------

Co-authored-by: Kaan Yalti <[email protected]>
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 Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants