Issue 2699 preview reset stops orbit (#2720)

* Stop orbit when running preview reset as well

* Detect if orbit is already running and dont start it again
This commit is contained in:
Tomas Touceda 2021-10-27 15:57:24 -03:00 committed by GitHub
parent 5832316335
commit a37024e3b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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":