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
49
cmd/frostfs-cli/internal/common/eacl.go
Normal file
49
cmd/frostfs-cli/internal/common/eacl.go
Normal file
|
@ -0,0 +1,49 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
"github.com/TrueCloudLab/frostfs-node/pkg/core/version"
|
||||
"github.com/TrueCloudLab/frostfs-sdk-go/eacl"
|
||||
versionSDK "github.com/TrueCloudLab/frostfs-sdk-go/version"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var errUnsupportedEACLFormat = errors.New("unsupported eACL format")
|
||||
|
||||
// ReadEACL reads extended ACL table from eaclPath.
|
||||
func ReadEACL(cmd *cobra.Command, eaclPath string) *eacl.Table {
|
||||
_, err := os.Stat(eaclPath) // check if `eaclPath` is an existing file
|
||||
if err != nil {
|
||||
ExitOnErr(cmd, "", errors.New("incorrect path to file with EACL"))
|
||||
}
|
||||
|
||||
PrintVerbose("Reading EACL from file: %s", eaclPath)
|
||||
|
||||
data, err := os.ReadFile(eaclPath)
|
||||
ExitOnErr(cmd, "can't read file with EACL: %w", err)
|
||||
|
||||
table := eacl.NewTable()
|
||||
|
||||
if err = table.UnmarshalJSON(data); err == nil {
|
||||
validateAndFixEACLVersion(table)
|
||||
PrintVerbose("Parsed JSON encoded EACL table")
|
||||
return table
|
||||
}
|
||||
|
||||
if err = table.Unmarshal(data); err == nil {
|
||||
validateAndFixEACLVersion(table)
|
||||
PrintVerbose("Parsed binary encoded EACL table")
|
||||
return table
|
||||
}
|
||||
|
||||
ExitOnErr(cmd, "", errUnsupportedEACLFormat)
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateAndFixEACLVersion(table *eacl.Table) {
|
||||
if !version.IsValid(table.Version()) {
|
||||
table.SetVersion(versionSDK.Current())
|
||||
}
|
||||
}
|
28
cmd/frostfs-cli/internal/common/epoch.go
Normal file
28
cmd/frostfs-cli/internal/common/epoch.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// ParseEpoch parses epoch argument. Second return value is true if
|
||||
// the specified epoch is relative, and false otherwise.
|
||||
func ParseEpoch(cmd *cobra.Command, flag string) (uint64, bool, error) {
|
||||
s, _ := cmd.Flags().GetString(flag)
|
||||
if len(s) == 0 {
|
||||
return 0, false, nil
|
||||
}
|
||||
|
||||
relative := s[0] == '+'
|
||||
if relative {
|
||||
s = s[1:]
|
||||
}
|
||||
|
||||
epoch, err := strconv.ParseUint(s, 10, 64)
|
||||
if err != nil {
|
||||
return 0, relative, fmt.Errorf("can't parse epoch for %s argument: %w", flag, err)
|
||||
}
|
||||
return epoch, relative, nil
|
||||
}
|
50
cmd/frostfs-cli/internal/common/exit.go
Normal file
50
cmd/frostfs-cli/internal/common/exit.go
Normal file
|
@ -0,0 +1,50 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
sdkstatus "github.com/TrueCloudLab/frostfs-sdk-go/client/status"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// ExitOnErr prints error and exits with a code that matches
|
||||
// one of the common errors from sdk library. If no errors
|
||||
// found, exits with 1 code.
|
||||
// Does nothing if passed error in nil.
|
||||
func ExitOnErr(cmd *cobra.Command, errFmt string, err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if errFmt != "" {
|
||||
err = fmt.Errorf(errFmt, err)
|
||||
}
|
||||
|
||||
const (
|
||||
_ = iota
|
||||
internal
|
||||
aclDenied
|
||||
)
|
||||
|
||||
var (
|
||||
code int
|
||||
|
||||
internalErr = new(sdkstatus.ServerInternal)
|
||||
accessErr = new(sdkstatus.ObjectAccessDenied)
|
||||
)
|
||||
|
||||
switch {
|
||||
case errors.As(err, &internalErr):
|
||||
code = internal
|
||||
case errors.As(err, &accessErr):
|
||||
code = aclDenied
|
||||
err = fmt.Errorf("%w: %s", err, accessErr.Reason())
|
||||
default:
|
||||
code = internal
|
||||
}
|
||||
|
||||
cmd.PrintErrln(err)
|
||||
os.Exit(code)
|
||||
}
|
23
cmd/frostfs-cli/internal/common/json.go
Normal file
23
cmd/frostfs-cli/internal/common/json.go
Normal file
|
@ -0,0 +1,23 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// PrettyPrintJSON prints m as an indented JSON to the cmd output.
|
||||
func PrettyPrintJSON(cmd *cobra.Command, m json.Marshaler, entity string) {
|
||||
data, err := m.MarshalJSON()
|
||||
if err != nil {
|
||||
PrintVerbose("Can't convert %s to json: %w", entity, err)
|
||||
return
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
if err := json.Indent(buf, data, "", " "); err != nil {
|
||||
PrintVerbose("Can't pretty print json: %w", err)
|
||||
return
|
||||
}
|
||||
cmd.Println(buf)
|
||||
}
|
49
cmd/frostfs-cli/internal/common/netmap.go
Normal file
49
cmd/frostfs-cli/internal/common/netmap.go
Normal file
|
@ -0,0 +1,49 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
|
||||
"github.com/TrueCloudLab/frostfs-sdk-go/netmap"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// PrettyPrintNodeInfo print information about network node with given indent and index.
|
||||
// To avoid printing attribute list use short parameter.
|
||||
func PrettyPrintNodeInfo(cmd *cobra.Command, node netmap.NodeInfo,
|
||||
index int, indent string, short bool) {
|
||||
var strState string
|
||||
|
||||
switch {
|
||||
default:
|
||||
strState = "STATE_UNSUPPORTED"
|
||||
case node.IsOnline():
|
||||
strState = "ONLINE"
|
||||
case node.IsOffline():
|
||||
strState = "OFFLINE"
|
||||
case node.IsMaintenance():
|
||||
strState = "MAINTENANCE"
|
||||
}
|
||||
|
||||
cmd.Printf("%sNode %d: %s %s ", indent, index+1, hex.EncodeToString(node.PublicKey()), strState)
|
||||
|
||||
netmap.IterateNetworkEndpoints(node, func(endpoint string) {
|
||||
cmd.Printf("%s ", endpoint)
|
||||
})
|
||||
cmd.Println()
|
||||
|
||||
if !short {
|
||||
node.IterateAttributes(func(key, value string) {
|
||||
cmd.Printf("%s\t%s: %s\n", indent, key, value)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// PrettyPrintNetMap print information about network map.
|
||||
func PrettyPrintNetMap(cmd *cobra.Command, nm netmap.NetMap) {
|
||||
cmd.Println("Epoch:", nm.Epoch())
|
||||
|
||||
nodes := nm.Nodes()
|
||||
for i := range nodes {
|
||||
PrettyPrintNodeInfo(cmd, nodes[i], i, "", false)
|
||||
}
|
||||
}
|
66
cmd/frostfs-cli/internal/common/token.go
Normal file
66
cmd/frostfs-cli/internal/common/token.go
Normal file
|
@ -0,0 +1,66 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/TrueCloudLab/frostfs-sdk-go/bearer"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// ReadBearerToken reads bearer token from the path provided in a specified flag.
|
||||
func ReadBearerToken(cmd *cobra.Command, flagname string) *bearer.Token {
|
||||
path, err := cmd.Flags().GetString(flagname)
|
||||
ExitOnErr(cmd, "", err)
|
||||
|
||||
if len(path) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
PrintVerbose("Reading bearer token from file [%s]...", path)
|
||||
|
||||
var tok bearer.Token
|
||||
|
||||
err = ReadBinaryOrJSON(&tok, path)
|
||||
ExitOnErr(cmd, "invalid bearer token: %v", err)
|
||||
|
||||
return &tok
|
||||
}
|
||||
|
||||
// BinaryOrJSON is an interface of entities which provide json.Unmarshaler
|
||||
// and NeoFS binary decoder.
|
||||
type BinaryOrJSON interface {
|
||||
Unmarshal([]byte) error
|
||||
json.Unmarshaler
|
||||
}
|
||||
|
||||
// ReadBinaryOrJSON reads file data using provided path and decodes
|
||||
// BinaryOrJSON from the data.
|
||||
func ReadBinaryOrJSON(dst BinaryOrJSON, fPath string) error {
|
||||
PrintVerbose("Reading file [%s]...", fPath)
|
||||
|
||||
// try to read session token from file
|
||||
data, err := os.ReadFile(fPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read file <%s>: %w", fPath, err)
|
||||
}
|
||||
|
||||
PrintVerbose("Trying to decode binary...")
|
||||
|
||||
err = dst.Unmarshal(data)
|
||||
if err != nil {
|
||||
PrintVerbose("Failed to decode binary: %v", err)
|
||||
|
||||
PrintVerbose("Trying to decode JSON...")
|
||||
|
||||
err = dst.UnmarshalJSON(data)
|
||||
if err != nil {
|
||||
PrintVerbose("Failed to decode JSON: %v", err)
|
||||
return errors.New("invalid format")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
47
cmd/frostfs-cli/internal/common/verbose.go
Normal file
47
cmd/frostfs-cli/internal/common/verbose.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
||||
"github.com/TrueCloudLab/frostfs-sdk-go/checksum"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// PrintVerbose prints to the stdout if the commonflags.Verbose flag is on.
|
||||
func PrintVerbose(format string, a ...interface{}) {
|
||||
if viper.GetBool(commonflags.Verbose) {
|
||||
fmt.Printf(format+"\n", a...)
|
||||
}
|
||||
}
|
||||
|
||||
// PrettyPrintUnixTime interprets s as unix timestamp and prints it as
|
||||
// a date. Is s is invalid, "malformed" is returned.
|
||||
func PrettyPrintUnixTime(s string) string {
|
||||
unixTime, err := strconv.ParseInt(s, 10, 64)
|
||||
if err != nil {
|
||||
return "malformed"
|
||||
}
|
||||
|
||||
timestamp := time.Unix(unixTime, 0)
|
||||
|
||||
return timestamp.String()
|
||||
}
|
||||
|
||||
// PrintChecksum prints checksum.
|
||||
func PrintChecksum(cmd *cobra.Command, name string, recv func() (checksum.Checksum, bool)) {
|
||||
var strVal string
|
||||
|
||||
cs, csSet := recv()
|
||||
if csSet {
|
||||
strVal = hex.EncodeToString(cs.Value())
|
||||
} else {
|
||||
strVal = "<empty>"
|
||||
}
|
||||
|
||||
cmd.Printf("%s: %s\n", name, strVal)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue