From bdf613f27bcb7857b96f3284ac6422428431f11f Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Thu, 24 Sep 2020 19:00:38 +0200 Subject: [PATCH] [Ingest manager] Copy Action store on upgrade (#21298) [Ingest manager] Copy Action store on upgrade (#21298) --- x-pack/elastic-agent/CHANGELOG.next.asciidoc | 2 ++ .../pkg/agent/application/config.go | 6 ++++- .../pkg/agent/application/upgrade/upgrade.go | 23 +++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/x-pack/elastic-agent/CHANGELOG.next.asciidoc b/x-pack/elastic-agent/CHANGELOG.next.asciidoc index 4178cfcbf62..c466d0c656d 100644 --- a/x-pack/elastic-agent/CHANGELOG.next.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.next.asciidoc @@ -10,6 +10,8 @@ - Docker container is not run as root by default. {pull}21213[21213] ==== Bugfixes +- Copy Action store on upgrade {pull}21298[21298] +- Include inputs in action store actions {pull}21298[21298] ==== New features diff --git a/x-pack/elastic-agent/pkg/agent/application/config.go b/x-pack/elastic-agent/pkg/agent/application/config.go index ff15ca44074..e42f3dcab28 100644 --- a/x-pack/elastic-agent/pkg/agent/application/config.go +++ b/x-pack/elastic-agent/pkg/agent/application/config.go @@ -11,6 +11,7 @@ import ( "gopkg.in/yaml.v2" + "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/configuration" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config" @@ -54,7 +55,10 @@ func LoadConfigFromFile(path string) (*config.Config, error) { // // This must be used to load the Agent configuration, so that variables defined in the inputs are not // parsed by go-ucfg. Variables from the inputs should be parsed by the transpiler. -func LoadConfig(m map[string]interface{}) (*config.Config, error) { +func LoadConfig(in map[string]interface{}) (*config.Config, error) { + // make copy of a map so we dont affect a caller + m := common.MapStr(in).Clone() + inputs, ok := m["inputs"] if ok { // remove the inputs 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 cc27846051f..08c38aba8c5 100644 --- a/x-pack/elastic-agent/pkg/agent/application/upgrade/upgrade.go +++ b/x-pack/elastic-agent/pkg/agent/application/upgrade/upgrade.go @@ -14,6 +14,7 @@ import ( "gopkg.in/yaml.v2" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/info" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/paths" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/artifact" @@ -78,6 +79,10 @@ func (u *Upgrader) Upgrade(ctx context.Context, a *fleetapi.ActionUpgrade) error return errors.New("upgrading to same version") } + if err := copyActionStore(newHash); err != nil { + return errors.New(err, "failed to copy action store") + } + if err := u.changeSymlink(ctx, newHash); err != nil { rollbackInstall(newHash) return err @@ -137,3 +142,21 @@ func isSubdir(base, target string) (bool, error) { func rollbackInstall(hash string) { os.RemoveAll(filepath.Join(paths.Data(), fmt.Sprintf("%s-%s", agentName, hash))) } + +func copyActionStore(newHash string) error { + currentActionStorePath := info.AgentActionStoreFile() + + 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 + } + + return ioutil.WriteFile(newActionStorePath, currentActionStore, 0600) +}