module_ethstats.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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: []};' > 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}}
  54. restart: always
  55. `
  56. // deployEthstats deploys a new ethstats container to a remote machine via SSH,
  57. // docker and docker-compose. If an instance with the specified network name
  58. // already exists there, it will be overwritten!
  59. func deployEthstats(client *sshClient, network string, port int, secret string, vhost string, trusted []string) ([]byte, error) {
  60. // Generate the content to upload to the server
  61. workdir := fmt.Sprintf("%d", rand.Int63())
  62. files := make(map[string][]byte)
  63. for i, address := range trusted {
  64. trusted[i] = fmt.Sprintf("\"%s\"", address)
  65. }
  66. dockerfile := new(bytes.Buffer)
  67. template.Must(template.New("").Parse(ethstatsDockerfile)).Execute(dockerfile, map[string]interface{}{
  68. "Trusted": strings.Join(trusted, ", "),
  69. })
  70. files[filepath.Join(workdir, "Dockerfile")] = dockerfile.Bytes()
  71. composefile := new(bytes.Buffer)
  72. template.Must(template.New("").Parse(ethstatsComposefile)).Execute(composefile, map[string]interface{}{
  73. "Network": network,
  74. "Port": port,
  75. "Secret": secret,
  76. "VHost": vhost,
  77. })
  78. files[filepath.Join(workdir, "docker-compose.yaml")] = composefile.Bytes()
  79. // Upload the deployment files to the remote server (and clean up afterwards)
  80. if out, err := client.Upload(files); err != nil {
  81. return out, err
  82. }
  83. defer client.Run("rm -rf " + workdir)
  84. // Build and deploy the ethstats service
  85. return nil, client.Stream(fmt.Sprintf("cd %s && docker-compose -p %s up -d --build", workdir, network))
  86. }
  87. // ethstatsInfos is returned from an ethstats status check to allow reporting
  88. // various configuration parameters.
  89. type ethstatsInfos struct {
  90. host string
  91. port int
  92. secret string
  93. config string
  94. }
  95. // String implements the stringer interface.
  96. func (info *ethstatsInfos) String() string {
  97. return fmt.Sprintf("host=%s, port=%d, secret=%s", info.host, info.port, info.secret)
  98. }
  99. // checkEthstats does a health-check against an ethstats server to verify whether
  100. // it's running, and if yes, gathering a collection of useful infos about it.
  101. func checkEthstats(client *sshClient, network string) (*ethstatsInfos, error) {
  102. // Inspect a possible ethstats container on the host
  103. infos, err := inspectContainer(client, fmt.Sprintf("%s_ethstats_1", network))
  104. if err != nil {
  105. return nil, err
  106. }
  107. if !infos.running {
  108. return nil, ErrServiceOffline
  109. }
  110. // Resolve the port from the host, or the reverse proxy
  111. port := infos.portmap["3000/tcp"]
  112. if port == 0 {
  113. if proxy, _ := checkNginx(client, network); proxy != nil {
  114. port = proxy.port
  115. }
  116. }
  117. if port == 0 {
  118. return nil, ErrNotExposed
  119. }
  120. // Resolve the host from the reverse-proxy and configure the connection string
  121. host := infos.envvars["VIRTUAL_HOST"]
  122. if host == "" {
  123. host = client.server
  124. }
  125. secret := infos.envvars["WS_SECRET"]
  126. config := fmt.Sprintf("%s@%s", secret, host)
  127. if port != 80 && port != 443 {
  128. config += fmt.Sprintf(":%d", port)
  129. }
  130. // Run a sanity check to see if the port is reachable
  131. if err = checkPort(host, port); err != nil {
  132. log.Warn("Ethstats service seems unreachable", "server", host, "port", port, "err", err)
  133. }
  134. // Container available, assemble and return the useful infos
  135. return &ethstatsInfos{
  136. host: host,
  137. port: port,
  138. secret: secret,
  139. config: config,
  140. }, nil
  141. }