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