Skip to content

Commit

Permalink
Merge "[FAB-2880] Fix configtxgen output"
Browse files Browse the repository at this point in the history
  • Loading branch information
christo4ferris authored and Gerrit Code Review committed Mar 26, 2017
2 parents 6316c37 + 9065a7c commit 2be0aa1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 45 deletions.
109 changes: 66 additions & 43 deletions common/configtx/tool/localconfig/config.go
Expand Up @@ -31,60 +31,74 @@ import (
bccsp "github.com/hyperledger/fabric/bccsp/factory"
)

var logger = logging.MustGetLogger("configtx/tool/localconfig")

const (
// SampleInsecureProfile references the sample profile which does not include any MSPs and uses solo for consensus
// SampleInsecureProfile references the sample profile which does not include any MSPs and uses solo for ordering.
SampleInsecureProfile = "SampleInsecureSolo"

// SampleSingleMSPSoloProfile references the sample profile which includes only the sample MSP and uses solo for consensus
// SampleSingleMSPSoloProfile references the sample profile which includes only the sample MSP and uses solo for ordering.
SampleSingleMSPSoloProfile = "SampleSingleMSPSolo"

// Prefix identifies the prefix for the configtxgen-related ENV vars.
Prefix string = "CONFIGTX"
)

var logger = logging.MustGetLogger("configtx/tool/localconfig")
var (
configName string
configFileName string
)

// Prefix is the default config prefix for the orderer
const Prefix string = "CONFIGTX"
func init() {
configName = strings.ToLower(Prefix)
configFileName = configName + ".yaml"
}

// TopLevel contains the genesis structures for use by the provisional bootstrapper
// TopLevel consists of the structs used by the configtxgen tool.
type TopLevel struct {
Profiles map[string]*Profile `yaml:"Profiles"`
Organizations []*Organization `yaml:"Organizations"`
Application *Application `yaml:"Application"`
Orderer *Orderer `yaml:"Orderer"`
}

// TopLevel contains the genesis structures for use by the provisional bootstrapper
// Profile encodes orderer/application configuration combinations for the configtxgen tool.
type Profile struct {
Application *Application `yaml:"Application"`
Orderer *Orderer `yaml:"Orderer"`
}

// Application encodes the configuration needed for the config transaction
// Application encodes the application-level configuration needed in config transactions.
type Application struct {
Organizations []*Organization `yaml:"Organizations"`
}

// Organization encodes the organization-level configuration needed in config transactions.
type Organization struct {
Name string `yaml:"Name"`
ID string `yaml:"ID"`
MSPDir string `yaml:"MSPDir"`
BCCSP *bccsp.FactoryOpts `yaml:"BCCSP"`

// Note, the viper deserialization does not seem to care for
// embedding of types, so we use one organization structure for
// both orderers and applications
// Note: Viper deserialization does not seem to care for
// embedding of types, so we use one organization struct
// for both orderers and applications.
AnchorPeers []*AnchorPeer `yaml:"AnchorPeers"`
}

// AnchorPeer encodes the necessary fields to identify an anchor peer.
type AnchorPeer struct {
Host string `yaml:"Host"`
Port int `yaml:"Port"`
}

// ApplicationOrganization ...
// TODO This should probably be removed
type ApplicationOrganization struct {
Organization `yaml:"Organization"`
}

