Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry-pick #21835 to 7.x: Use symlink path for reexecutions #22100

Merged
merged 1 commit into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -17,6 +17,7 @@
- Fix issue where inputs without processors defined would panic {pull}21628[21628]
- Prevent reporting ecs version twice {pull}21616[21616]
- Partial extracted beat result in failure to spawn beat {issue}21718[21718]
- Use symlink path for reexecutions {pull}21835[21835]
- Use ML_SYSTEM to detect if agent is running as a service {pull}21884[21884]
- Use local temp instead of system one {pull}21883[21883]
- Fix issue with named pipes on Windows 7 {pull}21931[21931]
Expand Down
20 changes: 19 additions & 1 deletion x-pack/elastic-agent/pkg/agent/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"os"
"os/signal"
"path/filepath"
"syscall"

"github.com/spf13/cobra"
Expand All @@ -26,6 +27,10 @@ import (
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/release"
)

const (
agentName = "elastic-agent"
)

func newRunCommandWithArgs(flags *globalFlags, _ []string, streams *cli.IOStreams) *cobra.Command {
return &cobra.Command{
Use: "run",
Expand Down Expand Up @@ -87,7 +92,7 @@ func run(flags *globalFlags, streams *cli.IOStreams) error { // Windows: Mark se
logger.Warn("Artifact has been build with security disabled. Elastic Agent will not verify signatures of used artifacts.")
}

execPath, err := os.Executable()
execPath, err := reexecPath()
if err != nil {
return err
}
Expand Down Expand Up @@ -146,3 +151,16 @@ func run(flags *globalFlags, streams *cli.IOStreams) error { // Windows: Mark se
rex.ShutdownComplete()
return err
}

func reexecPath() (string, error) {
// set executable path to symlink instead of binary
// in case of updated symlinks we should spin up new agent
potentialReexec := filepath.Join(paths.Top(), agentName)

// in case it does not exists fallback to executable
if _, err := os.Stat(potentialReexec); os.IsNotExist(err) {
return os.Executable()
}

return potentialReexec, nil
}