wizard_intro.go 4.6 KB

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