module_explorer.go 7.1 KB

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