2018-02-07 19:02:18 +01:00
|
|
|
package run
|
|
|
|
|
|
|
|
import (
|
2018-03-22 15:22:29 +01:00
|
|
|
"errors"
|
2018-02-13 17:11:22 +01:00
|
|
|
|
2018-02-07 19:02:18 +01:00
|
|
|
"github.com/alecthomas/kingpin"
|
2018-02-12 16:45:13 +01:00
|
|
|
"github.com/apex/log"
|
2018-06-29 16:50:05 +02:00
|
|
|
"github.com/fatih/color"
|
2019-12-02 14:15:50 +01:00
|
|
|
"github.com/ooni/probe-cli/internal/cli/onboard"
|
2018-05-03 14:59:55 +02:00
|
|
|
"github.com/ooni/probe-cli/internal/cli/root"
|
|
|
|
"github.com/ooni/probe-cli/internal/database"
|
2020-11-13 17:14:26 +01:00
|
|
|
"github.com/ooni/probe-cli/internal/nettests"
|
2020-11-13 18:42:10 +01:00
|
|
|
"github.com/ooni/probe-cli/internal/ooni"
|
2018-02-07 19:02:18 +01:00
|
|
|
)
|
|
|
|
|
2020-11-13 19:01:06 +01:00
|
|
|
func runNettestGroup(tg string, ctx *ooni.Probe, network *database.Network) error {
|
2020-02-20 12:24:24 +01:00
|
|
|
if ctx.IsTerminated() == true {
|
|
|
|
log.Debugf("context is terminated, stopping runNettestGroup early")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-04 11:19:38 +02:00
|
|
|
sess, err := ctx.NewSession()
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to create a measurement session")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer sess.Close()
|
|
|
|
|
|
|
|
err = sess.MaybeLookupLocation()
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to lookup the location of the probe")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
network, err = database.CreateNetwork(ctx.DB, sess)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to create the network row")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := sess.MaybeLookupBackends(); err != nil {
|
|
|
|
log.WithError(err).Warn("Failed to discover OONI backends")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-02 17:05:02 +01:00
|
|
|
group, ok := nettests.NettestGroups[tg]
|
2018-09-27 18:38:37 +02:00
|
|
|
if !ok {
|
|
|
|
log.Errorf("No test group named %s", tg)
|
|
|
|
return errors.New("invalid test group name")
|
|
|
|
}
|
|
|
|
log.Debugf("Running test group %s", group.Label)
|
|
|
|
|
|
|
|
result, err := database.CreateResult(ctx.DB, ctx.Home, tg, network.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("DB result error: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
Optionally treat EOF on stdin just like SIGTERM (#111)
* Optionally treat EOF on stdin just like SIGTERM
On Unix, Node.js allows us to gracefully kill a process. On Windows
this is more compex. You certainly cannot rely on the default `kill()`
function, which calls `TerminateProcess`.
There is a bunch of C/C++ extensions that in principle allow you to
attempt to gracefully shutdown a Windows process.
But, hey, here's a reality check. Node.js controls our stdin. Node.js
does IPC easy. Controlling uv_spawn flags and using the right not well maintained
C/C++ Node.js extension to kill a process is fragile.
So, treat EOF and any other error on stdin as equivalent to SIGTERM.
However, systemd.
The sane thing to do with systemd is `StandardInput=null`. With such
configuration, stdin immediately returns EOF.
Then, introduce the `OONI_STDIN_EOF_IMPLIES_SIGTERM` environment
variable. When it is `true`, this behaviour is enabled, e.g.:
```bash
export OONI_STDIN_EOF_IMPLIES_SIGTERM=true # behaviour enabled
ooniprobe run
```
I want the default to be disabled because:
1. in the future we may find a better way to solve this problem and I
don't want the _default behaviour_ to change in such case
2. we know we need this knob for ooniprobe-desktop, and we will not
fail to provide it, so it won't suprise/damage us
3. a person trying to write a systemd unit for ooniprobe would be very
surprised to find out they need to disable this behaviour, if it was
enabled by default by this PR
Hence, I believe this design is consistent with designing for the
future and for trying to minimize surprises.
Also, why an environment variable and not a command line flag? Because:
1. we don't want such hypothetical flag to be available where it does not
make sense, e.g., for all subcommands but `run`
2. we don't want the ooni/probe-desktop app to write conditional
code because it needs to check the command we're using and then decide
whether to add such hypothetical flag
Also, why not enabling this only on Windows? Because again we don't
want the ooni/probe-desktop app to write conditional code.
To summarize: we want ooni/probe-desktop app to see the same behaviour
everywhere and we want others to be the least surprised.
Related to https://github.com/ooni/probe/issues/1005
* Update ooni.go
2020-02-13 14:53:06 +01:00
|
|
|
ctx.ListenForSignals()
|
|
|
|
ctx.MaybeListenForStdinClosed()
|
2018-09-27 18:38:37 +02:00
|
|
|
for i, nt := range group.Nettests {
|
2019-12-27 11:32:08 +01:00
|
|
|
if ctx.IsTerminated() == true {
|
2020-02-20 12:24:24 +01:00
|
|
|
log.Debugf("context is terminated, stopping group.Nettests early")
|
2019-12-27 11:32:08 +01:00
|
|
|
break
|
|
|
|
}
|
2018-09-27 18:38:37 +02:00
|
|
|
log.Debugf("Running test %T", nt)
|
2020-06-04 11:19:38 +02:00
|
|
|
ctl := nettests.NewController(nt, ctx, result, sess)
|
2018-09-27 18:38:37 +02:00
|
|
|
ctl.SetNettestIndex(i, len(group.Nettests))
|
|
|
|
if err = nt.Run(ctl); err != nil {
|
|
|
|
log.WithError(err).Errorf("Failed to run %s", group.Label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = result.Finished(ctx.DB); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-07 19:02:18 +01:00
|
|
|
func init() {
|
|
|
|
cmd := root.Command("run", "Run a test group or OONI Run link")
|
|
|
|
|
2018-09-27 18:38:37 +02:00
|
|
|
var nettestGroupNamesBlue []string
|
2020-11-13 19:01:06 +01:00
|
|
|
var probe *ooni.Probe
|
2019-10-02 18:23:14 +02:00
|
|
|
var network *database.Network
|
|
|
|
|
2019-12-02 17:05:02 +01:00
|
|
|
for name := range nettests.NettestGroups {
|
2018-09-27 18:38:37 +02:00
|
|
|
nettestGroupNamesBlue = append(nettestGroupNamesBlue, color.BlueString(name))
|
2018-06-22 14:55:00 +02:00
|
|
|
}
|
|
|
|
|
2018-09-17 11:51:54 +02:00
|
|
|
noCollector := cmd.Flag("no-collector", "Disable uploading measurements to a collector").Bool()
|
|
|
|
|
2018-02-07 19:02:18 +01:00
|
|
|
cmd.Action(func(_ *kingpin.ParseContext) error {
|
2019-10-02 18:23:14 +02:00
|
|
|
var err error
|
2020-11-13 19:01:06 +01:00
|
|
|
probe, err = root.Init()
|
2018-02-12 16:45:13 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("%s", err)
|
|
|
|
return err
|
|
|
|
}
|
2018-06-22 11:01:15 +02:00
|
|
|
|
2020-11-13 19:01:06 +01:00
|
|
|
if err = onboard.MaybeOnboarding(probe); err != nil {
|
2018-06-22 11:01:15 +02:00
|
|
|
log.WithError(err).Error("failed to perform onboarding")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-17 11:51:54 +02:00
|
|
|
if *noCollector == true {
|
2020-11-13 19:01:06 +01:00
|
|
|
probe.Config.Sharing.UploadResults = false
|
2018-09-17 11:51:54 +02:00
|
|
|
}
|
2019-10-02 18:23:14 +02:00
|
|
|
return nil
|
|
|
|
})
|
2018-02-13 17:11:22 +01:00
|
|
|
|
2019-10-02 18:23:14 +02:00
|
|
|
websitesCmd := cmd.Command("websites", "")
|
|
|
|
websitesCmd.Action(func(_ *kingpin.ParseContext) error {
|
2020-11-13 19:01:06 +01:00
|
|
|
return runNettestGroup("websites", probe, network)
|
2019-10-02 18:23:14 +02:00
|
|
|
})
|
|
|
|
imCmd := cmd.Command("im", "")
|
|
|
|
imCmd.Action(func(_ *kingpin.ParseContext) error {
|
2020-11-13 19:01:06 +01:00
|
|
|
return runNettestGroup("im", probe, network)
|
2019-10-02 18:23:14 +02:00
|
|
|
})
|
|
|
|
performanceCmd := cmd.Command("performance", "")
|
|
|
|
performanceCmd.Action(func(_ *kingpin.ParseContext) error {
|
2020-11-13 19:01:06 +01:00
|
|
|
return runNettestGroup("performance", probe, network)
|
2019-10-02 18:23:14 +02:00
|
|
|
})
|
|
|
|
middleboxCmd := cmd.Command("middlebox", "")
|
|
|
|
middleboxCmd.Action(func(_ *kingpin.ParseContext) error {
|
2020-11-13 19:01:06 +01:00
|
|
|
return runNettestGroup("middlebox", probe, network)
|
2019-10-02 18:23:14 +02:00
|
|
|
})
|
2019-12-28 17:48:07 +01:00
|
|
|
circumventionCmd := cmd.Command("circumvention", "")
|
|
|
|
circumventionCmd.Action(func(_ *kingpin.ParseContext) error {
|
2020-11-13 19:01:06 +01:00
|
|
|
return runNettestGroup("circumvention", probe, network)
|
2019-12-28 17:48:07 +01:00
|
|
|
})
|
2019-10-02 18:23:14 +02:00
|
|
|
allCmd := cmd.Command("all", "").Default()
|
|
|
|
allCmd.Action(func(_ *kingpin.ParseContext) error {
|
|
|
|
log.Infof("Running %s tests", color.BlueString("all"))
|
2019-12-02 17:05:02 +01:00
|
|
|
for tg := range nettests.NettestGroups {
|
2020-11-13 19:01:06 +01:00
|
|
|
if err := runNettestGroup(tg, probe, network); err != nil {
|
2019-10-02 18:23:14 +02:00
|
|
|
log.WithError(err).Errorf("failed to run %s", tg)
|
2018-03-08 13:46:21 +01:00
|
|
|
}
|
2018-02-13 17:11:22 +01:00
|
|
|
}
|
2019-10-02 18:23:14 +02:00
|
|
|
return nil
|
2018-02-07 19:02:18 +01:00
|
|
|
})
|
|
|
|
}
|