cmd/puppeth: polish up config saving and reloading

This commit is contained in:
Péter Szilágyi 2019-07-08 19:59:23 +03:00
parent 54e3937f4e
commit 6ccf329857
No known key found for this signature in database
GPG key ID: E9AE538CEDF8293D
3 changed files with 60 additions and 64 deletions

View file

@ -32,12 +32,11 @@ import (
var explorerDockerfile = ` var explorerDockerfile = `
FROM puppeth/blockscout:latest FROM puppeth/blockscout:latest
ADD genesis.json /genesis.json ADD genesis.json /genesis.json
RUN \ RUN \
echo 'geth --cache 512 init /genesis.json' > explorer.sh && \ echo 'geth --cache 512 init /genesis.json' > explorer.sh && \
echo $'geth --networkid {{.NetworkID}} --syncmode "full" --gcmode "archive" --port {{.NodePort}} --bootnodes {{.Bootnodes}} --ethstats \'{{.Ethstats}}\' --cache=512 --rpc --rpcapi "net,web3,eth,shh,debug" --rpccorsdomain "*" --rpcvhosts "*" --ws --wsorigins "*" --exitwhensynced' >> explorer.sh && \ echo $'geth --networkid {{.NetworkID}} --syncmode "full" --gcmode "archive" --port {{.EthPort}} --bootnodes {{.Bootnodes}} --ethstats \'{{.Ethstats}}\' --cache=512 --rpc --rpcapi "net,web3,eth,shh,debug" --rpccorsdomain "*" --rpcvhosts "*" --ws --wsorigins "*" --exitwhensynced' >> explorer.sh && \
echo $'exec geth --networkid {{.NetworkID}} --syncmode "full" --gcmode "archive" --port {{.NodePort}} --bootnodes {{.Bootnodes}} --ethstats \'{{.Ethstats}}\' --cache=512 --rpc --rpcapi "net,web3,eth,shh,debug" --rpccorsdomain "*" --rpcvhosts "*" --ws --wsorigins "*" &' >> explorer.sh && \ echo $'exec geth --networkid {{.NetworkID}} --syncmode "full" --gcmode "archive" --port {{.EthPort}} --bootnodes {{.Bootnodes}} --ethstats \'{{.Ethstats}}\' --cache=512 --rpc --rpcapi "net,web3,eth,shh,debug" --rpccorsdomain "*" --rpcvhosts "*" --ws --wsorigins "*" &' >> explorer.sh && \
echo '/usr/local/bin/docker-entrypoint.sh postgres &' >> explorer.sh && \ echo '/usr/local/bin/docker-entrypoint.sh postgres &' >> explorer.sh && \
echo 'sleep 5' >> explorer.sh && \ echo 'sleep 5' >> explorer.sh && \
echo 'mix do ecto.drop --force, ecto.create, ecto.migrate' >> explorer.sh && \ echo 'mix do ecto.drop --force, ecto.create, ecto.migrate' >> explorer.sh && \
@ -56,18 +55,17 @@ services:
image: {{.Network}}/explorer image: {{.Network}}/explorer
container_name: {{.Network}}_explorer_1 container_name: {{.Network}}_explorer_1
ports: ports:
- "{{.NodePort}}:{{.NodePort}}" - "{{.EthPort}}:{{.EthPort}}"
- "{{.NodePort}}:{{.NodePort}}/udp"{{if not .VHost}} - "{{.EthPort}}:{{.EthPort}}/udp"{{if not .VHost}}
- "{{.WebPort}}:4000"{{end}} - "{{.WebPort}}:4000"{{end}}
environment: environment:
- NETWORK={{.NetworkName}}{{if .VHost}} - ETH_PORT={{.EthPort}}
- ETH_NAME={{.EthName}}
- BLOCK_TRANSFORMER={{.Transformer}}{{if .VHost}}
- VIRTUAL_HOST={{.VHost}} - VIRTUAL_HOST={{.VHost}}
- VIRTUAL_PORT=4000{{end}} - VIRTUAL_PORT=4000{{end}}
- NODE_PORT={{.NodePort}}/tcp
- STATS={{.Ethstats}}
- BLOCK_TRANSFORMER={{.Transformer}}
volumes: volumes:
- {{.Datadir}}:/root/.ethereum - {{.Datadir}}:/opt/app/.ethereum
- {{.DBDir}}:/var/lib/postgresql/data - {{.DBDir}}:/var/lib/postgresql/data
logging: logging:
driver: "json-file" driver: "json-file"
@ -87,10 +85,10 @@ func deployExplorer(client *sshClient, network string, bootnodes []string, confi
dockerfile := new(bytes.Buffer) dockerfile := new(bytes.Buffer)
template.Must(template.New("").Parse(explorerDockerfile)).Execute(dockerfile, map[string]interface{}{ template.Must(template.New("").Parse(explorerDockerfile)).Execute(dockerfile, map[string]interface{}{
"NetworkID": config.networkId, "NetworkID": config.node.network,
"Bootnodes": strings.Join(bootnodes, ","), "Bootnodes": strings.Join(bootnodes, ","),
"Ethstats": config.ethstats, "Ethstats": config.node.ethstats,
"NodePort": config.nodePort, "EthPort": config.node.port,
}) })
files[filepath.Join(workdir, "Dockerfile")] = dockerfile.Bytes() files[filepath.Join(workdir, "Dockerfile")] = dockerfile.Bytes()
@ -100,18 +98,18 @@ func deployExplorer(client *sshClient, network string, bootnodes []string, confi
} }
composefile := new(bytes.Buffer) composefile := new(bytes.Buffer)
template.Must(template.New("").Parse(explorerComposefile)).Execute(composefile, map[string]interface{}{ template.Must(template.New("").Parse(explorerComposefile)).Execute(composefile, map[string]interface{}{
"NetworkName": network,
"VHost": config.webHost,
"Ethstats": config.ethstats,
"Datadir": config.datadir,
"DBDir": config.dbdir,
"Network": network, "Network": network,
"NodePort": config.nodePort, "VHost": config.host,
"WebPort": config.webPort, "Ethstats": config.node.ethstats,
"Datadir": config.node.datadir,
"DBDir": config.dbdir,
"EthPort": config.node.port,
"EthName": config.node.ethstats[:strings.Index(config.node.ethstats, ":")],
"WebPort": config.port,
"Transformer": transformer, "Transformer": transformer,
}) })
files[filepath.Join(workdir, "docker-compose.yaml")] = composefile.Bytes() files[filepath.Join(workdir, "docker-compose.yaml")] = composefile.Bytes()
files[filepath.Join(workdir, "genesis.json")] = config.genesis files[filepath.Join(workdir, "genesis.json")] = config.node.genesis
// Upload the deployment files to the remote server (and clean up afterwards) // Upload the deployment files to the remote server (and clean up afterwards)
if out, err := client.Upload(files); err != nil { if out, err := client.Upload(files); err != nil {
@ -129,25 +127,20 @@ func deployExplorer(client *sshClient, network string, bootnodes []string, confi
// explorerInfos is returned from a block explorer status check to allow reporting // explorerInfos is returned from a block explorer status check to allow reporting
// various configuration parameters. // various configuration parameters.
type explorerInfos struct { type explorerInfos struct {
genesis []byte node *nodeInfos
datadir string
dbdir string dbdir string
ethstats string host string
nodePort int port int
webHost string
webPort int
networkId int64
} }
// Report converts the typed struct into a plain string->string map, containing // Report converts the typed struct into a plain string->string map, containing
// most - but not all - fields for reporting to the user. // most - but not all - fields for reporting to the user.
func (info *explorerInfos) Report() map[string]string { func (info *explorerInfos) Report() map[string]string {
report := map[string]string{ report := map[string]string{
"Data directory": info.datadir, "Website address ": info.host,
"Node listener port ": strconv.Itoa(info.nodePort), "Website listener port ": strconv.Itoa(info.port),
"Ethstats username": info.ethstats, "Ethereum listener port ": strconv.Itoa(info.node.port),
"Website address ": info.webHost, "Ethstats username": info.node.ethstats,
"Website listener port ": strconv.Itoa(info.webPort),
} }
return report return report
} }
@ -164,13 +157,13 @@ func checkExplorer(client *sshClient, network string) (*explorerInfos, error) {
return nil, ErrServiceOffline return nil, ErrServiceOffline
} }
// Resolve the port from the host, or the reverse proxy // Resolve the port from the host, or the reverse proxy
webPort := infos.portmap["4000/tcp"] port := infos.portmap["4000/tcp"]
if webPort == 0 { if port == 0 {
if proxy, _ := checkNginx(client, network); proxy != nil { if proxy, _ := checkNginx(client, network); proxy != nil {
webPort = proxy.port port = proxy.port
} }
} }
if webPort == 0 { if port == 0 {
return nil, ErrNotExposed return nil, ErrNotExposed
} }
// Resolve the host from the reverse-proxy and the config values // Resolve the host from the reverse-proxy and the config values
@ -179,18 +172,19 @@ func checkExplorer(client *sshClient, network string) (*explorerInfos, error) {
host = client.server host = client.server
} }
// Run a sanity check to see if the devp2p is reachable // Run a sanity check to see if the devp2p is reachable
nodePort := infos.portmap[infos.envvars["NODE_PORT"]] if err = checkPort(host, port); err != nil {
if err = checkPort(client.server, nodePort); err != nil { log.Warn("Explorer service seems unreachable", "server", host, "port", port, "err", err)
log.Warn(fmt.Sprintf("Explorer devp2p port seems unreachable"), "server", client.server, "port", nodePort, "err", err)
} }
// Assemble and return the useful infos // Assemble and return the useful infos
stats := &explorerInfos{ stats := &explorerInfos{
node: &nodeInfos{
datadir: infos.volumes["/opt/app/.ethereum"],
port: infos.portmap[infos.envvars["ETH_PORT"]+"/tcp"],
ethstats: infos.envvars["ETH_NAME"],
},
dbdir: infos.volumes["/var/lib/postgresql/data"], dbdir: infos.volumes["/var/lib/postgresql/data"],
datadir: infos.volumes["/root/.ethereum"], host: host,
nodePort: nodePort, port: port,
webHost: host,
webPort: webPort,
ethstats: infos.envvars["STATS"],
} }
return stats, nil return stats, nil
} }

