github.com/braveheart12/insolar-09-08-19@v0.8.7/scripts/generate_insolar_configs.go (about) 1 /* 2 * Copyright 2019 Insolar Technologies 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "bytes" 21 "fmt" 22 "html/template" 23 "os" 24 "path/filepath" 25 "strconv" 26 "strings" 27 "time" 28 29 pulsewatcher "github.com/insolar/insolar/cmd/pulsewatcher/config" 30 "github.com/insolar/insolar/configuration" 31 "github.com/insolar/insolar/genesis" 32 "github.com/spf13/cobra" 33 yaml "gopkg.in/yaml.v2" 34 ) 35 36 func check(msg string, err error) { 37 if err != nil { 38 fmt.Println(msg, err) 39 os.Exit(1) 40 } 41 } 42 43 const ( 44 defaultOutputConfigNameTmpl = "insolar_%d.yaml" 45 defaultHost = "127.0.0.1" 46 defaultJaegerEndPoint = defaultHost + ":6831" 47 defaultLogLevel = "Debug" 48 defaultGenesisFile = "genesis.yaml" 49 defaultPulsarTemplate = "scripts/insolard/pulsar_template.yaml" 50 discoveryDataDirectoryTemplate = "scripts/insolard/discoverynodes/%d/data" 51 discoveryCertificatePathTemplate = "scripts/insolard/discoverynodes/%d/cert.json" 52 nodeDataDirectoryTemplate = "scripts/insolard/nodes/%d/data" 53 nodeCertificatePathTemplate = "scripts/insolard/nodes/%d/cert.json" 54 pulsewatcherFileName = "pulsewatcher.yaml" 55 56 prometheusConfigTmpl = "scripts/prom/server.yml.tmpl" 57 prometheusFileName = "prometheus.yaml" 58 ) 59 60 var ( 61 genesisFile string 62 outputDir string 63 debugLevel string 64 gorundPortsPath string 65 pulsarTemplate string 66 ) 67 68 func parseInputParams() { 69 var rootCmd = &cobra.Command{} 70 71 rootCmd.Flags().StringVarP(&genesisFile, "genesis", "g", defaultGenesisFile, "input genesis file") 72 rootCmd.Flags().StringVarP(&pulsarTemplate, "pulsar-template", "t", defaultPulsarTemplate, "path to pulsar template file") 73 rootCmd.Flags().StringVarP(&outputDir, "output", "o", "", "output directory ( required )") 74 rootCmd.Flags().StringVarP(&debugLevel, "debuglevel", "d", defaultLogLevel, "debug level") 75 rootCmd.Flags().StringVarP(&gorundPortsPath, "gorundports", "p", "", "path to insgorund ports ( required )") 76 77 err := rootCmd.Execute() 78 check("Wrong input params:", err) 79 80 if outputDir == "" || gorundPortsPath == "" { 81 err := rootCmd.Usage() 82 check("[ parseInputParams ]", err) 83 } 84 } 85 86 func writeGorundPorts(gorundPorts [][]string) { 87 var portsData string 88 for _, ports := range gorundPorts { 89 portsData += ports[0] + " " + ports[1] + "\n" 90 } 91 err := genesis.WriteFile("./", gorundPortsPath, portsData) 92 check("Can't WriteFile: "+gorundPortsPath, err) 93 } 94 95 func writeInsolarConfigs(output string, insolarConfigs []configuration.Configuration) { 96 for index, conf := range insolarConfigs { 97 data, err := yaml.Marshal(conf) 98 check("Can't Marshal insolard config", err) 99 fileName := fmt.Sprintf(defaultOutputConfigNameTmpl, index+1) 100 err = genesis.WriteFile(output, fileName, string(data)) 101 check("Can't WriteFile: "+fileName, err) 102 } 103 } 104 105 func writePulsarConfig(conf configuration.Configuration) { 106 data, err := yaml.Marshal(conf) 107 check("Can't Marshal pulsard config", err) 108 err = genesis.WriteFile(outputDir, "pulsar.yaml", string(data)) 109 check("Can't WriteFile: pulsar.yaml", err) 110 } 111 112 type promContext struct { 113 Jobs map[string][]string 114 } 115 116 func newPromContext() *promContext { 117 return &promContext{Jobs: map[string][]string{}} 118 } 119 120 func (pctx *promContext) addTarget(name string, conf configuration.Configuration) { 121 jobs := pctx.Jobs 122 addrPair := strings.SplitN(conf.Metrics.ListenAddress, ":", 2) 123 addr := "host.docker.internal:" + addrPair[1] 124 jobs[name] = append(jobs[name], addr) 125 } 126 127 func writePromConfig(pctx *promContext) { 128 templates, err := template.ParseFiles(prometheusConfigTmpl) 129 check("Can't parse template: "+prometheusConfigTmpl, err) 130 131 var b bytes.Buffer 132 err = templates.Execute(&b, pctx) 133 check("Can't process template: "+prometheusConfigTmpl, err) 134 135 err = genesis.WriteFile(outputDir, prometheusFileName, b.String()) 136 check("Can't WriteFile: "+prometheusFileName, err) 137 } 138 139 func main() { 140 parseInputParams() 141 142 genesisConf, err := genesis.ParseGenesisConfig(genesisFile) 143 check("Can't read genesis config", err) 144 145 pwConfig := pulsewatcher.Config{} 146 discoveryNodesConfigs := make([]configuration.Configuration, 0, len(genesisConf.DiscoveryNodes)) 147 148 gorundPorts := [][]string{} 149 150 pctx := newPromContext() 151 152 for index, node := range genesisConf.DiscoveryNodes { 153 nodeIndex := index + 1 154 conf := configuration.NewConfiguration() 155 156 conf.Host.Transport.Address = node.Host 157 conf.Host.Transport.Protocol = "TCP" 158 159 rpcListenPort := 33300 + (index+nodeIndex)*nodeIndex 160 conf.LogicRunner = configuration.NewLogicRunner() 161 conf.LogicRunner.GoPlugin.RunnerListen = fmt.Sprintf(defaultHost+":%d", rpcListenPort-1) 162 conf.LogicRunner.RPCListen = fmt.Sprintf(defaultHost+":%d", rpcListenPort) 163 if node.Role == "virtual" { 164 gorundPorts = append(gorundPorts, []string{strconv.Itoa(rpcListenPort - 1), strconv.Itoa(rpcListenPort)}) 165 } 166 167 conf.APIRunner.Address = fmt.Sprintf(defaultHost+":191%02d", nodeIndex) 168 conf.Metrics.ListenAddress = fmt.Sprintf(defaultHost+":80%02d", nodeIndex) 169 170 conf.Tracer.Jaeger.AgentEndpoint = defaultJaegerEndPoint 171 conf.Log.Level = debugLevel 172 conf.Log.Adapter = "zerolog" 173 conf.Log.Formatter = "json" 174 conf.KeysPath = genesisConf.DiscoveryKeysDir + fmt.Sprintf(genesisConf.KeysNameFormat, index) 175 conf.Ledger.Storage.DataDirectory = fmt.Sprintf(discoveryDataDirectoryTemplate, nodeIndex) 176 conf.CertificatePath = fmt.Sprintf(discoveryCertificatePathTemplate, nodeIndex) 177 178 discoveryNodesConfigs = append(discoveryNodesConfigs, conf) 179 180 pctx.addTarget(node.Role, conf) 181 182 pwConfig.Nodes = append(pwConfig.Nodes, conf.APIRunner.Address) 183 } 184 185 nodesConfigs := make([]configuration.Configuration, 0, len(genesisConf.DiscoveryNodes)) 186 187 for index, node := range genesisConf.Nodes { 188 nodeIndex := index + 1 189 190 conf := configuration.NewConfiguration() 191 conf.Host.Transport.Address = node.Host 192 conf.Host.Transport.Protocol = "TCP" 193 194 rpcListenPort := 34300 + (index+nodeIndex+len(genesisConf.DiscoveryNodes)+1)*nodeIndex 195 conf.LogicRunner = configuration.NewLogicRunner() 196 conf.LogicRunner.GoPlugin.RunnerListen = fmt.Sprintf(defaultHost+":%d", rpcListenPort-1) 197 conf.LogicRunner.RPCListen = fmt.Sprintf(defaultHost+":%d", rpcListenPort) 198 if node.Role == "virtual" { 199 gorundPorts = append(gorundPorts, []string{strconv.Itoa(rpcListenPort - 1), strconv.Itoa(rpcListenPort)}) 200 } 201 202 conf.APIRunner.Address = fmt.Sprintf(defaultHost+":191%02d", nodeIndex+len(genesisConf.DiscoveryNodes)) 203 conf.Metrics.ListenAddress = fmt.Sprintf(defaultHost+":80%02d", nodeIndex+len(genesisConf.DiscoveryNodes)) 204 205 conf.Tracer.Jaeger.AgentEndpoint = defaultJaegerEndPoint 206 conf.Log.Level = debugLevel 207 conf.Log.Adapter = "zerolog" 208 conf.Log.Formatter = "json" 209 conf.KeysPath = node.KeysFile 210 conf.Ledger.Storage.DataDirectory = fmt.Sprintf(nodeDataDirectoryTemplate, nodeIndex) 211 conf.CertificatePath = fmt.Sprintf(nodeCertificatePathTemplate, nodeIndex) 212 213 nodesConfigs = append(nodesConfigs, conf) 214 215 pctx.addTarget(node.Role, conf) 216 217 pwConfig.Nodes = append(pwConfig.Nodes, conf.APIRunner.Address) 218 } 219 220 cfgHolder := configuration.NewHolder() 221 err = cfgHolder.LoadFromFile(pulsarTemplate) 222 check("Can't read pulsar template config", err) 223 err = cfgHolder.LoadEnv() 224 check("Can't read pulsar template config", err) 225 fmt.Println("pulsar template config: " + pulsarTemplate) 226 227 pulsarConfig := cfgHolder.Configuration 228 pulsarConfig.Pulsar.PulseDistributor.BootstrapHosts = []string{} 229 for _, node := range genesisConf.DiscoveryNodes { 230 pulsarConfig.Pulsar.PulseDistributor.BootstrapHosts = append(pulsarConfig.Pulsar.PulseDistributor.BootstrapHosts, node.Host) 231 } 232 233 writeInsolarConfigs(filepath.Join(outputDir, "/discoverynodes"), discoveryNodesConfigs) 234 writeInsolarConfigs(filepath.Join(outputDir, "/nodes"), nodesConfigs) 235 writeGorundPorts(gorundPorts) 236 writePulsarConfig(pulsarConfig) 237 writePromConfig(pctx) 238 239 pwConfig.Interval = 500 * time.Millisecond 240 pwConfig.Timeout = 1 * time.Second 241 err = pulsewatcher.WriteConfig(filepath.Join(outputDir, "/utils"), pulsewatcherFileName, pwConfig) 242 check("couldn't write pulsewatcher config file", err) 243 }