github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/cmd/puppeth/wizard_dashboard.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package main
    13  
    14  import (
    15  	"fmt"
    16  
    17  	"github.com/Sberex/go-sberex/log"
    18  )
    19  
    20  // deployDashboard queries the user for various input on deploying a web-service
    21  // dashboard, after which is pushes the container.
    22  func (w *wizard) deployDashboard() {
    23  	// Select the server to interact with
    24  	server := w.selectServer()
    25  	if server == "" {
    26  		return
    27  	}
    28  	client := w.servers[server]
    29  
    30  	// Retrieve any active dashboard configurations from the server
    31  	infos, err := checkDashboard(client, w.network)
    32  	if err != nil {
    33  		infos = &dashboardInfos{
    34  			port: 80,
    35  			host: client.server,
    36  		}
    37  	}
    38  	existed := err == nil
    39  
    40  	// Figure out which port to listen on
    41  	fmt.Println()
    42  	fmt.Printf("Which port should the dashboard listen on? (default = %d)\n", infos.port)
    43  	infos.port = w.readDefaultInt(infos.port)
    44  
    45  	// Figure which virtual-host to deploy the dashboard on
    46  	infos.host, err = w.ensureVirtualHost(client, infos.port, infos.host)
    47  	if err != nil {
    48  		log.Error("Failed to decide on dashboard host", "err", err)
    49  		return
    50  	}
    51  	// Port and proxy settings retrieved, figure out which services are available
    52  	available := make(map[string][]string)
    53  	for server, services := range w.services {
    54  		for _, service := range services {
    55  			available[service] = append(available[service], server)
    56  		}
    57  	}
    58  	for _, service := range []string{"ethstats", "explorer", "wallet", "faucet"} {
    59  		// Gather all the locally hosted pages of this type
    60  		var pages []string
    61  		for _, server := range available[service] {
    62  			client := w.servers[server]
    63  			if client == nil {
    64  				continue
    65  			}
    66  			// If there's a service running on the machine, retrieve it's port number
    67  			var port int
    68  			switch service {
    69  			case "ethstats":
    70  				if infos, err := checkEthstats(client, w.network); err == nil {
    71  					port = infos.port
    72  				}
    73  			case "explorer":
    74  				if infos, err := checkExplorer(client, w.network); err == nil {
    75  					port = infos.webPort
    76  				}
    77  			case "wallet":
    78  				if infos, err := checkWallet(client, w.network); err == nil {
    79  					port = infos.webPort
    80  				}
    81  			case "faucet":
    82  				if infos, err := checkFaucet(client, w.network); err == nil {
    83  					port = infos.port
    84  				}
    85  			}
    86  			if page, err := resolve(client, w.network, service, port); err == nil && page != "" {
    87  				pages = append(pages, page)
    88  			}
    89  		}
    90  		// Promt the user to chose one, enter manually or simply not list this service
    91  		defLabel, defChoice := "don't list", len(pages)+2
    92  		if len(pages) > 0 {
    93  			defLabel, defChoice = pages[0], 1
    94  		}
    95  		fmt.Println()
    96  		fmt.Printf("Which %s service to list? (default = %s)\n", service, defLabel)
    97  		for i, page := range pages {
    98  			fmt.Printf(" %d. %s\n", i+1, page)
    99  		}
   100  		fmt.Printf(" %d. List external %s service\n", len(pages)+1, service)
   101  		fmt.Printf(" %d. Don't list any %s service\n", len(pages)+2, service)
   102  
   103  		choice := w.readDefaultInt(defChoice)
   104  		if choice < 0 || choice > len(pages)+2 {
   105  			log.Error("Invalid listing choice, aborting")
   106  			return
   107  		}
   108  		var page string
   109  		switch {
   110  		case choice <= len(pages):
   111  			page = pages[choice-1]
   112  		case choice == len(pages)+1:
   113  			fmt.Println()
   114  			fmt.Printf("Which address is the external %s service at?\n", service)
   115  			page = w.readString()
   116  		default:
   117  			// No service hosting for this
   118  		}
   119  		// Save the users choice
   120  		switch service {
   121  		case "ethstats":
   122  			infos.ethstats = page
   123  		case "explorer":
   124  			infos.explorer = page
   125  		case "wallet":
   126  			infos.wallet = page
   127  		case "faucet":
   128  			infos.faucet = page
   129  		}
   130  	}
   131  	// If we have ethstats running, ask whether to make the secret public or not
   132  	if w.conf.ethstats != "" {
   133  		fmt.Println()
   134  		fmt.Println("Include ethstats secret on dashboard (y/n)? (default = yes)")
   135  		infos.trusted = w.readDefaultString("y") == "y"
   136  	}
   137  	// Try to deploy the dashboard container on the host
   138  	nocache := false
   139  	if existed {
   140  		fmt.Println()
   141  		fmt.Printf("Should the dashboard be built from scratch (y/n)? (default = no)\n")
   142  		nocache = w.readDefaultString("n") != "n"
   143  	}
   144  	if out, err := deployDashboard(client, w.network, &w.conf, infos, nocache); err != nil {
   145  		log.Error("Failed to deploy dashboard container", "err", err)
   146  		if len(out) > 0 {
   147  			fmt.Printf("%s\n", out)
   148  		}
   149  		return
   150  	}
   151  	// All ok, run a network scan to pick any changes up
   152  	w.networkStats()
   153  }