github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/cmd/puppeth/wizard_network.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:33</date>
    10  //</624450070112112640>
    11  
    12  
    13  package main
    14  
    15  import (
    16  	"fmt"
    17  	"strings"
    18  
    19  	"github.com/ethereum/go-ethereum/log"
    20  )
    21  
    22  //
    23  //连接到新服务器的选项。
    24  func (w *wizard) manageServers() {
    25  //
    26  	fmt.Println()
    27  
    28  	servers := w.conf.servers()
    29  	for i, server := range servers {
    30  		fmt.Printf(" %d. Disconnect %s\n", i+1, server)
    31  	}
    32  	fmt.Printf(" %d. Connect another server\n", len(w.conf.Servers)+1)
    33  
    34  	choice := w.readInt()
    35  	if choice < 0 || choice > len(w.conf.Servers)+1 {
    36  		log.Error("Invalid server choice, aborting")
    37  		return
    38  	}
    39  //
    40  	if choice <= len(w.conf.Servers) {
    41  		server := servers[choice-1]
    42  		client := w.servers[server]
    43  
    44  		delete(w.servers, server)
    45  		if client != nil {
    46  			client.Close()
    47  		}
    48  		delete(w.conf.Servers, server)
    49  		w.conf.flush()
    50  
    51  		log.Info("Disconnected existing server", "server", server)
    52  		w.networkStats()
    53  		return
    54  	}
    55  //
    56  	if w.makeServer() != "" {
    57  		w.networkStats()
    58  	}
    59  }
    60  
    61  //makeserver从stdin读取一行并将其解释为
    62  //
    63  //
    64  //
    65  //如果连接成功,服务器将添加到向导配置中!
    66  func (w *wizard) makeServer() string {
    67  	fmt.Println()
    68  	fmt.Println("What is the remote server's address ([username[:identity]@]hostname[:port])?")
    69  
    70  //读取并拨号服务器以确保Docker存在
    71  	input := w.readString()
    72  
    73  	client, err := dial(input, nil)
    74  	if err != nil {
    75  		log.Error("Server not ready for puppeth", "err", err)
    76  		return ""
    77  	}
    78  //
    79  	w.servers[input] = client
    80  	w.conf.Servers[input] = client.pubkey
    81  	w.conf.flush()
    82  
    83  	return input
    84  }
    85  
    86  //selectserver列出用户要从中选择的所有当前已知服务器,
    87  //
    88  func (w *wizard) selectServer() string {
    89  //
    90  	fmt.Println()
    91  	fmt.Println("Which server do you want to interact with?")
    92  
    93  	servers := w.conf.servers()
    94  	for i, server := range servers {
    95  		fmt.Printf(" %d. %s\n", i+1, server)
    96  	}
    97  	fmt.Printf(" %d. Connect another server\n", len(w.conf.Servers)+1)
    98  
    99  	choice := w.readInt()
   100  	if choice < 0 || choice > len(w.conf.Servers)+1 {
   101  		log.Error("Invalid server choice, aborting")
   102  		return ""
   103  	}
   104  //
   105  	if choice <= len(w.conf.Servers) {
   106  		return servers[choice-1]
   107  	}
   108  	return w.makeServer()
   109  }
   110  
   111  //
   112  //
   113  func (w *wizard) manageComponents() {
   114  //
   115  	fmt.Println()
   116  
   117  	var serviceHosts, serviceNames []string
   118  	for server, services := range w.services {
   119  		for _, service := range services {
   120  			serviceHosts = append(serviceHosts, server)
   121  			serviceNames = append(serviceNames, service)
   122  
   123  			fmt.Printf(" %d. Tear down %s on %s\n", len(serviceHosts), strings.Title(service), server)
   124  		}
   125  	}
   126  	fmt.Printf(" %d. Deploy new network component\n", len(serviceHosts)+1)
   127  
   128  	choice := w.readInt()
   129  	if choice < 0 || choice > len(serviceHosts)+1 {
   130  		log.Error("Invalid component choice, aborting")
   131  		return
   132  	}
   133  //
   134  	if choice <= len(serviceHosts) {
   135  //找出要销毁和执行它的服务
   136  		service := serviceNames[choice-1]
   137  		server := serviceHosts[choice-1]
   138  		client := w.servers[server]
   139  
   140  		if out, err := tearDown(client, w.network, service, true); err != nil {
   141  			log.Error("Failed to tear down component", "err", err)
   142  			if len(out) > 0 {
   143  				fmt.Printf("%s\n", out)
   144  			}
   145  			return
   146  		}
   147  //
   148  		services := w.services[server]
   149  		for i, name := range services {
   150  			if name == service {
   151  				w.services[server] = append(services[:i], services[i+1:]...)
   152  				if len(w.services[server]) == 0 {
   153  					delete(w.services, server)
   154  				}
   155  			}
   156  		}
   157  		log.Info("Torn down existing component", "server", server, "service", service)
   158  		return
   159  	}
   160  //
   161  	w.deployComponent()
   162  }
   163  
   164  //
   165  //
   166  func (w *wizard) deployComponent() {
   167  //
   168  	fmt.Println()
   169  	fmt.Println("What would you like to deploy? (recommended order)")
   170  	fmt.Println(" 1. Ethstats  - Network monitoring tool")
   171  	fmt.Println(" 2. Bootnode  - Entry point of the network")
   172  	fmt.Println(" 3. Sealer    - Full node minting new blocks")
   173  	fmt.Println(" 4. Explorer  - Chain analysis webservice (ethash only)")
   174  	fmt.Println(" 5. Wallet    - Browser wallet for quick sends")
   175  	fmt.Println(" 6. Faucet    - Crypto faucet to give away funds")
   176  	fmt.Println(" 7. Dashboard - Website listing above web-services")
   177  
   178  	switch w.read() {
   179  	case "1":
   180  		w.deployEthstats()
   181  	case "2":
   182  		w.deployNode(true)
   183  	case "3":
   184  		w.deployNode(false)
   185  	case "4":
   186  		w.deployExplorer()
   187  	case "5":
   188  		w.deployWallet()
   189  	case "6":
   190  		w.deployFaucet()
   191  	case "7":
   192  		w.deployDashboard()
   193  	default:
   194  		log.Error("That's not something I can do")
   195  	}
   196  }
   197