Skip to content
Merged
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
17 changes: 17 additions & 0 deletions internal/kibana/agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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))
Expand Down Expand Up @@ -71,7 +82,12 @@ func (c *Client) AssignPolicyToAgent(a Agent, p Policy) error {
}

func (c *Client) waitUntilPolicyAssigned(a Agent, p Policy) error {
timeout := time.Now().Add(waitForPolicyAssignedTimeout)
for {
if time.Now().After(timeout) {
return errors.New("timeout: policy hasn't been assigned in time")
}

if signal.SIGINT() {
return errors.New("SIGINT: cancel waiting for policy assigned")
}
Expand All @@ -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)
Expand Down