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.

114 lines
2.8 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. var err error
  34. p2pMode, err := cmd.Flags().GetString("p2p")
  35. if err != nil {
  36. return err
  37. }
  38. switch mode := P2PMode(p2pMode); mode {
  39. case NewP2PMode, LegacyP2PMode, HybridP2PMode, MixedP2PMode:
  40. cli.opts.P2P = mode
  41. default:
  42. return fmt.Errorf("p2p mode must be either new, legacy, hybrid or mixed got %s", p2pMode)
  43. }
  44. return cli.generate()
  45. },
  46. }
  47. cli.root.PersistentFlags().StringVarP(&cli.opts.Directory, "dir", "d", "", "Output directory for manifests")
  48. _ = cli.root.MarkPersistentFlagRequired("dir")
  49. cli.root.Flags().BoolVarP(&cli.opts.Reverse, "reverse", "r", false, "Reverse sort order")
  50. cli.root.PersistentFlags().IntVarP(&cli.opts.NumGroups, "groups", "g", 0, "Number of groups")
  51. cli.root.PersistentFlags().StringP("p2p", "p", string(MixedP2PMode),
  52. "P2P typology to be generated [\"new\", \"legacy\", \"hybrid\" or \"mixed\" ]")
  53. cli.root.PersistentFlags().IntVarP(&cli.opts.MinNetworkSize, "min-size", "", 1,
  54. "Minimum network size (nodes)")
  55. cli.root.PersistentFlags().IntVarP(&cli.opts.MaxNetworkSize, "max-size", "", 0,
  56. "Maxmum network size (nodes), 0 is unlimited")
  57. return cli
  58. }
  59. // generate generates manifests in a directory.
  60. func (cli *CLI) generate() error {
  61. err := os.MkdirAll(cli.opts.Directory, 0755)
  62. if err != nil {
  63. return err
  64. }
  65. manifests, err := Generate(rand.New(rand.NewSource(randomSeed)), cli.opts)
  66. if err != nil {
  67. return err
  68. }
  69. switch {
  70. case cli.opts.NumGroups <= 0:
  71. e2e.SortManifests(manifests, cli.opts.Reverse)
  72. if err := e2e.WriteManifests(filepath.Join(cli.opts.Directory, "gen"), manifests); err != nil {
  73. return err
  74. }
  75. default:
  76. groupManifests := e2e.SplitGroups(cli.opts.NumGroups, manifests)
  77. for idx, gm := range groupManifests {
  78. e2e.SortManifests(gm, cli.opts.Reverse)
  79. prefix := filepath.Join(cli.opts.Directory, fmt.Sprintf("gen-group%02d", idx))
  80. if err := e2e.WriteManifests(prefix, gm); err != nil {
  81. return err
  82. }
  83. }
  84. }
  85. return nil
  86. }
  87. // Run runs the CLI.
  88. func (cli *CLI) Run() {
  89. if err := cli.root.Execute(); err != nil {
  90. logger.Error(err.Error())
  91. os.Exit(1)
  92. }
  93. }