wizard_intro.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. // Docker accepts hyphens in image names, but doesn't like it for container names
  57. if w.network == "" {
  58. fmt.Println("Please specify a network name to administer (no spaces, hyphens or capital letters please)")
  59. for {
  60. w.network = w.readString()
  61. if !strings.Contains(w.network, " ") && !strings.Contains(w.network, "-") && strings.ToLower(w.network) == w.network {
  62. fmt.Printf("\nSweet, you can set this via --network=%s next time!\n\n", w.network)
  63. break
  64. }
  65. log.Error("I also like to live dangerously, still no spaces, hyphens or capital letters")
  66. }
  67. }
  68. log.Info("Administering Ethereum network", "name", w.network)
  69. // Load initial configurations and connect to all live servers
  70. w.conf.path = filepath.Join(os.Getenv("HOME"), ".puppeth", w.network)
  71. blob, err := ioutil.ReadFile(w.conf.path)
  72. if err != nil {
  73. log.Warn("No previous configurations found", "path", w.conf.path)
  74. } else if err := json.Unmarshal(blob, &w.conf); err != nil {
  75. log.Crit("Previous configuration corrupted", "path", w.conf.path, "err", err)
  76. } else {
  77. // Dial all previously known servers concurrently
  78. var pend sync.WaitGroup
  79. for server, pubkey := range w.conf.Servers {
  80. pend.Add(1)
  81. go func(server string, pubkey []byte) {
  82. defer pend.Done()
  83. log.Info("Dialing previously configured server", "server", server)
  84. client, err := dial(server, pubkey)
  85. if err != nil {
  86. log.Error("Previous server unreachable", "server", server, "err", err)
  87. }
  88. w.lock.Lock()
  89. w.servers[server] = client
  90. w.lock.Unlock()
  91. }(server, pubkey)
  92. }
  93. pend.Wait()
  94. w.networkStats()
  95. }
  96. // Basics done, loop ad infinitum about what to do
  97. for {
  98. fmt.Println()
  99. fmt.Println("What would you like to do? (default = stats)")
  100. fmt.Println(" 1. Show network stats")
  101. if w.conf.Genesis == nil {
  102. fmt.Println(" 2. Configure new genesis")
  103. } else {
  104. fmt.Println(" 2. Manage existing genesis")
  105. }
  106. if len(w.servers) == 0 {
  107. fmt.Println(" 3. Track new remote server")
  108. } else {
  109. fmt.Println(" 3. Manage tracked machines")
  110. }
  111. if len(w.services) == 0 {
  112. fmt.Println(" 4. Deploy network components")
  113. } else {
  114. fmt.Println(" 4. Manage network components")
  115. }
  116. choice := w.read()
  117. switch {
  118. case choice == "" || choice == "1":
  119. w.networkStats()
  120. case choice == "2":
  121. if w.conf.Genesis == nil {
  122. fmt.Println()
  123. fmt.Println("What would you like to do? (default = create)")
  124. fmt.Println(" 1. Create new genesis from scratch")
  125. fmt.Println(" 2. Import already existing genesis")
  126. choice := w.read()
  127. switch {
  128. case choice == "" || choice == "1":
  129. w.makeGenesis()
  130. case choice == "2":
  131. w.importGenesis()
  132. default:
  133. log.Error("That's not something I can do")
  134. }
  135. } else {
  136. w.manageGenesis()
  137. }
  138. case choice == "3":
  139. if len(w.servers) == 0 {
  140. if w.makeServer() != "" {
  141. w.networkStats()
  142. }
  143. } else {
  144. w.manageServers()
  145. }
  146. case choice == "4":
  147. if len(w.services) == 0 {
  148. w.deployComponent()
  149. } else {
  150. w.manageComponents()
  151. }
  152. default:
  153. log.Error("That's not something I can do")
  154. }
  155. }
  156. }