Skip to content

Commit

Permalink
[Elastic Agent] Improve Endpoint installation logging (elastic#24429)
Browse files Browse the repository at this point in the history
* Improve logging of exec_file_step.

* Add --log stderr to endpoint.

* Add changelog.
  • Loading branch information
blakerouse authored Mar 9, 2021
1 parent 5608276 commit 24799c5
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 6 deletions.
3 changes: 2 additions & 1 deletion x-pack/elastic-agent/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ elastic-agent
elastic-agent.dev.yml
pkg/agent/operation/tests/scripts/short--1.0.yml
pkg/agent/operation/tests/scripts/configurable-1.0-darwin-x86/configurable
pkg/agent/operation/tests/scripts/servicable-1.0-darwin-x86/configurable
pkg/agent/transpiler/tests/exec-1.0-darwin-x86_64/exec
pkg/agent/application/fleet.yml
pkg/core/plugin/operation/tests/scripts/configurable/1.0/configurable
1 change: 1 addition & 0 deletions x-pack/elastic-agent/CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
- Fix issues with dynamic inputs and conditions {pull}23886[23886]
- Fix bad substitution of API key. {pull}24036[24036]
- Fix docker enrollment issue related to Fleet Server change. {pull}24155[24155]
- Improve log on failure of Endpoint Security installation. {pull}24429[24429]

==== New features

Expand Down
5 changes: 4 additions & 1 deletion x-pack/elastic-agent/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,19 @@ func (Build) Clean() {
// TestBinaries build the required binaries for the test suite.
func (Build) TestBinaries() error {
p := filepath.Join("pkg", "agent", "operation", "tests", "scripts")

p2 := filepath.Join("pkg", "agent", "transpiler", "tests")
configurableName := "configurable"
serviceableName := "serviceable"
execName := "exec"
if runtime.GOOS == "windows" {
configurableName += ".exe"
serviceableName += ".exe"
execName += ".exe"
}
return combineErr(
RunGo("build", "-o", filepath.Join(p, "configurable-1.0-darwin-x86_64", configurableName), filepath.Join(p, "configurable-1.0-darwin-x86_64", "main.go")),
RunGo("build", "-o", filepath.Join(p, "serviceable-1.0-darwin-x86_64", serviceableName), filepath.Join(p, "serviceable-1.0-darwin-x86_64", "main.go")),
RunGo("build", "-o", filepath.Join(p2, "exec-1.0-darwin-x86_64", execName), filepath.Join(p2, "exec-1.0-darwin-x86_64", "main.go")),
)
}

Expand Down
2 changes: 1 addition & 1 deletion x-pack/elastic-agent/pkg/agent/program/supported.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions x-pack/elastic-agent/pkg/agent/transpiler/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,21 @@ func (r *ExecFileStep) Execute(ctx context.Context, rootDir string) error {
}
if err != nil {
exitErr, ok := err.(*exec.ExitError)
if ok && exitErr.Stderr != nil && len(exitErr.Stderr) > 0 {
return fmt.Errorf("operation 'Exec' failed: %s", string(exitErr.Stderr))
if ok && exitErr.Stderr != nil {
errStr := strings.TrimSpace(string(exitErr.Stderr))
if len(errStr) > 0 {
return fmt.Errorf("operation 'Exec' failed (return code: %d): %s", exitErr.ExitCode(), errStr)
}
}
exitCode := 1
if ok {
exitCode = exitErr.ExitCode()
}
outStr := strings.TrimSpace(string(output))
if len(outStr) == 0 {
outStr = "(command had no output)"
}
return fmt.Errorf("operation 'Exec' failed: %s", string(output))
return fmt.Errorf("operation 'Exec' failed (return code: %d): %s", exitCode, outStr)
}
return nil
}
Expand Down
66 changes: 66 additions & 0 deletions x-pack/elastic-agent/pkg/agent/transpiler/steps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
package transpiler

import (
"context"
"fmt"
"os"
"runtime"
"testing"

Expand Down Expand Up @@ -69,3 +71,67 @@ func TestIsSubpath(t *testing.T) {
})
}
}

func TestExecFile_Success(t *testing.T) {
pwd, err := os.Getwd()
if err != nil {
panic(err)
}
binaryPath := "tests/exec-1.0-darwin-x86_64/exec"
step := ExecFile(10, binaryPath, "-output=stdout", "-exitcode=0")
err = step.Execute(context.Background(), pwd)
if err != nil {
t.Fatal("command should not have errored")
}
}

func TestExecFile_StdErr(t *testing.T) {
pwd, err := os.Getwd()
if err != nil {
panic(err)
}
binaryPath := "tests/exec-1.0-darwin-x86_64/exec"
step := ExecFile(10, binaryPath, "-output=stderr", "-exitcode=15")
err = step.Execute(context.Background(), pwd)
if err == nil {
t.Fatal("command should have errored")
}
errMsg := "operation 'Exec' failed (return code: 15): message written to stderr"
if err.Error() != errMsg {
t.Fatalf("got unexpected error: %s", err)
}
}

func TestExecFile_StdOut(t *testing.T) {
pwd, err := os.Getwd()
if err != nil {
panic(err)
}
binaryPath := "tests/exec-1.0-darwin-x86_64/exec"
step := ExecFile(10, binaryPath, "-output=stdout", "-exitcode=16")
err = step.Execute(context.Background(), pwd)
if err == nil {
t.Fatal("command should have errored")
}
errMsg := "operation 'Exec' failed (return code: 16): message written to stdout"
if err.Error() != errMsg {
t.Fatalf("got unexpected error: %s", err)
}
}

func TestExecFile_NoOutput(t *testing.T) {
pwd, err := os.Getwd()
if err != nil {
panic(err)
}
binaryPath := "tests/exec-1.0-darwin-x86_64/exec"
step := ExecFile(10, binaryPath, "-no-output", "-exitcode=17")
err = step.Execute(context.Background(), pwd)
if err == nil {
t.Fatal("command should have errored")
}
errMsg := "operation 'Exec' failed (return code: 17): (command had no output)"
if err.Error() != errMsg {
t.Fatalf("got unexpected error: %s", err)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package main

import (
"flag"
"fmt"
"io"
"os"
)

func main() {
noOutput := flag.Bool("no-output", false, "disable output")
output := flag.String("output", "stderr", "output destination")
exitcode := flag.Int("exitcode", 0, "exit code")
flag.Parse()

if *noOutput {
os.Exit(*exitcode)
}

var dest io.Writer
if *output == "stdout" {
dest = os.Stdout
} else if *output == "stderr" {
dest = os.Stderr
} else {
panic("unknown destination")
}

fmt.Fprintf(dest, "message written to %s", *output)
os.Exit(*exitcode)
}
6 changes: 6 additions & 0 deletions x-pack/elastic-agent/spec/endpoint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ check_install:
path: "endpoint-security"
args:
- "verify"
- "--log"
- "stderr"
timeout: 30
post_install:
- exec_file:
path: "endpoint-security"
args:
- "install"
- "--log"
- "stderr"
- "--upgrade"
- "--resources"
- "endpoint-security-resources.zip"
Expand All @@ -26,6 +30,8 @@ pre_uninstall:
path: "endpoint-security"
args:
- "uninstall"
- "--log"
- "stderr"
timeout: 600
rules:
- fix_stream: {}
Expand Down

0 comments on commit 24799c5

Please sign in to comment.