Skip to content

Commit

Permalink
CLI - config & version commands + tests
Browse files Browse the repository at this point in the history
Signed-off-by: May Rosenbaum <[email protected]>
  • Loading branch information
MayRosenbaum committed Sep 20, 2023
1 parent 65153d1 commit ec24b80
Show file tree
Hide file tree
Showing 29 changed files with 411 additions and 142 deletions.
22 changes: 13 additions & 9 deletions cli/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Config Orion via CLI

This command-line tool provides you a simple way to config an orion database server.
This command-line tool provides a simple way to config an orion database server.

## Building the tool
1. Run from `orion-sdk` root folder
2. Run `make binary` to create an executable file named bcdbadmin under `bin` directory.

## Commands

Expand All @@ -11,7 +15,7 @@ We provide real-world examples demonstrating how to use the CLI tool for various

### Version Command
This command prints the version of the CLI tool.
1. Run from 'orion-sdk' root folder
1. Run from `orion-sdk` root folder.
2. Run `bin/bcdbadmin version`. This command has no flags.


Expand All @@ -20,25 +24,25 @@ This command prints the version of the CLI tool.
This command enables to config an orion server or ask for the configuration of an orion server.

#### Get Config Command
1. Run from 'orion-sdk' root folder
1. Run from 'orion-sdk' root folder.
2. For Get Config Run `bin/bcdbadmin config get [args]`.

Replace `[args]` with flags.

###
##### Flags
| Flags | Description |
|-----------------------------|-------------------------------------------------------------------|
| `-c, --cli-config-path` | the absolute path of CLI connection configuration file |
| `-p, --cluster-config-path` | the absolute path to which the server configuration will be saved |
| Flags | Description |
|-----------------------------|---------------------------------------------------------------------|
| `-c, --cli-config-path` | the absolute or relative path of CLI connection configuration file |
| `-p, --cluster-config-path` | the absolute path to which the server configuration will be saved |

Both flags are necessary flags. If any flag is missing, the cli will raise an error.

###
##### Example:

Running
`bin/bcdbadmin config get -c "A/connection-session-config.yaml" -p "A/B/C"`
reads the connection and session details needed for connecting to a server from `A/connection-session-config.yaml` and
`bin/bcdbadmin config get -c "connection-session-config.yaml" -p "A/B/C"`
reads the connection and session details needed for connecting to a server from `connection-session-config.yaml` and
sends a config TX.
It creates a directory in `A/B/C` with the respective certificates and a yaml file, named shared_cluster_config.yaml, that includes the cluster configuration.
2 changes: 1 addition & 1 deletion cli/commands/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/spf13/cobra"
)

func AdminCmd() *cobra.Command {
func adminCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "admin",
Short: "manage administrators",
Expand Down
26 changes: 26 additions & 0 deletions cli/commands/admin_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
package commands

import (
"github.com/hyperledger-labs/orion-sdk-go/examples/util"
"github.com/stretchr/testify/require"
"io/ioutil"
"os"
"testing"
)

func TestAdminCommand(t *testing.T) {
// 1. Create crypto material and start server
tempDir, err := ioutil.TempDir(os.TempDir(), "ExampleTest")
require.NoError(t, err)

testServer, _, _, err := util.SetupTestEnv(t, tempDir, uint32(6003))
require.NoError(t, err)
defer testServer.Stop()
util.StartTestServer(t, testServer)

// 2. Check cas command response
rootCmd := InitializeOrionCli()
rootCmd.SetArgs([]string{"admin"})
err = rootCmd.Execute()
require.Error(t, err)
require.Equal(t, err.Error(), "not implemented yet")
}
2 changes: 1 addition & 1 deletion cli/commands/cas.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/spf13/cobra"
)

