-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
103 lines (82 loc) · 2.79 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"context"
"os"
"os/signal"
"syscall"
"github.com/araskachoi/go-libp2p-pubsub-benchmark-tools/pkg/logger"
"github.com/araskachoi/go-libp2p-pubsub-benchmark-tools/pkg/orchestra"
"github.com/araskachoi/go-libp2p-pubsub-benchmark-tools/pkg/orchestra/config"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func setup() *cobra.Command {
var (
confLoc, msgLoc, loggerLoc string
)
rootCmd := &cobra.Command{
Use: "start",
Short: "Orchestrate a test run of clients and optionally a subnet",
Long: `Spins up clients and optionally a subnet and sends the hosts messages at the specified interval.`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := logger.Set(logger.ContextHook{}, loggerLoc, true, false); err != nil {
logrus.Errorf("err initiating logger:\n%v", err)
return err
}
logger.Infof("Loading config: %s", confLoc)
conf, err := config.Load(confLoc)
if err != nil {
logger.Errorf("error loading config\n%v", err)
return err
}
logger.Infof("Loaded configuration. Starting host.\n%v", conf)
// check the logger location in the conf file
if conf.General.LoggerLocation != "" {
switch loggerLoc {
case conf.General.LoggerLocation:
break
case "":
logger.Warnf("logs will now be written to %s", conf.General.LoggerLocation)
if err = logger.SetLoggerLoc(conf.General.LoggerLocation); err != nil {
logger.Errorf("err setting log location to %s:\n%v", conf.General.LoggerLocation, err)
return err
}
break
default:
logger.Warnf("log location confliction between flag (%s) and config file (%s); defering to flag (%s)", loggerLoc, conf.General.LoggerLocation, loggerLoc)
}
}
logger.SetLoggerInfo()
// capture the ctrl+c signal
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGINT)
// create a context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
orch, err := orchestra.New(orchestra.Props{
Conf: conf,
CTX: ctx,
})
if err != nil {
logger.Errorf("err building new test orchestra:\n%v", err)
return err
}
if err = orch.Orchestrate(stop); err != nil {
logger.Errorf("err running test orchestration:\n%v", err)
return err
}
return nil
},
}
rootCmd.PersistentFlags().StringVarP(&confLoc, "config", "c", "configs/orchestra/config.json", "The configuration file.")
rootCmd.PersistentFlags().StringVarP(&loggerLoc, "log", "", "", "Log file location. Defaults to standard out.")
rootCmd.Flags().StringVarP(&msgLoc, "message", "m", "client.message.json", "The message file to send to peers.")
return rootCmd
}
func main() {
rootCmd := setup()
if err := rootCmd.Execute(); err != nil {
logrus.Fatalf("err executing command\n%v", err)
}
logger.Info("done")
}