module_explorer.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/blockscout:latest
  30. ADD genesis.json /genesis.json
  31. RUN \
  32. echo 'geth --cache 512 init /genesis.json' > explorer.sh && \
  33. echo $'geth --networkid {{.NetworkID}} --syncmode "full" --gcmode "archive" --port {{.EthPort}} --bootnodes {{.Bootnodes}} --ethstats \'{{.Ethstats}}\' --cache=512 --http --http.api "net,web3,eth,shh,debug" --http.corsdomain "*" --http.vhosts "*" --ws --ws.origins "*" --exitwhensynced' >> explorer.sh && \
  34. echo $'exec geth --networkid {{.NetworkID}} --syncmode "full" --gcmode "archive" --port {{.EthPort}} --bootnodes {{.Bootnodes}} --ethstats \'{{.Ethstats}}\' --cache=512 --http --http.api "net,web3,eth,shh,debug" --http.corsdomain "*" --http.vhosts "*" --ws --ws.origins "*" &' >> explorer.sh && \
  35. echo '/usr/local/bin/docker-entrypoint.sh postgres &' >> explorer.sh && \
  36. echo 'sleep 5' >> explorer.sh && \
  37. echo 'mix do ecto.drop --force, ecto.create, ecto.migrate' >> explorer.sh && \
  38. echo 'mix phx.server' >> explorer.sh
  39. ENTRYPOINT ["/bin/sh", "explorer.sh"]
  40. `
  41. // explorerComposefile is the docker-compose.yml file required to deploy and
  42. // maintain a block explorer.
  43. var explorerComposefile = `
  44. version: '2'
  45. services:
  46. explorer:
  47. build: .
  48. image: {{.Network}}/explorer
  49. container_name: {{.Network}}_explorer_1
  50. ports:
  51. - "{{.EthPort}}:{{.EthPort}}"
  52. - "{{.EthPort}}:{{.EthPort}}/udp"{{if not .VHost}}
  53. - "{{.WebPort}}:4000"{{end}}
  54. environment:
  55. - ETH_PORT={{.EthPort}}
  56. - ETH_NAME={{.EthName}}
  57. - BLOCK_TRANSFORMER={{.Transformer}}{{if .VHost}}
  58. - VIRTUAL_HOST={{.VHost}}
  59. - VIRTUAL_PORT=4000{{end}}
  60. volumes:
  61. - {{.Datadir}}:/opt/app/.ethereum
  62. - {{.DBDir}}:/var/lib/postgresql/data
  63. logging:
  64. driver: "json-file"
  65. options:
  66. max-size: "1m"
  67. max-file: "10"
  68. restart: always
  69. `
  70. // deployExplorer deploys a new block explorer container to a remote machine via
  71. // SSH, docker and docker-compose. If an instance with the specified network name
  72. // already exists there, it will be overwritten!
  73. func deployExplorer(client *sshClient, network string, bootnodes []string, config *explorerInfos, nocache bool, isClique bool) ([]byte, error) {
  74. // Generate the content to upload to the server
  75. workdir := fmt.Sprintf("%d", rand.Int63())
  76. files := make(map[string][]byte)
  77. dockerfile := new(bytes.Buffer)
  78. template.Must(template.New("").Parse(explorerDockerfile)).Execute(dockerfile, map[string]interface{}{
  79. "NetworkID": config.node.network,
  80. "Bootnodes": strings.Join(bootnodes, ","),
  81. "Ethstats": config.node.ethstats,
  82. "EthPort": config.node.port,
  83. })
  84. files[filepath.Join(workdir, "Dockerfile")] = dockerfile.Bytes()
  85. transformer := "base"
  86. if isClique {
  87. transformer = "clique"
  88. }
  89. composefile := new(bytes.Buffer)
  90. template.Must(template.New("").Parse(explorerComposefile)).Execute(composefile, map[string]interface{}{
  91. "Network": network,
  92. "VHost": config.host,
  93. "Ethstats": config.node.ethstats,
  94. "Datadir": config.node.datadir,
  95. "DBDir": config.dbdir,
  96. "EthPort": config.node.port,
  97. "EthName": config.node.ethstats[:strings.Index(config.node.ethstats, ":")],
  98. "WebPort": config.port,
  99. "Transformer": transformer,
  100. })
  101. files[filepath.Join(workdir, "docker-compose.yaml")] = composefile.Bytes()
  102. files[filepath.Join(workdir, "genesis.json")] = config.node.genesis
  103. // Upload the deployment files to the remote server (and clean up afterwards)
  104. if out, err := client.Upload(files); err != nil {
  105. return out, err
  106. }
  107. defer client.Run("rm -rf " + workdir)
  108. // Build and deploy the boot or seal node service
  109. if nocache {
  110. 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))
  111. }
  112. return nil, client.Stream(fmt.Sprintf("cd %s && docker-compose -p %s up -d --build --force-recreate --timeout 60", workdir, network))
  113. }
  114. // explorerInfos is returned from a block explorer status check to allow reporting
  115. // various configuration parameters.
  116. type explorerInfos struct {
  117. node *nodeInfos
  118. dbdir string
  119. host string
  120. port int
  121. }
  122. // Report converts the typed struct into a plain string->string map, containing
  123. // most - but not all - fields for reporting to the user.
  124. func (info *explorerInfos) Report() map[string]string {
  125. report := map[string]string{
  126. "Website address ": info.host,
  127. "Website listener port ": strconv.Itoa(info.port),
  128. "Ethereum listener port ": strconv.Itoa(info.node.port),
  129. "Ethstats username": info.node.ethstats,
  130. }
  131. return report
  132. }
  133. // checkExplorer does a health-check against a block explorer server to verify
  134. // whether it's running, and if yes, whether it's responsive.
  135. func checkExplorer(client *sshClient, network string) (*explorerInfos, error) {
  136. // Inspect a possible explorer container on the host
  137. infos, err := inspectContainer(client, fmt.Sprintf("%s_explorer_1", network))
  138. if err != nil {
  139. return nil, err
  140. }
  141. if !infos.running {
  142. return nil, ErrServiceOffline
  143. }
  144. // Resolve the port from the host, or the reverse proxy
  145. port := infos.portmap["4000/tcp"]
  146. if port == 0 {
  147. if proxy, _ := checkNginx(client, network); proxy != nil {
  148. port = proxy.port
  149. }
  150. }
  151. if port == 0 {
  152. return nil, ErrNotExposed
  153. }
  154. // Resolve the host from the reverse-proxy and the config values
  155. host := infos.envvars["VIRTUAL_HOST"]
  156. if host == "" {
  157. host = client.server
  158. }
  159. // Run a sanity check to see if the devp2p is reachable
  160. p2pPort := infos.portmap[infos.envvars["ETH_PORT"]+"/tcp"]
  161. if err = checkPort(host, p2pPort); err != nil {
  162. log.Warn("Explorer node seems unreachable", "server", host, "port", p2pPort, "err", err)
  163. }
  164. if err = checkPort(host, port); err != nil {
  165. log.Warn("Explorer service seems unreachable", "server", host, "port", port, "err", err)
  166. }
  167. // Assemble and return the useful infos
  168. stats := &explorerInfos{
  169. node: &nodeInfos{
  170. datadir: infos.volumes["/opt/app/.ethereum"],
  171. port: infos.portmap[infos.envvars["ETH_PORT"]+"/tcp"],
  172. ethstats: infos.envvars["ETH_NAME"],
  173. },
  174. dbdir: infos.volumes["/var/lib/postgresql/data"],
  175. host: host,
  176. port: port,
  177. }
  178. return stats, nil
  179. }