|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2016 All Rights Reserved. |
| 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 config |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "strings" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/op/go-logging" |
| 27 | + "github.com/spf13/viper" |
| 28 | +) |
| 29 | + |
| 30 | +var logger = logging.MustGetLogger("orderer/config") |
| 31 | + |
| 32 | +func init() { |
| 33 | + logging.SetLevel(logging.DEBUG, "") |
| 34 | +} |
| 35 | + |
| 36 | +// Prefix is the default config prefix for the orderer |
| 37 | +const Prefix string = "ORDERER" |
| 38 | + |
| 39 | +// General contains config which should be common among all orderer types |
| 40 | +type General struct { |
| 41 | + OrdererType string |
| 42 | + LedgerType string |
| 43 | + BatchTimeout time.Duration |
| 44 | + BatchSize uint |
| 45 | + QueueSize uint |
| 46 | + MaxWindowSize uint |
| 47 | + ListenAddress string |
| 48 | + ListenPort uint16 |
| 49 | +} |
| 50 | + |
| 51 | +// RAMLedger contains config for the RAM ledger |
| 52 | +type RAMLedger struct { |
| 53 | + HistorySize uint |
| 54 | +} |
| 55 | + |
| 56 | +// FileLedger contains config for the File ledger |
| 57 | +type FileLedger struct { |
| 58 | + Location string |
| 59 | + Prefix string |
| 60 | +} |
| 61 | + |
| 62 | +// TopLevel directly corresponds to the orderer config yaml |
| 63 | +// Note, for non 1-1 mappings, you may append |
| 64 | +// something like `mapstructure:"weirdFoRMat"` to |
| 65 | +// modify the default mapping, see the "Unmarshal" |
| 66 | +// section of https://github.com/spf13/viper for more info |
| 67 | +type TopLevel struct { |
| 68 | + General General |
| 69 | + RAMLedger RAMLedger |
| 70 | + FileLedger FileLedger |
| 71 | +} |
| 72 | + |
| 73 | +var defaults = TopLevel{ |
| 74 | + General: General{ |
| 75 | + OrdererType: "solo", |
| 76 | + LedgerType: "ram", |
| 77 | + BatchTimeout: 10 * time.Second, |
| 78 | + BatchSize: 10, |
| 79 | + QueueSize: 1000, |
| 80 | + MaxWindowSize: 1000, |
| 81 | + ListenAddress: "127.0.0.1", |
| 82 | + ListenPort: 5151, |
| 83 | + }, |
| 84 | + RAMLedger: RAMLedger{ |
| 85 | + HistorySize: 10000, |
| 86 | + }, |
| 87 | + FileLedger: FileLedger{ |
| 88 | + Location: "", |
| 89 | + Prefix: "hyperledger-fabric-rawledger", |
| 90 | + }, |
| 91 | +} |
| 92 | + |
| 93 | +func (c *TopLevel) completeInitialization() { |
| 94 | + defer logger.Infof("Validated configuration to: %+v", c) |
| 95 | + |
| 96 | + for { |
| 97 | + switch { |
| 98 | + case c.General.OrdererType == "": |
| 99 | + logger.Infof("General.OrdererType unset, setting to %s", defaults.General.OrdererType) |
| 100 | + c.General.OrdererType = defaults.General.OrdererType |
| 101 | + case c.General.LedgerType == "": |
| 102 | + logger.Infof("General.LedgerType unset, setting to %s", defaults.General.LedgerType) |
| 103 | + c.General.LedgerType = defaults.General.LedgerType |
| 104 | + case c.General.BatchTimeout == 0: |
| 105 | + logger.Infof("General.BatchTimeout unset, setting to %s", defaults.General.BatchTimeout) |
| 106 | + c.General.BatchTimeout = defaults.General.BatchTimeout |
| 107 | + case c.General.BatchSize == 0: |
| 108 | + logger.Infof("General.BatchSize unset, setting to %s", defaults.General.BatchSize) |
| 109 | + c.General.BatchSize = defaults.General.BatchSize |
| 110 | + case c.General.QueueSize == 0: |
| 111 | + logger.Infof("General.QueueSize unset, setting to %s", defaults.General.QueueSize) |
| 112 | + c.General.QueueSize = defaults.General.QueueSize |
| 113 | + case c.General.MaxWindowSize == 0: |
| 114 | + logger.Infof("General.MaxWindowSize unset, setting to %s", defaults.General.MaxWindowSize) |
| 115 | + c.General.MaxWindowSize = defaults.General.MaxWindowSize |
| 116 | + case c.General.ListenAddress == "": |
| 117 | + logger.Infof("General.ListenAddress unset, setting to %s", defaults.General.ListenAddress) |
| 118 | + c.General.ListenAddress = defaults.General.ListenAddress |
| 119 | + case c.General.ListenPort == 0: |
| 120 | + logger.Infof("General.ListenPort unset, setting to %s", defaults.General.ListenPort) |
| 121 | + c.General.ListenPort = defaults.General.ListenPort |
| 122 | + case c.FileLedger.Prefix == "": |
| 123 | + logger.Infof("FileLedger.Prefix unset, setting to %s", defaults.FileLedger.Prefix) |
| 124 | + c.FileLedger.Prefix = defaults.FileLedger.Prefix |
| 125 | + default: |
| 126 | + return |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// Load parses the orderer.yaml file and environment, producing a struct suitable for config use |
| 132 | +func Load() *TopLevel { |
| 133 | + config := viper.New() |
| 134 | + |
| 135 | + // for environment variables |
| 136 | + config.SetEnvPrefix(Prefix) |
| 137 | + config.AutomaticEnv() |
| 138 | + replacer := strings.NewReplacer(".", "_") |
| 139 | + config.SetEnvKeyReplacer(replacer) |
| 140 | + |
| 141 | + config.SetConfigName("orderer") |
| 142 | + config.AddConfigPath("./") |
| 143 | + config.AddConfigPath("../orderer/") |
| 144 | + config.AddConfigPath("../../orderer/") |
| 145 | + // Path to look for the config file in based on GOPATH |
| 146 | + gopath := os.Getenv("GOPATH") |
| 147 | + for _, p := range filepath.SplitList(gopath) { |
| 148 | + ordererPath := filepath.Join(p, "src/github.com/hyperledger/fabric/orderer/") |
| 149 | + config.AddConfigPath(ordererPath) |
| 150 | + } |
| 151 | + |
| 152 | + err := config.ReadInConfig() |
| 153 | + if err != nil { |
| 154 | + panic(fmt.Errorf("Error reading %s plugin config: %s", Prefix, err)) |
| 155 | + } |
| 156 | + |
| 157 | + var uconf TopLevel |
| 158 | + |
| 159 | + err = ExactWithDateUnmarshal(config, &uconf) |
| 160 | + if err != nil { |
| 161 | + panic(fmt.Errorf("Error unmarshaling into structure: %s", err)) |
| 162 | + } |
| 163 | + |
| 164 | + uconf.completeInitialization() |
| 165 | + |
| 166 | + return &uconf |
| 167 | +} |
0 commit comments