wizard_intro.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. "encoding/json"
  19. "fmt"
  20. "os"
  21. "path/filepath"
  22. "strings"
  23. "github.com/ethereum/go-ethereum/log"
  24. )
  25. // makeWizard creates and returns a new puppeth wizard.
  26. func makeWizard(network string) *wizard {
  27. return &wizard{
  28. network: network,
  29. conf: config{
  30. Servers: make(map[string][]byte),
  31. },
  32. servers: make(map[string]*sshClient),
  33. services: make(map[string][]string),
  34. }
  35. }
  36. // run displays some useful infos to the user, starting on the journey of
  37. // setting up a new or managing an existing Ethereum private network.
  38. func (w *wizard) run() {
  39. fmt.Println("+-----------------------------------------------------------+")
  40. fmt.Println("| Welcome to puppeth, your Ethereum private network manager |")
  41. fmt.Println("| |")
  42. fmt.Println("| This tool lets you create a new Ethereum network down to |")
  43. fmt.Println("| the genesis block, bootnodes, miners and ethstats servers |")
  44. fmt.Println("| without the hassle that it would normally entail. |")
  45. fmt.Println("| |")
  46. fmt.Println("| Puppeth uses SSH to dial in to remote servers, and builds |")
  47. fmt.Println("| its network components out of Docker containers using the |")
  48. fmt.Println("| docker-compose toolset. |")
  49. fmt.Println("+-----------------------------------------------------------+")
  50. fmt.Println()
  51. // Make sure we have a good network name to work with fmt.Println()
  52. // Docker accepts hyphens in image names, but doesn't like it for container names
  53. if w.network == "" {
  54. fmt.Println("Please specify a network name to administer (no spaces, hyphens or capital letters please)")
  55. for {
  56. w.network = w.readString()
  57. if !strings.Contains(w.network, " ") && !strings.Contains(w.network, "-") && strings.ToLower(w.network) == w.network {
  58. fmt.Printf("\nSweet, you can set this via --network=%s next time!\n\n", w.network)
  59. break
  60. }
  61. log.Error("I also like to live dangerously, still no spaces, hyphens or capital letters")
  62. }
  63. }
  64. log.Info("Administering Ethereum network", "name", w.network)
  65. // Load initial configurations and connect to all live servers
  66. w.conf.path = filepath.Join(os.Getenv("HOME"), ".puppeth", w.network)
  67. blob, err := os.ReadFile(w.conf.path)
  68. if err != nil {
  69. log.Warn("No previous configurations found", "path", w.conf.path)
  70. } else if err := json.Unmarshal(blob, &w.conf); err != nil {
  71. log.Crit("Previous configuration corrupted", "path", w.conf.path, "err", err)
  72. } else {
  73. // Dial all previously known servers
  74. for server, pubkey := range w.conf.Servers {
  75. log.Info("Dialing previously configured server", "server", server)
  76. client, err := dial(server, pubkey)
  77. if err != nil {
  78. log.Error("Previous server unreachable", "server", server, "err", err)
  79. }
  80. w.lock.Lock()
  81. w.servers[server] = client
  82. w.lock.Unlock()
  83. }
  84. w.networkStats()
  85. }
  86. // Basics done, loop ad infinitum about what to do
  87. for {
  88. fmt.Println()
  89. fmt.Println("What would you like to do? (default = stats)")
  90. fmt.Println(" 1. Show network stats")
  91. if w.conf.Genesis == nil {
  92. fmt.Println(" 2. Configure new genesis")
  93. } else {
  94. fmt.Println(" 2. Manage existing genesis")
  95. }
  96. if len(w.servers) == 0 {
  97. fmt.Println(" 3. Track new remote server")
  98. } else {
  99. fmt.Println(" 3. Manage tracked machines")
  100. }
  101. if len(w.services) == 0 {
  102. fmt.Println(" 4. Deploy network components")
  103. } else {
  104. fmt.Println(" 4. Manage network components")
  105. }
  106. choice := w.read()
  107. switch {
  108. case choice == "" || choice == "1":
  109. w.networkStats()
  110. case choice == "2":
  111. if w.conf.Genesis == nil {
  112. fmt.Println()
  113. fmt.Println("What would you like to do? (default = create)")
  114. fmt.Println(" 1. Create new genesis from scratch")
  115. fmt.Println(" 2. Import already existing genesis")
  116. choice := w.read()
  117. switch {
  118. case choice == "" || choice == "1":
  119. w.makeGenesis()
  120. case choice == "2":
  121. w.importGenesis()
  122. default:
  123. log.Error("That's not something I can do")
  124. }
  125. } else {
  126. w.manageGenesis()
  127. }
  128. case choice == "3":
  129. if len(w.servers) == 0 {
  130. if w.makeServer() != "" {
  131. w.networkStats()
  132. }
  133. } else {
  134. w.manageServers()
  135. }
  136. case choice == "4":
  137. if len(w.services) == 0 {
  138. w.deployComponent()
  139. } else {
  140. w.manageComponents()
  141. }
  142. default:
  143. log.Error("That's not something I can do")
  144. }
  145. }
  146. }