github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/cmd/puppeth/module_ethstats.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:32</date>
    10  //</624450069281640448>
    11  
    12  
    13  package main
    14  
    15  import (
    16  	"bytes"
    17  	"fmt"
    18  	"math/rand"
    19  	"path/filepath"
    20  	"strconv"
    21  	"strings"
    22  	"text/template"
    23  
    24  	"github.com/ethereum/go-ethereum/log"
    25  )
    26  
    27  //ethstatsDockerfile是构建ethstats后端所需的Dockerfile。
    28  //以及相关的监测站点。
    29  var ethstatsDockerfile = `
    30  FROM puppeth/ethstats:latest
    31  
    32  RUN echo 'module.exports = {trusted: [{{.Trusted}}], banned: [{{.Banned}}], reserved: ["yournode"]};' > lib/utils/config.js
    33  `
    34  
    35  //ethstatscomposefile是部署和
    36  //维护一个ethstats监控站点。
    37  var ethstatsComposefile = `
    38  version: '2'
    39  services:
    40    ethstats:
    41      build: .
    42      image: {{.Network}}/ethstats
    43      container_name: {{.Network}}_ethstats_1{{if not .VHost}}
    44      ports:
    45        - "{{.Port}}:3000"{{end}}
    46      environment:
    47        - WS_SECRET={{.Secret}}{{if .VHost}}
    48        - VIRTUAL_HOST={{.VHost}}{{end}}{{if .Banned}}
    49        - BANNED={{.Banned}}{{end}}
    50      logging:
    51        driver: "json-file"
    52        options:
    53          max-size: "1m"
    54          max-file: "10"
    55      restart: always
    56  `
    57  
    58  //deployethstats通过ssh将新的ethstats容器部署到远程计算机,
    59  //Docker和Docker组合。如果具有指定网络名称的实例
    60  //已经存在,将被覆盖!
    61  func deployEthstats(client *sshClient, network string, port int, secret string, vhost string, trusted []string, banned []string, nocache bool) ([]byte, error) {
    62  //生成要上载到服务器的内容
    63  	workdir := fmt.Sprintf("%d", rand.Int63())
    64  	files := make(map[string][]byte)
    65  
    66  	trustedLabels := make([]string, len(trusted))
    67  	for i, address := range trusted {
    68  		trustedLabels[i] = fmt.Sprintf("\"%s\"", address)
    69  	}
    70  	bannedLabels := make([]string, len(banned))
    71  	for i, address := range banned {
    72  		bannedLabels[i] = fmt.Sprintf("\"%s\"", address)
    73  	}
    74  
    75  	dockerfile := new(bytes.Buffer)
    76  	template.Must(template.New("").Parse(ethstatsDockerfile)).Execute(dockerfile, map[string]interface{}{
    77  		"Trusted": strings.Join(trustedLabels, ", "),
    78  		"Banned":  strings.Join(bannedLabels, ", "),
    79  	})
    80  	files[filepath.Join(workdir, "Dockerfile")] = dockerfile.Bytes()
    81  
    82  	composefile := new(bytes.Buffer)
    83  	template.Must(template.New("").Parse(ethstatsComposefile)).Execute(composefile, map[string]interface{}{
    84  		"Network": network,
    85  		"Port":    port,
    86  		"Secret":  secret,
    87  		"VHost":   vhost,
    88  		"Banned":  strings.Join(banned, ","),
    89  	})
    90  	files[filepath.Join(workdir, "docker-compose.yaml")] = composefile.Bytes()
    91  
    92  //将部署文件上载到远程服务器(然后清理)
    93  	if out, err := client.Upload(files); err != nil {
    94  		return out, err
    95  	}
    96  	defer client.Run("rm -rf " + workdir)
    97  
    98  //构建和部署ethstats服务
    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 --timeout 60", workdir, network, network))
   101  	}
   102  	return nil, client.Stream(fmt.Sprintf("cd %s && docker-compose -p %s up -d --build --force-recreate --timeout 60", workdir, network))
   103  }
   104  
   105  //ethstatsInfo从ethstats状态检查返回以允许报告
   106  //各种配置参数。
   107  type ethstatsInfos struct {
   108  	host   string
   109  	port   int
   110  	secret string
   111  	config string
   112  	banned []string
   113  }
   114  
   115  //报表将类型化结构转换为纯字符串->字符串映射,其中包含
   116  //大多数(但不是全部)字段用于向用户报告。
   117  func (info *ethstatsInfos) Report() map[string]string {
   118  	return map[string]string{
   119  		"Website address":       info.host,
   120  		"Website listener port": strconv.Itoa(info.port),
   121  		"Login secret":          info.secret,
   122  		"Banned addresses":      strings.Join(info.banned, "\n"),
   123  	}
   124  }
   125  
   126  //check ethstats对ethstats服务器执行运行状况检查,以验证
   127  //它正在运行,如果是,收集有关它的有用信息。
   128  func checkEthstats(client *sshClient, network string) (*ethstatsInfos, error) {
   129  //检查主机上可能的ethstats容器
   130  	infos, err := inspectContainer(client, fmt.Sprintf("%s_ethstats_1", network))
   131  	if err != nil {
   132  		return nil, err
   133  	}
   134  	if !infos.running {
   135  		return nil, ErrServiceOffline
   136  	}
   137  //从主机或反向代理解析端口
   138  	port := infos.portmap["3000/tcp"]
   139  	if port == 0 {
   140  		if proxy, _ := checkNginx(client, network); proxy != nil {
   141  			port = proxy.port
   142  		}
   143  	}
   144  	if port == 0 {
   145  		return nil, ErrNotExposed
   146  	}
   147  //从反向代理解析主机并配置连接字符串
   148  	host := infos.envvars["VIRTUAL_HOST"]
   149  	if host == "" {
   150  		host = client.server
   151  	}
   152  	secret := infos.envvars["WS_SECRET"]
   153  	config := fmt.Sprintf("%s@%s", secret, host)
   154  	if port != 80 && port != 443 {
   155  		config += fmt.Sprintf(":%d", port)
   156  	}
   157  //检索IP黑名单
   158  	banned := strings.Split(infos.envvars["BANNED"], ",")
   159  
   160  //运行健全检查以查看端口是否可访问
   161  	if err = checkPort(host, port); err != nil {
   162  		log.Warn("Ethstats service seems unreachable", "server", host, "port", port, "err", err)
   163  	}
   164  //容器可用,组装并返回有用的信息
   165  	return &ethstatsInfos{
   166  		host:   host,
   167  		port:   port,
   168  		secret: secret,
   169  		config: config,
   170  		banned: banned,
   171  	}, nil
   172  }
   173