You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.3 KiB

  1. //nolint: gosec
  2. package main
  3. import (
  4. "fmt"
  5. "math/rand"
  6. "os"
  7. "path/filepath"
  8. "github.com/spf13/cobra"
  9. "github.com/tendermint/tendermint/libs/log"
  10. e2e "github.com/tendermint/tendermint/test/e2e/pkg"
  11. )
  12. const (
  13. randomSeed int64 = 4827085738
  14. )
  15. var logger = log.MustNewDefaultLogger(log.LogFormatPlain, log.LogLevelInfo, false)
  16. func main() {
  17. NewCLI().Run()
  18. }
  19. // CLI is the Cobra-based command-line interface.
  20. type CLI struct {
  21. root *cobra.Command
  22. opts Options
  23. }
  24. // NewCLI sets up the CLI.
  25. func NewCLI() *CLI {
  26. cli := &CLI{}
  27. cli.root = &cobra.Command{
  28. Use: "generator",
  29. Short: "End-to-end testnet generator",
  30. SilenceUsage: true,
  31. SilenceErrors: true, // we'll output them ourselves in Run()
  32. RunE: func(cmd *cobra.Command, args []string) error {
  33. return cli.generate()
  34. },
  35. }
  36. cli.root.PersistentFlags().StringVarP(&cli.opts.Directory, "dir", "d", "", "Output directory for manifests")
  37. _ = cli.root.MarkPersistentFlagRequired("dir")
  38. cli.root.Flags().BoolVarP(&cli.opts.Reverse, "reverse", "r", false, "Reverse sort order")
  39. cli.root.PersistentFlags().IntVarP(&cli.opts.NumGroups, "groups", "g", 0, "Number of groups")
  40. cli.root.PersistentFlags().IntVarP(&cli.opts.MinNetworkSize, "min-size", "", 1,
  41. "Minimum network size (nodes)")
  42. cli.root.PersistentFlags().IntVarP(&cli.opts.MaxNetworkSize, "max-size", "", 0,
  43. "Maxmum network size (nodes), 0 is unlimited")
  44. return cli
  45. }
  46. // generate generates manifests in a directory.
  47. func (cli *CLI) generate() error {
  48. err := os.MkdirAll(cli.opts.Directory, 0755)
  49. if err != nil {
  50. return err
  51. }
  52. manifests, err := Generate(rand.New(rand.NewSource(randomSeed)), cli.opts)
  53. if err != nil {
  54. return err
  55. }
  56. switch {
  57. case cli.opts.NumGroups <= 0:
  58. e2e.SortManifests(manifests, cli.opts.Reverse)
  59. if err := e2e.WriteManifests(filepath.Join(cli.opts.Directory, "gen"), manifests); err != nil {
  60. return err
  61. }
  62. default:
  63. groupManifests := e2e.SplitGroups(cli.opts.NumGroups, manifests)
  64. for idx, gm := range groupManifests {
  65. e2e.SortManifests(gm, cli.opts.Reverse)
  66. prefix := filepath.Join(cli.opts.Directory, fmt.Sprintf("gen-group%02d", idx))
  67. if err := e2e.WriteManifests(prefix, gm); err != nil {
  68. return err
  69. }
  70. }
  71. }
  72. return nil
  73. }
  74. // Run runs the CLI.
  75. func (cli *CLI) Run() {
  76. if err := cli.root.Execute(); err != nil {
  77. logger.Error(err.Error())
  78. os.Exit(1)
  79. }
  80. }