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.

154 lines
3.4 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "strconv"
  8. "strings"
  9. "github.com/tendermint/netmon/handlers"
  10. "github.com/tendermint/netmon/types"
  11. "github.com/codegangsta/cli"
  12. . "github.com/tendermint/go-common"
  13. cfg "github.com/tendermint/go-config"
  14. pcm "github.com/tendermint/go-process"
  15. "github.com/tendermint/go-rpc/server"
  16. tmcfg "github.com/tendermint/tendermint/config/tendermint"
  17. )
  18. func init() {
  19. config := tmcfg.GetConfig("")
  20. config.Set("log_level", "debug")
  21. cfg.ApplyConfig(config) // Notify modules of new config
  22. }
  23. func main() {
  24. app := cli.NewApp()
  25. app.Name = "netmon"
  26. app.Usage = "netmon [command] [args...]"
  27. app.Commands = []cli.Command{
  28. {
  29. Name: "config",
  30. Usage: "Create a config from a mintnet testnet",
  31. ArgsUsage: "[chainID] [prefix] [N]",
  32. Action: func(c *cli.Context) {
  33. cmdConfig(c)
  34. },
  35. },
  36. {
  37. Name: "monitor",
  38. Usage: "Monitor a chain",
  39. ArgsUsage: "[config file]",
  40. Action: func(c *cli.Context) {
  41. cmdMonitor(c)
  42. },
  43. },
  44. }
  45. app.Run(os.Args)
  46. }
  47. func cmdMonitor(c *cli.Context) {
  48. args := c.Args()
  49. if len(args) != 1 {
  50. Exit("monitor expectes 1 arg")
  51. }
  52. chainConfigFile := args[0]
  53. chainConfig, err := types.LoadChainFromFile(chainConfigFile)
  54. if err != nil {
  55. Exit(err.Error())
  56. }
  57. // the main object that watches for changes and serves the rpc requests
  58. network := handlers.NewTendermintNetwork()
  59. network.RegisterChain(chainConfig)
  60. // the routes are functions on the network object
  61. routes := handlers.Routes(network)
  62. // serve http and ws
  63. mux := http.NewServeMux()
  64. wm := rpcserver.NewWebsocketManager(routes, nil) // TODO: evsw
  65. mux.HandleFunc("/websocket", wm.WebsocketHandler)
  66. rpcserver.RegisterRPCFuncs(mux, routes)
  67. if _, err := rpcserver.StartHTTPServer("0.0.0.0:46670", mux); err != nil {
  68. Exit(err.Error())
  69. }
  70. TrapSignal(func() {
  71. network.Stop()
  72. })
  73. }
  74. func cmdConfig(c *cli.Context) {
  75. args := c.Args()
  76. if len(args) != 3 {
  77. Exit("config expects 3 args")
  78. }
  79. id, prefix := args[0], args[1]
  80. n, err := strconv.Atoi(args[2])
  81. if err != nil {
  82. Exit(err.Error())
  83. }
  84. chain, err := ConfigFromMachines(id, prefix, n)
  85. if err != nil {
  86. Exit(err.Error())
  87. }
  88. b, err := json.Marshal(chain)
  89. if err != nil {
  90. Exit(err.Error())
  91. }
  92. fmt.Println(string(b))
  93. }
  94. func ConfigFromMachines(chainID, prefix string, N int) (*types.BlockchainConfig, error) {
  95. chain := &types.BlockchainConfig{
  96. ID: chainID,
  97. Validators: make([]*types.ChainValidator, N),
  98. }
  99. for i := 0; i < N; i++ {
  100. id := fmt.Sprintf("%s%d", prefix, i+1)
  101. ip, success := runProcessGetResult(id+"-ip", "docker-machine", []string{"ip", id})
  102. if !success {
  103. return nil, fmt.Errorf(ip)
  104. }
  105. val := &types.Validator{
  106. ID: id,
  107. // TODO: pubkey
  108. }
  109. chainVal := &types.ChainValidator{
  110. Validator: val,
  111. Addr: fmt.Sprintf("%s:%d", strings.Trim(ip, "\n"), 46657),
  112. Index: i,
  113. }
  114. chain.Validators[i] = chainVal
  115. }
  116. return chain, nil
  117. }
  118. func runProcessGetResult(label string, command string, args []string) (string, bool) {
  119. outFile := NewBufferCloser(nil)
  120. fmt.Println(Green(command), Green(args))
  121. proc, err := pcm.StartProcess(label, command, args, nil, outFile)
  122. if err != nil {
  123. return "", false
  124. }
  125. <-proc.WaitCh
  126. if proc.ExitState.Success() {
  127. fmt.Println(Blue(string(outFile.Bytes())))
  128. return string(outFile.Bytes()), true
  129. } else {
  130. // Error!
  131. fmt.Println(Red(string(outFile.Bytes())))
  132. return string(outFile.Bytes()), false
  133. }
  134. }