module_ethstats.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. "math/rand"
  21. "path/filepath"
  22. "strconv"
  23. "strings"
  24. "text/template"
  25. "github.com/ethereum/go-ethereum/log"
  26. )
  27. // ethstatsDockerfile is the Dockerfile required to build an ethstats backend
  28. // and associated monitoring site.
  29. var ethstatsDockerfile = `
  30. FROM puppeth/ethstats:latest
  31. RUN echo 'module.exports = {trusted: [{{.Trusted}}], banned: [{{.Banned}}], reserved: ["yournode"]};' > lib/utils/config.js
  32. `
  33. // ethstatsComposefile is the docker-compose.yml file required to deploy and
  34. // maintain an ethstats monitoring site.
  35. var ethstatsComposefile = `
  36. version: '2'
  37. services:
  38. ethstats:
  39. build: .
  40. image: {{.Network}}/ethstats
  41. container_name: {{.Network}}_ethstats_1{{if not .VHost}}
  42. ports:
  43. - "{{.Port}}:3000"{{end}}
  44. environment:
  45. - WS_SECRET={{.Secret}}{{if .VHost}}
  46. - VIRTUAL_HOST={{.VHost}}{{end}}{{if .Banned}}
  47. - BANNED={{.Banned}}{{end}}
  48. logging:
  49. driver: "json-file"
  50. options:
  51. max-size: "1m"
  52. max-file: "10"
  53. restart: always
  54. `
  55. // deployEthstats deploys a new ethstats container to a remote machine via SSH,
  56. // docker and docker-compose. If an instance with the specified network name
  57. // already exists there, it will be overwritten!
  58. func deployEthstats(client *sshClient, network string, port int, secret string, vhost string, trusted []string, banned []string, nocache bool) ([]byte, error) {
  59. // Generate the content to upload to the server
  60. workdir := fmt.Sprintf("%d", rand.Int63())
  61. files := make(map[string][]byte)
  62. trustedLabels := make([]string, len(trusted))
  63. for i, address := range trusted {
  64. trustedLabels[i] = fmt.Sprintf("\"%s\"", address)
  65. }
  66. bannedLabels := make([]string, len(banned))
  67. for i, address := range banned {
  68. bannedLabels[i] = fmt.Sprintf("\"%s\"", address)
  69. }
  70. dockerfile := new(bytes.Buffer)
  71. template.Must(template.New("").Parse(ethstatsDockerfile)).Execute(dockerfile, map[string]interface{}{
  72. "Trusted": strings.Join(trustedLabels, ", "),
  73. "Banned": strings.Join(bannedLabels, ", "),
  74. })
  75. files[filepath.Join(workdir, "Dockerfile")] = dockerfile.Bytes()
  76. composefile := new(bytes.Buffer)
  77. template.Must(template.New("").Parse(ethstatsComposefile)).Execute(composefile, map[string]interface{}{
  78. "Network": network,
  79. "Port": port,
  80. "Secret": secret,
  81. "VHost": vhost,
  82. "Banned": strings.Join(banned, ","),
  83. })
  84. files[filepath.Join(workdir, "docker-compose.yaml")] = composefile.Bytes()
  85. // Upload the deployment files to the remote server (and clean up afterwards)
  86. if out, err := client.Upload(files); err != nil {
  87. return out, err
  88. }
  89. defer client.Run("rm -rf " + workdir)
  90. // Build and deploy the ethstats service
  91. if nocache {
  92. 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))
  93. }
  94. return nil, client.Stream(fmt.Sprintf("cd %s && docker-compose -p %s up -d --build --force-recreate --timeout 60", workdir, network))
  95. }
  96. // ethstatsInfos is returned from an ethstats status check to allow reporting
  97. // various configuration parameters.
  98. type ethstatsInfos struct {
  99. host string
  100. port int
  101. secret string
  102. config string
  103. banned []string
  104. }
  105. // Report converts the typed struct into a plain string->string map, containing
  106. // most - but not all - fields for reporting to the user.
  107. func (info *ethstatsInfos) Report() map[string]string {
  108. return map[string]string{
  109. "Website address": info.host,
  110. "Website listener port": strconv.Itoa(info.port),
  111. "Login secret": info.secret,
  112. "Banned addresses": strings.Join(info.banned, "\n"),
  113. }
  114. }
  115. // checkEthstats does a health-check against an ethstats server to verify whether
  116. // it's running, and if yes, gathering a collection of useful infos about it.
  117. func checkEthstats(client *sshClient, network string) (*ethstatsInfos, error) {
  118. // Inspect a possible ethstats container on the host
  119. infos, err := inspectContainer(client, fmt.Sprintf("%s_ethstats_1", network))
  120. if err != nil {
  121. return nil, err
  122. }
  123. if !infos.running {
  124. return nil, ErrServiceOffline
  125. }
  126. // Resolve the port from the host, or the reverse proxy
  127. port := infos.portmap["3000/tcp"]
  128. if port == 0 {
  129. if proxy, _ := checkNginx(client, network); proxy != nil {
  130. port = proxy.port
  131. }
  132. }
  133. if port == 0 {
  134. return nil, ErrNotExposed
  135. }
  136. // Resolve the host from the reverse-proxy and configure the connection string
  137. host := infos.envvars["VIRTUAL_HOST"]
  138. if host == "" {
  139. host = client.server
  140. }
  141. secret := infos.envvars["WS_SECRET"]
  142. config := fmt.Sprintf("%s@%s", secret, host)
  143. if port != 80 && port != 443 {
  144. config += fmt.Sprintf(":%d", port)
  145. }
  146. // Retrieve the IP blacklist
  147. banned := strings.Split(infos.envvars["BANNED"], ",")
  148. // Run a sanity check to see if the port is reachable
  149. if err = checkPort(host, port); err != nil {
  150. log.Warn("Ethstats service seems unreachable", "server", host, "port", port, "err", err)
  151. }
  152. // Container available, assemble and return the useful infos
  153. return &ethstatsInfos{
  154. host: host,
  155. port: port,
  156. secret: secret,
  157. config: config,
  158. banned: banned,
  159. }, nil
  160. }