func CasCmd() *cobra.Command {
func casCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "CAs",
Short: "manage CA's",
Expand Down
26 changes: 26 additions & 0 deletions cli/commands/cas_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
package commands

import (
"github.com/hyperledger-labs/orion-sdk-go/examples/util"
"github.com/stretchr/testify/require"
"io/ioutil"
"os"
"testing"
)

func TestCasCommand(t *testing.T) {
// 1. Create crypto material and start server
tempDir, err := ioutil.TempDir(os.TempDir(), "ExampleTest")
require.NoError(t, err)

testServer, _, _, err := util.SetupTestEnv(t, tempDir, uint32(6003))
require.NoError(t, err)
defer testServer.Stop()
util.StartTestServer(t, testServer)

// 2. Check cas command response
rootCmd := InitializeOrionCli()
rootCmd.SetArgs([]string{"CAs"})
err = rootCmd.Execute()
require.Error(t, err)
require.Equal(t, err.Error(), "not implemented yet")
}
85 changes: 35 additions & 50 deletions cli/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ package commands

import (
"encoding/pem"
"fmt"
"io/ioutil"
"os"
"path"
"strconv"

"github.com/hyperledger-labs/orion-sdk-go/pkg/bcdb"
"github.com/hyperledger-labs/orion-sdk-go/pkg/config"
orionconfig "github.com/hyperledger-labs/orion-server/config"
Expand All @@ -11,10 +17,6 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"path"
"strconv"
)

const (
Expand All @@ -33,8 +35,10 @@ type cliConfigParams struct {
session bcdb.DBSession
}

var params cliConfigParams
var getClusterConfigPath string
var (
params cliConfigParams
getClusterConfigPath string
)

func configCmd() *cobra.Command {
configCmd := &cobra.Command{
Expand Down Expand Up @@ -63,14 +67,17 @@ func getConfigCmd() *cobra.Command {
getConfigCmd.PersistentFlags().StringVarP(&getClusterConfigPath, "cluster-config-path", "p", "", "set the absolute path to which the server configuration will be saved")
getConfigCmd.MarkPersistentFlagRequired("cluster-config-path")

fmt.Println()
return getConfigCmd
}

func setConfigCmd() *cobra.Command {
setConfigCmd := &cobra.Command{
Use: "set",
Short: "Set cluster configuration",
RunE: setConfig,
RunE: func(cmd *cobra.Command, args []string) error {
return errors.New("not implemented yet")
},
}
return setConfigCmd
}
Expand Down Expand Up @@ -105,34 +112,6 @@ func getConfig(cmd *cobra.Command, args []string) error {
return nil
}

func setConfig(cmd *cobra.Command, args []string) error {
_, err := orionconfig.Read(params.cliConfigPath)
if err != nil {
return err
}

err = params.CreateDbAndOpenSession()
if err != nil {
return err
}

tx, err := params.session.ConfigTx()
if err != nil {
return errors.Wrapf(err, "failed to instanciate a config TX")
}
defer abort(tx)
// TODO: set the cluster configuration
//err := tx.SetClusterConfig()
//if err != nil {
// return errors.Wrapf(err, "failed to fetch cluster config")
//}

//configCmd.SilenceUsage = true
//configCmd.Printf(params.cliConfigPath)

return nil
}

// ReadAndConstructCliConnConfig read unmarshal the yaml config file into a CliConnectionConfig object
func (c *CliConnectionConfig) ReadAndConstructCliConnConfig(filePath string) error {
if filePath == "" {
Expand Down Expand Up @@ -282,10 +261,10 @@ func parseAndSaveCerts(clusterConfig *types.ClusterConfig, getClusterConfigPath
}

// buildSharedClusterConfig builds the shared configuration from a clusterConfig
func buildSharedClusterConfig(clusterConfig *types.ClusterConfig, configYamlFilePath string) *orionconfig.SharedConfiguration {
var nodesSharedConfiguration []*orionconfig.NodeConf
func buildSharedClusterConfig(clusterConfig *types.ClusterConfig, configYamlFilePath string) *SharedConfiguration {
var nodesSharedConfiguration []*NodeConf
for _, node := range clusterConfig.Nodes {
nodeSharedConfiguration := &orionconfig.NodeConf{
nodeSharedConfiguration := &NodeConf{
NodeID: node.GetId(),
Host: node.GetAddress(),
Port: node.GetPort(),
Expand All @@ -294,9 +273,9 @@ func buildSharedClusterConfig(clusterConfig *types.ClusterConfig, configYamlFile
nodesSharedConfiguration = append(nodesSharedConfiguration, nodeSharedConfiguration)
}

var membersSharedConfiguration []*orionconfig.PeerConf
var membersSharedConfiguration []*PeerConf
for _, member := range clusterConfig.ConsensusConfig.Members {
memberSharedConfiguration := &orionconfig.PeerConf{
memberSharedConfiguration := &PeerConf{
NodeId: member.GetNodeId(),
RaftId: member.GetRaftId(),
PeerHost: member.GetPeerHost(),
Expand All @@ -305,9 +284,9 @@ func buildSharedClusterConfig(clusterConfig *types.ClusterConfig, configYamlFile
membersSharedConfiguration = append(membersSharedConfiguration, memberSharedConfiguration)
}

var observersSharedConfiguration []*orionconfig.PeerConf
var observersSharedConfiguration []*PeerConf
for _, observer := range clusterConfig.ConsensusConfig.Observers {
observerSharedConfiguration := &orionconfig.PeerConf{
observerSharedConfiguration := &PeerConf{
NodeId: observer.GetNodeId(),
RaftId: observer.GetRaftId(),
PeerHost: observer.GetPeerHost(),
Expand All @@ -334,13 +313,22 @@ func buildSharedClusterConfig(clusterConfig *types.ClusterConfig, configYamlFile
intermediateCACertsPathSharedConfiguration = append(intermediateCACertsPathSharedConfiguration, intermediateCACertPathSharedConfiguration)
}

sharedConfiguration := &orionconfig.SharedConfiguration{
var adminsSharedConfiguration []*AdminConf
for i, admin := range clusterConfig.Admins {
adminSharedConfiguration := &AdminConf{
ID: admin.GetId(),
CertificatePath: path.Join(getClusterConfigPath, ConfigDirName, "admins", clusterConfig.Admins[i].GetId()+".pem"),
}
adminsSharedConfiguration = append(adminsSharedConfiguration, adminSharedConfiguration)
}

sharedConfiguration := &SharedConfiguration{
Nodes: nodesSharedConfiguration,
Consensus: &orionconfig.ConsensusConf{
Consensus: &ConsensusConf{
Algorithm: clusterConfig.ConsensusConfig.Algorithm,
Members: membersSharedConfiguration,
Observers: observersSharedConfiguration,
RaftConfig: &orionconfig.RaftConf{
RaftConfig: &RaftConf{
TickInterval: clusterConfig.ConsensusConfig.RaftConfig.TickInterval,
ElectionTicks: clusterConfig.ConsensusConfig.RaftConfig.ElectionTicks,
HeartbeatTicks: clusterConfig.ConsensusConfig.RaftConfig.HeartbeatTicks,
Expand All @@ -352,11 +340,8 @@ func buildSharedClusterConfig(clusterConfig *types.ClusterConfig, configYamlFile
RootCACertsPath: rootCACertsPathSharedConfiguration,
IntermediateCACertsPath: intermediateCACertsPathSharedConfiguration,
},
Admin: orionconfig.AdminConf{
ID: clusterConfig.Admins[0].Id,
CertificatePath: path.Join(getClusterConfigPath, ConfigDirName, "admins", clusterConfig.Admins[0].Id+".pem"),
},
Ledger: orionconfig.LedgerConf{StateMerklePatriciaTrieDisabled: clusterConfig.LedgerConfig.StateMerklePatriciaTrieDisabled},
Admin: adminsSharedConfiguration,
Ledger: LedgerConf{StateMerklePatriciaTrieDisabled: clusterConfig.LedgerConfig.StateMerklePatriciaTrieDisabled},
}

return sharedConfiguration
Expand Down
Loading

0 comments on commit ec24b80

Please sign in to comment.