wizard.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. "math/big"
  21. "net"
  22. "net/url"
  23. "os"
  24. "path/filepath"
  25. "sort"
  26. "strconv"
  27. "strings"
  28. "sync"
  29. "github.com/ethereum/go-ethereum/common"
  30. "github.com/ethereum/go-ethereum/console/prompt"
  31. "github.com/ethereum/go-ethereum/core"
  32. "github.com/ethereum/go-ethereum/log"
  33. "github.com/peterh/liner"
  34. "golang.org/x/term"
  35. )
  36. // config contains all the configurations needed by puppeth that should be saved
  37. // between sessions.
  38. type config struct {
  39. path string // File containing the configuration values
  40. bootnodes []string // Bootnodes to always connect to by all nodes
  41. ethstats string // Ethstats settings to cache for node deploys
  42. Genesis *core.Genesis `json:"genesis,omitempty"` // Genesis block to cache for node deploys
  43. Servers map[string][]byte `json:"servers,omitempty"`
  44. }
  45. // servers retrieves an alphabetically sorted list of servers.
  46. func (c config) servers() []string {
  47. servers := make([]string, 0, len(c.Servers))
  48. for server := range c.Servers {
  49. servers = append(servers, server)
  50. }
  51. sort.Strings(servers)
  52. return servers
  53. }
  54. // flush dumps the contents of config to disk.
  55. func (c config) flush() {
  56. os.MkdirAll(filepath.Dir(c.path), 0755)
  57. out, _ := json.MarshalIndent(c, "", " ")
  58. if err := os.WriteFile(c.path, out, 0644); err != nil {
  59. log.Warn("Failed to save puppeth configs", "file", c.path, "err", err)
  60. }
  61. }
  62. type wizard struct {
  63. network string // Network name to manage
  64. conf config // Configurations from previous runs
  65. servers map[string]*sshClient // SSH connections to servers to administer
  66. services map[string][]string // Ethereum services known to be running on servers
  67. lock sync.Mutex // Lock to protect configs during concurrent service discovery
  68. }
  69. // prompts the user for input with the given prompt string. Returns when a value is entered.
  70. // Causes the wizard to exit if ctrl-d is pressed
  71. func promptInput(p string) string {
  72. for {
  73. text, err := prompt.Stdin.PromptInput(p)
  74. if err != nil {
  75. if err != liner.ErrPromptAborted {
  76. log.Crit("Failed to read user input", "err", err)
  77. }
  78. } else {
  79. return text
  80. }
  81. }
  82. }
  83. // read reads a single line from stdin, trimming if from spaces.
  84. func (w *wizard) read() string {
  85. text := promptInput("> ")
  86. return strings.TrimSpace(text)
  87. }
  88. // readString reads a single line from stdin, trimming if from spaces, enforcing
  89. // non-emptyness.
  90. func (w *wizard) readString() string {
  91. for {
  92. text := promptInput("> ")
  93. if text = strings.TrimSpace(text); text != "" {
  94. return text
  95. }
  96. }
  97. }
  98. // readDefaultString reads a single line from stdin, trimming if from spaces. If
  99. // an empty line is entered, the default value is returned.
  100. func (w *wizard) readDefaultString(def string) string {
  101. text := promptInput("> ")
  102. if text = strings.TrimSpace(text); text != "" {
  103. return text
  104. }
  105. return def
  106. }
  107. // readDefaultYesNo reads a single line from stdin, trimming if from spaces and
  108. // interpreting it as a 'yes' or a 'no'. If an empty line is entered, the default
  109. // value is returned.
  110. func (w *wizard) readDefaultYesNo(def bool) bool {
  111. for {
  112. text := promptInput("> ")
  113. if text = strings.ToLower(strings.TrimSpace(text)); text == "" {
  114. return def
  115. }
  116. if text == "y" || text == "yes" {
  117. return true
  118. }
  119. if text == "n" || text == "no" {
  120. return false
  121. }
  122. log.Error("Invalid input, expected 'y', 'yes', 'n', 'no' or empty")
  123. }
  124. }
  125. // readURL reads a single line from stdin, trimming if from spaces and trying to
  126. // interpret it as a URL (http, https or file).
  127. func (w *wizard) readURL() *url.URL {
  128. for {
  129. text := promptInput("> ")
  130. uri, err := url.Parse(strings.TrimSpace(text))
  131. if err != nil {
  132. log.Error("Invalid input, expected URL", "err", err)
  133. continue
  134. }
  135. return uri
  136. }
  137. }
  138. // readInt reads a single line from stdin, trimming if from spaces, enforcing it
  139. // to parse into an integer.
  140. func (w *wizard) readInt() int {
  141. for {
  142. text := promptInput("> ")
  143. if text = strings.TrimSpace(text); text == "" {
  144. continue
  145. }
  146. val, err := strconv.Atoi(strings.TrimSpace(text))
  147. if err != nil {
  148. log.Error("Invalid input, expected integer", "err", err)
  149. continue
  150. }
  151. return val
  152. }
  153. }
  154. // readDefaultInt reads a single line from stdin, trimming if from spaces, enforcing
  155. // it to parse into an integer. If an empty line is entered, the default value is
  156. // returned.
  157. func (w *wizard) readDefaultInt(def int) int {
  158. for {
  159. text := promptInput("> ")
  160. if text = strings.TrimSpace(text); text == "" {
  161. return def
  162. }
  163. val, err := strconv.Atoi(strings.TrimSpace(text))
  164. if err != nil {
  165. log.Error("Invalid input, expected integer", "err", err)
  166. continue
  167. }
  168. return val
  169. }
  170. }
  171. // readDefaultBigInt reads a single line from stdin, trimming if from spaces,
  172. // enforcing it to parse into a big integer. If an empty line is entered, the
  173. // default value is returned.
  174. func (w *wizard) readDefaultBigInt(def *big.Int) *big.Int {
  175. for {
  176. text := promptInput("> ")
  177. if text = strings.TrimSpace(text); text == "" {
  178. return def
  179. }
  180. val, ok := new(big.Int).SetString(text, 0)
  181. if !ok {
  182. log.Error("Invalid input, expected big integer")
  183. continue
  184. }
  185. return val
  186. }
  187. }
  188. // readDefaultFloat reads a single line from stdin, trimming if from spaces, enforcing
  189. // it to parse into a float. If an empty line is entered, the default value is returned.
  190. func (w *wizard) readDefaultFloat(def float64) float64 {
  191. for {
  192. text := promptInput("> ")
  193. if text = strings.TrimSpace(text); text == "" {
  194. return def
  195. }
  196. val, err := strconv.ParseFloat(strings.TrimSpace(text), 64)
  197. if err != nil {
  198. log.Error("Invalid input, expected float", "err", err)
  199. continue
  200. }
  201. return val
  202. }
  203. }
  204. // readPassword reads a single line from stdin, trimming it from the trailing new
  205. // line and returns it. The input will not be echoed.
  206. func (w *wizard) readPassword() string {
  207. fmt.Printf("> ")
  208. text, err := term.ReadPassword(int(os.Stdin.Fd()))
  209. if err != nil {
  210. log.Crit("Failed to read password", "err", err)
  211. }
  212. fmt.Println()
  213. return string(text)
  214. }
  215. // readAddress reads a single line from stdin, trimming if from spaces and converts
  216. // it to an Ethereum address.
  217. func (w *wizard) readAddress() *common.Address {
  218. for {
  219. text := promptInput("> 0x")
  220. if text = strings.TrimSpace(text); text == "" {
  221. return nil
  222. }
  223. // Make sure it looks ok and return it if so
  224. if len(text) != 40 {
  225. log.Error("Invalid address length, please retry")
  226. continue
  227. }
  228. bigaddr, _ := new(big.Int).SetString(text, 16)
  229. address := common.BigToAddress(bigaddr)
  230. return &address
  231. }
  232. }
  233. // readDefaultAddress reads a single line from stdin, trimming if from spaces and
  234. // converts it to an Ethereum address. If an empty line is entered, the default
  235. // value is returned.
  236. func (w *wizard) readDefaultAddress(def common.Address) common.Address {
  237. for {
  238. // Read the address from the user
  239. text := promptInput("> 0x")
  240. if text = strings.TrimSpace(text); text == "" {
  241. return def
  242. }
  243. // Make sure it looks ok and return it if so
  244. if len(text) != 40 {
  245. log.Error("Invalid address length, please retry")
  246. continue
  247. }
  248. bigaddr, _ := new(big.Int).SetString(text, 16)
  249. return common.BigToAddress(bigaddr)
  250. }
  251. }
  252. // readJSON reads a raw JSON message and returns it.
  253. func (w *wizard) readJSON() string {
  254. var blob json.RawMessage
  255. for {
  256. text := promptInput("> ")
  257. reader := strings.NewReader(text)
  258. if err := json.NewDecoder(reader).Decode(&blob); err != nil {
  259. log.Error("Invalid JSON, please try again", "err", err)
  260. continue
  261. }
  262. return string(blob)
  263. }
  264. }
  265. // readIPAddress reads a single line from stdin, trimming if from spaces and
  266. // returning it if it's convertible to an IP address. The reason for keeping
  267. // the user input format instead of returning a Go net.IP is to match with
  268. // weird formats used by ethstats, which compares IPs textually, not by value.
  269. func (w *wizard) readIPAddress() string {
  270. for {
  271. // Read the IP address from the user
  272. fmt.Printf("> ")
  273. text := promptInput("> ")
  274. if text = strings.TrimSpace(text); text == "" {
  275. return ""
  276. }
  277. // Make sure it looks ok and return it if so
  278. if ip := net.ParseIP(text); ip == nil {
  279. log.Error("Invalid IP address, please retry")
  280. continue
  281. }
  282. return text
  283. }
  284. }