2022-01-25 11:57:58 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mr-tron/base58"
|
2022-03-11 15:24:11 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
|
2022-05-18 11:03:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
|
2022-05-18 09:22:02 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
|
2022-01-25 11:57:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/control"
|
|
|
|
controlSvc "github.com/nspcc-dev/neofs-node/pkg/services/control/server"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
restoreFilepathFlag = "path"
|
|
|
|
restoreIgnoreErrorsFlag = "no-errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
var restoreShardCmd = &cobra.Command{
|
|
|
|
Use: "restore",
|
|
|
|
Short: "Restore objects from shard",
|
|
|
|
Long: "Restore objects from shard to a file",
|
|
|
|
Run: restoreShard,
|
|
|
|
}
|
|
|
|
|
|
|
|
func restoreShard(cmd *cobra.Command, _ []string) {
|
|
|
|
key, err := getKeyNoGenerate()
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "", err)
|
2022-01-25 11:57:58 +00:00
|
|
|
|
|
|
|
body := new(control.RestoreShardRequest_Body)
|
|
|
|
|
|
|
|
rawID, err := base58.Decode(shardID)
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "incorrect shard ID encoding: %w", err)
|
2022-01-25 11:57:58 +00:00
|
|
|
body.SetShardID(rawID)
|
|
|
|
|
|
|
|
p, _ := cmd.Flags().GetString(restoreFilepathFlag)
|
|
|
|
body.SetFilepath(p)
|
|
|
|
|
|
|
|
ignore, _ := cmd.Flags().GetBool(restoreIgnoreErrorsFlag)
|
|
|
|
body.SetIgnoreErrors(ignore)
|
|
|
|
|
|
|
|
req := new(control.RestoreShardRequest)
|
|
|
|
req.SetBody(body)
|
|
|
|
|
|
|
|
err = controlSvc.SignMessage(key, req)
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "could not sign request: %w", err)
|
2022-01-25 11:57:58 +00:00
|
|
|
|
|
|
|
cli, err := getControlSDKClient(key)
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "", err)
|
2022-01-25 11:57:58 +00:00
|
|
|
|
2022-03-11 15:24:11 +00:00
|
|
|
var resp *control.RestoreShardResponse
|
|
|
|
err = cli.ExecRaw(func(client *client.Client) error {
|
|
|
|
resp, err = control.RestoreShard(client, req)
|
|
|
|
return err
|
|
|
|
})
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "rpc error: %w", err)
|
2022-01-25 11:57:58 +00:00
|
|
|
|
2022-05-16 13:15:31 +00:00
|
|
|
verifyResponseControl(cmd, resp.GetSignature(), resp.GetBody())
|
2022-01-25 11:57:58 +00:00
|
|
|
|
|
|
|
cmd.Println("Shard has been restored successfully.")
|
|
|
|
}
|
|
|
|
|
|
|
|
func initControlRestoreShardCmd() {
|
2022-05-18 09:22:02 +00:00
|
|
|
commonflags.InitWithoutRPC(restoreShardCmd)
|
2022-01-25 11:57:58 +00:00
|
|
|
|
|
|
|
flags := restoreShardCmd.Flags()
|
|
|
|
flags.String(controlRPC, controlRPCDefault, controlRPCUsage)
|
|
|
|
flags.StringVarP(&shardID, shardIDFlag, "", "", "Shard ID in base58 encoding")
|
|
|
|
flags.String(restoreFilepathFlag, "", "File to read objects from")
|
|
|
|
flags.Bool(restoreIgnoreErrorsFlag, false, "Skip invalid/unreadable objects")
|
|
|
|
|
|
|
|
_ = restoreShardCmd.MarkFlagRequired(shardIDFlag)
|
|
|
|
_ = restoreShardCmd.MarkFlagRequired(restoreFilepathFlag)
|
|
|
|
_ = restoreShardCmd.MarkFlagRequired(controlRPC)
|
|
|
|
}
|