module_ethstats.go 5.6 KB

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