From 0866c1fb90116b46bffbb6830ff783d59f409749 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Fri, 15 Oct 2021 14:24:02 +0300 Subject: [PATCH] [#920] cli: Rename RPC flag for control service Signed-off-by: Pavel Karpy --- CHANGELOG.md | 3 ++ cmd/neofs-cli/modules/control.go | 78 ++++++++++++++++++++++++++------ cmd/neofs-cli/modules/root.go | 6 +-- 3 files changed, 71 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f243022..5d7d256f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,8 +8,11 @@ Deleted `NEOFS_IR_NOTARY_SIDE_DEPOSIT_AMOUNT`, `NEOFS_IR_NOTARY_MAIN_DEPOSIT_AMO and `NEOFS_IR_TIMERS_SIDE_NOTARY`, `NEOFS_IR_TIMERS_MAIN_NOTARY` Inner Ring envs. Deleted `NEOFS_MORPH_NOTARY_DEPOSIT_AMOUNT` and `NEOFS_MORPH_NOTARY_DEPOSIT_DURATION` Storage Node envs. +`control` CLI command does not have `--rpc-endpoint`/`r` flag, use `endpoint` +instead. ### Changed +- Renamed `--rpc-endpoint` CLI flag for `control command` (#879) - Do not use global flags in CLI; delete useless flags from `accounting balance` command (#810) - SN and IR notary deposits are made dynamically depending on the Notary and diff --git a/cmd/neofs-cli/modules/control.go b/cmd/neofs-cli/modules/control.go index 49de17d7..bd830a5d 100644 --- a/cmd/neofs-cli/modules/control.go +++ b/cmd/neofs-cli/modules/control.go @@ -2,6 +2,7 @@ package cmd import ( "crypto/ecdsa" + "crypto/tls" "fmt" "github.com/nspcc-dev/neofs-api-go/pkg/client" @@ -12,6 +13,7 @@ import ( ircontrolsrv "github.com/nspcc-dev/neofs-node/pkg/services/control/ir/server" controlSvc "github.com/nspcc-dev/neofs-node/pkg/services/control/server" "github.com/spf13/cobra" + "github.com/spf13/viper" ) var controlCmd = &cobra.Command{ @@ -19,7 +21,15 @@ var controlCmd = &cobra.Command{ Short: "Operations with storage node", Long: `Operations with storage node`, PersistentPreRun: func(cmd *cobra.Command, args []string) { - bindCommonFlags(cmd) + ff := cmd.Flags() + + _ = viper.BindPFlag(generateKey, ff.Lookup(generateKey)) + _ = viper.BindPFlag(binaryKey, ff.Lookup(binaryKey)) + _ = viper.BindPFlag(walletPath, ff.Lookup(walletPath)) + _ = viper.BindPFlag(wif, ff.Lookup(wif)) + _ = viper.BindPFlag(address, ff.Lookup(address)) + _ = viper.BindPFlag(controlRPC, ff.Lookup(controlRPC)) + _ = viper.BindPFlag(verbose, ff.Lookup(verbose)) }, } @@ -44,6 +54,12 @@ const ( netmapStatusOffline = "offline" ) +const ( + controlRPC = "endpoint" + controlRPCDefault = "" + controlRPCUsage = "remote node control address (as 'multiaddr' or ':')" +) + // control healthcheck flags const ( healthcheckIRFlag = "ir" @@ -57,15 +73,21 @@ var ( var netmapStatus string func initControlHealthCheckCmd() { - initCommonFlags(healthCheckCmd) + initCommonFlagsWithoutRPC(healthCheckCmd) - healthCheckCmd.Flags().BoolVar(&healthCheckIRVar, healthcheckIRFlag, false, "Communicate with IR node") + flags := healthCheckCmd.Flags() + + flags.String(controlRPC, controlRPCDefault, controlRPCUsage) + flags.BoolVar(&healthCheckIRVar, healthcheckIRFlag, false, "Communicate with IR node") } func initControlSetNetmapStatusCmd() { - initCommonFlags(setNetmapStatusCmd) + initCommonFlagsWithoutRPC(setNetmapStatusCmd) - setNetmapStatusCmd.Flags().StringVarP(&netmapStatus, netmapStatusFlag, "", "", + flags := setNetmapStatusCmd.Flags() + + flags.String(controlRPC, controlRPCDefault, controlRPCUsage) + flags.StringVarP(&netmapStatus, netmapStatusFlag, "", "", fmt.Sprintf("new netmap status keyword ('%s', '%s')", netmapStatusOnline, netmapStatusOffline, @@ -76,18 +98,23 @@ func initControlSetNetmapStatusCmd() { } func initControlDropObjectsCmd() { - initCommonFlags(dropObjectsCmd) + initCommonFlagsWithoutRPC(dropObjectsCmd) - dropObjectsCmd.Flags().StringSliceVarP(&dropObjectsList, dropObjectsFlag, "o", nil, + flags := dropObjectsCmd.Flags() + + flags.String(controlRPC, controlRPCDefault, controlRPCUsage) + flags.StringSliceVarP(&dropObjectsList, dropObjectsFlag, "o", nil, "List of object addresses to be removed in string format") _ = dropObjectsCmd.MarkFlagRequired(dropObjectsFlag) } func initControlSnapshotCmd() { - initCommonFlags(snapshotCmd) + initCommonFlagsWithoutRPC(snapshotCmd) - snapshotCmd.Flags().BoolVar(&netmapSnapshotJSON, "json", false, + flags := snapshotCmd.Flags() + + flags.BoolVar(&netmapSnapshotJSON, "json", false, "print netmap structure in JSON format") } @@ -111,7 +138,7 @@ func healthCheck(cmd *cobra.Command, _ []string) { key, err := getKey() exitOnErr(cmd, err) - cli, err := getSDKClient(key) + cli, err := getControlSDKClient(key) exitOnErr(cmd, err) if healthCheckIRVar { @@ -192,7 +219,7 @@ func setNetmapStatus(cmd *cobra.Command, _ []string) { err = controlSvc.SignMessage(key, req) exitOnErr(cmd, errf("could not sign request: %w", err)) - cli, err := getSDKClient(key) + cli, err := getControlSDKClient(key) exitOnErr(cmd, err) resp, err := control.SetNetmapStatus(cli.Raw(), req) @@ -249,7 +276,7 @@ var dropObjectsCmd = &cobra.Command{ err = controlSvc.SignMessage(key, req) exitOnErr(cmd, errf("could not sign request: %w", err)) - cli, err := getSDKClient(key) + cli, err := getControlSDKClient(key) exitOnErr(cmd, err) resp, err := control.DropObjects(cli.Raw(), req) @@ -283,7 +310,7 @@ var snapshotCmd = &cobra.Command{ err = controlSvc.SignMessage(key, req) exitOnErr(cmd, errf("could not sign request: %w", err)) - cli, err := getSDKClient(key) + cli, err := getControlSDKClient(key) exitOnErr(cmd, err) resp, err := control.NetmapSnapshot(cli.Raw(), req) @@ -302,3 +329,28 @@ var snapshotCmd = &cobra.Command{ prettyPrintNetmap(cmd, resp.GetBody().GetNetmap(), netmapSnapshotJSON) }, } + +// getControlSDKClient is the same getSDKClient but with +// another RPC endpoint flag. +func getControlSDKClient(key *ecdsa.PrivateKey) (client.Client, error) { + netAddr, err := getEndpointAddress(controlRPC) + if err != nil { + return nil, err + } + + options := []client.Option{ + client.WithAddress(netAddr.HostAddr()), + client.WithDefaultPrivateKey(key), + } + + if netAddr.TLSEnabled() { + options = append(options, client.WithTLSConfig(&tls.Config{})) + } + + c, err := client.New(options...) + if err != nil { + return nil, fmt.Errorf("coult not init api client:%w", err) + } + + return c, err +} diff --git a/cmd/neofs-cli/modules/root.go b/cmd/neofs-cli/modules/root.go index 38f60072..e41e2bcb 100644 --- a/cmd/neofs-cli/modules/root.go +++ b/cmd/neofs-cli/modules/root.go @@ -276,8 +276,8 @@ func getKeyFromWallet(w *wallet.Wallet, addrStr string) (*ecdsa.PrivateKey, erro // getEndpointAddress returns network address structure that stores multiaddr // inside, parsed from global arguments. -func getEndpointAddress() (addr network.Address, err error) { - endpoint := viper.GetString(rpc) +func getEndpointAddress(endpointFlag string) (addr network.Address, err error) { + endpoint := viper.GetString(endpointFlag) err = addr.FromString(endpoint) if err != nil { @@ -290,7 +290,7 @@ func getEndpointAddress() (addr network.Address, err error) { // getSDKClient returns default neofs-api-go sdk client. Consider using // opts... to provide TTL or other global configuration flags. func getSDKClient(key *ecdsa.PrivateKey) (client.Client, error) { - netAddr, err := getEndpointAddress() + netAddr, err := getEndpointAddress(rpc) if err != nil { return nil, err }