github.com/waltonchain/waltonchain_gwtc_src@v1.1.4-0.20201225072101-8a298c95a819/cmd/puppeth/wizard_intro.go (about) 1 // Copyright 2017 The go-ethereum Authors 2 // This file is part of go-wtc. 3 // 4 // go-wtc 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-wtc 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-wtc. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 import ( 20 "bufio" 21 "encoding/json" 22 "fmt" 23 "io/ioutil" 24 "os" 25 "path/filepath" 26 "strings" 27 28 "github.com/wtc/go-wtc/log" 29 ) 30 31 // makeWizard creates and returns a new puppeth wizard. 32 func makeWizard(network string) *wizard { 33 return &wizard{ 34 network: network, 35 conf: config{ 36 Servers: make(map[string][]byte), 37 }, 38 servers: make(map[string]*sshClient), 39 services: make(map[string][]string), 40 in: bufio.NewReader(os.Stdin), 41 } 42 } 43 44 // run displays some useful infos to the user, starting on the journey of 45 // setting up a new or managing an existing Wtc private network. 46 func (w *wizard) run() { 47 fmt.Println("+-----------------------------------------------------------+") 48 fmt.Println("| Welcome to puppeth, your Wtc private network manager |") 49 fmt.Println("| |") 50 fmt.Println("| This tool lets you create a new Wtc network down to |") 51 fmt.Println("| the genesis block, bootnodes, miners and ethstats servers |") 52 fmt.Println("| without the hassle that it would normally entail. |") 53 fmt.Println("| |") 54 fmt.Println("| Puppeth uses SSH to dial in to remote servers, and builds |") 55 fmt.Println("| its network components out of Docker containers using the |") 56 fmt.Println("| docker-compose toolset. |") 57 fmt.Println("+-----------------------------------------------------------+") 58 fmt.Println() 59 60 // Make sure we have a good network name to work with fmt.Println() 61 if w.network == "" { 62 fmt.Println("Please specify a network name to administer (no spaces, please)") 63 for { 64 w.network = w.readString() 65 if !strings.Contains(w.network, " ") { 66 fmt.Printf("Sweet, you can set this via --network=%s next time!\n\n", w.network) 67 break 68 } 69 log.Error("I also like to live dangerously, still no spaces") 70 } 71 } 72 log.Info("Administering Wtc network", "name", w.network) 73 74 // Load initial configurations and connect to all live servers 75 w.conf.path = filepath.Join(os.Getenv("HOME"), ".puppeth", w.network) 76 77 blob, err := ioutil.ReadFile(w.conf.path) 78 if err != nil { 79 log.Warn("No previous configurations found", "path", w.conf.path) 80 } else if err := json.Unmarshal(blob, &w.conf); err != nil { 81 log.Crit("Previous configuration corrupted", "path", w.conf.path, "err", err) 82 } else { 83 for server, pubkey := range w.conf.Servers { 84 log.Info("Dialing previously configured server", "server", server) 85 client, err := dial(server, pubkey) 86 if err != nil { 87 log.Error("Previous server unreachable", "server", server, "err", err) 88 } 89 w.servers[server] = client 90 } 91 w.networkStats(false) 92 } 93 // Basics done, loop ad infinitum about what to do 94 for { 95 fmt.Println() 96 fmt.Println("What would you like to do? (default = stats)") 97 fmt.Println(" 1. Show network stats") 98 if w.conf.genesis == nil { 99 fmt.Println(" 2. Configure new genesis") 100 } else { 101 fmt.Println(" 2. Save existing genesis") 102 } 103 if len(w.servers) == 0 { 104 fmt.Println(" 3. Track new remote server") 105 } else { 106 fmt.Println(" 3. Manage tracked machines") 107 } 108 if len(w.services) == 0 { 109 fmt.Println(" 4. Deploy network components") 110 } else { 111 fmt.Println(" 4. Manage network components") 112 } 113 //fmt.Println(" 5. ProTips for common usecases") 114 115 choice := w.read() 116 switch { 117 case choice == "" || choice == "1": 118 w.networkStats(false) 119 120 case choice == "2": 121 // If we don't have a genesis, make one 122 if w.conf.genesis == nil { 123 w.makeGenesis() 124 } else { 125 // Otherwise just save whatever we currently have 126 fmt.Println() 127 fmt.Printf("Which file to save the genesis into? (default = %s.json)\n", w.network) 128 out, _ := json.MarshalIndent(w.conf.genesis, "", " ") 129 if err := ioutil.WriteFile(w.readDefaultString(fmt.Sprintf("%s.json", w.network)), out, 0644); err != nil { 130 log.Error("Failed to save genesis file", "err", err) 131 } 132 log.Info("Exported existing genesis block") 133 } 134 case choice == "3": 135 if len(w.servers) == 0 { 136 if w.makeServer() != "" { 137 w.networkStats(false) 138 } 139 } else { 140 w.manageServers() 141 } 142 case choice == "4": 143 if len(w.services) == 0 { 144 w.deployComponent() 145 } else { 146 w.manageComponents() 147 } 148 149 case choice == "5": 150 w.networkStats(true) 151 152 default: 153 log.Error("That's not something I can do") 154 } 155 } 156 }