github.com/klaytn/klaytn@v1.12.1/cmd/homi/docker/service/constellation.go (about)

     1  // Copyright 2017 AMIS Technologies
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser 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  // The go-ethereum library 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 Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package service
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"strings"
    23  	"text/template"
    24  )
    25  
    26  type Constellation struct {
    27  	Identity   int
    28  	Name       string
    29  	IP         string
    30  	Port       int
    31  	OtherNodes string
    32  	PublicKey  string
    33  	PrivateKey string
    34  	SocketPath string
    35  	ConfigPath string
    36  	Folder     string
    37  	KeyPath    string
    38  }
    39  
    40  func (c *Constellation) SetOtherNodes(nodes []string) {
    41  	c.OtherNodes = strings.Join(nodes, ",")
    42  }
    43  
    44  func (c Constellation) Host() string {
    45  	return fmt.Sprintf("http://%v:%v/", c.IP, c.Port)
    46  }
    47  
    48  func (c Constellation) String() string {
    49  	tmpl, err := template.New("constellation").Parse(constellationTemplate)
    50  	if err != nil {
    51  		fmt.Printf("Failed to parse template, %v", err)
    52  		return ""
    53  	}
    54  
    55  	result := new(bytes.Buffer)
    56  	err = tmpl.Execute(result, c)
    57  	if err != nil {
    58  		fmt.Printf("Failed to render template, %v", err)
    59  		return ""
    60  	}
    61  
    62  	return result.String()
    63  }
    64  
    65  var constellationTemplate = `{{ .Name }}:
    66      hostname: {{ .Name }}
    67      image: quay.io/amis/constellation:latest
    68      ports:
    69        - '{{ .Port }}:{{ .Port }}'
    70      volumes:
    71        - {{ .Identity }}:{{ .Folder }}:z
    72        - .:/tmp/
    73      entrypoint:
    74        - /bin/sh
    75        - -c
    76        - |
    77          mkdir -p {{ .Folder }}
    78          echo "socket=\"{{ .SocketPath }}\"\npublickeys=[\"{{ .PublicKey }}\"]\n" > {{ .ConfigPath }}
    79          constellation-node --generatekeys={{ .KeyPath }}
    80          cp {{ .KeyPath }}.pub /tmp/tm{{ .Identity }}.pub
    81          constellation-node \
    82            --url={{ .Host }} \
    83            --port={{ .Port }} \
    84            --socket={{ .SocketPath }} \
    85            --othernodes={{ .OtherNodes }} \
    86            --publickeys={{ .PublicKey }} \
    87            --privatekeys={{ .PrivateKey }} \
    88            --storage={{ .Folder }} \
    89            --verbosity=4
    90      networks:
    91        app_net:
    92          ipv4_address: {{ .IP }}
    93      restart: always`