module_wallet.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. "html/template"
  21. "math/rand"
  22. "path/filepath"
  23. "strconv"
  24. "strings"
  25. "github.com/ethereum/go-ethereum/log"
  26. )
  27. // walletDockerfile is the Dockerfile required to run a web wallet.
  28. var walletDockerfile = `
  29. FROM puppeth/wallet:latest
  30. ADD genesis.json /genesis.json
  31. RUN \
  32. echo 'node server.js &' > wallet.sh && \
  33. echo 'geth --cache 512 init /genesis.json' >> wallet.sh && \
  34. echo $'geth --networkid {{.NetworkID}} --port {{.NodePort}} --bootnodes {{.Bootnodes}} --ethstats \'{{.Ethstats}}\' --cache=512 --rpc --rpcaddr=0.0.0.0 --rpccorsdomain "*" --rpcvhosts "*"' >> wallet.sh
  35. RUN \
  36. sed -i 's/PuppethNetworkID/{{.NetworkID}}/g' dist/js/etherwallet-master.js && \
  37. sed -i 's/PuppethNetwork/{{.Network}}/g' dist/js/etherwallet-master.js && \
  38. sed -i 's/PuppethDenom/{{.Denom}}/g' dist/js/etherwallet-master.js && \
  39. sed -i 's/PuppethHost/{{.Host}}/g' dist/js/etherwallet-master.js && \
  40. sed -i 's/PuppethRPCPort/{{.RPCPort}}/g' dist/js/etherwallet-master.js
  41. ENTRYPOINT ["/bin/sh", "wallet.sh"]
  42. `
  43. // walletComposefile is the docker-compose.yml file required to deploy and
  44. // maintain a web wallet.
  45. var walletComposefile = `
  46. version: '2'
  47. services:
  48. wallet:
  49. build: .
  50. image: {{.Network}}/wallet
  51. ports:
  52. - "{{.NodePort}}:{{.NodePort}}"
  53. - "{{.NodePort}}:{{.NodePort}}/udp"
  54. - "{{.RPCPort}}:8545"{{if not .VHost}}
  55. - "{{.WebPort}}:80"{{end}}
  56. volumes:
  57. - {{.Datadir}}:/root/.ethereum
  58. environment:
  59. - NODE_PORT={{.NodePort}}/tcp
  60. - STATS={{.Ethstats}}{{if .VHost}}
  61. - VIRTUAL_HOST={{.VHost}}
  62. - VIRTUAL_PORT=80{{end}}
  63. logging:
  64. driver: "json-file"
  65. options:
  66. max-size: "1m"
  67. max-file: "10"
  68. restart: always
  69. `
  70. // deployWallet deploys a new web wallet container to a remote machine via SSH,
  71. // docker and docker-compose. If an instance with the specified network name
  72. // already exists there, it will be overwritten!
  73. func deployWallet(client *sshClient, network string, bootnodes []string, config *walletInfos, nocache bool) ([]byte, error) {
  74. // Generate the content to upload to the server
  75. workdir := fmt.Sprintf("%d", rand.Int63())
  76. files := make(map[string][]byte)
  77. dockerfile := new(bytes.Buffer)
  78. template.Must(template.New("").Parse(walletDockerfile)).Execute(dockerfile, map[string]interface{}{
  79. "Network": strings.ToTitle(network),
  80. "Denom": strings.ToUpper(network),
  81. "NetworkID": config.network,
  82. "NodePort": config.nodePort,
  83. "RPCPort": config.rpcPort,
  84. "Bootnodes": strings.Join(bootnodes, ","),
  85. "Ethstats": config.ethstats,
  86. "Host": client.address,
  87. })
  88. files[filepath.Join(workdir, "Dockerfile")] = dockerfile.Bytes()
  89. composefile := new(bytes.Buffer)
  90. template.Must(template.New("").Parse(walletComposefile)).Execute(composefile, map[string]interface{}{
  91. "Datadir": config.datadir,
  92. "Network": network,
  93. "NodePort": config.nodePort,
  94. "RPCPort": config.rpcPort,
  95. "VHost": config.webHost,
  96. "WebPort": config.webPort,
  97. "Ethstats": config.ethstats[:strings.Index(config.ethstats, ":")],
  98. })
  99. files[filepath.Join(workdir, "docker-compose.yaml")] = composefile.Bytes()
  100. files[filepath.Join(workdir, "genesis.json")] = config.genesis
  101. // Upload the deployment files to the remote server (and clean up afterwards)
  102. if out, err := client.Upload(files); err != nil {
  103. return out, err
  104. }
  105. defer client.Run("rm -rf " + workdir)
  106. // Build and deploy the boot or seal node service
  107. if nocache {
  108. 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))
  109. }
  110. return nil, client.Stream(fmt.Sprintf("cd %s && docker-compose -p %s up -d --build --force-recreate", workdir, network))
  111. }
  112. // walletInfos is returned from a web wallet status check to allow reporting
  113. // various configuration parameters.
  114. type walletInfos struct {
  115. genesis []byte
  116. network int64
  117. datadir string
  118. ethstats string
  119. nodePort int
  120. rpcPort int
  121. webHost string
  122. webPort int
  123. }
  124. // Report converts the typed struct into a plain string->string map, containing
  125. // most - but not all - fields for reporting to the user.
  126. func (info *walletInfos) Report() map[string]string {
  127. report := map[string]string{
  128. "Data directory": info.datadir,
  129. "Ethstats username": info.ethstats,
  130. "Node listener port ": strconv.Itoa(info.nodePort),
  131. "RPC listener port ": strconv.Itoa(info.rpcPort),
  132. "Website address ": info.webHost,
  133. "Website listener port ": strconv.Itoa(info.webPort),
  134. }
  135. return report
  136. }
  137. // checkWallet does a health-check against web wallet server to verify whether
  138. // it's running, and if yes, whether it's responsive.
  139. func checkWallet(client *sshClient, network string) (*walletInfos, error) {
  140. // Inspect a possible web wallet container on the host
  141. infos, err := inspectContainer(client, fmt.Sprintf("%s_wallet_1", network))
  142. if err != nil {
  143. return nil, err
  144. }
  145. if !infos.running {
  146. return nil, ErrServiceOffline
  147. }
  148. // Resolve the port from the host, or the reverse proxy
  149. webPort := infos.portmap["80/tcp"]
  150. if webPort == 0 {
  151. if proxy, _ := checkNginx(client, network); proxy != nil {
  152. webPort = proxy.port
  153. }
  154. }
  155. if webPort == 0 {
  156. return nil, ErrNotExposed
  157. }
  158. // Resolve the host from the reverse-proxy and the config values
  159. host := infos.envvars["VIRTUAL_HOST"]
  160. if host == "" {
  161. host = client.server
  162. }
  163. // Run a sanity check to see if the devp2p and RPC ports are reachable
  164. nodePort := infos.portmap[infos.envvars["NODE_PORT"]]
  165. if err = checkPort(client.server, nodePort); err != nil {
  166. log.Warn(fmt.Sprintf("Wallet devp2p port seems unreachable"), "server", client.server, "port", nodePort, "err", err)
  167. }
  168. rpcPort := infos.portmap["8545/tcp"]
  169. if err = checkPort(client.server, rpcPort); err != nil {
  170. log.Warn(fmt.Sprintf("Wallet RPC port seems unreachable"), "server", client.server, "port", rpcPort, "err", err)
  171. }
  172. // Assemble and return the useful infos
  173. stats := &walletInfos{
  174. datadir: infos.volumes["/root/.ethereum"],
  175. nodePort: nodePort,
  176. rpcPort: rpcPort,
  177. webHost: host,
  178. webPort: webPort,
  179. ethstats: infos.envvars["STATS"],
  180. }
  181. return stats, nil
  182. }