diff --git a/cmd/fleetctl/preview.go b/cmd/fleetctl/preview.go index be0cffc278..a611af7ca1 100644 --- a/cmd/fleetctl/preview.go +++ b/cmd/fleetctl/preview.go @@ -15,6 +15,7 @@ import ( "runtime" "strconv" "strings" + "syscall" "time" "github.com/cenkalti/backoff/v4" @@ -520,6 +521,10 @@ func previewResetCommand() *cli.Command { return errors.Errorf("Failed to run docker-compose rm -sf for simulated hosts.") } + if err := stopOrbit(previewDir); err != nil { + return errors.Wrap(err, "Failed to stop orbit") + } + fmt.Println("Fleet preview server and dependencies reset. Start again with fleetctl preview.") return nil @@ -545,7 +550,31 @@ func readPidFromFile(destDir string) (int, error) { return strconv.Atoi(string(data)) } +func isOrbitAlreadyRunning(destDir string) bool { + pid, err := readPidFromFile(destDir) + if err != nil { + // if any error occurs reading the pid file, we assume orbit is not running + return false + } + process, err := os.FindProcess(pid) + if err != nil { + // if there are any errors looking for process, we assume orbit is not running + return false + } + // otherwise, we found the process, so it's running + err = process.Signal(syscall.Signal(0)) + if err != nil { + // Unix will always return a process for the pid, so we try sending a signal to see if it's running + return false + } + return true +} + func downloadOrbitAndStart(destDir string, enrollSecret string, address string) error { + if isOrbitAlreadyRunning(destDir) { + fmt.Println("Orbit is already running.") + return nil + } updateOpt := update.DefaultOptions switch runtime.GOOS { case "linux":