[#920] cli: Rename RPC flag for control service

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-10-15 14:24:02 +03:00 committed by Alex Vanin
parent f0252e00c1
commit 0866c1fb90
3 changed files with 71 additions and 16 deletions

View file

@ -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

View file

@ -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 '<host>:<port>')"
)
// 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
}

View file

@ -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
}