forked from hyperledger-archives/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
204 lines (176 loc) · 6.52 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright 2017 Monax Industries Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"bytes"
"fmt"
"text/template"
lconfig "github.com/hyperledger/burrow/logging/config"
"github.com/hyperledger/burrow/version"
"github.com/spf13/viper"
)
type ConfigServiceGeneral struct {
ChainImageName string
UseDataContainer bool
ExportedPorts string
ContainerEntrypoint string
}
// TODO: [ben] increase the configurability upon need
type ConfigChainGeneral struct {
AssertChainId string
BurrowMajorVersion uint8
BurrowMinorVersion uint8
GenesisRelativePath string
}
type ConfigChainModule struct {
Name string
ModuleRelativeRoot string
}
type ConfigTendermint struct {
Moniker string
Seeds string
FastSync bool
}
var serviceGeneralTemplate *template.Template
var chainGeneralTemplate *template.Template
var chainConsensusTemplate *template.Template
var chainApplicationManagerTemplate *template.Template
var tendermintTemplate *template.Template
func init() {
var err error
if serviceGeneralTemplate, err = template.New("serviceGeneral").Parse(sectionServiceGeneral); err != nil {
panic(err)
}
if chainGeneralTemplate, err = template.New("chainGeneral").Parse(sectionChainGeneral); err != nil {
panic(err)
}
if chainConsensusTemplate, err = template.New("chainConsensus").Parse(sectionChainConsensus); err != nil {
panic(err)
}
if chainApplicationManagerTemplate, err = template.New("chainApplicationManager").Parse(sectionChainApplicationManager); err != nil {
panic(err)
}
if tendermintTemplate, err = template.New("tendermint").Parse(sectionTendermint); err != nil {
panic(err)
}
}
// NOTE: [ben] for 0.12.0-rc3 we only have a single configuration path
// with Tendermint in-process as the consensus engine and BurrowMint
// in-process as the application manager, so we hard-code the few
// parameters that are already templated.
// Let's learn to walk before we can run.
func GetConfigurationFileBytes(chainId, moniker, seeds string, chainImageName string,
useDataContainer bool, exportedPortsString, containerEntrypoint string) ([]byte, error) {
burrowService := &ConfigServiceGeneral{
ChainImageName: chainImageName,
UseDataContainer: useDataContainer,
ExportedPorts: exportedPortsString,
ContainerEntrypoint: containerEntrypoint,
}
// We want to encode in the config file which Burrow version generated the config
burrowVersion := version.GetBurrowVersion()
burrowChain := &ConfigChainGeneral{
AssertChainId: chainId,
BurrowMajorVersion: burrowVersion.MajorVersion,
BurrowMinorVersion: burrowVersion.MinorVersion,
GenesisRelativePath: "genesis.json",
}
chainConsensusModule := &ConfigChainModule{
Name: "tendermint",
ModuleRelativeRoot: "tendermint",
}
chainApplicationManagerModule := &ConfigChainModule{
Name: "burrowmint",
ModuleRelativeRoot: "burrowmint",
}
tendermintModule := &ConfigTendermint{
Moniker: moniker,
Seeds: seeds,
FastSync: false,
}
// NOTE: [ben] according to StackOverflow appending strings with copy is
// more efficient than bytes.WriteString, but for readability and because
// this is not performance critical code we opt for bytes, which is
// still more efficient than + concatentation operator.
var buffer bytes.Buffer
// write copyright header
buffer.WriteString(headerCopyright)
// write section [service]
if err := serviceGeneralTemplate.Execute(&buffer, burrowService); err != nil {
return nil, fmt.Errorf("Failed to write template service general for %s: %s",
chainId, err)
}
// write section for service dependencies; this is currently a static section
// with a fixed dependency on monax-keys
buffer.WriteString(sectionServiceDependencies)
// write section [chain]
if err := chainGeneralTemplate.Execute(&buffer, burrowChain); err != nil {
return nil, fmt.Errorf("Failed to write template chain general for %s: %s",
chainId, err)
}
// write separator chain consensus
buffer.WriteString(separatorChainConsensus)
// write section [chain.consensus]
if err := chainConsensusTemplate.Execute(&buffer, chainConsensusModule); err != nil {
return nil, fmt.Errorf("Failed to write template chain consensus for %s: %s",
chainId, err)
}
// write separator chain application manager
buffer.WriteString(separatorChainApplicationManager)
// write section [chain.consensus]
if err := chainApplicationManagerTemplate.Execute(&buffer,
chainApplicationManagerModule); err != nil {
return nil, fmt.Errorf("Failed to write template chain application manager for %s: %s",
chainId, err)
}
// write separator servers
buffer.WriteString(separatorServerConfiguration)
// TODO: [ben] upon necessity replace this with template too
// write static section servers
buffer.WriteString(sectionServers)
// write separator modules
buffer.WriteString(separatorModules)
// write section module Tendermint
if err := tendermintTemplate.Execute(&buffer, tendermintModule); err != nil {
return nil, fmt.Errorf("Failed to write template tendermint for %s, moniker %s: %s",
chainId, moniker, err)
}
// write static section burrowmint
buffer.WriteString(sectionBurrowMint)
buffer.WriteString(sectionLoggingHeader)
buffer.WriteString(lconfig.DefaultNodeLoggingConfig().RootTOMLString())
return buffer.Bytes(), nil
}
func AssertConfigCompatibleWithRuntime(conf *viper.Viper) error {
burrowVersion := version.GetBurrowVersion()
majorVersion := uint8(conf.GetInt(fmt.Sprintf("chain.%s", majorVersionKey)))
minorVersion := uint8(conf.GetInt(fmt.Sprintf("chain.%s", majorVersionKey)))
if burrowVersion.MajorVersion != majorVersion ||
burrowVersion.MinorVersion != minorVersion {
fmt.Errorf("Runtime Burrow version %s is not compatible with "+
"configuration file version: major=%s, minor=%s",
burrowVersion.GetVersionString(), majorVersion, minorVersion)
}
return nil
}
func GetExampleConfigFileBytes() ([]byte, error) {
return GetConfigurationFileBytes(
"simplechain",
"delectable_marmot",
"192.168.168.255",
"db:latest",
true,
"46657",
"burrow")
}