module_faucet.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. // faucetDockerfile is the Dockerfile required to build an faucet container to
  28. // grant crypto tokens based on GitHub authentications.
  29. var faucetDockerfile = `
  30. FROM alpine:latest
  31. RUN mkdir /go
  32. ENV GOPATH /go
  33. RUN \
  34. apk add --update git go make gcc musl-dev ca-certificates linux-headers && \
  35. mkdir -p $GOPATH/src/github.com/ethereum && \
  36. (cd $GOPATH/src/github.com/ethereum && git clone --depth=1 https://github.com/ethereum/go-ethereum) && \
  37. go build -v github.com/ethereum/go-ethereum/cmd/faucet && \
  38. apk del git go make gcc musl-dev linux-headers && \
  39. rm -rf $GOPATH && rm -rf /var/cache/apk/*
  40. ADD genesis.json /genesis.json
  41. ADD account.json /account.json
  42. ADD account.pass /account.pass
  43. EXPOSE 8080
  44. CMD [ \
  45. "/faucet", "--genesis", "/genesis.json", "--network", "{{.NetworkID}}", "--bootnodes", "{{.Bootnodes}}", "--ethstats", "{{.Ethstats}}", \
  46. "--ethport", "{{.EthPort}}", "--faucet.name", "{{.FaucetName}}", "--faucet.amount", "{{.FaucetAmount}}", "--faucet.minutes", "{{.FaucetMinutes}}", \
  47. "--github.user", "{{.GitHubUser}}", "--github.token", "{{.GitHubToken}}", "--account.json", "/account.json", "--account.pass", "/account.pass" \
  48. ]`
  49. // faucetComposefile is the docker-compose.yml file required to deploy and maintain
  50. // a crypto faucet.
  51. var faucetComposefile = `
  52. version: '2'
  53. services:
  54. faucet:
  55. build: .
  56. image: {{.Network}}/faucet
  57. ports:
  58. - "{{.EthPort}}:{{.EthPort}}"{{if not .VHost}}
  59. - "{{.ApiPort}}:8080"{{end}}
  60. volumes:
  61. - {{.Datadir}}:/root/.faucet
  62. environment:
  63. - ETH_PORT={{.EthPort}}
  64. - ETH_NAME={{.EthName}}
  65. - FAUCET_AMOUNT={{.FaucetAmount}}
  66. - FAUCET_MINUTES={{.FaucetMinutes}}
  67. - GITHUB_USER={{.GitHubUser}}
  68. - GITHUB_TOKEN={{.GitHubToken}}{{if .VHost}}
  69. - VIRTUAL_HOST={{.VHost}}
  70. - VIRTUAL_PORT=8080{{end}}
  71. restart: always
  72. `
  73. // deployFaucet deploys a new faucet container to a remote machine via SSH,
  74. // docker and docker-compose. If an instance with the specified network name
  75. // already exists there, it will be overwritten!
  76. func deployFaucet(client *sshClient, network string, bootnodes []string, config *faucetInfos) ([]byte, error) {
  77. // Generate the content to upload to the server
  78. workdir := fmt.Sprintf("%d", rand.Int63())
  79. files := make(map[string][]byte)
  80. dockerfile := new(bytes.Buffer)
  81. template.Must(template.New("").Parse(faucetDockerfile)).Execute(dockerfile, map[string]interface{}{
  82. "NetworkID": config.node.network,
  83. "Bootnodes": strings.Join(bootnodes, ","),
  84. "Ethstats": config.node.ethstats,
  85. "EthPort": config.node.portFull,
  86. "GitHubUser": config.githubUser,
  87. "GitHubToken": config.githubToken,
  88. "FaucetName": strings.Title(network),
  89. "FaucetAmount": config.amount,
  90. "FaucetMinutes": config.minutes,
  91. })
  92. files[filepath.Join(workdir, "Dockerfile")] = dockerfile.Bytes()
  93. composefile := new(bytes.Buffer)
  94. template.Must(template.New("").Parse(faucetComposefile)).Execute(composefile, map[string]interface{}{
  95. "Network": network,
  96. "Datadir": config.node.datadir,
  97. "VHost": config.host,
  98. "ApiPort": config.port,
  99. "EthPort": config.node.portFull,
  100. "EthName": config.node.ethstats[:strings.Index(config.node.ethstats, ":")],
  101. "GitHubUser": config.githubUser,
  102. "GitHubToken": config.githubToken,
  103. "FaucetAmount": config.amount,
  104. "FaucetMinutes": config.minutes,
  105. })
  106. files[filepath.Join(workdir, "docker-compose.yaml")] = composefile.Bytes()
  107. files[filepath.Join(workdir, "genesis.json")] = []byte(config.node.genesis)
  108. files[filepath.Join(workdir, "account.json")] = []byte(config.node.keyJSON)
  109. files[filepath.Join(workdir, "account.pass")] = []byte(config.node.keyPass)
  110. // Upload the deployment files to the remote server (and clean up afterwards)
  111. if out, err := client.Upload(files); err != nil {
  112. return out, err
  113. }
  114. defer client.Run("rm -rf " + workdir)
  115. // Build and deploy the faucet service
  116. return nil, client.Stream(fmt.Sprintf("cd %s && docker-compose -p %s up -d --build", workdir, network))
  117. }
  118. // faucetInfos is returned from an faucet status check to allow reporting various
  119. // configuration parameters.
  120. type faucetInfos struct {
  121. node *nodeInfos
  122. host string
  123. port int
  124. amount int
  125. minutes int
  126. githubUser string
  127. githubToken string
  128. }
  129. // String implements the stringer interface.
  130. func (info *faucetInfos) String() string {
  131. return fmt.Sprintf("host=%s, api=%d, eth=%d, amount=%d, minutes=%d, github=%s, ethstats=%s", info.host, info.port, info.node.portFull, info.amount, info.minutes, info.githubUser, info.node.ethstats)
  132. }
  133. // checkFaucet does a health-check against an faucet server to verify whether
  134. // it's running, and if yes, gathering a collection of useful infos about it.
  135. func checkFaucet(client *sshClient, network string) (*faucetInfos, error) {
  136. // Inspect a possible faucet container on the host
  137. infos, err := inspectContainer(client, fmt.Sprintf("%s_faucet_1", network))
  138. if err != nil {
  139. return nil, err
  140. }
  141. if !infos.running {
  142. return nil, ErrServiceOffline
  143. }
  144. // Resolve the port from the host, or the reverse proxy
  145. port := infos.portmap["8080/tcp"]
  146. if port == 0 {
  147. if proxy, _ := checkNginx(client, network); proxy != nil {
  148. port = proxy.port
  149. }
  150. }
  151. if port == 0 {
  152. return nil, ErrNotExposed
  153. }
  154. // Resolve the host from the reverse-proxy and the config values
  155. host := infos.envvars["VIRTUAL_HOST"]
  156. if host == "" {
  157. host = client.server
  158. }
  159. amount, _ := strconv.Atoi(infos.envvars["FAUCET_AMOUNT"])
  160. minutes, _ := strconv.Atoi(infos.envvars["FAUCET_MINUTES"])
  161. // Retrieve the funding account informations
  162. var out []byte
  163. keyJSON, keyPass := "", ""
  164. if out, err = client.Run(fmt.Sprintf("docker exec %s_faucet_1 cat /account.json", network)); err == nil {
  165. keyJSON = string(bytes.TrimSpace(out))
  166. }
  167. if out, err = client.Run(fmt.Sprintf("docker exec %s_faucet_1 cat /account.pass", network)); err == nil {
  168. keyPass = string(bytes.TrimSpace(out))
  169. }
  170. // Run a sanity check to see if the port is reachable
  171. if err = checkPort(host, port); err != nil {
  172. log.Warn("Faucet service seems unreachable", "server", host, "port", port, "err", err)
  173. }
  174. // Container available, assemble and return the useful infos
  175. return &faucetInfos{
  176. node: &nodeInfos{
  177. datadir: infos.volumes["/root/.faucet"],
  178. portFull: infos.portmap[infos.envvars["ETH_PORT"]+"/tcp"],
  179. ethstats: infos.envvars["ETH_NAME"],
  180. keyJSON: keyJSON,
  181. keyPass: keyPass,
  182. },
  183. host: host,
  184. port: port,
  185. amount: amount,
  186. minutes: minutes,
  187. githubUser: infos.envvars["GITHUB_USER"],
  188. githubToken: infos.envvars["GITHUB_TOKEN"],
  189. }, nil
  190. }