From 6bf01a0a2219f106b7f369afda9f8e72e8bcf5de Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Fri, 9 Jul 2021 12:53:10 +0300 Subject: [PATCH] [#683] cmd/neofs-adm: Add CLI flags for morph commands Signed-off-by: Alex Vanin --- .../internal/modules/config/config.go | 13 +++ cmd/neofs-adm/internal/modules/morph/epoch.go | 13 +++ .../internal/modules/morph/generate.go | 45 ++++++++++ .../internal/modules/morph/initialize.go | 22 +++++ cmd/neofs-adm/internal/modules/morph/root.go | 90 +++++++++++++++++++ cmd/neofs-adm/internal/modules/root.go | 2 + 6 files changed, 185 insertions(+) create mode 100644 cmd/neofs-adm/internal/modules/morph/epoch.go create mode 100644 cmd/neofs-adm/internal/modules/morph/generate.go create mode 100644 cmd/neofs-adm/internal/modules/morph/initialize.go create mode 100644 cmd/neofs-adm/internal/modules/morph/root.go diff --git a/cmd/neofs-adm/internal/modules/config/config.go b/cmd/neofs-adm/internal/modules/config/config.go index 1d21c227..9eb31395 100644 --- a/cmd/neofs-adm/internal/modules/config/config.go +++ b/cmd/neofs-adm/internal/modules/config/config.go @@ -8,8 +8,10 @@ import ( "path/filepath" "text/template" + "github.com/nspcc-dev/neo-go/cli/input" "github.com/nspcc-dev/neofs-node/pkg/innerring" "github.com/spf13/cobra" + "github.com/spf13/viper" ) type configTemplate struct { @@ -122,3 +124,14 @@ func generateConfigExample(appDir string, credSize int) (string, error) { return buf.String(), nil } + +func AlphabetPassword(v *viper.Viper, index int) (string, error) { + letter := innerring.GlagoliticLetter(index) + key := "credentials." + letter.String() + if v.IsSet(key) { + return v.GetString(key), nil + } + + prompt := "Password for " + letter.String() + " wallet > " + return input.ReadPassword(prompt) +} diff --git a/cmd/neofs-adm/internal/modules/morph/epoch.go b/cmd/neofs-adm/internal/modules/morph/epoch.go new file mode 100644 index 00000000..8879addf --- /dev/null +++ b/cmd/neofs-adm/internal/modules/morph/epoch.go @@ -0,0 +1,13 @@ +package morph + +import ( + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +func forceNewEpochCmd(cmd *cobra.Command, args []string) error { + cmd.Println("endpoint:", viper.GetString(endpointFlag)) + cmd.Println("alphabet-wallets:", viper.GetString(alphabetWalletsFlag)) + + return nil +} diff --git a/cmd/neofs-adm/internal/modules/morph/generate.go b/cmd/neofs-adm/internal/modules/morph/generate.go new file mode 100644 index 00000000..6c027e05 --- /dev/null +++ b/cmd/neofs-adm/internal/modules/morph/generate.go @@ -0,0 +1,45 @@ +package morph + +import ( + "github.com/nspcc-dev/neofs-node/cmd/neofs-adm/internal/modules/config" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +func generateAlphabetCreds(cmd *cobra.Command, args []string) error { + // alphabet size is not part of the config + size, err := cmd.Flags().GetUint(alphabetSizeFlag) + if err != nil { + return err + } + + pwds := make([]string, size) + for i := 0; i < int(size); i++ { + pwds[i], err = config.AlphabetPassword(viper.GetViper(), i) + if err != nil { + return err + } + } + + cmd.Println("size:", size) + cmd.Println("alphabet-wallets:", viper.GetString(alphabetWalletsFlag)) + for i := range pwds { + cmd.Printf("wallet[%d]: %s\n", i, pwds[i]) + } + + return nil +} + +func generateStorageCreds(cmd *cobra.Command, args []string) error { + // storage wallet path is not part of the config + storageWalletPath, err := cmd.Flags().GetString(storageWalletFlag) + if err != nil { + return err + } + + cmd.Println("endpoint:", viper.GetString(endpointFlag)) + cmd.Println("alphabet-wallets:", viper.GetString(alphabetWalletsFlag)) + cmd.Println("storage-wallet:", storageWalletPath) + + return nil +} diff --git a/cmd/neofs-adm/internal/modules/morph/initialize.go b/cmd/neofs-adm/internal/modules/morph/initialize.go new file mode 100644 index 00000000..18a0da04 --- /dev/null +++ b/cmd/neofs-adm/internal/modules/morph/initialize.go @@ -0,0 +1,22 @@ +package morph + +import ( + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +func initializeSideChainCmd(cmd *cobra.Command, args []string) error { + // contract path is not part of the config + contractsPath, err := cmd.Flags().GetString(contractsInitFlag) + if err != nil { + return err + } + + cmd.Println("endpoint:", viper.GetString(endpointFlag)) + cmd.Println("alphabet-wallets:", viper.GetString(alphabetWalletsFlag)) + cmd.Println("contracts:", contractsPath) + cmd.Println("epoch-duration:", viper.GetUint(epochDurationInitFlag)) + cmd.Println("max-object-size:", viper.GetUint(maxObjectSizeInitFlag)) + + return nil +} diff --git a/cmd/neofs-adm/internal/modules/morph/root.go b/cmd/neofs-adm/internal/modules/morph/root.go new file mode 100644 index 00000000..0911a338 --- /dev/null +++ b/cmd/neofs-adm/internal/modules/morph/root.go @@ -0,0 +1,90 @@ +package morph + +import ( + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +const ( + alphabetWalletsFlag = "alphabet-wallets" + alphabetSizeFlag = "size" + endpointFlag = "rpc-endpoint" + storageWalletFlag = "storage-wallet" + contractsInitFlag = "contracts" + maxObjectSizeInitFlag = "network.max_object_size" + maxObjectSizeCLIFlag = "max-object-size" + epochDurationInitFlag = "network.epoch_duration" + epochDurationCLIFlag = "epoch-duration" +) + +var ( + // RootCmd is a root command of config section. + RootCmd = &cobra.Command{ + Use: "morph", + Short: "Section for morph network configuration commands.", + } + + generateAlphabetCmd = &cobra.Command{ + Use: "generate-alphabet", + Short: "Generate alphabet wallets for consensus nodes of the morph network", + PreRun: func(cmd *cobra.Command, _ []string) { + // PreRun fixes https://github.com/spf13/viper/issues/233 + _ = viper.BindPFlag(alphabetWalletsFlag, cmd.Flags().Lookup(alphabetWalletsFlag)) + }, + RunE: generateAlphabetCreds, + } + + initCmd = &cobra.Command{ + Use: "init", + Short: "Initialize side chain network with smart-contracts and network settings", + PreRun: func(cmd *cobra.Command, _ []string) { + _ = viper.BindPFlag(alphabetWalletsFlag, cmd.Flags().Lookup(alphabetWalletsFlag)) + _ = viper.BindPFlag(endpointFlag, cmd.Flags().Lookup(endpointFlag)) + _ = viper.BindPFlag(epochDurationInitFlag, cmd.Flags().Lookup(epochDurationCLIFlag)) + _ = viper.BindPFlag(maxObjectSizeInitFlag, cmd.Flags().Lookup(maxObjectSizeCLIFlag)) + }, + RunE: initializeSideChainCmd, + } + + generateStorageCmd = &cobra.Command{ + Use: "generate-storage-wallet", + Short: "Generate storage node wallet for the morph network", + PreRun: func(cmd *cobra.Command, _ []string) { + _ = viper.BindPFlag(alphabetWalletsFlag, cmd.Flags().Lookup(alphabetWalletsFlag)) + _ = viper.BindPFlag(endpointFlag, cmd.Flags().Lookup(endpointFlag)) + }, + RunE: generateStorageCreds, + } + + forceNewEpoch = &cobra.Command{ + Use: "force-new-epoch", + Short: "Create new NeoFS epoch event in the side chain", + PreRun: func(cmd *cobra.Command, _ []string) { + _ = viper.BindPFlag(alphabetWalletsFlag, cmd.Flags().Lookup(alphabetWalletsFlag)) + _ = viper.BindPFlag(endpointFlag, cmd.Flags().Lookup(endpointFlag)) + }, + RunE: forceNewEpochCmd, + } +) + +func init() { + RootCmd.AddCommand(generateAlphabetCmd) + generateAlphabetCmd.Flags().String(alphabetWalletsFlag, "", "path to alphabet wallets dir") + generateAlphabetCmd.Flags().Uint(alphabetSizeFlag, 7, "amount of alphabet wallets to generate") + + RootCmd.AddCommand(initCmd) + initCmd.Flags().String(alphabetWalletsFlag, "", "path to alphabet wallets dir") + initCmd.Flags().StringP(endpointFlag, "r", "", "N3 RPC node endpoint") + initCmd.Flags().String(contractsInitFlag, "", "path to archive with compiled NeoFS contracts (default fetched from latest github release)") + initCmd.Flags().Uint(epochDurationCLIFlag, 240, "amount of side chain blocks in one NeoFS epoch") + initCmd.Flags().Uint(maxObjectSizeCLIFlag, 67108864, "max single object size in bytes") + + RootCmd.AddCommand(generateStorageCmd) + generateStorageCmd.Flags().String(alphabetWalletsFlag, "", "path to alphabet wallets dir") + generateStorageCmd.Flags().StringP(endpointFlag, "r", "", "N3 RPC node endpoint") + generateStorageCmd.Flags().String(storageWalletFlag, "", "path to new storage node wallet") + + RootCmd.AddCommand(forceNewEpoch) + forceNewEpoch.Flags().String(alphabetWalletsFlag, "", "path to alphabet wallets dir") + forceNewEpoch.Flags().StringP(endpointFlag, "r", "", "N3 RPC node endpoint") +} diff --git a/cmd/neofs-adm/internal/modules/root.go b/cmd/neofs-adm/internal/modules/root.go index 58e97d64..1efd4f85 100644 --- a/cmd/neofs-adm/internal/modules/root.go +++ b/cmd/neofs-adm/internal/modules/root.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/nspcc-dev/neofs-node/cmd/neofs-adm/internal/modules/config" + "github.com/nspcc-dev/neofs-node/cmd/neofs-adm/internal/modules/morph" "github.com/nspcc-dev/neofs-node/misc" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -31,6 +32,7 @@ func init() { rootCmd.Flags().Bool("version", false, "application version") rootCmd.AddCommand(config.RootCmd) + rootCmd.AddCommand(morph.RootCmd) } func Execute() error {