github.com/waltonchain/waltonchain_gwtc_src@v1.1.4-0.20201225072101-8a298c95a819/cmd/puppeth/module_nginx.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of go-wtc.
     3  //
     4  // go-wtc 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-wtc 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-wtc. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"html/template"
    23  	"math/rand"
    24  	"path/filepath"
    25  
    26  	"github.com/wtc/go-wtc/log"
    27  )
    28  
    29  // nginxDockerfile is theis the Dockerfile required to build an nginx reverse-
    30  // proxy.
    31  var nginxDockerfile = `FROM jwilder/nginx-proxy`
    32  
    33  // nginxComposefile is the docker-compose.yml file required to deploy and maintain
    34  // an nginx reverse-proxy. The proxy is responsible for exposing one or more HTTP
    35  // services running on a single host.
    36  var nginxComposefile = `
    37  version: '2'
    38  services:
    39    nginx:
    40      build: .
    41      image: {{.Network}}/nginx
    42      ports:
    43        - "{{.Port}}:80"
    44      volumes:
    45        - /var/run/docker.sock:/tmp/docker.sock:ro
    46      logging:
    47        driver: "json-file"
    48        options:
    49          max-size: "1m"
    50          max-file: "10"
    51      restart: always
    52  `
    53  
    54  // deployNginx deploys a new nginx reverse-proxy container to expose one or more
    55  // HTTP services running on a single host. If an instance with the specified
    56  // network name already exists there, it will be overwritten!
    57  func deployNginx(client *sshClient, network string, port int) ([]byte, error) {
    58  	log.Info("Deploying nginx reverse-proxy", "server", client.server, "port", port)
    59  
    60  	// Generate the content to upload to the server
    61  	workdir := fmt.Sprintf("%d", rand.Int63())
    62  	files := make(map[string][]byte)
    63  
    64  	dockerfile := new(bytes.Buffer)
    65  	template.Must(template.New("").Parse(nginxDockerfile)).Execute(dockerfile, nil)
    66  	files[filepath.Join(workdir, "Dockerfile")] = dockerfile.Bytes()
    67  
    68  	composefile := new(bytes.Buffer)
    69  	template.Must(template.New("").Parse(nginxComposefile)).Execute(composefile, map[string]interface{}{
    70  		"Network": network,
    71  		"Port":    port,
    72  	})
    73  	files[filepath.Join(workdir, "docker-compose.yaml")] = composefile.Bytes()
    74  
    75  	// Upload the deployment files to the remote server (and clean up afterwards)
    76  	if out, err := client.Upload(files); err != nil {
    77  		return out, err
    78  	}
    79  	defer client.Run("rm -rf " + workdir)
    80  
    81  	// Build and deploy the ethstats service
    82  	return nil, client.Stream(fmt.Sprintf("cd %s && docker-compose -p %s up -d --build", workdir, network))
    83  }
    84  
    85  // nginxInfos is returned from an nginx reverse-proxy status check to allow
    86  // reporting various configuration parameters.
    87  type nginxInfos struct {
    88  	port int
    89  }
    90  
    91  // String implements the stringer interface.
    92  func (info *nginxInfos) String() string {
    93  	return fmt.Sprintf("port=%d", info.port)
    94  }
    95  
    96  // checkNginx does a health-check against an nginx reverse-proxy to verify whether
    97  // it's running, and if yes, gathering a collection of useful infos about it.
    98  func checkNginx(client *sshClient, network string) (*nginxInfos, error) {
    99  	// Inspect a possible nginx container on the host
   100  	infos, err := inspectContainer(client, fmt.Sprintf("%s_nginx_1", network))
   101  	if err != nil {
   102  		return nil, err
   103  	}
   104  	if !infos.running {
   105  		return nil, ErrServiceOffline
   106  	}
   107  	// Container available, assemble and return the useful infos
   108  	return &nginxInfos{
   109  		port: infos.portmap["80/tcp"],
   110  	}, nil
   111  }