From 734ee734e95a47991a37ac6ad7a66909938ee4fb Mon Sep 17 00:00:00 2001 From: mtojek Date: Wed, 4 Aug 2021 08:49:33 +0200 Subject: [PATCH 1/2] Debug: get agent data --- internal/kibana/agents.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/internal/kibana/agents.go b/internal/kibana/agents.go index 7e8c92dea0..22d52f5f3a 100644 --- a/internal/kibana/agents.go +++ b/internal/kibana/agents.go @@ -15,6 +15,8 @@ import ( "github.com/elastic/elastic-package/internal/signal" ) +var waitForPolicyAssignedTimeout = 10 * time.Minute + // Agent represents an Elastic Agent enrolled with fleet. type Agent struct { ID string `json:"id"` @@ -27,6 +29,15 @@ type Agent struct { } `json:"local_metadata"` } +// String method returns string representation of an agent. +func (a *Agent) String() string { + b, err := json.Marshal(a) + if err != nil { + return err.Error() + } + return string(b) +} + // ListAgents returns the list of agents enrolled with Fleet. func (c *Client) ListAgents() ([]Agent, error) { statusCode, respBody, err := c.get(fmt.Sprintf("%s/agents", FleetAPI)) @@ -71,7 +82,12 @@ func (c *Client) AssignPolicyToAgent(a Agent, p Policy) error { } func (c *Client) waitUntilPolicyAssigned(a Agent, p Policy) error { + startTime := time.Now() for { + if startTime.Add(waitForPolicyAssignedTimeout).After(time.Now()) { + return errors.New("timeout: policy hasn't been assigned in time") + } + if signal.SIGINT() { return errors.New("SIGINT: cancel waiting for policy assigned") } @@ -80,6 +96,7 @@ func (c *Client) waitUntilPolicyAssigned(a Agent, p Policy) error { if err != nil { return errors.Wrap(err, "can't get the agent") } + logger.Debugf("Agent data: %s", agent.String()) if agent.PolicyID == p.ID && agent.PolicyRevision == p.Revision { logger.Debugf("Policy revision assigned to the agent (ID: %s)...", a.ID) From eae2f5eb5fee03b67a11968e19fe9e0d123f041d Mon Sep 17 00:00:00 2001 From: mtojek Date: Wed, 4 Aug 2021 09:07:40 +0200 Subject: [PATCH 2/2] Fix --- internal/kibana/agents.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/kibana/agents.go b/internal/kibana/agents.go index 22d52f5f3a..3ce0c52ffd 100644 --- a/internal/kibana/agents.go +++ b/internal/kibana/agents.go @@ -82,9 +82,9 @@ func (c *Client) AssignPolicyToAgent(a Agent, p Policy) error { } func (c *Client) waitUntilPolicyAssigned(a Agent, p Policy) error { - startTime := time.Now() + timeout := time.Now().Add(waitForPolicyAssignedTimeout) for { - if startTime.Add(waitForPolicyAssignedTimeout).After(time.Now()) { + if time.Now().After(timeout) { return errors.New("timeout: policy hasn't been assigned in time") }