github.com/Cleverse/go-ethereum@v0.0.0-20220927095127-45113064e7f2/cmd/puppeth/wizard_dashboard.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  	"fmt"
    21  
    22  	"github.com/ethereum/go-ethereum/log"
    23  )
    24  
    25  // deployDashboard queries the user for various input on deploying a web-service
    26  // dashboard, after which is pushes the container.
    27  func (w *wizard) deployDashboard() {
    28  	// Select the server to interact with
    29  	server := w.selectServer()
    30  	if server == "" {
    31  		return
    32  	}
    33  	client := w.servers[server]
    34  
    35  	// Retrieve any active dashboard configurations from the server
    36  	infos, err := checkDashboard(client, w.network)
    37  	if err != nil {
    38  		infos = &dashboardInfos{
    39  			port: 80,
    40  			host: client.server,
    41  		}
    42  	}
    43  	existed := err == nil
    44  
    45  	// Figure out which port to listen on
    46  	fmt.Println()
    47  	fmt.Printf("Which port should the dashboard listen on? (default = %d)\n", infos.port)
    48  	infos.port = w.readDefaultInt(infos.port)
    49  
    50  	// Figure which virtual-host to deploy the dashboard on
    51  	infos.host, err = w.ensureVirtualHost(client, infos.port, infos.host)
    52  	if err != nil {
    53  		log.Error("Failed to decide on dashboard host", "err", err)
    54  		return
    55  	}
    56  	// Port and proxy settings retrieved, figure out which services are available
    57  	available := make(map[string][]string)
    58  	for server, services := range w.services {
    59  		for _, service := range services {
    60  			available[service] = append(available[service], server)
    61  		}
    62  	}
    63  	for _, service := range []string{"ethstats", "explorer", "faucet"} {
    64  		// Gather all the locally hosted pages of this type
    65  		var pages []string
    66  		for _, server := range available[service] {
    67  			client := w.servers[server]
    68  			if client == nil {
    69  				continue
    70  			}
    71  			// If there's a service running on the machine, retrieve it's port number
    72  			var port int
    73  			switch service {
    74  			case "ethstats":
    75  				if infos, err := checkEthstats(client, w.network); err == nil {
    76  					port = infos.port
    77  				}
    78  			case "explorer":
    79  				if infos, err := checkExplorer(client, w.network); err == nil {
    80  					port = infos.port
    81  				}
    82  			case "faucet":
    83  				if infos, err := checkFaucet(client, w.network); err == nil {
    84  					port = infos.port
    85  				}
    86  			}
    87  			if page, err := resolve(client, w.network, service, port); err == nil && page != "" {
    88  				pages = append(pages, page)
    89  			}
    90  		}
    91  		// Prompt the user to chose one, enter manually or simply not list this service
    92  		defLabel, defChoice := "don't list", len(pages)+2
    93  		if len(pages) > 0 {
    94  			defLabel, defChoice = pages[0], 1
    95  		}
    96  		fmt.Println()
    97  		fmt.Printf("Which %s service to list? (default = %s)\n", service, defLabel)
    98  		for i, page := range pages {
    99  			fmt.Printf(" %d. %s\n", i+1, page)
   100  		}
   101  		fmt.Printf(" %d. List external %s service\n", len(pages)+1, service)
   102  		fmt.Printf(" %d. Don't list any %s service\n", len(pages)+2, service)
   103  
   104  		choice := w.readDefaultInt(defChoice)
   105  		if choice < 0 || choice > len(pages)+2 {
   106  			log.Error("Invalid listing choice, aborting")
   107  			return
   108  		}
   109  		var page string
   110  		switch {
   111  		case choice <= len(pages):
   112  			page = pages[choice-1]
   113  		case choice == len(pages)+1:
   114  			fmt.Println()
   115  			fmt.Printf("Which address is the external %s service at?\n", service)
   116  			page = w.readString()
   117  		default:
   118  			// No service hosting for this
   119  		}
   120  		// Save the users choice
   121  		switch service {
   122  		case "ethstats":
   123  			infos.ethstats = page
   124  		case "explorer":
   125  			infos.explorer = page
   126  		case "faucet":
   127  			infos.faucet = page
   128  		}
   129  	}
   130  	// If we have ethstats running, ask whether to make the secret public or not
   131  	if w.conf.ethstats != "" {
   132  		fmt.Println()
   133  		fmt.Println("Include ethstats secret on dashboard (y/n)? (default = yes)")
   134  		infos.trusted = w.readDefaultYesNo(true)
   135  	}
   136  	// Try to deploy the dashboard container on the host
   137  	nocache := false
   138  	if existed {
   139  		fmt.Println()
   140  		fmt.Printf("Should the dashboard be built from scratch (y/n)? (default = no)\n")
   141  		nocache = w.readDefaultYesNo(false)
   142  	}
   143  	if out, err := deployDashboard(client, w.network, &w.conf, infos, nocache); err != nil {
   144  		log.Error("Failed to deploy dashboard container", "err", err)
   145  		if len(out) > 0 {
   146  			fmt.Printf("%s\n", out)
   147  		}
   148  		return
   149  	}
   150  	// All ok, run a network scan to pick any changes up
   151  	w.networkStats()
   152  }