ooni-probe-cli/internal/cli/run/run.go

72 lines
1.8 KiB
Go
Raw Normal View History

package run
import (
"errors"
"fmt"
"path/filepath"
2018-02-13 17:11:22 +01:00
"time"
"github.com/alecthomas/kingpin"
2018-02-12 16:45:13 +01:00
"github.com/apex/log"
"github.com/ooni/probe-cli/internal/cli/root"
"github.com/ooni/probe-cli/internal/database"
"github.com/ooni/probe-cli/nettests"
"github.com/ooni/probe-cli/nettests/groups"
)
func init() {
cmd := root.Command("run", "Run a test group or OONI Run link")
nettestGroup := cmd.Arg("name", "the nettest group to run").Required().String()
cmd.Action(func(_ *kingpin.ParseContext) error {
log.Infof("Starting %s", *nettestGroup)
2018-03-23 12:10:14 +01:00
ctx, err := root.Init()
2018-02-12 16:45:13 +01:00
if err != nil {
log.Errorf("%s", err)
return err
}
group, ok := groups.NettestGroups[*nettestGroup]
if !ok {
log.Errorf("No test group named %s", *nettestGroup)
return errors.New("invalid test group name")
}
2018-02-13 17:11:22 +01:00
log.Debugf("Running test group %s", group.Label)
2018-02-13 16:16:23 +01:00
err = ctx.MaybeLocationLookup()
if err != nil {
log.WithError(err).Error("Failed to lookup the location of the probe")
return err
}
2018-03-23 12:10:14 +01:00
result, err := database.CreateResult(ctx.DB, ctx.Home, database.Result{
Name: *nettestGroup,
StartTime: time.Now().UTC(),
Country: ctx.Location.CountryCode,
NetworkName: ctx.Location.NetworkName,
ASN: fmt.Sprintf("%d", ctx.Location.ASN),
2018-02-13 17:11:22 +01:00
})
if err != nil {
2018-03-08 11:53:04 +01:00
log.Errorf("DB result error: %s", err)
2018-02-13 17:11:22 +01:00
return err
}
for _, nt := range group.Nettests {
2018-03-14 14:44:37 +01:00
log.Debugf("Running test %T", nt)
msmtPath := filepath.Join(ctx.TempDir,
2018-03-19 19:28:32 +01:00
fmt.Sprintf("msmt-%T-%s.jsonl", nt,
time.Now().UTC().Format(time.RFC3339Nano)))
ctl := nettests.NewController(nt, ctx, result, msmtPath)
if err = nt.Run(ctl); err != nil {
2018-03-08 13:46:21 +01:00
log.WithError(err).Errorf("Failed to run %s", group.Label)
return err
}
2018-02-13 17:11:22 +01:00
}
if err = result.Finished(ctx.DB, group.Summary); err != nil {
return err
}
return nil
})
}