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