forked from TrueCloudLab/frostfs-node
Move to frostfs-node
Signed-off-by: Pavel Karpy <p.karpy@yadro.com>
This commit is contained in:
parent
42554a9298
commit
923f84722a
934 changed files with 3470 additions and 3451 deletions
95
cmd/frostfs-cli/modules/tree/add.go
Normal file
95
cmd/frostfs-cli/modules/tree/add.go
Normal file
|
@ -0,0 +1,95 @@
|
|||
package tree
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/common"
|
||||
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
||||
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
||||
"github.com/TrueCloudLab/frostfs-node/pkg/services/tree"
|
||||
cid "github.com/TrueCloudLab/frostfs-sdk-go/container/id"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var addCmd = &cobra.Command{
|
||||
Use: "add",
|
||||
Short: "Add a node to the tree service",
|
||||
Run: add,
|
||||
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
|
||||
commonflags.Bind(cmd)
|
||||
},
|
||||
}
|
||||
|
||||
func initAddCmd() {
|
||||
commonflags.Init(addCmd)
|
||||
initCTID(addCmd)
|
||||
|
||||
ff := addCmd.Flags()
|
||||
ff.StringSlice(metaFlagKey, nil, "Meta pairs in the form of Key1=[0x]Value1,Key2=[0x]Value2")
|
||||
ff.Uint64(parentIDFlagKey, 0, "Parent node ID")
|
||||
|
||||
_ = cobra.MarkFlagRequired(ff, commonflags.RPC)
|
||||
}
|
||||
|
||||
func add(cmd *cobra.Command, _ []string) {
|
||||
pk := key.GetOrGenerate(cmd)
|
||||
|
||||
var cnr cid.ID
|
||||
err := cnr.DecodeString(cmd.Flag(commonflags.CIDFlag).Value.String())
|
||||
common.ExitOnErr(cmd, "decode container ID string: %w", err)
|
||||
|
||||
tid, _ := cmd.Flags().GetString(treeIDFlagKey)
|
||||
pid, _ := cmd.Flags().GetUint64(parentIDFlagKey)
|
||||
|
||||
meta, err := parseMeta(cmd)
|
||||
common.ExitOnErr(cmd, "meta data parsing: %w", err)
|
||||
|
||||
ctx := cmd.Context()
|
||||
|
||||
cli, err := _client(ctx)
|
||||
common.ExitOnErr(cmd, "client: %w", err)
|
||||
|
||||
rawCID := make([]byte, sha256.Size)
|
||||
cnr.Encode(rawCID)
|
||||
|
||||
req := new(tree.AddRequest)
|
||||
req.Body = &tree.AddRequest_Body{
|
||||
ContainerId: rawCID,
|
||||
TreeId: tid,
|
||||
ParentId: pid,
|
||||
Meta: meta,
|
||||
BearerToken: nil, // TODO: #1891 add token handling
|
||||
}
|
||||
|
||||
common.ExitOnErr(cmd, "message signing: %w", tree.SignMessage(req, pk))
|
||||
|
||||
resp, err := cli.Add(ctx, req)
|
||||
common.ExitOnErr(cmd, "rpc call: %w", err)
|
||||
|
||||
cmd.Println("Node ID: ", resp.Body.NodeId)
|
||||
}
|
||||
|
||||
func parseMeta(cmd *cobra.Command) ([]*tree.KeyValue, error) {
|
||||
raws, _ := cmd.Flags().GetStringSlice(metaFlagKey)
|
||||
if len(raws) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
pairs := make([]*tree.KeyValue, 0, len(raws))
|
||||
for i := range raws {
|
||||
kv := strings.SplitN(raws[i], "=", 2)
|
||||
if len(kv) != 2 {
|
||||
return nil, fmt.Errorf("invalid meta pair format: %s", raws[i])
|
||||
}
|
||||
|
||||
var pair tree.KeyValue
|
||||
pair.Key = kv[0]
|
||||
pair.Value = []byte(kv[1])
|
||||
|
||||
pairs = append(pairs, &pair)
|
||||
}
|
||||
|
||||
return pairs, nil
|
||||
}
|
94
cmd/frostfs-cli/modules/tree/add_by_path.go
Normal file
94
cmd/frostfs-cli/modules/tree/add_by_path.go
Normal file
|
@ -0,0 +1,94 @@
|
|||
package tree
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"strings"
|
||||
|
||||
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/common"
|
||||
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
||||
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
||||
"github.com/TrueCloudLab/frostfs-node/pkg/services/tree"
|
||||
cid "github.com/TrueCloudLab/frostfs-sdk-go/container/id"
|
||||
"github.com/TrueCloudLab/frostfs-sdk-go/object"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var addByPathCmd = &cobra.Command{
|
||||
Use: "add-by-path",
|
||||
Short: "Add a node by the path",
|
||||
Run: addByPath,
|
||||
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
|
||||
commonflags.Bind(cmd)
|
||||
},
|
||||
}
|
||||
|
||||
func initAddByPathCmd() {
|
||||
commonflags.Init(addByPathCmd)
|
||||
initCTID(addByPathCmd)
|
||||
|
||||
ff := addByPathCmd.Flags()
|
||||
|
||||
// tree service does not allow any attribute except
|
||||
// the 'FileName' but that's a limitation of the
|
||||
// current implementation, not the rule
|
||||
//ff.String(pathAttributeFlagKey, "", "Path attribute")
|
||||
ff.String(pathFlagKey, "", "Path to a node")
|
||||
ff.StringSlice(metaFlagKey, nil, "Meta pairs in the form of Key1=[0x]Value1,Key2=[0x]Value2")
|
||||
|
||||
_ = cobra.MarkFlagRequired(ff, commonflags.RPC)
|
||||
_ = cobra.MarkFlagRequired(ff, pathFlagKey)
|
||||
}
|
||||
|
||||
func addByPath(cmd *cobra.Command, _ []string) {
|
||||
pk := key.GetOrGenerate(cmd)
|
||||
|
||||
cidRaw, _ := cmd.Flags().GetString(commonflags.CIDFlag)
|
||||
|
||||
var cnr cid.ID
|
||||
err := cnr.DecodeString(cidRaw)
|
||||
common.ExitOnErr(cmd, "decode container ID string: %w", err)
|
||||
|
||||
tid, _ := cmd.Flags().GetString(treeIDFlagKey)
|
||||
ctx := cmd.Context()
|
||||
|
||||
cli, err := _client(ctx)
|
||||
common.ExitOnErr(cmd, "client: %w", err)
|
||||
|
||||
rawCID := make([]byte, sha256.Size)
|
||||
cnr.Encode(rawCID)
|
||||
|
||||
meta, err := parseMeta(cmd)
|
||||
common.ExitOnErr(cmd, "meta data parsing: %w", err)
|
||||
|
||||
path, _ := cmd.Flags().GetString(pathFlagKey)
|
||||
//pAttr, _ := cmd.Flags().GetString(pathAttributeFlagKey)
|
||||
|
||||
req := new(tree.AddByPathRequest)
|
||||
req.Body = &tree.AddByPathRequest_Body{
|
||||
ContainerId: rawCID,
|
||||
TreeId: tid,
|
||||
PathAttribute: object.AttributeFileName,
|
||||
//PathAttribute: pAttr,
|
||||
Path: strings.Split(path, "/"),
|
||||
Meta: meta,
|
||||
BearerToken: nil, // TODO: #1891 add token handling
|
||||
}
|
||||
|
||||
common.ExitOnErr(cmd, "message signing: %w", tree.SignMessage(req, pk))
|
||||
|
||||
resp, err := cli.AddByPath(ctx, req)
|
||||
common.ExitOnErr(cmd, "rpc call: %w", err)
|
||||
|
||||
cmd.Printf("Parent ID: %d\n", resp.GetBody().GetParentId())
|
||||
|
||||
nn := resp.GetBody().GetNodes()
|
||||
if len(nn) == 0 {
|
||||
common.PrintVerbose("No new nodes were created")
|
||||
return
|
||||
}
|
||||
|
||||
cmd.Println("Created nodes:")
|
||||
for _, node := range resp.GetBody().GetNodes() {
|
||||
cmd.Printf("\t%d\n", node)
|
||||
}
|
||||
}
|
40
cmd/frostfs-cli/modules/tree/client.go
Normal file
40
cmd/frostfs-cli/modules/tree/client.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
package tree
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
||||
"github.com/TrueCloudLab/frostfs-node/pkg/network"
|
||||
"github.com/TrueCloudLab/frostfs-node/pkg/services/tree"
|
||||
"github.com/spf13/viper"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
)
|
||||
|
||||
// _client returns grpc Tree service client. Should be removed
|
||||
// after making Tree API public.
|
||||
func _client(ctx context.Context) (tree.TreeServiceClient, error) {
|
||||
var netAddr network.Address
|
||||
err := netAddr.FromString(viper.GetString(commonflags.RPC))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
opts := make([]grpc.DialOption, 1, 2)
|
||||
opts[0] = grpc.WithBlock()
|
||||
|
||||
if !strings.HasPrefix(netAddr.URIAddr(), "grpcs:") {
|
||||
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
}
|
||||
|
||||
// a default connection establishing timeout
|
||||
const defaultClientConnectTimeout = time.Second * 2
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, defaultClientConnectTimeout)
|
||||
cc, err := grpc.DialContext(ctx, netAddr.URIAddr(), opts...)
|
||||
cancel()
|
||||
|
||||
return tree.NewTreeServiceClient(cc), err
|
||||
}
|
98
cmd/frostfs-cli/modules/tree/get_by_path.go
Normal file
98
cmd/frostfs-cli/modules/tree/get_by_path.go
Normal file
|
@ -0,0 +1,98 @@
|
|||
package tree
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"strings"
|
||||
|
||||
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/common"
|
||||
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
||||
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
||||
"github.com/TrueCloudLab/frostfs-node/pkg/services/tree"
|
||||
cid "github.com/TrueCloudLab/frostfs-sdk-go/container/id"
|
||||
"github.com/TrueCloudLab/frostfs-sdk-go/object"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var getByPathCmd = &cobra.Command{
|
||||
Use: "get-by-path",
|
||||
Short: "Get a node by its path",
|
||||
Run: getByPath,
|
||||
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
|
||||
commonflags.Bind(cmd)
|
||||
},
|
||||
}
|
||||
|
||||
func initGetByPathCmd() {
|
||||
commonflags.Init(getByPathCmd)
|
||||
initCTID(getByPathCmd)
|
||||
|
||||
ff := getByPathCmd.Flags()
|
||||
|
||||
// tree service does not allow any attribute except
|
||||
// the 'FileName' but that's a limitation of the
|
||||
// current implementation, not the rule
|
||||
//ff.String(pathAttributeFlagKey, "", "Path attribute")
|
||||
ff.String(pathFlagKey, "", "Path to a node")
|
||||
|
||||
ff.Bool(latestOnlyFlagKey, false, "Look only for the latest version of a node")
|
||||
|
||||
_ = cobra.MarkFlagRequired(ff, commonflags.RPC)
|
||||
}
|
||||
|
||||
func getByPath(cmd *cobra.Command, _ []string) {
|
||||
pk := key.GetOrGenerate(cmd)
|
||||
|
||||
cidRaw, _ := cmd.Flags().GetString(commonflags.CIDFlag)
|
||||
|
||||
var cnr cid.ID
|
||||
err := cnr.DecodeString(cidRaw)
|
||||
common.ExitOnErr(cmd, "decode container ID string: %w", err)
|
||||
|
||||
tid, _ := cmd.Flags().GetString(treeIDFlagKey)
|
||||
ctx := cmd.Context()
|
||||
|
||||
cli, err := _client(ctx)
|
||||
common.ExitOnErr(cmd, "client: %w", err)
|
||||
|
||||
rawCID := make([]byte, sha256.Size)
|
||||
cnr.Encode(rawCID)
|
||||
|
||||
latestOnly, _ := cmd.Flags().GetBool(latestOnlyFlagKey)
|
||||
path, _ := cmd.Flags().GetString(pathFlagKey)
|
||||
//pAttr, _ := cmd.Flags().GetString(pathAttributeFlagKey)
|
||||
|
||||
req := new(tree.GetNodeByPathRequest)
|
||||
req.Body = &tree.GetNodeByPathRequest_Body{
|
||||
ContainerId: rawCID,
|
||||
TreeId: tid,
|
||||
PathAttribute: object.AttributeFileName,
|
||||
//PathAttribute: pAttr,
|
||||
Path: strings.Split(path, "/"),
|
||||
LatestOnly: latestOnly,
|
||||
AllAttributes: true,
|
||||
BearerToken: nil, // TODO: #1891 add token handling
|
||||
}
|
||||
|
||||
common.ExitOnErr(cmd, "message signing: %w", tree.SignMessage(req, pk))
|
||||
|
||||
resp, err := cli.GetNodeByPath(ctx, req)
|
||||
common.ExitOnErr(cmd, "rpc call: %w", err)
|
||||
|
||||
nn := resp.GetBody().GetNodes()
|
||||
if len(nn) == 0 {
|
||||
common.PrintVerbose("The node is not found")
|
||||
return
|
||||
}
|
||||
|
||||
for _, n := range nn {
|
||||
cmd.Printf("%d:\n", n.GetNodeId())
|
||||
|
||||
cmd.Println("\tParent ID: ", n.GetParentId())
|
||||
cmd.Println("\tTimestamp: ", n.GetTimestamp())
|
||||
|
||||
cmd.Println("\tMeta pairs: ")
|
||||
for _, kv := range n.GetMeta() {
|
||||
cmd.Printf("\t\t%s: %s\n", kv.GetKey(), string(kv.GetValue()))
|
||||
}
|
||||
}
|
||||
}
|
63
cmd/frostfs-cli/modules/tree/list.go
Normal file
63
cmd/frostfs-cli/modules/tree/list.go
Normal file
|
@ -0,0 +1,63 @@
|
|||
package tree
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
|
||||
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/common"
|
||||
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
||||
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
||||
"github.com/TrueCloudLab/frostfs-node/pkg/services/tree"
|
||||
cid "github.com/TrueCloudLab/frostfs-sdk-go/container/id"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var listCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "Get tree IDs",
|
||||
Run: list,
|
||||
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
|
||||
commonflags.Bind(cmd)
|
||||
},
|
||||
}
|
||||
|
||||
func initListCmd() {
|
||||
commonflags.Init(listCmd)
|
||||
|
||||
ff := listCmd.Flags()
|
||||
ff.String(commonflags.CIDFlag, "", commonflags.CIDFlagUsage)
|
||||
_ = listCmd.MarkFlagRequired(commonflags.CIDFlag)
|
||||
|
||||
_ = cobra.MarkFlagRequired(ff, commonflags.RPC)
|
||||
}
|
||||
|
||||
func list(cmd *cobra.Command, _ []string) {
|
||||
pk := key.GetOrGenerate(cmd)
|
||||
cidString, _ := cmd.Flags().GetString(commonflags.CIDFlag)
|
||||
|
||||
var cnr cid.ID
|
||||
err := cnr.DecodeString(cidString)
|
||||
common.ExitOnErr(cmd, "decode container ID string: %w", err)
|
||||
|
||||
ctx := cmd.Context()
|
||||
|
||||
cli, err := _client(ctx)
|
||||
common.ExitOnErr(cmd, "client: %w", err)
|
||||
|
||||
rawCID := make([]byte, sha256.Size)
|
||||
cnr.Encode(rawCID)
|
||||
|
||||
req := &tree.TreeListRequest{
|
||||
Body: &tree.TreeListRequest_Body{
|
||||
ContainerId: rawCID,
|
||||
},
|
||||
}
|
||||
|
||||
common.ExitOnErr(cmd, "message signing: %w", tree.SignMessage(req, pk))
|
||||
|
||||
resp, err := cli.TreeList(ctx, req)
|
||||
common.ExitOnErr(cmd, "rpc call: %w", err)
|
||||
|
||||
for _, treeID := range resp.GetBody().GetIds() {
|
||||
cmd.Println(treeID)
|
||||
}
|
||||
}
|
45
cmd/frostfs-cli/modules/tree/root.go
Normal file
45
cmd/frostfs-cli/modules/tree/root.go
Normal file
|
@ -0,0 +1,45 @@
|
|||
package tree
|
||||
|
||||
import (
|
||||
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var Cmd = &cobra.Command{
|
||||
Use: "tree",
|
||||
Short: "Operations with the Tree service",
|
||||
}
|
||||
|
||||
func init() {
|
||||
Cmd.AddCommand(addCmd)
|
||||
Cmd.AddCommand(getByPathCmd)
|
||||
Cmd.AddCommand(addByPathCmd)
|
||||
Cmd.AddCommand(listCmd)
|
||||
|
||||
initAddCmd()
|
||||
initGetByPathCmd()
|
||||
initAddByPathCmd()
|
||||
initListCmd()
|
||||
}
|
||||
|
||||
const (
|
||||
treeIDFlagKey = "tid"
|
||||
parentIDFlagKey = "pid"
|
||||
|
||||
metaFlagKey = "meta"
|
||||
|
||||
pathFlagKey = "path"
|
||||
pathAttributeFlagKey = "pattr"
|
||||
|
||||
latestOnlyFlagKey = "latest"
|
||||
)
|
||||
|
||||
func initCTID(cmd *cobra.Command) {
|
||||
ff := cmd.Flags()
|
||||
|
||||
ff.String(commonflags.CIDFlag, "", commonflags.CIDFlagUsage)
|
||||
_ = cmd.MarkFlagRequired(commonflags.CIDFlag)
|
||||
|
||||
ff.String(treeIDFlagKey, "", "Tree ID")
|
||||
_ = cmd.MarkFlagRequired(treeIDFlagKey)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue