module_ethstats.go 6.0 KB

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