wizard_intro.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. "github.com/ethereum/go-ethereum/log"
  26. )
  27. // makeWizard creates and returns a new puppeth wizard.
  28. func makeWizard(network string) *wizard {
  29. return &wizard{
  30. network: network,
  31. servers: make(map[string]*sshClient),
  32. services: make(map[string][]string),
  33. in: bufio.NewReader(os.Stdin),
  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. if w.network == "" {
  53. fmt.Println("Please specify a network name to administer (no spaces, please)")
  54. for {
  55. w.network = w.readString()
  56. if !strings.Contains(w.network, " ") {
  57. fmt.Printf("Sweet, you can set this via --network=%s next time!\n\n", w.network)
  58. break
  59. }
  60. log.Error("I also like to live dangerously, still no spaces")
  61. }
  62. }
  63. log.Info("Administering Ethereum network", "name", w.network)
  64. // Load initial configurations and connect to all live servers
  65. w.conf.path = filepath.Join(os.Getenv("HOME"), ".puppeth", w.network)
  66. blob, err := ioutil.ReadFile(w.conf.path)
  67. if err != nil {
  68. log.Warn("No previous configurations found", "path", w.conf.path)
  69. } else if err := json.Unmarshal(blob, &w.conf); err != nil {
  70. log.Crit("Previous configuration corrupted", "path", w.conf.path, "err", err)
  71. } else {
  72. for _, server := range w.conf.Servers {
  73. log.Info("Dialing previously configured server", "server", server)
  74. client, err := dial(server)
  75. if err != nil {
  76. log.Error("Previous server unreachable", "server", server, "err", err)
  77. }
  78. w.servers[server] = client
  79. }
  80. w.networkStats(false)
  81. }
  82. // Basics done, loop ad infinitum about what to do
  83. for {
  84. fmt.Println()
  85. fmt.Println("What would you like to do? (default = stats)")
  86. fmt.Println(" 1. Show network stats")
  87. if w.conf.genesis == nil {
  88. fmt.Println(" 2. Configure new genesis")
  89. } else {
  90. fmt.Println(" 2. Save existing genesis")
  91. }
  92. if len(w.servers) == 0 {
  93. fmt.Println(" 3. Track new remote server")
  94. } else {
  95. fmt.Println(" 3. Manage tracked machines")
  96. }
  97. if len(w.services) == 0 {
  98. fmt.Println(" 4. Deploy network components")
  99. } else {
  100. fmt.Println(" 4. Manage network components")
  101. }
  102. //fmt.Println(" 5. ProTips for common usecases")
  103. choice := w.read()
  104. switch {
  105. case choice == "" || choice == "1":
  106. w.networkStats(false)
  107. case choice == "2":
  108. // If we don't have a genesis, make one
  109. if w.conf.genesis == nil {
  110. w.makeGenesis()
  111. } else {
  112. // Otherwise just save whatever we currently have
  113. fmt.Println()
  114. fmt.Printf("Which file to save the genesis into? (default = %s.json)\n", w.network)
  115. out, _ := json.MarshalIndent(w.conf.genesis, "", " ")
  116. if err := ioutil.WriteFile(w.readDefaultString(fmt.Sprintf("%s.json", w.network)), out, 0644); err != nil {
  117. log.Error("Failed to save genesis file", "err", err)
  118. }
  119. log.Info("Exported existing genesis block")
  120. }
  121. case choice == "3":
  122. if len(w.servers) == 0 {
  123. if w.makeServer() != "" {
  124. w.networkStats(false)
  125. }
  126. } else {
  127. w.manageServers()
  128. }
  129. case choice == "4":
  130. if len(w.services) == 0 {
  131. w.deployComponent()
  132. } else {
  133. w.manageComponents()
  134. }
  135. case choice == "5":
  136. w.networkStats(true)
  137. default:
  138. log.Error("That's not something I can do")
  139. }
  140. }
  141. }