github.com/XinFinOrg/xdcchain@v1.1.0/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/ethereum/go-ethereum/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 container_name: {{.Network}}_explorer_1 81 ports: 82 - "{{.NodePort}}:{{.NodePort}}" 83 - "{{.NodePort}}:{{.NodePort}}/udp"{{if not .VHost}} 84 - "{{.WebPort}}:3000"{{end}} 85 volumes: 86 - {{.Datadir}}:/root/.local/share/io.parity.ethereum 87 environment: 88 - NODE_PORT={{.NodePort}}/tcp 89 - STATS={{.Ethstats}}{{if .VHost}} 90 - VIRTUAL_HOST={{.VHost}} 91 - VIRTUAL_PORT=3000{{end}} 92 logging: 93 driver: "json-file" 94 options: 95 max-size: "1m" 96 max-file: "10" 97 restart: always 98 ` 99 100 // deployExplorer deploys a new block explorer container to a remote machine via 101 // SSH, docker and docker-compose. If an instance with the specified network name 102 // already exists there, it will be overwritten! 103 func deployExplorer(client *sshClient, network string, chainspec []byte, config *explorerInfos, nocache bool) ([]byte, error) { 104 // Generate the content to upload to the server 105 workdir := fmt.Sprintf("%d", rand.Int63()) 106 files := make(map[string][]byte) 107 108 dockerfile := new(bytes.Buffer) 109 template.Must(template.New("").Parse(explorerDockerfile)).Execute(dockerfile, map[string]interface{}{ 110 "NodePort": config.nodePort, 111 }) 112 files[filepath.Join(workdir, "Dockerfile")] = dockerfile.Bytes() 113 114 ethstats := new(bytes.Buffer) 115 template.Must(template.New("").Parse(explorerEthstats)).Execute(ethstats, map[string]interface{}{ 116 "Port": config.nodePort, 117 "Name": config.ethstats[:strings.Index(config.ethstats, ":")], 118 "Secret": config.ethstats[strings.Index(config.ethstats, ":")+1 : strings.Index(config.ethstats, "@")], 119 "Host": config.ethstats[strings.Index(config.ethstats, "@")+1:], 120 }) 121 files[filepath.Join(workdir, "ethstats.json")] = ethstats.Bytes() 122 123 composefile := new(bytes.Buffer) 124 template.Must(template.New("").Parse(explorerComposefile)).Execute(composefile, map[string]interface{}{ 125 "Datadir": config.datadir, 126 "Network": network, 127 "NodePort": config.nodePort, 128 "VHost": config.webHost, 129 "WebPort": config.webPort, 130 "Ethstats": config.ethstats[:strings.Index(config.ethstats, ":")], 131 }) 132 files[filepath.Join(workdir, "docker-compose.yaml")] = composefile.Bytes() 133 134 files[filepath.Join(workdir, "chain.json")] = chainspec 135 136 // Upload the deployment files to the remote server (and clean up afterwards) 137 if out, err := client.Upload(files); err != nil { 138 return out, err 139 } 140 defer client.Run("rm -rf " + workdir) 141 142 // Build and deploy the boot or seal node service 143 if nocache { 144 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)) 145 } 146 return nil, client.Stream(fmt.Sprintf("cd %s && docker-compose -p %s up -d --build --force-recreate --timeout 60", workdir, network)) 147 } 148 149 // explorerInfos is returned from a block explorer status check to allow reporting 150 // various configuration parameters. 151 type explorerInfos struct { 152 datadir string 153 ethstats string 154 nodePort int 155 webHost string 156 webPort int 157 } 158 159 // Report converts the typed struct into a plain string->string map, containing 160 // most - but not all - fields for reporting to the user. 161 func (info *explorerInfos) Report() map[string]string { 162 report := map[string]string{ 163 "Data directory": info.datadir, 164 "Node listener port ": strconv.Itoa(info.nodePort), 165 "Ethstats username": info.ethstats, 166 "Website address ": info.webHost, 167 "Website listener port ": strconv.Itoa(info.webPort), 168 } 169 return report 170 } 171 172 // checkExplorer does a health-check against a block explorer server to verify 173 // whether it's running, and if yes, whether it's responsive. 174 func checkExplorer(client *sshClient, network string) (*explorerInfos, error) { 175 // Inspect a possible block explorer container on the host 176 infos, err := inspectContainer(client, fmt.Sprintf("%s_explorer_1", network)) 177 if err != nil { 178 return nil, err 179 } 180 if !infos.running { 181 return nil, ErrServiceOffline 182 } 183 // Resolve the port from the host, or the reverse proxy 184 webPort := infos.portmap["3000/tcp"] 185 if webPort == 0 { 186 if proxy, _ := checkNginx(client, network); proxy != nil { 187 webPort = proxy.port 188 } 189 } 190 if webPort == 0 { 191 return nil, ErrNotExposed 192 } 193 // Resolve the host from the reverse-proxy and the config values 194 host := infos.envvars["VIRTUAL_HOST"] 195 if host == "" { 196 host = client.server 197 } 198 // Run a sanity check to see if the devp2p is reachable 199 nodePort := infos.portmap[infos.envvars["NODE_PORT"]] 200 if err = checkPort(client.server, nodePort); err != nil { 201 log.Warn(fmt.Sprintf("Explorer devp2p port seems unreachable"), "server", client.server, "port", nodePort, "err", err) 202 } 203 // Assemble and return the useful infos 204 stats := &explorerInfos{ 205 datadir: infos.volumes["/root/.local/share/io.parity.ethereum"], 206 nodePort: nodePort, 207 webHost: host, 208 webPort: webPort, 209 ethstats: infos.envvars["STATS"], 210 } 211 return stats, nil 212 }