github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/common/configtx/tool/configtxgen/main.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 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 main
    18  
    19  import (
    20  	"flag"
    21  	"io/ioutil"
    22  
    23  	"github.com/hyperledger/fabric/bccsp/factory"
    24  	"github.com/hyperledger/fabric/common/configtx"
    25  	genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
    26  	"github.com/hyperledger/fabric/common/configtx/tool/provisional"
    27  	"github.com/hyperledger/fabric/msp"
    28  	"github.com/hyperledger/fabric/protos/utils"
    29  
    30  	logging "github.com/op/go-logging"
    31  )
    32  
    33  var logger = logging.MustGetLogger("common/configtx/tool")
    34  
    35  func main() {
    36  	var outputBlock, outputChannelCreateTx, profile, channelID string
    37  
    38  	flag.StringVar(&outputBlock, "outputBlock", "", "The path to write the genesis block to (if set)")
    39  	flag.StringVar(&channelID, "channelID", provisional.TestChainID, "The channel ID to use in the configtx")
    40  	flag.StringVar(&outputChannelCreateTx, "outputCreateChannelTx", "", "The path to write a channel creation configtx to (if set)")
    41  	flag.StringVar(&profile, "profile", genesisconfig.SampleInsecureProfile, "The profile from configtx.yaml to use for generation.")
    42  	flag.Parse()
    43  
    44  	logging.SetLevel(logging.INFO, "")
    45  
    46  	logger.Info("Loading configuration")
    47  	factory.InitFactories(nil)
    48  	config := genesisconfig.Load(profile)
    49  	pgen := provisional.New(config)
    50  
    51  	if outputBlock != "" {
    52  		logger.Info("Generating genesis block")
    53  		genesisBlock := pgen.GenesisBlock()
    54  		logger.Info("Writing genesis block")
    55  		err := ioutil.WriteFile(outputBlock, utils.MarshalOrPanic(genesisBlock), 0644)
    56  		if err != nil {
    57  			logger.Errorf("Error writing genesis block: %s", err)
    58  		}
    59  	}
    60  
    61  	if outputChannelCreateTx != "" {
    62  		logger.Info("Generating new channel configtx")
    63  		// TODO, use actual MSP eventually
    64  		signer, err := msp.NewNoopMsp().GetDefaultSigningIdentity()
    65  		if err != nil {
    66  			logger.Fatalf("Error getting signing identity: %s", err)
    67  		}
    68  		configtx, err := configtx.MakeChainCreationTransaction(provisional.AcceptAllPolicyKey, channelID, signer, pgen.ChannelTemplate())
    69  		if err != nil {
    70  			logger.Fatalf("Error generating configtx: %s", err)
    71  		}
    72  		logger.Info("Writing new channel tx")
    73  		err = ioutil.WriteFile(outputChannelCreateTx, utils.MarshalOrPanic(configtx), 0644)
    74  		if err != nil {
    75  			logger.Errorf("Error writing channel create tx: %s", err)
    76  		}
    77  	}
    78  }