From ed8864d26a029834726d46882ce88f142533f56c Mon Sep 17 00:00:00 2001 From: Gari Singh Date: Tue, 6 Jun 2017 06:20:45 -0400 Subject: [PATCH] [FAB-4411] Enable orderer to report version info The orderer shim currently does not report any version information. The following functionality has been implemented: - Add CLI for orderer which supports "start" and "version" commands. Note that the "start" cmd is the default so need not be specified. This ensures that starting the orderer with ./orderer still works - Version info is also printed at start up - Given the above, removed a duplicate log message from server.go as well Change-Id: Ia4c3a82b9844391823107d10b51dd7136689d4c8 Signed-off-by: Gari Singh --- orderer/main.go | 44 +++++++++++++++++++++++-------- orderer/metadata/metadata.go | 35 ++++++++++++++++++++++++ orderer/metadata/metadata_test.go | 26 ++++++++++++++++++ orderer/server.go | 2 -- 4 files changed, 94 insertions(+), 13 deletions(-) create mode 100644 orderer/metadata/metadata.go create mode 100644 orderer/metadata/metadata_test.go diff --git a/orderer/main.go b/orderer/main.go index 65ed9324ad4..6508066b159 100644 --- a/orderer/main.go +++ b/orderer/main.go @@ -34,6 +34,7 @@ import ( "github.com/hyperledger/fabric/orderer/kafka" "github.com/hyperledger/fabric/orderer/ledger" "github.com/hyperledger/fabric/orderer/localconfig" + "github.com/hyperledger/fabric/orderer/metadata" "github.com/hyperledger/fabric/orderer/multichain" "github.com/hyperledger/fabric/orderer/solo" cb "github.com/hyperledger/fabric/protos/common" @@ -44,22 +45,43 @@ import ( "github.com/hyperledger/fabric/common/localmsp" mspmgmt "github.com/hyperledger/fabric/msp/mgmt" logging "github.com/op/go-logging" + "gopkg.in/alecthomas/kingpin.v2" ) var logger = logging.MustGetLogger("orderer/main") +//command line flags +var ( + app = kingpin.New("orderer", "Hyperledger Fabric orderer node") + + start = app.Command("start", "Start the orderer node").Default() + version = app.Command("version", "Show version information") +) + func main() { - conf := config.Load() - initializeLoggingLevel(conf) - initializeProfilingService(conf) - grpcServer := initializeGrpcServer(conf) - initializeLocalMsp(conf) - signer := localmsp.NewSigner() - manager := initializeMultiChainManager(conf, signer) - server := NewServer(manager, signer) - ab.RegisterAtomicBroadcastServer(grpcServer.Server(), server) - logger.Info("Beginning to serve requests") - grpcServer.Start() + + kingpin.Version("0.0.1") + switch kingpin.MustParse(app.Parse(os.Args[1:])) { + + // "start" command + case start.FullCommand(): + logger.Infof("Starting %s", metadata.GetVersionInfo()) + conf := config.Load() + initializeLoggingLevel(conf) + initializeProfilingService(conf) + grpcServer := initializeGrpcServer(conf) + initializeLocalMsp(conf) + signer := localmsp.NewSigner() + manager := initializeMultiChainManager(conf, signer) + server := NewServer(manager, signer) + ab.RegisterAtomicBroadcastServer(grpcServer.Server(), server) + logger.Info("Beginning to serve requests") + grpcServer.Start() + // "version" command + case version.FullCommand(): + fmt.Println(metadata.GetVersionInfo()) + } + } // Set the logging level diff --git a/orderer/metadata/metadata.go b/orderer/metadata/metadata.go new file mode 100644 index 00000000000..78029d26cc2 --- /dev/null +++ b/orderer/metadata/metadata.go @@ -0,0 +1,35 @@ +/* +Copyright IBM Corp. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 +*/ + +package metadata + +import ( + "fmt" + "runtime" + + common "github.com/hyperledger/fabric/common/metadata" +) + +// package-scoped variables + +// Package version +var Version string + +// package-scoped constants + +// Program name +const ProgramName = "orderer" + +func GetVersionInfo() string { + Version = common.Version + if Version == "" { + Version = "development build" + } + + return fmt.Sprintf("%s:\n Version: %s\n Go version: %s\n OS/Arch: %s", + ProgramName, Version, runtime.Version(), + fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)) +} diff --git a/orderer/metadata/metadata_test.go b/orderer/metadata/metadata_test.go new file mode 100644 index 00000000000..337a19a8d3d --- /dev/null +++ b/orderer/metadata/metadata_test.go @@ -0,0 +1,26 @@ +/* +Copyright IBM Corp. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 +*/ + +package metadata_test + +import ( + "fmt" + "runtime" + "testing" + + "github.com/hyperledger/fabric/common/tools/cryptogen/metadata" + "github.com/stretchr/testify/assert" +) + +func TestGetVersionInfo(t *testing.T) { + testVersion := "TestVersion" + metadata.Version = testVersion + + expected := fmt.Sprintf("%s:\n Version: %s\n Go version: %s\n OS/Arch: %s", + metadata.ProgramName, testVersion, runtime.Version(), + fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)) + assert.Equal(t, expected, metadata.GetVersionInfo()) +} diff --git a/orderer/server.go b/orderer/server.go index 0287d83fe6b..22be389daf7 100644 --- a/orderer/server.go +++ b/orderer/server.go @@ -57,8 +57,6 @@ type server struct { // NewServer creates an ab.AtomicBroadcastServer based on the broadcast target and ledger Reader func NewServer(ml multichain.Manager, signer crypto.LocalSigner) ab.AtomicBroadcastServer { - logger.Infof("Starting orderer") - s := &server{ dh: deliver.NewHandlerImpl(deliverSupport{Manager: ml}), bh: broadcast.NewHandlerImpl(broadcastSupport{