Skip to content

Commit

Permalink
[Ingest Manage] Remove installed services on agent uninstall (#24151)
Browse files Browse the repository at this point in the history
[Ingest Manage] Remove installed services on agent uninstall (#24151)
  • Loading branch information
michalpristas authored Mar 2, 2021
1 parent a84508c commit 34e9bc5
Show file tree
Hide file tree
Showing 54 changed files with 1,777 additions and 1,303 deletions.
1 change: 1 addition & 0 deletions x-pack/elastic-agent/CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
- Windows agent doesn't uninstall with a lowercase `c:` drive in the path {pull}23998[23998]
- Fix reloading of log level for services {pull}[24055]24055
- Fix: Successfully installed and enrolled agent running standalone{pull}[24128]24128
- Remove installed services on agent uninstall {pull}[24151]24151

==== New features

Expand Down
17 changes: 4 additions & 13 deletions x-pack/elastic-agent/pkg/agent/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/status"

"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/application/upgrade"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/configuration"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/warn"
Expand Down Expand Up @@ -70,7 +71,7 @@ func createApplication(
return nil, err
}

if IsStandalone(cfg.Fleet) {
if configuration.IsStandalone(cfg.Fleet) {
log.Info("Agent is managed locally")
return newLocal(ctx, log, pathConfigFile, rawConfig, reexec, statusCtrl, uc, agentInfo)
}
Expand All @@ -82,7 +83,7 @@ func createApplication(
return nil, err
}

if IsFleetServerBootstrap(cfg.Fleet) {
if configuration.IsFleetServerBootstrap(cfg.Fleet) {
log.Info("Agent is in Fleet Server bootstrap mode")
return newFleetServerBootstrap(ctx, log, pathConfigFile, rawConfig, statusCtrl, agentInfo)
}
Expand All @@ -91,18 +92,8 @@ func createApplication(
return newManaged(ctx, log, store, cfg, rawConfig, reexec, statusCtrl, agentInfo)
}

// IsStandalone decides based on missing of fleet.enabled: true or fleet.{access_token,kibana} will place Elastic Agent into standalone mode.
func IsStandalone(cfg *configuration.FleetAgentConfig) bool {
return cfg == nil || !cfg.Enabled
}

// IsFleetServerBootstrap decides if Elastic Agent is started in bootstrap mode.
func IsFleetServerBootstrap(cfg *configuration.FleetAgentConfig) bool {
return cfg != nil && cfg.Server != nil && cfg.Server.Bootstrap
}

func mergeFleetConfig(rawConfig *config.Config) (storage.Store, *configuration.Configuration, error) {
path := info.AgentConfigFile()
path := paths.AgentConfigFile()
store := storage.NewDiskStore(path)
reader, err := store.Load()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions x-pack/elastic-agent/pkg/agent/application/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func testMgmtMode(t *testing.T) {
err := c.Unpack(&m)
require.NoError(t, err)
assert.Equal(t, false, m.Fleet.Enabled)
assert.Equal(t, true, IsStandalone(m.Fleet))
assert.Equal(t, true, configuration.IsStandalone(m.Fleet))

})

Expand All @@ -40,7 +40,7 @@ func testMgmtMode(t *testing.T) {
err := c.Unpack(&m)
require.NoError(t, err)
assert.Equal(t, true, m.Fleet.Enabled)
assert.Equal(t, false, IsStandalone(m.Fleet))
assert.Equal(t, false, configuration.IsStandalone(m.Fleet))
})
}

Expand Down
90 changes: 1 addition & 89 deletions x-pack/elastic-agent/pkg/agent/application/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package application

import (
"context"
"fmt"
"strings"
"sync"

Expand Down Expand Up @@ -123,7 +122,7 @@ func (e *emitterController) update() error {
ast := rawAst.Clone()
inputs, ok := transpiler.Lookup(ast, "inputs")
if ok {
renderedInputs, err := renderInputs(inputs, varsArray)
renderedInputs, err := transpiler.RenderInputs(inputs, varsArray)
if err != nil {
return err
}
Expand Down Expand Up @@ -191,90 +190,3 @@ func readfiles(files []string, emitter emitterFunc) error {

return emitter(c)
}

func renderInputs(inputs transpiler.Node, varsArray []*transpiler.Vars) (transpiler.Node, error) {
l, ok := inputs.Value().(*transpiler.List)
if !ok {
return nil, fmt.Errorf("inputs must be an array")
}
nodes := []*transpiler.Dict{}
nodesMap := map[string]*transpiler.Dict{}
for _, vars := range varsArray {
for _, node := range l.Value().([]transpiler.Node) {
dict, ok := node.Clone().(*transpiler.Dict)
if !ok {
continue
}
n, err := dict.Apply(vars)
if err == transpiler.ErrNoMatch {
// has a variable that didn't exist, so we ignore it
continue
}
if err != nil {
// another error that needs to be reported
return nil, err
}
if n == nil {
// condition removed it
continue
}
dict = n.(*transpiler.Dict)
hash := string(dict.Hash())
_, exists := nodesMap[hash]
if !exists {
nodesMap[hash] = dict
nodes = append(nodes, dict)
}
}
}
nInputs := []transpiler.Node{}
for _, node := range nodes {
nInputs = append(nInputs, promoteProcessors(node))
}
return transpiler.NewList(nInputs), nil
}

func promoteProcessors(dict *transpiler.Dict) *transpiler.Dict {
p := dict.Processors()
if p == nil {
return dict
}
var currentList *transpiler.List
current, ok := dict.Find("processors")
if ok {
currentList, ok = current.Value().(*transpiler.List)
if !ok {
return dict
}
}
ast, _ := transpiler.NewAST(map[string]interface{}{
"processors": p,
})
procs, _ := transpiler.Lookup(ast, "processors")
nodes := nodesFromList(procs.Value().(*transpiler.List))
if ok && currentList != nil {
nodes = append(nodes, nodesFromList(currentList)...)
}
dictNodes := dict.Value().([]transpiler.Node)
set := false
for i, node := range dictNodes {
switch n := node.(type) {
case *transpiler.Key:
if n.Name() == "processors" {
dictNodes[i] = transpiler.NewKey("processors", transpiler.NewList(nodes))
set = true
}
}
if set {
break
}
}
if !set {
dictNodes = append(dictNodes, transpiler.NewKey("processors", transpiler.NewList(nodes)))
}
return transpiler.NewDict(dictNodes)
}

func nodesFromList(list *transpiler.List) []transpiler.Node {
return list.Value().([]transpiler.Node)
}
Loading

0 comments on commit 34e9bc5

Please sign in to comment.