From 3a5445bd62d58361bebaae0f5b3dabcce27e8b7a Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Tue, 26 Jan 2021 14:27:33 +0100 Subject: [PATCH 1/3] fixed reenroll --- .../pkg/agent/application/enroll_cmd.go | 6 ++++ .../pkg/agent/application/upgrade/upgrade.go | 29 ++++++++++++------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/x-pack/elastic-agent/pkg/agent/application/enroll_cmd.go b/x-pack/elastic-agent/pkg/agent/application/enroll_cmd.go index 7d905b80f8cf..fe4cafa75941 100644 --- a/x-pack/elastic-agent/pkg/agent/application/enroll_cmd.go +++ b/x-pack/elastic-agent/pkg/agent/application/enroll_cmd.go @@ -209,6 +209,12 @@ func (c *EnrollCmd) Execute() error { return err } + // clear action store + // fail only if file exists and there was a failure + if err := os.Remove(info.AgentStateStoreFile()); !os.IsNotExist(err) { + return err + } + return nil } diff --git a/x-pack/elastic-agent/pkg/agent/application/upgrade/upgrade.go b/x-pack/elastic-agent/pkg/agent/application/upgrade/upgrade.go index 3734e0ea3e09..8c2bfb949f38 100644 --- a/x-pack/elastic-agent/pkg/agent/application/upgrade/upgrade.go +++ b/x-pack/elastic-agent/pkg/agent/application/upgrade/upgrade.go @@ -247,19 +247,26 @@ func rollbackInstall(ctx context.Context, hash string) { } func copyActionStore(newHash string) error { - currentActionStorePath := info.AgentActionStoreFile() + storePaths := []string{info.AgentActionStoreFile(), info.AgentStateStoreFile()} - newHome := filepath.Join(filepath.Dir(paths.Home()), fmt.Sprintf("%s-%s", agentName, newHash)) - newActionStorePath := filepath.Join(newHome, filepath.Base(currentActionStorePath)) + for _, currentActionStorePath := range storePaths { - currentActionStore, err := ioutil.ReadFile(currentActionStorePath) - if os.IsNotExist(err) { - // nothing to copy - return nil - } - if err != nil { - return err + newHome := filepath.Join(filepath.Dir(paths.Home()), fmt.Sprintf("%s-%s", agentName, newHash)) + newActionStorePath := filepath.Join(newHome, filepath.Base(currentActionStorePath)) + + currentActionStore, err := ioutil.ReadFile(currentActionStorePath) + if os.IsNotExist(err) { + // nothing to copy + return nil + } + if err != nil { + return err + } + + if err := ioutil.WriteFile(newActionStorePath, currentActionStore, 0600); err != nil { + return err + } } - return ioutil.WriteFile(newActionStorePath, currentActionStore, 0600) + return nil } From 0c460a84614eddf38ed6a783c45e435717941dac Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Tue, 26 Jan 2021 14:30:28 +0100 Subject: [PATCH 2/3] changelog --- x-pack/elastic-agent/CHANGELOG.asciidoc | 1 + 1 file changed, 1 insertion(+) diff --git a/x-pack/elastic-agent/CHANGELOG.asciidoc b/x-pack/elastic-agent/CHANGELOG.asciidoc index 619850f4bc82..6a216900089e 100644 --- a/x-pack/elastic-agent/CHANGELOG.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.asciidoc @@ -30,6 +30,7 @@ - Fix incorrect hash when upgrading agent {pull}22322[22322] - Fix refresh of monitoring configuration {pull}23619[23619] - Fixed nil pointer during unenroll {pull}23609[23609] +- Fixed reenroll scenario {pull}23686[23686] ==== New features From 466f7d4e1e6ef60d0ab63be3505ed49419a7f9ec Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Tue, 26 Jan 2021 14:31:17 +0100 Subject: [PATCH 3/3] changelog --- x-pack/elastic-agent/pkg/agent/application/upgrade/upgrade.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/x-pack/elastic-agent/pkg/agent/application/upgrade/upgrade.go b/x-pack/elastic-agent/pkg/agent/application/upgrade/upgrade.go index 8c2bfb949f38..9b7a271a9d74 100644 --- a/x-pack/elastic-agent/pkg/agent/application/upgrade/upgrade.go +++ b/x-pack/elastic-agent/pkg/agent/application/upgrade/upgrade.go @@ -250,14 +250,13 @@ func copyActionStore(newHash string) error { storePaths := []string{info.AgentActionStoreFile(), info.AgentStateStoreFile()} for _, currentActionStorePath := range storePaths { - newHome := filepath.Join(filepath.Dir(paths.Home()), fmt.Sprintf("%s-%s", agentName, newHash)) newActionStorePath := filepath.Join(newHome, filepath.Base(currentActionStorePath)) currentActionStore, err := ioutil.ReadFile(currentActionStorePath) if os.IsNotExist(err) { // nothing to copy - return nil + continue } if err != nil { return err