Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create installation marker on upgrade from versions < 8.8.0 #2655

Merged
merged 9 commits into from
May 11, 2023

Conversation

ycombinator
Copy link
Contributor

@ycombinator ycombinator commented May 10, 2023

What does this PR do?

This PR checks if Agent is being run as part of an ongoing upgrade (i.e. being re-exec'd) and, if so, creates the installation marker file if the previous version of the Agent (the one being upgraded from) was one before the installation marker file was introduced.

Why is it important?

In v8.8.0, we introduced an installation marker file to indicate that an Agent was running as installed (this was necessary to implement the custom installation --base-path feature). When an installed Agent that's older than v8.8.0 is upgraded, this installation marker file is not present. In such cases, we need to create it manually post-upgrade. Otherwise, the upgrade will be unsuccessful.

Checklist

  • 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 will be covered as part of Port over Agent Upgrade test #2618

How to test this PR locally

  1. Install a version of Elastic Agent older than 8.8.0.

    cd /tmp
    wget https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-8.2.3-darwin-aarch64.tar.gz
    tar xzf elastic-agent-8.2.3-darwin-aarch64.tar.gz
    cd elastic-agent-8.2.3-darwin-aarch64
    sudo ./elastic-agent install
    
  2. Build Agent with this PR (should result in an Agent with version 8.9.0-SNAPSHOT).

    cd /path/to/agent/repo
    DEV=true SNAPSHOT=true EXTERNAL=true PLATFORMS="darwin/arm64" PACKAGES="tar.gz" mage -v clean package
    
  3. Upgrade to the Agent built with this PR.

    cd
    sudo elastic-agent upgrade --source-uri file:///path/to/agent/repo/build/distributions/ --skip-verify 8.9.0-SNAPSHOT
    
  4. Wait until the upgrade watcher has finished running. This should be 10 minutes. Waiting ensures that the Agent is either successfully completed the upgrade or has been rolled back. You can also keep checking the process list and wait until there is no elastic-agent watch process present.

    ps auxw | grep 'elastic-agent watch' | grep -v grep
    
  5. Check that the new version of Agent is running.

    sudo elastic-agent version
    
  6. Check that the running Agent is healthy.

    sudo elastic-agent status
    

Related issues

Use cases

Screenshots

Logs

Questions to ask yourself

  • How are we going to support this in production?
  • How are we going to measure its adoption?
  • How are we going to debug this?
  • What are the metrics I should take care of?
  • ...

@mergify
Copy link
Contributor

mergify bot commented May 10, 2023

This pull request does not have a backport label. Could you fix it @ycombinator? 🙏
To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-v./d./d./d is the label to automatically backport to the 8./d branch. /d is the digit

NOTE: backport-skip has been added to this pull request.

@elasticmachine
Copy link
Contributor

elasticmachine commented May 10, 2023

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview preview

Expand to view the summary

Build stats

  • Start Time: 2023-05-11T07:52:14.278+0000

  • Duration: 17 min 10 sec

Test stats 🧪

Test Results
Failed 0
Passed 5675
Skipped 23
Total 5698

💚 Flaky test report

Tests succeeded.

🤖 GitHub comments

Expand to view the GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

  • /package : Generate the packages.

  • run integration tests : Run the Elastic Agent Integration tests.

  • run end-to-end tests : Generate the packages and run the E2E Tests.

  • run elasticsearch-ci/docs : Re-trigger the docs validation. (use unformatted text in the comment!)

@ycombinator ycombinator added backport-v8.8.0 Automated backport with mergify and removed backport-skip labels May 10, 2023
@elasticmachine
Copy link
Contributor

elasticmachine commented May 10, 2023

🌐 Coverage report

Name Metrics % (covered/total) Diff
Packages 98.592% (70/71) 👍
Files 68.98% (169/245) 👍
Classes 67.965% (314/462) 👍
Methods 54.077% (955/1766) 👍
Lines 39.755% (10957/27561) 👍 0.01
Conditionals 100.0% (0/0) 💚

@ycombinator ycombinator added the QA:Needs Validation Needs validation by the QA Team label May 10, 2023
// https://github.com/elastic/elastic-agent/issues/2645).

installMarkerVersion := semver.MustParse("8.8.0")
prevVersion, err := semver.NewVersion(upgradeMarker.PrevVersion)
Copy link
Member

Choose a reason for hiding this comment

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

Probably worth checking to see if this PrevVersion field always exists in every version of the upgrade marker file that can be created. The 7.17.0 release is a good place to start checking.

Does 8.8.0 always create the installation marker? If it does then the absence of the installation marker implies that this is an agent version prior to 8.8.0 installed in the default location since that was the only option. Is that possibly safer since it doesn't depend on the format of the upgrade marker file?

Copy link
Member

Choose a reason for hiding this comment

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

I have no issue with approach as long as we can prove it will always work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does 8.8.0 always create the installation marker? If it does then the absence of the installation marker implies that this is an agent version prior to 8.8.0 installed in the default location since that was the only option. Is that possibly safer since it doesn't depend on the format of the upgrade marker file?

Yes, we always create the installation marker in 8.8.0 when Agent is installed using elastic-agent install. That said, I also like the idea of not depending on the format of the upgrade marker file to determine the pre-upgrade version.

I do think we need to check for the existence of the upgrade marker file, though, to know that we're in the midst of an upgrade. Otherwise, we'll end up creating the installation marker even if a user simply ran elastic-agent run or more likely, in the Docker use case (probably harmless).

Copy link
Member

Choose a reason for hiding this comment

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

Yes, checking for the existence of the file should be fine. Looking inside it is more likely to have unexpected results. I'm not sure what guarantees we have about its format so that likely means we have none.

Copy link
Member

@pchila pchila May 11, 2023

Choose a reason for hiding this comment

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

The format is guaranteed by the mapping of the UpdateMarker struct
It's not a file that a user is supposed to touch...
OTOH I don't know if we ever formalized such format anywhere and if we want to keep it forward/backward compatible by putting in place some rules about what is required/optional

@ycombinator ycombinator marked this pull request as ready for review May 10, 2023 20:19
@ycombinator ycombinator requested a review from a team as a code owner May 10, 2023 20:19
@ycombinator ycombinator added the Team:Elastic-Agent Label for the Agent team label May 10, 2023
@ycombinator ycombinator enabled auto-merge (squash) May 10, 2023 22:29
@ycombinator ycombinator disabled auto-merge May 10, 2023 22:29
blakerouse
blakerouse previously approved these changes May 11, 2023
Copy link
Contributor

@blakerouse blakerouse left a comment

Choose a reason for hiding this comment

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

Looks good.

// If the installation marker file is present, we're all set.
if info.RunningInstalled() {
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think adding a comment saying that only installed Elastic Agent's can be self-upgraded so if this returns true then we know that the marker is present as info.RunningInstalled() checks for the marker to be present.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in 4a420fa.

// Otherwise, we're being upgraded from a version of an installed Agent
// that didn't use an installation marker file (that is, before v8.8.0).
// So create the file now.
if err := info.CreateInstallMarker(paths.Top()); err != nil {
Copy link
Member

@pchila pchila May 11, 2023

Choose a reason for hiding this comment

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

This has a bit of overlap with the upgrade migrations I am trying to implement in #2597: I can see creating the install marker as part of an upgrade migration then the PrevVersion (yes I have to rely on the update marker to know which version we are coming from) is < 8.8.0

It's not something to do now but we could think of moving this part into its own migration when available

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes! While writing this I was thinking how it parallels DB migrations so, indeed, there's some overlap here. ++ to incorporating this and #2597 into a more structured migrations process if that makes sense.

Copy link
Member

@pchila pchila left a comment

Choose a reason for hiding this comment

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

LGTM

@ycombinator ycombinator merged commit 9df6514 into elastic:main May 11, 2023
@ycombinator ycombinator deleted the upgrade-installation-marker branch May 11, 2023 12:59
mergify bot pushed a commit that referenced this pull request May 11, 2023
* Create installation marker on upgrade from versions < 8.8.0

* Removing debugging lines

* Mention re-exec for additional clarity

* Adding changelog entry

* Removing scratch statement

* Re-ordering imports

* Error check

* Do not depend on parsing pre-upgrade version from upgrade marker file

* Clarify comment about only installed Agent being eligible for self-upgrade

(cherry picked from commit 9df6514)
ycombinator added a commit that referenced this pull request May 11, 2023
…2668)

* Create installation marker on upgrade from versions < 8.8.0

* Removing debugging lines

* Mention re-exec for additional clarity

* Adding changelog entry

* Removing scratch statement

* Re-ordering imports

* Error check

* Do not depend on parsing pre-upgrade version from upgrade marker file

* Clarify comment about only installed Agent being eligible for self-upgrade

(cherry picked from commit 9df6514)

Co-authored-by: Shaunak Kashyap <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-v8.8.0 Automated backport with mergify QA:Needs Validation Needs validation by the QA Team Team:Elastic-Agent Label for the Agent team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] Upgrade not working
5 participants