wizard_intro.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "bufio"
  19. "encoding/json"
  20. "fmt"
  21. "io/ioutil"
  22. "os"
  23. "path/filepath"
  24. "strings"
  25. "sync"
  26. "github.com/ethereum/go-ethereum/log"
  27. )
  28. // makeWizard creates and returns a new puppeth wizard.
  29. func makeWizard(network string) *wizard {
  30. return &wizard{
  31. network: network,
  32. conf: config{
  33. Servers: make(map[string][]byte),
  34. },
  35. servers: make(map[string]*sshClient),
  36. services: make(map[string][]string),
  37. in: bufio.NewReader(os.Stdin),
  38. }
  39. }
  40. // run displays some useful infos to the user, starting on the journey of
  41. // setting up a new or managing an existing Ethereum private network.
  42. func (w *wizard) run() {
  43. fmt.Println("+-----------------------------------------------------------+")
  44. fmt.Println("| Welcome to puppeth, your Ethereum private network manager |")
  45. fmt.Println("| |")
  46. fmt.Println("| This tool lets you create a new Ethereum network down to |")
  47. fmt.Println("| the genesis block, bootnodes, miners and ethstats servers |")
  48. fmt.Println("| without the hassle that it would normally entail. |")
  49. fmt.Println("| |")
  50. fmt.Println("| Puppeth uses SSH to dial in to remote servers, and builds |")
  51. fmt.Println("| its network components out of Docker containers using the |")
  52. fmt.Println("| docker-compose toolset. |")
  53. fmt.Println("+-----------------------------------------------------------+")
  54. fmt.Println()
  55. // Make sure we have a good network name to work with fmt.Println()
  56. if w.network == "" {
  57. fmt.Println("Please specify a network name to administer (no spaces, please)")
  58. for {
  59. w.network = w.readString()
  60. if !strings.Contains(w.network, " ") {
  61. fmt.Printf("\nSweet, you can set this via --network=%s next time!\n\n", w.network)
  62. break
  63. }
  64. log.Error("I also like to live dangerously, still no spaces")
  65. }
  66. }
  67. log.Info("Administering Ethereum network", "name", w.network)
  68. // Load initial configurations and connect to all live servers
  69. w.conf.path = filepath.Join(os.Getenv("HOME"), ".puppeth", w.network)
  70. blob, err := ioutil.ReadFile(w.conf.path)
  71. if err != nil {
  72. log.Warn("No previous configurations found", "path", w.conf.path)
  73. } else if err := json.Unmarshal(blob, &w.conf); err != nil {
  74. log.Crit("Previous configuration corrupted", "path", w.conf.path, "err", err)
  75. } else {
  76. // Dial all previously known servers concurrently
  77. var pend sync.WaitGroup
  78. for server, pubkey := range w.conf.Servers {
  79. pend.Add(1)
  80. go func(server string, pubkey []byte) {
  81. defer pend.Done()
  82. log.Info("Dialing previously configured server", "server", server)
  83. client, err := dial(server, pubkey)
  84. if err != nil {
  85. log.Error("Previous server unreachable", "server", server, "err", err)
  86. }
  87. w.lock.Lock()
  88. w.servers[server] = client
  89. w.lock.Unlock()
  90. }(server, pubkey)
  91. }
  92. pend.Wait()
  93. w.networkStats()
  94. }
  95. // Basics done, loop ad infinitum about what to do
  96. for {
  97. fmt.Println()
  98. fmt.Println("What would you like to do? (default = stats)")
  99. fmt.Println(" 1. Show network stats")
  100. if w.conf.Genesis == nil {
  101. fmt.Println(" 2. Configure new genesis")
  102. } else {
  103. fmt.Println(" 2. Manage existing genesis")
  104. }
  105. if len(w.servers) == 0 {
  106. fmt.Println(" 3. Track new remote server")
  107. } else {
  108. fmt.Println(" 3. Manage tracked machines")
  109. }
  110. if len(w.services) == 0 {
  111. fmt.Println(" 4. Deploy network components")
  112. } else {
  113. fmt.Println(" 4. Manage network components")
  114. }
  115. choice := w.read()
  116. switch {
  117. case choice == "" || choice == "1":
  118. w.networkStats()
  119. case choice == "2":
  120. if w.conf.Genesis == nil {
  121. w.makeGenesis()
  122. } else {
  123. w.manageGenesis()
  124. }
  125. case choice == "3":
  126. if len(w.servers) == 0 {
  127. if w.makeServer() != "" {
  128. w.networkStats()
  129. }
  130. } else {
  131. w.manageServers()
  132. }
  133. case choice == "4":
  134. if len(w.services) == 0 {
  135. w.deployComponent()
  136. } else {
  137. w.manageComponents()
  138. }
  139. default:
  140. log.Error("That's not something I can do")
  141. }
  142. }
  143. }