View file

@ -77,7 +77,7 @@ func (w *wizard) deployDashboard() {
} }
case "explorer": case "explorer":
if infos, err := checkExplorer(client, w.network); err == nil { if infos, err := checkExplorer(client, w.network); err == nil {
port = infos.webPort port = infos.port
} }
case "wallet": case "wallet":
if infos, err := checkWallet(client, w.network); err == nil { if infos, err := checkWallet(client, w.network); err == nil {

View file

@ -46,32 +46,34 @@ func (w *wizard) deployExplorer() {
infos, err := checkExplorer(client, w.network) infos, err := checkExplorer(client, w.network)
if err != nil { if err != nil {
infos = &explorerInfos{ infos = &explorerInfos{
nodePort: 30303, webPort: 80, webHost: client.server, node: &nodeInfos{port: 30303},
port: 80,
host: client.server,
} }
} }
existed := err == nil existed := err == nil
infos.genesis, _ = json.MarshalIndent(w.conf.Genesis, "", " ") infos.node.genesis, _ = json.MarshalIndent(w.conf.Genesis, "", " ")
infos.networkId = w.conf.Genesis.Config.ChainID.Int64() infos.node.network = w.conf.Genesis.Config.ChainID.Int64()
// Figure out which port to listen on // Figure out which port to listen on
fmt.Println() fmt.Println()
fmt.Printf("Which port should the explorer listen on? (default = %d)\n", infos.webPort) fmt.Printf("Which port should the explorer listen on? (default = %d)\n", infos.port)
infos.webPort = w.readDefaultInt(infos.webPort) infos.port = w.readDefaultInt(infos.port)
// Figure which virtual-host to deploy ethstats on // Figure which virtual-host to deploy ethstats on
if infos.webHost, err = w.ensureVirtualHost(client, infos.webPort, infos.webHost); err != nil { if infos.host, err = w.ensureVirtualHost(client, infos.port, infos.host); err != nil {
log.Error("Failed to decide on explorer host", "err", err) log.Error("Failed to decide on explorer host", "err", err)
return return
} }
// Figure out where the user wants to store the persistent data // Figure out where the user wants to store the persistent data
fmt.Println() fmt.Println()
if infos.datadir == "" { if infos.node.datadir == "" {
fmt.Printf("Where should node data be stored on the remote machine?\n") fmt.Printf("Where should node data be stored on the remote machine?\n")
infos.datadir = w.readString() infos.node.datadir = w.readString()
} else { } else {
fmt.Printf("Where should node data be stored on the remote machine? (default = %s)\n", infos.datadir) fmt.Printf("Where should node data be stored on the remote machine? (default = %s)\n", infos.node.datadir)
infos.datadir = w.readDefaultString(infos.datadir) infos.node.datadir = w.readDefaultString(infos.node.datadir)
} }
// Figure out where the user wants to store the persistent data for backend database // Figure out where the user wants to store the persistent data for backend database
fmt.Println() fmt.Println()
@ -84,17 +86,17 @@ func (w *wizard) deployExplorer() {
} }
// Figure out which port to listen on // Figure out which port to listen on
fmt.Println() fmt.Println()
fmt.Printf("Which TCP/UDP port should the archive node listen on? (default = %d)\n", infos.nodePort) fmt.Printf("Which TCP/UDP port should the archive node listen on? (default = %d)\n", infos.node.port)
infos.nodePort = w.readDefaultInt(infos.nodePort) infos.node.port = w.readDefaultInt(infos.node.port)
// Set a proper name to report on the stats page // Set a proper name to report on the stats page
fmt.Println() fmt.Println()
if infos.ethstats == "" { if infos.node.ethstats == "" {
fmt.Printf("What should the explorer be called on the stats page?\n") fmt.Printf("What should the explorer be called on the stats page?\n")
infos.ethstats = w.readString() + ":" + w.conf.ethstats infos.node.ethstats = w.readString() + ":" + w.conf.ethstats
} else { } else {
fmt.Printf("What should the explorer be called on the stats page? (default = %s)\n", infos.ethstats) fmt.Printf("What should the explorer be called on the stats page? (default = %s)\n", infos.node.ethstats)
infos.ethstats = w.readDefaultString(infos.ethstats) + ":" + w.conf.ethstats infos.node.ethstats = w.readDefaultString(infos.node.ethstats) + ":" + w.conf.ethstats
} }
// Try to deploy the explorer on the host // Try to deploy the explorer on the host
nocache := false nocache := false