2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-12-11 18:24:31 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-01-01 09:41:27 +00:00
|
|
|
"log"
|
2018-12-11 18:24:31 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2020-02-18 23:31:25 +00:00
|
|
|
"github.com/BurntSushi/toml"
|
2018-12-11 18:24:31 +00:00
|
|
|
"github.com/spf13/cobra"
|
2019-01-01 09:41:27 +00:00
|
|
|
"github.com/zeebo/errs"
|
2018-12-11 18:24:31 +00:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/fpath"
|
2019-01-01 09:41:27 +00:00
|
|
|
"storj.io/storj/cmd/statreceiver/luacfg"
|
2018-12-11 18:24:31 +00:00
|
|
|
"storj.io/storj/pkg/cfgstruct"
|
|
|
|
"storj.io/storj/pkg/process"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the set of configuration values we care about
|
|
|
|
var Config struct {
|
2020-02-18 23:31:25 +00:00
|
|
|
Input string `default:"" help:"path to configuration file"`
|
|
|
|
DowngradeMetrics string `default:"" help:"path to downgrade metrics toml file"`
|
2018-12-11 18:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2019-01-01 09:41:27 +00:00
|
|
|
defaultConfDir := fpath.ApplicationDir("storj", "statreceiver")
|
|
|
|
|
2018-12-11 18:24:31 +00:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: os.Args[0],
|
|
|
|
Short: "stat receiving",
|
|
|
|
RunE: Main,
|
|
|
|
}
|
2019-04-19 19:17:30 +01:00
|
|
|
defaults := cfgstruct.DefaultsFlag(cmd)
|
Command line flags features and cleanup (#2068)
* change BindSetup to be an option to Bind
* add process.Bind to allow composite structures
* hack fix for noprefix flags
* used tagged version of structs
Before this PR, some flags were created by calling `cfgstruct.Bind` and having their fields create a flag. Once the flags were parsed, `viper` was used to acquire all the values from them and config files, and the fields in the struct were set through the flag interface.
This doesn't work for slices of things on config structs very well, since it can only set strings, and for a string slice, it turns out that the implementation in `pflag` appends an entry rather than setting it.
This changes three things:
1. Only have a `Bind` call instead of `Bind` and `BindSetup`, and make `BindSetup` an option instead.
2. Add a `process.Bind` call that takes in a `*cobra.Cmd`, binds the struct to the command's flags, and keeps track of that struct in a global map keyed by the command.
3. Use `viper` to get the values and load them into the bound configuration structs instead of using the flags to propagate the changes.
In this way, we can support whatever rich configuration we want in the config yaml files, while still getting command like flags when important.
2019-05-29 18:56:22 +01:00
|
|
|
process.Bind(cmd, &Config, defaults, cfgstruct.ConfDir(defaultConfDir))
|
2019-01-01 09:41:27 +00:00
|
|
|
cmd.Flags().String("config", filepath.Join(defaultConfDir, "config.yaml"), "path to configuration")
|
2018-12-11 18:24:31 +00:00
|
|
|
process.Exec(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Main is the real main method
|
|
|
|
func Main(cmd *cobra.Command, args []string) error {
|
|
|
|
var input io.Reader
|
|
|
|
switch Config.Input {
|
|
|
|
case "":
|
|
|
|
return fmt.Errorf("--input path to script is required")
|
|
|
|
case "stdin":
|
|
|
|
input = os.Stdin
|
|
|
|
default:
|
2019-01-01 09:41:27 +00:00
|
|
|
inputFile, err := os.Open(Config.Input)
|
2018-12-11 18:24:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-01 09:41:27 +00:00
|
|
|
defer func() {
|
|
|
|
if err := inputFile.Close(); err != nil {
|
|
|
|
log.Printf("Failed to close input: %scope", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
input = inputFile
|
2018-12-11 18:24:31 +00:00
|
|
|
}
|
|
|
|
|
2020-02-18 23:31:25 +00:00
|
|
|
if Config.DowngradeMetrics == "" {
|
|
|
|
return fmt.Errorf("--downgrade-metrics toml path required")
|
|
|
|
}
|
|
|
|
if _, err := toml.DecodeFile(Config.DowngradeMetrics, &knownMetrics); err != nil {
|
|
|
|
return fmt.Errorf("failed to decode toml file: %s", err)
|
|
|
|
}
|
|
|
|
|
2019-01-01 09:41:27 +00:00
|
|
|
scope := luacfg.NewScope()
|
|
|
|
err := errs.Combine(
|
|
|
|
scope.RegisterVal("deliver", Deliver),
|
|
|
|
scope.RegisterVal("filein", NewFileSource),
|
|
|
|
scope.RegisterVal("fileout", NewFileDest),
|
|
|
|
scope.RegisterVal("udpin", NewUDPSource),
|
|
|
|
scope.RegisterVal("udpout", NewUDPDest),
|
|
|
|
scope.RegisterVal("parse", NewParser),
|
|
|
|
scope.RegisterVal("print", NewPrinter),
|
|
|
|
scope.RegisterVal("pcopy", NewPacketCopier),
|
|
|
|
scope.RegisterVal("mcopy", NewMetricCopier),
|
|
|
|
scope.RegisterVal("pbuf", NewPacketBuffer),
|
|
|
|
scope.RegisterVal("mbuf", NewMetricBuffer),
|
|
|
|
scope.RegisterVal("packetfilter", NewPacketFilter),
|
|
|
|
scope.RegisterVal("appfilter", NewApplicationFilter),
|
|
|
|
scope.RegisterVal("instfilter", NewInstanceFilter),
|
|
|
|
scope.RegisterVal("keyfilter", NewKeyFilter),
|
|
|
|
scope.RegisterVal("sanitize", NewSanitizer),
|
|
|
|
scope.RegisterVal("graphite", NewGraphiteDest),
|
2019-11-08 20:40:39 +00:00
|
|
|
scope.RegisterVal("influx", NewInfluxDest),
|
2019-01-01 09:41:27 +00:00
|
|
|
scope.RegisterVal("db", NewDBDest),
|
|
|
|
scope.RegisterVal("pbufprep", NewPacketBufPrep),
|
|
|
|
scope.RegisterVal("mbufprep", NewMetricBufPrep),
|
2020-01-10 15:53:14 +00:00
|
|
|
scope.RegisterVal("downgrade", NewMetricDowngrade),
|
|
|
|
scope.RegisterVal("versionsplit", NewVersionSplit),
|
2018-12-11 18:24:31 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-01 09:41:27 +00:00
|
|
|
|
|
|
|
err = scope.Run(input)
|
2018-12-11 18:24:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
select {}
|
|
|
|
}
|