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