github.com/alex403s/heco-chain@v1.2.3-0.20220611014610-f10f4107d8b9/cmd/puppeth/wizard_intro.go (about)

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