// Orderer contains config which is used for orderer genesis by the provisional bootstrapper
// Orderer contains configuration which is used for the
// bootstrapping of an orderer by the provisional bootstrapper.
type Orderer struct {
OrdererType string `yaml:"OrdererType"`
Addresses []string `yaml:"Addresses"`
Expand All @@ -95,14 +109,14 @@ type Orderer struct {
MaxChannels uint64 `yaml:"MaxChannels"`
}

// BatchSize contains configuration affecting the size of batches
// BatchSize contains configuration affecting the size of batches.
type BatchSize struct {
MaxMessageCount uint32 `yaml:"MaxMessageSize"`
AbsoluteMaxBytes uint32 `yaml:"AbsoluteMaxBytes"`
PreferredMaxBytes uint32 `yaml:"PreferredMaxBytes"`
}

// Kafka contains config for the Kafka orderer
// Kafka contains configuration for the Kafka-based orderer.
type Kafka struct {
Brokers []string `yaml:"Brokers"`
}
Expand All @@ -111,7 +125,7 @@ var genesisDefaults = TopLevel{
Orderer: &Orderer{
OrdererType: "solo",
Addresses: []string{"127.0.0.1:7050"},
BatchTimeout: 10 * time.Second,
BatchTimeout: 2 * time.Second,
BatchSize: BatchSize{
MaxMessageCount: 10,
AbsoluteMaxBytes: 100000000,
Expand All @@ -123,43 +137,44 @@ var genesisDefaults = TopLevel{
},
}

func (g *Profile) completeInitialization() {
func (p *Profile) completeInitialization() {
for {
switch {
case g.Orderer.OrdererType == "":
case p.Orderer.OrdererType == "":
logger.Infof("Orderer.OrdererType unset, setting to %s", genesisDefaults.Orderer.OrdererType)
g.Orderer.OrdererType = genesisDefaults.Orderer.OrdererType
case g.Orderer.Addresses == nil:
p.Orderer.OrdererType = genesisDefaults.Orderer.OrdererType
case p.Orderer.Addresses == nil:
logger.Infof("Orderer.Addresses unset, setting to %s", genesisDefaults.Orderer.Addresses)
g.Orderer.Addresses = genesisDefaults.Orderer.Addresses
case g.Orderer.BatchTimeout == 0:
p.Orderer.Addresses = genesisDefaults.Orderer.Addresses
case p.Orderer.BatchTimeout == 0:
logger.Infof("Orderer.BatchTimeout unset, setting to %s", genesisDefaults.Orderer.BatchTimeout)
g.Orderer.BatchTimeout = genesisDefaults.Orderer.BatchTimeout
case g.Orderer.BatchSize.MaxMessageCount == 0:
p.Orderer.BatchTimeout = genesisDefaults.Orderer.BatchTimeout
case p.Orderer.BatchSize.MaxMessageCount == 0:
logger.Infof("Orderer.BatchSize.MaxMessageCount unset, setting to %s", genesisDefaults.Orderer.BatchSize.MaxMessageCount)
g.Orderer.BatchSize.MaxMessageCount = genesisDefaults.Orderer.BatchSize.MaxMessageCount
case g.Orderer.BatchSize.AbsoluteMaxBytes == 0:
p.Orderer.BatchSize.MaxMessageCount = genesisDefaults.Orderer.BatchSize.MaxMessageCount
case p.Orderer.BatchSize.AbsoluteMaxBytes == 0:
logger.Infof("Orderer.BatchSize.AbsoluteMaxBytes unset, setting to %s", genesisDefaults.Orderer.BatchSize.AbsoluteMaxBytes)
g.Orderer.BatchSize.AbsoluteMaxBytes = genesisDefaults.Orderer.BatchSize.AbsoluteMaxBytes
case g.Orderer.BatchSize.PreferredMaxBytes == 0:
p.Orderer.BatchSize.AbsoluteMaxBytes = genesisDefaults.Orderer.BatchSize.AbsoluteMaxBytes
case p.Orderer.BatchSize.PreferredMaxBytes == 0:
logger.Infof("Orderer.BatchSize.PreferredMaxBytes unset, setting to %s", genesisDefaults.Orderer.BatchSize.PreferredMaxBytes)
g.Orderer.BatchSize.PreferredMaxBytes = genesisDefaults.Orderer.BatchSize.PreferredMaxBytes
case g.Orderer.Kafka.Brokers == nil:
p.Orderer.BatchSize.PreferredMaxBytes = genesisDefaults.Orderer.BatchSize.PreferredMaxBytes
case p.Orderer.Kafka.Brokers == nil:
logger.Infof("Orderer.Kafka.Brokers unset, setting to %v", genesisDefaults.Orderer.Kafka.Brokers)
g.Orderer.Kafka.Brokers = genesisDefaults.Orderer.Kafka.Brokers
p.Orderer.Kafka.Brokers = genesisDefaults.Orderer.Kafka.Brokers
default:
return
}
}
}

// Load returns the orderer/application config combination that corresponds to a given profile.
func Load(profile string) *Profile {
config := viper.New()

config.SetConfigName("configtx")
var cfgPath string

// Path to look for the config file in based on GOPATH
// Candidate paths to look for the config file in, based on GOPATH
searchPath := []string{
os.Getenv("ORDERER_CFG_PATH"),
os.Getenv("PEER_CFG_PATH"),
Expand All @@ -169,21 +184,30 @@ func Load(profile string) *Profile {
searchPath = append(searchPath, filepath.Join(p, "src/github.com/hyperledger/fabric/common/configtx/tool/"))
}

for _, genesisPath := range searchPath {
logger.Infof("Checking for configtx.yaml at: %s", genesisPath)
if _, err := os.Stat(filepath.Join(genesisPath, "configtx.yaml")); err != nil {
// The yaml file does not exist in this component of the path
for _, path := range searchPath {
if len(path) == 0 {
// No point printing a "checking for" message below for an empty path
continue
}
logger.Infof("Looking for %s in: %s", configFileName, path)
if _, err := os.Stat(filepath.Join(path, configFileName)); err != nil {
// The YAML file does not exist in this component of the path
continue
}
cfgPath = genesisPath
cfgPath = path
logger.Infof("Found %s there", configFileName)
}

if cfgPath == "" {
logger.Fatalf("Could not find configtx.yaml in paths of %s. Try setting ORDERER_CFG_PATH, PEER_CFG_PATH, or GOPATH correctly", searchPath)
logger.Fatalf("Could not find %s in paths of %s."+
"Try setting ORDERER_CFG_PATH, PEER_CFG_PATH, or GOPATH correctly.",
configFileName, searchPath)
}
config.AddConfigPath(cfgPath) // Path to look for the config file in

// for environment variables
// Path to look for the config file in
config.AddConfigPath(cfgPath)

// For environment variables
config.SetEnvPrefix(Prefix)
config.AutomaticEnv()
// This replacer allows substitution within the particular profile without having to fully qualify the name
Expand All @@ -192,14 +216,13 @@ func Load(profile string) *Profile {

err := config.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Error reading %s plugin config from %s: %s", Prefix, cfgPath, err))
logger.Panicf("Error reading configuration from %s in %s: %s", configFileName, cfgPath, err)
}

var uconf TopLevel

err = viperutil.EnhancedExactUnmarshal(config, &uconf)
if err != nil {
panic(fmt.Errorf("Error unmarshaling into structure: %s", err))
logger.Panicf("Error unmarshaling config into struct: %s", err)
}

result, ok := uconf.Profiles[profile]
Expand Down
4 changes: 2 additions & 2 deletions common/viperutil/config_util.go
Expand Up @@ -219,7 +219,7 @@ func pemBlocksFromFileDecodeHook() mapstructure.DecodeHookFunc {
var fileI interface{}
fileI, ok = d["File"]
if !ok {
fileI, ok = d["file"]
fileI, _ = d["file"]
}
fileName, ok = fileI.(string)
}
Expand Down Expand Up @@ -259,7 +259,7 @@ func EnhancedExactUnmarshal(v *viper.Viper, output interface{}) error {
baseKeys := v.AllSettings() // AllKeys doesn't actually return all keys, it only returns the base ones
leafKeys := getKeysRecursively("", v, baseKeys)

logger.Infof("%+v", leafKeys)
logger.Debugf("%+v", leafKeys)
config := &mapstructure.DecoderConfig{
ErrorUnused: true,
Metadata: nil,
Expand Down

0 comments on commit 2be0aa1

Please sign in to comment.