2023-06-01 09:56:51 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/hex"
|
2023-07-14 08:11:45 +00:00
|
|
|
"encoding/json"
|
2023-06-01 09:56:51 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
internalclient "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
|
|
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
type policyPlaygroundREPL struct {
|
|
|
|
cmd *cobra.Command
|
|
|
|
nodes map[string]netmap.NodeInfo
|
|
|
|
}
|
|
|
|
|
2024-04-27 12:24:11 +00:00
|
|
|
func newPolicyPlaygroundREPL(cmd *cobra.Command) (*policyPlaygroundREPL, error) {
|
2023-06-01 09:56:51 +00:00
|
|
|
return &policyPlaygroundREPL{
|
|
|
|
cmd: cmd,
|
|
|
|
nodes: map[string]netmap.NodeInfo{},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repl *policyPlaygroundREPL) handleLs(args []string) error {
|
|
|
|
if len(args) > 0 {
|
|
|
|
return fmt.Errorf("too many arguments for command 'ls': got %d, want 0", len(args))
|
|
|
|
}
|
|
|
|
i := 1
|
|
|
|
for id, node := range repl.nodes {
|
|
|
|
var attrs []string
|
|
|
|
node.IterateAttributes(func(k, v string) {
|
2023-07-14 08:11:45 +00:00
|
|
|
attrs = append(attrs, fmt.Sprintf("%s:%q", k, v))
|
2023-06-01 09:56:51 +00:00
|
|
|
})
|
|
|
|
fmt.Printf("\t%2d: id=%s attrs={%v}\n", i, id, strings.Join(attrs, " "))
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repl *policyPlaygroundREPL) handleAdd(args []string) error {
|
|
|
|
if len(args) == 0 {
|
|
|
|
return fmt.Errorf("too few arguments for command 'add': got %d, want >0", len(args))
|
|
|
|
}
|
|
|
|
id := args[0]
|
|
|
|
key, err := hex.DecodeString(id)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("node id must be a hex string: got %q: %v", id, err)
|
|
|
|
}
|
|
|
|
node := repl.nodes[id]
|
|
|
|
node.SetPublicKey(key)
|
|
|
|
for _, attr := range args[1:] {
|
|
|
|
kv := strings.Split(attr, ":")
|
|
|
|
if len(kv) != 2 {
|
|
|
|
return fmt.Errorf("node attributes must be in the format 'KEY:VALUE': got %q", attr)
|
|
|
|
}
|
|
|
|
node.SetAttribute(kv[0], kv[1])
|
|
|
|
}
|
|
|
|
repl.nodes[id] = node
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-14 08:11:45 +00:00
|
|
|
func (repl *policyPlaygroundREPL) handleLoad(args []string) error {
|
|
|
|
if len(args) != 1 {
|
|
|
|
return fmt.Errorf("too few arguments for command 'add': got %d, want 1", len(args))
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonNetmap := map[string]map[string]string{}
|
|
|
|
|
|
|
|
b, err := os.ReadFile(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("reading netmap file %q: %v", args[0], err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(b, &jsonNetmap); err != nil {
|
|
|
|
return fmt.Errorf("decoding json netmap: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
repl.nodes = make(map[string]netmap.NodeInfo)
|
|
|
|
for id, attrs := range jsonNetmap {
|
|
|
|
key, err := hex.DecodeString(id)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("node id must be a hex string: got %q: %v", id, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
node := repl.nodes[id]
|
|
|
|
node.SetPublicKey(key)
|
|
|
|
for k, v := range attrs {
|
|
|
|
node.SetAttribute(k, v)
|
|
|
|
}
|
|
|
|
repl.nodes[id] = node
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-01 09:56:51 +00:00
|
|
|
func (repl *policyPlaygroundREPL) handleRemove(args []string) error {
|
|
|
|
if len(args) == 0 {
|
|
|
|
return fmt.Errorf("too few arguments for command 'remove': got %d, want >0", len(args))
|
|
|
|
}
|
|
|
|
id := args[0]
|
|
|
|
if _, exists := repl.nodes[id]; exists {
|
|
|
|
delete(repl.nodes, id)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("node not found: id=%q", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repl *policyPlaygroundREPL) handleEval(args []string) error {
|
2023-07-17 10:59:37 +00:00
|
|
|
policyStr := strings.TrimSpace(strings.Join(args, " "))
|
|
|
|
var nodes [][]netmap.NodeInfo
|
2023-06-01 09:56:51 +00:00
|
|
|
nm := repl.netMap()
|
2023-07-17 10:59:37 +00:00
|
|
|
|
|
|
|
if strings.HasPrefix(policyStr, "CBF") || strings.HasPrefix(policyStr, "SELECT") || strings.HasPrefix(policyStr, "FILTER") {
|
|
|
|
// Assume that the input is a partial SELECT-FILTER expression.
|
|
|
|
// Full inline policies always start with UNIQUE or REP keywords,
|
|
|
|
// or different prefixes when it's the case of an external file.
|
|
|
|
sfExpr, err := netmap.DecodeSelectFilterString(policyStr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing select-filter expression: %v", err)
|
|
|
|
}
|
|
|
|
nodes, err = nm.SelectFilterNodes(sfExpr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("building select-filter nodes: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Assume that the input is a full policy or input file otherwise.
|
|
|
|
placementPolicy, err := parseContainerPolicy(repl.cmd, policyStr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing placement policy: %v", err)
|
|
|
|
}
|
|
|
|
nodes, err = nm.ContainerNodes(*placementPolicy, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("building container nodes: %v", err)
|
|
|
|
}
|
2023-06-01 09:56:51 +00:00
|
|
|
}
|
|
|
|
for i, ns := range nodes {
|
|
|
|
var ids []string
|
|
|
|
for _, node := range ns {
|
|
|
|
ids = append(ids, hex.EncodeToString(node.PublicKey()))
|
|
|
|
}
|
|
|
|
fmt.Printf("\t%2d: %v\n", i+1, ids)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repl *policyPlaygroundREPL) netMap() netmap.NetMap {
|
|
|
|
var nm netmap.NetMap
|
|
|
|
var nodes []netmap.NodeInfo
|
|
|
|
for _, node := range repl.nodes {
|
|
|
|
nodes = append(nodes, node)
|
|
|
|
}
|
|
|
|
nm.SetNodes(nodes)
|
|
|
|
return nm
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repl *policyPlaygroundREPL) run() error {
|
|
|
|
if len(viper.GetString(commonflags.RPC)) > 0 {
|
|
|
|
key := key.GetOrGenerate(repl.cmd)
|
|
|
|
cli := internalclient.GetSDKClientByFlag(repl.cmd, key, commonflags.RPC)
|
|
|
|
|
|
|
|
var prm internalclient.NetMapSnapshotPrm
|
|
|
|
prm.SetClient(cli)
|
|
|
|
|
2023-06-02 11:45:30 +00:00
|
|
|
resp, err := internalclient.NetMapSnapshot(repl.cmd.Context(), prm)
|
2023-06-01 09:56:51 +00:00
|
|
|
commonCmd.ExitOnErr(repl.cmd, "unable to get netmap snapshot to populate initial netmap: %w", err)
|
|
|
|
|
|
|
|
for _, node := range resp.NetMap().Nodes() {
|
|
|
|
id := hex.EncodeToString(node.PublicKey())
|
|
|
|
repl.nodes[id] = node
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdHandlers := map[string]func([]string) error{
|
2023-07-17 07:23:51 +00:00
|
|
|
"list": repl.handleLs,
|
2023-06-01 09:56:51 +00:00
|
|
|
"ls": repl.handleLs,
|
|
|
|
"add": repl.handleAdd,
|
2023-07-14 08:11:45 +00:00
|
|
|
"load": repl.handleLoad,
|
2023-06-01 09:56:51 +00:00
|
|
|
"remove": repl.handleRemove,
|
2023-07-17 07:23:51 +00:00
|
|
|
"rm": repl.handleRemove,
|
2023-06-01 09:56:51 +00:00
|
|
|
"eval": repl.handleEval,
|
|
|
|
}
|
|
|
|
for reader := bufio.NewReader(os.Stdin); ; {
|
|
|
|
fmt.Print("> ")
|
|
|
|
line, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("reading line: %v", err)
|
|
|
|
}
|
|
|
|
parts := strings.Fields(line)
|
|
|
|
if len(parts) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cmd := parts[0]
|
|
|
|
handler, exists := cmdHandlers[cmd]
|
|
|
|
if exists {
|
|
|
|
if err := handler(parts[1:]); err != nil {
|
|
|
|
fmt.Printf("error: %v\n", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Printf("error: unknown command %q\n", cmd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var policyPlaygroundCmd = &cobra.Command{
|
|
|
|
Use: "policy-playground",
|
|
|
|
Short: "A REPL for testing placement policies",
|
|
|
|
Long: `A REPL for testing placement policies.
|
|
|
|
If a wallet and endpoint is provided, the initial netmap data will be loaded from the snapshot of the node. Otherwise, an empty playground is created.`,
|
2024-04-27 12:24:11 +00:00
|
|
|
Run: func(cmd *cobra.Command, _ []string) {
|
|
|
|
repl, err := newPolicyPlaygroundREPL(cmd)
|
2023-06-01 09:56:51 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "could not create policy playground: %w", err)
|
|
|
|
commonCmd.ExitOnErr(cmd, "policy playground failed: %w", repl.run())
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func initContainerPolicyPlaygroundCmd() {
|
|
|
|
commonflags.Init(policyPlaygroundCmd)
|
|
|
|
}
|