Add sleep to give orbit a chance to win the id 1 for the host (#2723)

* Add sleep to give orbit a chance to win the id 1 for the host

* Wait until we have the current host
This commit is contained in:
Tomas Touceda 2021-10-27 17:41:03 -03:00 committed by GitHub
parent a37024e3b1
commit 21153e9400
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -250,6 +250,12 @@ Use the stop and reset subcommands to manage the server and dependencies once st
return errors.Wrap(err, "downloading orbit and osqueryd")
}
// Give it a bit of time so the current device is the one with id 1
fmt.Println("Waiting for current host to enroll...")
if err := waitFirstHost(client); err != nil {
return errors.Wrap(err, "wait for current host")
}
fmt.Println("Starting simulated hosts...")
cmd = exec.Command("docker-compose", "up", "-d", "--remove-orphans")
cmd.Dir = filepath.Join(previewDir, "osquery")
@ -409,6 +415,30 @@ func waitStartup() error {
return nil
}
func waitFirstHost(client *service.Client) error {
retryStrategy := backoff.NewExponentialBackOff()
retryStrategy.MaxInterval = 1 * time.Second
if err := backoff.Retry(
func() error {
hosts, err := client.GetHosts("")
if err != nil {
return err
}
if len(hosts) == 0 {
return errors.New("no hosts yet")
}
return nil
},
retryStrategy,
); err != nil {
return errors.Wrap(err, "checking host count")
}
return nil
}
func checkDocker() error {
// Check installed
if _, err := exec.LookPath("docker"); err != nil {