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

[Elastic Agent] Improve version, restart, enroll CLI commands #20359

Merged
merged 11 commits into from
Aug 3, 2020
3 changes: 3 additions & 0 deletions x-pack/elastic-agent/CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,6 @@
- Add --staging option to enroll command {pull}20026[20026]
- Add `event.dataset` to all events {pull}20076[20076]
- Prepare packaging for endpoint and asc files {pull}20186[20186]
- Improved version CLI {pull}20359[20359]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need 3 entries here, they link the same pr

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it was easier to read being 3 different things, but if you prefer it to be 1 line, I can do that.

- Enroll CLI now restarts running daemon {pull}20359[20359]
- Add restart CLI cmd {pull}20359[20359]
2 changes: 1 addition & 1 deletion x-pack/elastic-agent/control.proto
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ message StatusResponse {
repeated ApplicationStatus applications = 3;
}

service ElasticAgent {
service ElasticAgentControl {
// Fetches the currently running version of the Elastic Agent.
rpc Version(Empty) returns (VersionResponse);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@ type ExecManager interface {
ShutdownComplete()
}

// Manager returns the global reexec manager.
func Manager(log *logger.Logger, exec string) ExecManager {
// NewManager returns the global reexec manager.
func NewManager(log *logger.Logger, exec string) ExecManager {
execSingletonOnce.Do(func() {
execSingleton = newManager(log, exec)
})
return execSingleton
}

// Manager returns the global reexec manager.
func Manager() ExecManager {
return execSingleton
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not look like a good idea why not having just one method with Once-initialization

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to, but that deep into the code, I didn't want to have to pass in the logger and the path to the executable. I wanted that to be set at startup and later usages of the manager, can get the singleton.

}

type manager struct {
logger *logger.Logger
exec string
Expand Down
24 changes: 23 additions & 1 deletion x-pack/elastic-agent/pkg/agent/cmd/enroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
package cmd

import (
"context"
"fmt"
"math/rand"
"os"
"time"

"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/control/client"

"github.com/spf13/cobra"

"github.com/elastic/beats/v7/libbeat/common/backoff"
Expand Down Expand Up @@ -45,6 +48,7 @@ func newEnrollCommandWithArgs(flags *globalFlags, _ []string, streams *cli.IOStr
cmd.Flags().BoolP("force", "f", false, "Force overwrite the current and do not prompt for confirmation")
cmd.Flags().BoolP("insecure", "i", false, "Allow insecure connection to Kibana")
cmd.Flags().StringP("staging", "", "", "Configures agent to download artifacts from a staging build")
cmd.Flags().Bool("no-restart", false, "Skip restarting the currently running daemon")

return cmd
}
Expand Down Expand Up @@ -144,7 +148,25 @@ func enroll(streams *cli.IOStreams, cmd *cobra.Command, flags *globalFlags, args
return errors.New(err, "fail to enroll")
}

fmt.Fprintln(streams.Out, "Successfully enrolled the Agent.")
fmt.Fprintln(streams.Out, "Successfully enrolled the Elastic Agent.")

// skip restarting
noRestart, _ := cmd.Flags().GetBool("no-restart")
if noRestart {
return nil
}

daemon := client.New()
err = daemon.Start(context.Background())
if err == nil {
defer daemon.Stop()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you stopping after restart is done?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stop just disconnects the client from the socket, it does not stop the running daemon.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this all makes sense then :-)

err = daemon.Restart(context.Background())
if err == nil {
fmt.Fprintln(streams.Out, "Successfully triggered restart on running Elastic Agent.")
return nil
}
}
fmt.Fprintln(streams.Out, "Elastic Agent might not be running; unable to trigger restart")
return nil
}

Expand Down
10 changes: 9 additions & 1 deletion x-pack/elastic-agent/pkg/agent/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"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/reexec"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/configuration"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/control/server"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/cli"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config"
Expand Down Expand Up @@ -80,7 +81,14 @@ func run(flags *globalFlags, streams *cli.IOStreams) error {
return err
}
rexLogger := logger.Named("reexec")
rex := reexec.Manager(rexLogger, execPath)
rex := reexec.NewManager(rexLogger, execPath)

// start the control listener
control := server.New(logger.Named("control"))
if err := control.Start(); err != nil {
return err
}
defer control.Stop()

app, err := application.New(logger, pathConfigFile)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions x-pack/elastic-agent/pkg/agent/control/addr_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (

// Address returns the address to connect to Elastic Agent daemon.
func Address() string {
data = paths.Data()
data := paths.Data()
// entire string cannot be longer than 256 characters, this forces the
// length to always be 87 characters (but unique per data path)
return fmt.Sprintf(`\\.\pipe\elastic-agent-%s`, sha256.Sum256(data))
return fmt.Sprintf(`\\.\pipe\elastic-agent-%x`, sha256.Sum256([]byte(data)))
}
6 changes: 3 additions & 3 deletions x-pack/elastic-agent/pkg/agent/control/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"context"
"encoding/json"
"fmt"

"sync"
"time"

"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/control"

"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/control/proto"
)

Expand Down Expand Up @@ -81,7 +81,7 @@ type client struct {
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
client proto.ElasticAgentClient
client proto.ElasticAgentControlClient
cfgLock sync.RWMutex
obsLock sync.RWMutex
}
Expand All @@ -98,7 +98,7 @@ func (c *client) Start(ctx context.Context) error {
if err != nil {
return err
}
c.client = proto.NewElasticAgentClient(conn)
c.client = proto.NewElasticAgentControlClient(conn)
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ func dialContext(ctx context.Context) (*grpc.ClientConn, error) {
}

func dialer(ctx context.Context, addr string) (net.Conn, error) {
return npipe.DialContext(arr)(ctx, "", "")
return npipe.DialContext(addr)(ctx, "", "")
}
Loading