diff --git a/cmd/fleetctl/preview.go b/cmd/fleetctl/preview.go index a611af7ca1..07f9d94cd4 100644 --- a/cmd/fleetctl/preview.go +++ b/cmd/fleetctl/preview.go @@ -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 {