Skip to content

Commit

Permalink
[Ingest manager] Copy Action store on upgrade (elastic#21298)
Browse files Browse the repository at this point in the history
[Ingest manager] Copy Action store on upgrade (elastic#21298)
  • Loading branch information
michalpristas committed Sep 25, 2020
1 parent e12a310 commit 7742949
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions x-pack/elastic-agent/CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
==== Breaking changes

==== Bugfixes
- Copy Action store on upgrade {pull}21298[21298]
- Include inputs in action store actions {pull}21298[21298]

==== New features

Expand Down
6 changes: 5 additions & 1 deletion x-pack/elastic-agent/pkg/agent/application/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions x-pack/elastic-agent/pkg/agent/application/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

0 comments on commit 7742949

Please sign in to comment.