Start adding support for writing measurements to disk

This commit is contained in:
Arturo Filastò 2018-03-19 13:20:42 +01:00
parent efb7e87d1b
commit 90c1c2de87
4 changed files with 52 additions and 18 deletions

View File

@ -37,15 +37,15 @@ func init() {
} }
Init = func() (*ooni.Config, *ooni.Context, error) { Init = func() (*ooni.Config, *ooni.Context, error) {
var c *ooni.Config var config *ooni.Config
var err error var err error
if *configPath != "" { if *configPath != "" {
log.Debugf("Reading config file from %s", *configPath) log.Debugf("Reading config file from %s", *configPath)
c, err = ooni.ReadConfig(*configPath) config, err = ooni.ReadConfig(*configPath)
} else { } else {
log.Debug("Reading default config file") log.Debug("Reading default config file")
c, err = ooni.ReadDefaultConfigPaths() config, err = ooni.ReadDefaultConfigPaths()
} }
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@ -62,9 +62,13 @@ func init() {
return nil, nil, err return nil, nil, err
} }
o := ooni.New(c, db) ctx := ooni.New(config, db)
o.Init() err = ctx.Init()
return c, o, nil if err != nil {
return nil, nil, err
}
return config, ctx, nil
} }
return nil return nil

View File

@ -1,6 +1,8 @@
package run package run
import ( import (
"fmt"
"path/filepath"
"time" "time"
"github.com/alecthomas/kingpin" "github.com/alecthomas/kingpin"
@ -37,7 +39,11 @@ func init() {
for _, nt := range group.Nettests { for _, nt := range group.Nettests {
log.Debugf("Running test %T", nt) log.Debugf("Running test %T", nt)
ctl := nettests.NewController(ctx, result) msmtPath := filepath.Join(ctx.TempDir,
fmt.Sprintf("msmt-%s-%T.jsonl", nt,
time.Now().UTC().Format(time.RFC3339Nano)))
ctl := nettests.NewController(ctx, result, msmtPath)
if err := nt.Run(ctl); err != nil { if err := nt.Run(ctl); err != nil {
log.WithError(err).Errorf("Failed to run %s", group.Label) log.WithError(err).Errorf("Failed to run %s", group.Label)
return err return err
@ -47,6 +53,7 @@ func init() {
// 2. Link the measurement to the Result (this should probably happen in // 2. Link the measurement to the Result (this should probably happen in
// the nettest class) // the nettest class)
// 3. Update the summary of the result and the other metadata in the db // 3. Update the summary of the result and the other metadata in the db
// 4. Move the msmtPath into the final location ~/.ooni/msmts/
} }
// result.Update(ctx.DB) // result.Update(ctx.DB)
return nil return nil

View File

@ -23,22 +23,27 @@ type NettestGroup struct {
} }
// NewController creates a nettest controller // NewController creates a nettest controller
func NewController(ctx *ooni.Context, res *database.Result) *Controller { func NewController(ctx *ooni.Context, res *database.Result, msmtPath string) *Controller {
return &Controller{ return &Controller{
ctx, ctx,
res, res,
msmtPath,
} }
} }
// Controller is passed to the run method of every Nettest // Controller is passed to the run method of every Nettest
// each nettest instance has one controller
type Controller struct { type Controller struct {
Ctx *ooni.Context Ctx *ooni.Context
res *database.Result res *database.Result
msmtPath string
} }
// Init should be called once to initialise the nettest // Init should be called once to initialise the nettest
func (c *Controller) Init(nt *mk.Nettest) { func (c *Controller) Init(nt *mk.Nettest) error {
log.Debugf("Init: %v", nt) log.Debugf("Init: %v", nt)
log.Debugf("OutputPath: %s", c.msmtPath)
nt.Options = mk.NettestOptions{ nt.Options = mk.NettestOptions{
IncludeIP: c.Ctx.Config.Sharing.IncludeIP, IncludeIP: c.Ctx.Config.Sharing.IncludeIP,
IncludeASN: c.Ctx.Config.Sharing.IncludeASN, IncludeASN: c.Ctx.Config.Sharing.IncludeASN,
@ -50,9 +55,10 @@ func (c *Controller) Init(nt *mk.Nettest) {
// XXX // XXX
GeoIPCountryPath: "", GeoIPCountryPath: "",
GeoIPASNPath: "", GeoIPASNPath: "",
OutputPath: "/tmp/measurement.jsonl", OutputPath: c.msmtPath,
CaBundlePath: "/etc/ssl/cert.pem", CaBundlePath: "/etc/ssl/cert.pem",
} }
nt.On("log", func(e mk.Event) { nt.On("log", func(e mk.Event) {
level := e.Value["verbosity"].(string) level := e.Value["verbosity"].(string)
msg := e.Value["message"].(string) msg := e.Value["message"].(string)
@ -106,6 +112,11 @@ func (c *Controller) Init(nt *mk.Nettest) {
c.OnEntry(e.Value["json_str"].(string)) c.OnEntry(e.Value["json_str"].(string))
}) })
nt.On("end", func(e mk.Event) {
c.OnEntry(e.Value["json_str"].(string))
})
return nil
} }
// OnProgress should be called when a new progress event is available. // OnProgress should be called when a new progress event is available.

24
ooni.go
View File

@ -34,8 +34,9 @@ func Onboarding(c *Config) error {
// Context for OONI Probe // Context for OONI Probe
type Context struct { type Context struct {
Config *Config Config *Config
DB *sqlx.DB DB *sqlx.DB
TempDir string
} }
// Init the OONI manager // Init the OONI manager
@ -48,6 +49,13 @@ func (c *Context) Init() error {
return errors.Wrap(err, "onboarding") return errors.Wrap(err, "onboarding")
} }
} }
tempDir, err := ioutil.TempDir("", "ooni")
if err != nil {
return errors.Wrap(err, "creating TempDir")
}
c.TempDir = tempDir
return nil return nil
} }
@ -154,12 +162,16 @@ func EnsureDefaultOONIHomeDir() (string, error) {
return "", err return "", err
} }
if _, e := os.Stat(filepath.Join(home, "db")); e != nil { requiredDirs := []string{"db", "msmts"}
err = os.MkdirAll(filepath.Join(home, "db"), 0700) for _, d := range requiredDirs {
if err != nil { if _, e := os.Stat(filepath.Join(home, d)); e != nil {
return "", err err = os.MkdirAll(filepath.Join(home, d), 0700)
if err != nil {
return "", err
}
} }
} }
return home, nil return home, nil
} }