From 2419c17ff3c15aeda88a47941b612a500b630c31 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 20 Apr 2021 07:23:49 +0200 Subject: [PATCH] [Elastic Agent] Log the container command output with LOGS_PATH (#25150) (#25155) * Log the container command. * Add changelog. (cherry picked from commit 8c1b8f2a094fe2f5af289f4ea7d0d0614521bebe) Co-authored-by: Blake Rouse --- x-pack/elastic-agent/CHANGELOG.next.asciidoc | 1 + .../elastic-agent/pkg/agent/cmd/container.go | 25 ++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/x-pack/elastic-agent/CHANGELOG.next.asciidoc b/x-pack/elastic-agent/CHANGELOG.next.asciidoc index fe2c3b56509..7bc3f2157e7 100644 --- a/x-pack/elastic-agent/CHANGELOG.next.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.next.asciidoc @@ -89,3 +89,4 @@ - Add status subcommand {pull}24856[24856] - Add leader_election provider for k8s {pull}24267[24267] - Add --fleet-server-service-token and FLEET_SERVER_SERVICE_TOKEN options {pull}25083[25083] +- Log output of container to $LOGS_PATH/elastic-agent-start.log when LOGS_PATH set {pull}25150[25150] diff --git a/x-pack/elastic-agent/pkg/agent/cmd/container.go b/x-pack/elastic-agent/pkg/agent/cmd/container.go index 0eb20b608d2..93075da784d 100644 --- a/x-pack/elastic-agent/pkg/agent/cmd/container.go +++ b/x-pack/elastic-agent/pkg/agent/cmd/container.go @@ -121,7 +121,7 @@ all the above actions will be skipped, because the Elastic Agent has already bee occurs on every start of the container set FLEET_FORCE to 1. `, Run: func(c *cobra.Command, args []string) { - if err := containerCmd(streams, c); err != nil { + if err := logContainerCmd(streams, c); err != nil { logError(streams, err) os.Exit(1) } @@ -138,6 +138,25 @@ func logInfo(streams *cli.IOStreams, msg string) { fmt.Fprintln(streams.Out, msg) } +func logContainerCmd(streams *cli.IOStreams, cmd *cobra.Command) error { + logsPath := envWithDefault("", "LOGS_PATH") + if logsPath != "" { + // log this entire command to a file as well as to the passed streams + if err := os.MkdirAll(logsPath, 0755); err != nil { + return fmt.Errorf("preparing LOGS_PATH(%s) failed: %s", logsPath, err) + } + logPath := filepath.Join(logsPath, "elastic-agent-startup.log") + w, err := os.Create(logPath) + if err != nil { + return fmt.Errorf("opening startup log(%s) failed: %s", logPath, err) + } + defer w.Close() + streams.Out = io.MultiWriter(streams.Out, w) + streams.Err = io.MultiWriter(streams.Out, w) + } + return containerCmd(streams, cmd) +} + func containerCmd(streams *cli.IOStreams, cmd *cobra.Command) error { // set paths early so all action below use the defined paths if err := setPaths(); err != nil { @@ -274,8 +293,8 @@ func runContainerCmd(streams *cli.IOStreams, cmd *cobra.Command, cfg setupConfig return err } enroll := exec.Command(executable, cmdArgs...) - enroll.Stdout = os.Stdout - enroll.Stderr = os.Stderr + enroll.Stdout = streams.Out + enroll.Stderr = streams.Err err = enroll.Start() if err != nil { return errors.New("failed to execute enrollment command", err)