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

     1  // Copyright 2018 The klaytn Authors
     2  // Copyright 2017 AMIS Technologies
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package compose
    19  
    20  import (
    21  	"bytes"
    22  	"fmt"
    23  	"strings"
    24  	"text/template"
    25  
    26  	"github.com/klaytn/klaytn/cmd/homi/docker/service"
    27  )
    28  
    29  type Homi struct {
    30  	IPPrefix          string
    31  	EthStats          *service.KlayStats
    32  	Services          []*service.Validator
    33  	PrometheusService *service.PrometheusService
    34  	GrafanaService    *service.GrafanaService
    35  	UseGrafana        bool
    36  	Proxies           []*service.Validator
    37  	UseTxGen          bool
    38  	TxGenService      *service.TxGenService
    39  	TxGenOpt          service.TxGenOption
    40  }
    41  
    42  func New(ipPrefix string, number int, secret string, addresses, nodeKeys []string,
    43  	genesis, scGenesis, staticCNNodes, staticPNNodes, staticENNodes, staticSCNNodes, staticSPNNodes, staticSENNodes,
    44  	bridgeNodes, dockerImageId string, useFastHttp bool, networkId, parentChainId int,
    45  	useGrafana bool, proxyNodeKeys, enNodeKeys, scnNodeKeys, spnNodeKeys, senNodeKeys []string, useTxGen bool,
    46  	txGenOpt service.TxGenOption,
    47  ) *Homi {
    48  	ist := &Homi{
    49  		IPPrefix:   ipPrefix,
    50  		EthStats:   service.NewEthStats(fmt.Sprintf("%v.9", ipPrefix), secret),
    51  		UseGrafana: useGrafana,
    52  		UseTxGen:   useTxGen,
    53  	}
    54  	ist.init(number, addresses, nodeKeys, genesis, scGenesis, staticCNNodes, staticPNNodes, staticENNodes,
    55  		staticSCNNodes, staticSPNNodes, staticSENNodes, bridgeNodes, dockerImageId, useFastHttp, networkId,
    56  		parentChainId, proxyNodeKeys, enNodeKeys, scnNodeKeys, spnNodeKeys, senNodeKeys, txGenOpt)
    57  	return ist
    58  }
    59  
    60  func (ist *Homi) init(number int, addresses, nodeKeys []string, genesis, scGenesis, staticCNNodes, staticPNNodes,
    61  	staticENNodes, staticSCNNodes, staticSPNNodes, staticSENNodes, bridgeNodes, dockerImageId string, useFastHttp bool,
    62  	networkId, parentChainId int, proxyNodeKeys, enNodeKeys, scnNodeKeys, spnNodeKeys, senNodeKeys []string, txGenOpt service.TxGenOption,
    63  ) {
    64  	var validatorNames []string
    65  	for i := 0; i < number; i++ {
    66  		s := service.NewValidator(i,
    67  			genesis,
    68  			"",
    69  			addresses[i],
    70  			nodeKeys[i],
    71  			"",
    72  			"",
    73  			32323+i,
    74  			8551+i,
    75  			61001+i,
    76  			ist.EthStats.Host(),
    77  			// from subnet ip 10
    78  			fmt.Sprintf("%v.%v", ist.IPPrefix, i+10),
    79  			dockerImageId,
    80  			useFastHttp,
    81  			networkId,
    82  			0,
    83  			"CN",
    84  			"cn",
    85  			false,
    86  		)
    87  
    88  		staticCNNodes = strings.Replace(staticCNNodes, "0.0.0.0", s.IP, 1)
    89  		ist.Services = append(ist.Services, s)
    90  		validatorNames = append(validatorNames, s.Name)
    91  	}
    92  
    93  	numPNs := len(proxyNodeKeys)
    94  	for i := 0; i < numPNs; i++ {
    95  		s := service.NewValidator(i,
    96  			genesis,
    97  			"",
    98  			"",
    99  			proxyNodeKeys[i],
   100  			"",
   101  			"",
   102  			32323+number+i,
   103  			8551+number+i,
   104  			61001+number+i,
   105  			ist.EthStats.Host(),
   106  			// from subnet ip 10
   107  			fmt.Sprintf("%v.%v", ist.IPPrefix, number+i+10),
   108  			dockerImageId,
   109  			useFastHttp,
   110  			networkId,
   111  			0,
   112  			"PN",
   113  			"pn",
   114  			false,
   115  		)
   116  
   117  		staticPNNodes = strings.Replace(staticPNNodes, "0.0.0.0", s.IP, 1)
   118  		ist.Services = append(ist.Services, s)
   119  		validatorNames = append(validatorNames, s.Name)
   120  	}
   121  
   122  	numENs := len(enNodeKeys)
   123  	for i := 0; i < len(enNodeKeys); i++ {
   124  		s := service.NewValidator(i,
   125  			genesis,
   126  			"",
   127  			"",
   128  			enNodeKeys[i],
   129  			"",
   130  			"",
   131  			32323+number+numPNs+i,
   132  			8551+number+numPNs+i,
   133  			61001+number+numPNs+i,
   134  			ist.EthStats.Host(),
   135  			// from subnet ip 10
   136  			fmt.Sprintf("%v.%v", ist.IPPrefix, number+numPNs+i+10),
   137  			dockerImageId,
   138  			useFastHttp,
   139  			networkId,
   140  			0,
   141  			"EN",
   142  			"en",
   143  			false,
   144  		)
   145  		if i == 0 {
   146  			bridgeNodes = strings.Replace(bridgeNodes, "0.0.0.0", s.IP, 1)
   147  			bridgeNodes = strings.Replace(bridgeNodes, "32323", "50505", 1)
   148  		}
   149  		staticENNodes = strings.Replace(staticENNodes, "0.0.0.0", s.IP, 1)
   150  		ist.Services = append(ist.Services, s)
   151  		validatorNames = append(validatorNames, s.Name)
   152  	}
   153  
   154  	numSCNs := len(scnNodeKeys)
   155  	scnParentChainId := parentChainId
   156  	scnBridgeNodes := bridgeNodes
   157  	if len(spnNodeKeys) > 0 || len(senNodeKeys) > 0 {
   158  		scnParentChainId = 0
   159  		scnBridgeNodes = ""
   160  	}
   161  	for i := 0; i < len(scnNodeKeys); i++ {
   162  		s := service.NewValidator(i,
   163  			"",
   164  			scGenesis,
   165  			"",
   166  			scnNodeKeys[i],
   167  			"",
   168  			scnBridgeNodes,
   169  			32323+number+numPNs+numENs+i,
   170  			8551+number+numPNs+numENs+i,
   171  			61001+number+numPNs+numENs+i,
   172  			ist.EthStats.Host(),
   173  			// from subnet ip 10
   174  			fmt.Sprintf("%v.%v", ist.IPPrefix, number+numPNs+numENs+i+10),
   175  			dockerImageId,
   176  			useFastHttp,
   177  			networkId,
   178  			scnParentChainId,
   179  			"SCN",
   180  			"scn",
   181  			false,
   182  		)
   183  
   184  		staticSCNNodes = strings.Replace(staticSCNNodes, "0.0.0.0", s.IP, 1)
   185  		ist.Services = append(ist.Services, s)
   186  		validatorNames = append(validatorNames, s.Name)
   187  	}
   188  
   189  	numSPNs := len(spnNodeKeys)
   190  	spnParentChainId := parentChainId
   191  	spnBridgeNodes := bridgeNodes
   192  	if len(senNodeKeys) > 0 {
   193  		spnParentChainId = 0
   194  		spnBridgeNodes = ""
   195  	}
   196  	for i := 0; i < len(spnNodeKeys); i++ {
   197  		s := service.NewValidator(i,
   198  			"",
   199  			scGenesis,
   200  			"",
   201  			spnNodeKeys[i],
   202  			"",
   203  			spnBridgeNodes,
   204  			32323+number+numPNs+numENs+numSCNs+i,
   205  			8551+number+numPNs+numENs+numSCNs+i,
   206  			61001+number+numPNs+numENs+numSCNs+i,
   207  			ist.EthStats.Host(),
   208  			// from subnet ip 10
   209  			fmt.Sprintf("%v.%v", ist.IPPrefix, number+numPNs+numENs+numSCNs+i+10),
   210  			dockerImageId,
   211  			useFastHttp,
   212  			networkId,
   213  			spnParentChainId,
   214  			"SPN",
   215  			"spn",
   216  			false,
   217  		)
   218  
   219  		staticSPNNodes = strings.Replace(staticSPNNodes, "0.0.0.0", s.IP, 1)
   220  		ist.Services = append(ist.Services, s)
   221  		validatorNames = append(validatorNames, s.Name)
   222  	}
   223  
   224  	for i := 0; i < len(senNodeKeys); i++ {
   225  		s := service.NewValidator(i,
   226  			"",
   227  			scGenesis,
   228  			"",
   229  			senNodeKeys[i],
   230  			"",
   231  			bridgeNodes,
   232  			32323+number+numPNs+numENs+numSCNs+numSPNs+i,
   233  			8551+number+numPNs+numENs+numSCNs+numSPNs+i,
   234  			61001+number+numPNs+numENs+numSCNs+numSPNs+i,
   235  			ist.EthStats.Host(),
   236  			// from subnet ip 10
   237  			fmt.Sprintf("%v.%v", ist.IPPrefix, number+numPNs+numENs+numSCNs+numSPNs+i+10),
   238  			dockerImageId,
   239  			useFastHttp,
   240  			networkId,
   241  			parentChainId,
   242  			"SEN",
   243  			"sen",
   244  			false,
   245  		)
   246  
   247  		ist.Services = append(ist.Services, s)
   248  		validatorNames = append(validatorNames, s.Name)
   249  	}
   250  
   251  	// update static nodes
   252  	for i := range ist.Services {
   253  		if ist.Services[i].NodeType == "scn" {
   254  			ist.Services[i].StaticNodes = staticSCNNodes
   255  		} else if ist.Services[i].NodeType == "spn" {
   256  			ist.Services[i].StaticNodes = staticSCNNodes
   257  		} else if ist.Services[i].NodeType == "sen" {
   258  			ist.Services[i].StaticNodes = staticSPNNodes
   259  		} else if ist.Services[i].NodeType == "en" {
   260  			ist.Services[i].StaticNodes = staticPNNodes
   261  		} else {
   262  			ist.Services[i].StaticNodes = staticCNNodes
   263  		}
   264  	}
   265  
   266  	ist.PrometheusService = service.NewPrometheusService(
   267  		fmt.Sprintf("%v.%v", ist.IPPrefix, 9),
   268  		validatorNames)
   269  
   270  	if ist.UseGrafana {
   271  		ist.GrafanaService = service.NewGrafanaService(fmt.Sprintf("%v.%v", ist.IPPrefix, 8))
   272  	}
   273  
   274  	ist.TxGenService = service.NewTxGenService(
   275  		fmt.Sprintf("%v.%v", ist.IPPrefix, 7),
   276  		fmt.Sprintf("http://%v.%v:8551", ist.IPPrefix, number+10),
   277  		txGenOpt)
   278  }
   279  
   280  func (ist Homi) String() string {
   281  	tmpl, err := template.New("istanbul").Parse(istanbulTemplate)
   282  	if err != nil {
   283  		fmt.Printf("Failed to parse template, %v", err)
   284  		return ""
   285  	}
   286  
   287  	result := new(bytes.Buffer)
   288  	err = tmpl.Execute(result, ist)
   289  	if err != nil {
   290  		fmt.Printf("Failed to render template, %v", err)
   291  		return ""
   292  	}
   293  
   294  	return result.String()
   295  }
   296  
   297  var istanbulTemplate = `version: '3'
   298  services:
   299    {{- range .Services }}
   300    {{ . }}
   301    {{- end }}
   302    {{- range .Proxies }}
   303    {{ . }}
   304    {{- end }}
   305    {{ .PrometheusService }}
   306    {{- if .UseGrafana }}
   307    {{ .GrafanaService }}
   308    {{- end }}
   309    {{- if .UseTxGen }}
   310    {{ .TxGenService }}
   311    {{- end }}
   312  networks:
   313    app_net:
   314      driver: bridge
   315      ipam:
   316        driver: default
   317        config:
   318        - subnet: {{ .IPPrefix }}.0/24`