2022-05-23 16:26:27 +00:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
|
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
|
2022-10-18 07:29:25 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
|
2022-05-24 08:22:23 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
|
2022-05-23 16:26:27 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/control"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
netmapStatusFlag = "status"
|
|
|
|
|
|
|
|
netmapStatusOnline = "online"
|
|
|
|
netmapStatusOffline = "offline"
|
|
|
|
netmapStatusMaintenance = "maintenance"
|
|
|
|
)
|
|
|
|
|
|
|
|
var setNetmapStatusCmd = &cobra.Command{
|
|
|
|
Use: "set-status",
|
|
|
|
Short: "Set status of the storage node in NeoFS network map",
|
|
|
|
Long: "Set status of the storage node in NeoFS network map",
|
|
|
|
Run: setNetmapStatus,
|
|
|
|
}
|
|
|
|
|
|
|
|
func initControlSetNetmapStatusCmd() {
|
2022-10-17 15:37:15 +00:00
|
|
|
initControlFlags(setNetmapStatusCmd)
|
2022-05-23 16:26:27 +00:00
|
|
|
|
|
|
|
flags := setNetmapStatusCmd.Flags()
|
|
|
|
flags.String(netmapStatusFlag, "",
|
2022-10-11 14:02:03 +00:00
|
|
|
fmt.Sprintf("New netmap status keyword ('%s', '%s', '%s')",
|
2022-05-23 16:26:27 +00:00
|
|
|
netmapStatusOnline,
|
|
|
|
netmapStatusOffline,
|
|
|
|
netmapStatusMaintenance,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
_ = setNetmapStatusCmd.MarkFlagRequired(netmapStatusFlag)
|
2022-10-18 15:42:14 +00:00
|
|
|
|
|
|
|
flags.BoolP(commonflags.ForceFlag, commonflags.ForceFlagShorthand, false,
|
|
|
|
"Force turning to local maintenance")
|
2022-05-23 16:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setNetmapStatus(cmd *cobra.Command, _ []string) {
|
2022-05-24 08:22:23 +00:00
|
|
|
pk := key.Get(cmd)
|
2022-10-18 15:42:14 +00:00
|
|
|
body := new(control.SetNetmapStatusRequest_Body)
|
|
|
|
force, _ := cmd.Flags().GetBool(commonflags.ForceFlag)
|
2022-05-23 16:26:27 +00:00
|
|
|
|
2022-10-18 15:42:14 +00:00
|
|
|
printIgnoreForce := func(st control.NetmapStatus) {
|
|
|
|
if force {
|
|
|
|
common.PrintVerbose("Ignore --%s flag for %s state.", commonflags.ForceFlag, st)
|
|
|
|
}
|
|
|
|
}
|
2022-05-23 16:26:27 +00:00
|
|
|
|
|
|
|
switch st, _ := cmd.Flags().GetString(netmapStatusFlag); st {
|
|
|
|
default:
|
|
|
|
common.ExitOnErr(cmd, "", fmt.Errorf("unsupported status %s", st))
|
|
|
|
case netmapStatusOnline:
|
2022-10-18 15:42:14 +00:00
|
|
|
body.SetStatus(control.NetmapStatus_ONLINE)
|
|
|
|
printIgnoreForce(control.NetmapStatus_ONLINE)
|
2022-05-23 16:26:27 +00:00
|
|
|
case netmapStatusOffline:
|
2022-10-18 15:42:14 +00:00
|
|
|
body.SetStatus(control.NetmapStatus_OFFLINE)
|
|
|
|
printIgnoreForce(control.NetmapStatus_OFFLINE)
|
2022-05-23 16:26:27 +00:00
|
|
|
case netmapStatusMaintenance:
|
2022-10-18 15:42:14 +00:00
|
|
|
body.SetStatus(control.NetmapStatus_MAINTENANCE)
|
2022-10-18 07:29:25 +00:00
|
|
|
|
2022-10-18 15:42:14 +00:00
|
|
|
if force {
|
|
|
|
body.SetForceMaintenance()
|
|
|
|
common.PrintVerbose("Local maintenance will be forced.")
|
2022-10-18 07:29:25 +00:00
|
|
|
}
|
2022-05-23 16:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req := new(control.SetNetmapStatusRequest)
|
|
|
|
req.SetBody(body)
|
|
|
|
|
|
|
|
signRequest(cmd, pk, req)
|
|
|
|
|
|
|
|
cli := getClient(cmd, pk)
|
|
|
|
|
|
|
|
var resp *control.SetNetmapStatusResponse
|
|
|
|
var err error
|
|
|
|
err = cli.ExecRaw(func(client *rawclient.Client) error {
|
|
|
|
resp, err = control.SetNetmapStatus(client, req)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
common.ExitOnErr(cmd, "rpc error: %w", err)
|
|
|
|
|
|
|
|
verifyResponse(cmd, resp.GetSignature(), resp.GetBody())
|
|
|
|
|
|
|
|
cmd.Println("Network status update request successfully sent.")
|
|
|
|
}
|