github.com/luckypickle/go-ethereum-vet@v1.14.2/cmd/puppeth/module_explorer.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 "bytes" 21 "fmt" 22 "html/template" 23 "math/rand" 24 "path/filepath" 25 "strconv" 26 "strings" 27 28 "github.com/luckypickle/go-ethereum-vet/log" 29 ) 30 31 // explorerDockerfile is the Dockerfile required to run a block explorer. 32 var explorerDockerfile = ` 33 FROM puppeth/explorer:latest 34 35 ADD ethstats.json /ethstats.json 36 ADD chain.json /chain.json 37 38 RUN \ 39 echo '(cd ../eth-net-intelligence-api && pm2 start /ethstats.json)' > explorer.sh && \ 40 echo '(cd ../etherchain-light && npm start &)' >> explorer.sh && \ 41 echo 'exec /parity/parity --chain=/chain.json --port={{.NodePort}} --tracing=on --fat-db=on --pruning=archive' >> explorer.sh 42 43 ENTRYPOINT ["/bin/sh", "explorer.sh"] 44 ` 45 46 // explorerEthstats is the configuration file for the ethstats javascript client. 47 var explorerEthstats = `[ 48 { 49 "name" : "node-app", 50 "script" : "app.js", 51 "log_date_format" : "YYYY-MM-DD HH:mm Z", 52 "merge_logs" : false, 53 "watch" : false, 54 "max_restarts" : 10, 55 "exec_interpreter" : "node", 56 "exec_mode" : "fork_mode", 57 "env": 58 { 59 "NODE_ENV" : "production", 60 "RPC_HOST" : "localhost", 61 "RPC_PORT" : "8545", 62 "LISTENING_PORT" : "{{.Port}}", 63 "INSTANCE_NAME" : "{{.Name}}", 64 "CONTACT_DETAILS" : "", 65 "WS_SERVER" : "{{.Host}}", 66 "WS_SECRET" : "{{.Secret}}", 67 "VERBOSITY" : 2 68 } 69 } 70 ]` 71 72 // explorerComposefile is the docker-compose.yml file required to deploy and 73 // maintain a block explorer. 74 var explorerComposefile = ` 75 version: '2' 76 services: 77 explorer: 78 build: . 79 image: {{.Network}}/explorer 80 ports: 81 - "{{.NodePort}}:{{.NodePort}}" 82 - "{{.NodePort}}:{{.NodePort}}/udp"{{if not .VHost}} 83 - "{{.WebPort}}:3000"{{end}} 84 volumes: 85 - {{.Datadir}}:/root/.local/share/io.parity.ethereum 86 environment: 87 - NODE_PORT={{.NodePort}}/tcp 88 - STATS={{.Ethstats}}{{if .VHost}} 89 - VIRTUAL_HOST={{.VHost}} 90 - VIRTUAL_PORT=3000{{end}} 91 logging: 92 driver: "json-file" 93 options: 94 max-size: "1m" 95 max-file: "10" 96 restart: always 97 ` 98 99 // deployExplorer deploys a new block explorer container to a remote machine via 100 // SSH, docker and docker-compose. If an instance with the specified network name 101 // already exists there, it will be overwritten! 102 func deployExplorer(client *sshClient, network string, chainspec []byte, config *explorerInfos, nocache bool) ([]byte, error) { 103 // Generate the content to upload to the server 104 workdir := fmt.Sprintf("%d", rand.Int63()) 105 files := make(map[string][]byte) 106 107 dockerfile := new(bytes.Buffer) 108 template.Must(template.New("").Parse(explorerDockerfile)).Execute(dockerfile, map[string]interface{}{ 109 "NodePort": config.nodePort, 110 }) 111 files[filepath.Join(workdir, "Dockerfile")] = dockerfile.Bytes() 112 113 ethstats := new(bytes.Buffer) 114 template.Must(template.New("").Parse(explorerEthstats)).Execute(ethstats, map[string]interface{}{ 115 "Port": config.nodePort, 116 "Name": config.ethstats[:strings.Index(config.ethstats, ":")], 117 "Secret": config.ethstats[strings.Index(config.ethstats, ":")+1 : strings.Index(config.ethstats, "@")], 118 "Host": config.ethstats[strings.Index(config.ethstats, "@")+1:], 119 }) 120 files[filepath.Join(workdir, "ethstats.json")] = ethstats.Bytes() 121 122 composefile := new(bytes.Buffer) 123 template.Must(template.New("").Parse(explorerComposefile)).Execute(composefile, map[string]interface{}{ 124 "Datadir": config.datadir, 125 "Network": network, 126 "NodePort": config.nodePort, 127 "VHost": config.webHost, 128 "WebPort": config.webPort, 129 "Ethstats": config.ethstats[:strings.Index(config.ethstats, ":")], 130 }) 131 files[filepath.Join(workdir, "docker-compose.yaml")] = composefile.Bytes() 132 133 files[filepath.Join(workdir, "chain.json")] = chainspec 134 135 // Upload the deployment files to the remote server (and clean up afterwards) 136 if out, err := client.Upload(files); err != nil { 137 return out, err 138 } 139 defer client.Run("rm -rf " + workdir) 140 141 // Build and deploy the boot or seal node service 142 if nocache { 143 return nil, client.Stream(fmt.Sprintf("cd %s && docker-compose -p %s build --pull --no-cache && docker-compose -p %s up -d --force-recreate --timeout 60", workdir, network, network)) 144 } 145 return nil, client.Stream(fmt.Sprintf("cd %s && docker-compose -p %s up -d --build --force-recreate --timeout 60", workdir, network)) 146 } 147 148 // explorerInfos is returned from a block explorer status check to allow reporting 149 // various configuration parameters. 150 type explorerInfos struct { 151 datadir string 152 ethstats string 153 nodePort int 154 webHost string 155 webPort int 156 } 157 158 // Report converts the typed struct into a plain string->string map, containing 159 // most - but not all - fields for reporting to the user. 160 func (info *explorerInfos) Report() map[string]string { 161 report := map[string]string{ 162 "Data directory": info.datadir, 163 "Node listener port ": strconv.Itoa(info.nodePort), 164 "Ethstats username": info.ethstats, 165 "Website address ": info.webHost, 166 "Website listener port ": strconv.Itoa(info.webPort), 167 } 168 return report 169 } 170 171 // checkExplorer does a health-check against a block explorer server to verify 172 // whether it's running, and if yes, whether it's responsive. 173 func checkExplorer(client *sshClient, network string) (*explorerInfos, error) { 174 // Inspect a possible block explorer container on the host 175 infos, err := inspectContainer(client, fmt.Sprintf("%s_explorer_1", network)) 176 if err != nil { 177 return nil, err 178 } 179 if !infos.running { 180 return nil, ErrServiceOffline 181 } 182 // Resolve the port from the host, or the reverse proxy 183 webPort := infos.portmap["3000/tcp"] 184 if webPort == 0 { 185 if proxy, _ := checkNginx(client, network); proxy != nil { 186 webPort = proxy.port 187 } 188 } 189 if webPort == 0 { 190 return nil, ErrNotExposed 191 } 192 // Resolve the host from the reverse-proxy and the config values 193 host := infos.envvars["VIRTUAL_HOST"] 194 if host == "" { 195 host = client.server 196 } 197 // Run a sanity check to see if the devp2p is reachable 198 nodePort := infos.portmap[infos.envvars["NODE_PORT"]] 199 if err = checkPort(client.server, nodePort); err != nil { 200 log.Warn(fmt.Sprintf("Explorer devp2p port seems unreachable"), "server", client.server, "port", nodePort, "err", err) 201 } 202 // Assemble and return the useful infos 203 stats := &explorerInfos{ 204 datadir: infos.volumes["/root/.local/share/io.parity.ethereum"], 205 nodePort: nodePort, 206 webHost: host, 207 webPort: webPort, 208 ethstats: infos.envvars["STATS"], 209 } 210 return stats, nil 211 }