feat: implement darwin launch agent (#192)
* feat: sketch out periodic command * feat: sketch out periodic command for macOS * feat: implement darwin's launch agent * refactor: better way to run on darwin Make sure we have code that builds on all platforms. * fix(run): max 10 URLs with darwin in unattended mode * feat: add support for seeing/streaming logs * feat: implement the status command and add usage hints * feat(periodic): run onboarding if needed * fix: no too confusing function names * fix: s/periodic/autorun/ Discussed earlier this morning with @hellais. * fix: we cannot show logs before Big Sur Bug reported by @hellais.
This commit is contained in:
parent
6fbad8555f
commit
504a4e79d4
7 changed files with 334 additions and 0 deletions
95
internal/cli/autorun/autorun.go
Normal file
95
internal/cli/autorun/autorun.go
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
package autorun
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"runtime"
|
||||
|
||||
"github.com/alecthomas/kingpin"
|
||||
"github.com/apex/log"
|
||||
"github.com/ooni/probe-cli/internal/autorun"
|
||||
"github.com/ooni/probe-cli/internal/cli/onboard"
|
||||
"github.com/ooni/probe-cli/internal/cli/root"
|
||||
)
|
||||
|
||||
var errNotImplemented = errors.New("autorun: not implemented on this platform")
|
||||
|
||||
func init() {
|
||||
cmd := root.Command("autorun", "Run automatic tests in the background")
|
||||
cmd.Action(func(_ *kingpin.ParseContext) error {
|
||||
probe, err := root.Init()
|
||||
if err != nil {
|
||||
log.Errorf("%s", err)
|
||||
return err
|
||||
}
|
||||
if err := onboard.MaybeOnboarding(probe); err != nil {
|
||||
log.WithError(err).Error("failed to perform onboarding")
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
start := cmd.Command("start", "Start running automatic tests in the background")
|
||||
start.Action(func(_ *kingpin.ParseContext) error {
|
||||
svc := autorun.Get(runtime.GOOS)
|
||||
if svc == nil {
|
||||
return errNotImplemented
|
||||
}
|
||||
if err := svc.Start(); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Info("hint: use 'ooniprobe autorun log stream' to follow logs")
|
||||
return nil
|
||||
})
|
||||
|
||||
stop := cmd.Command("stop", "Stop running automatic tests in the background")
|
||||
stop.Action(func(_ *kingpin.ParseContext) error {
|
||||
svc := autorun.Get(runtime.GOOS)
|
||||
if svc == nil {
|
||||
return errNotImplemented
|
||||
}
|
||||
return svc.Stop()
|
||||
})
|
||||
|
||||
logCmd := cmd.Command("log", "Access background runs logs")
|
||||
stream := logCmd.Command("stream", "Stream background runs logs")
|
||||
stream.Action(func(_ *kingpin.ParseContext) error {
|
||||
svc := autorun.Get(runtime.GOOS)
|
||||
if svc == nil {
|
||||
return errNotImplemented
|
||||
}
|
||||
return svc.LogStream()
|
||||
})
|
||||
|
||||
show := logCmd.Command("show", "Show background runs logs")
|
||||
show.Action(func(_ *kingpin.ParseContext) error {
|
||||
svc := autorun.Get(runtime.GOOS)
|
||||
if svc == nil {
|
||||
return errNotImplemented
|
||||
}
|
||||
return svc.LogShow()
|
||||
})
|
||||
|
||||
status := cmd.Command("status", "Shows autorun instance status")
|
||||
status.Action(func(_ *kingpin.ParseContext) error {
|
||||
svc := autorun.Get(runtime.GOOS)
|
||||
if svc == nil {
|
||||
return errNotImplemented
|
||||
}
|
||||
out, err := svc.Status()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Infof("status: %s", out)
|
||||
switch out {
|
||||
case autorun.StatusRunning:
|
||||
log.Info("hint: use 'ooniprobe autorun stop' to stop")
|
||||
log.Info("hint: use 'ooniprobe autorun log stream' to follow logs")
|
||||
case autorun.StatusScheduled:
|
||||
log.Info("hint: use 'ooniprobe autorun stop' to stop")
|
||||
log.Info("hint: use 'ooniprobe autorun log show' to see previous logs")
|
||||
case autorun.StatusStopped:
|
||||
log.Info("hint: use 'ooniprobe autorun start' to start")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
package run
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
|
||||
"github.com/alecthomas/kingpin"
|
||||
"github.com/apex/log"
|
||||
"github.com/fatih/color"
|
||||
|
|
@ -74,6 +76,13 @@ func init() {
|
|||
|
||||
unattendedCmd := cmd.Command("unattended", "")
|
||||
unattendedCmd.Action(func(_ *kingpin.ParseContext) error {
|
||||
if runtime.GOOS == "darwin" {
|
||||
// Until we have enabled the check-in API we're called every
|
||||
// hour on darwin and we need to self throttle.
|
||||
// TODO(bassosimone): switch to check-in and remove this hack.
|
||||
const veryFew = 10
|
||||
probe.Config().Nettests.WebsitesURLLimit = veryFew
|
||||
}
|
||||
return functionalRun(func(name string, gr nettests.Group) bool {
|
||||
return gr.UnattendedOK == true
|
||||
})
|
||||
|
|
|
|||
Loading…
Reference in a new issue