mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
232 lines
6.1 KiB
Go
232 lines
6.1 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweed-block/core/ops"
|
|
)
|
|
|
|
type inspectionShell struct {
|
|
master, filer, chunkPath, statusAddr, serverID string
|
|
timeout, maxClockSkew time.Duration
|
|
}
|
|
|
|
func runShell(args []string, input io.Reader, stdout, stderr io.Writer) int {
|
|
fs := flag.NewFlagSet("sw-block shell", flag.ContinueOnError)
|
|
fs.SetOutput(stderr)
|
|
s := inspectionShell{}
|
|
var command string
|
|
fs.StringVar(&s.master, "master", "", "blockmaster address")
|
|
fs.StringVar(&s.filer, "filer", "", "filer address, for direct store inspections")
|
|
fs.StringVar(&s.chunkPath, "chunkPath", defaultChunkPathCLI, "block metadata directory")
|
|
fs.StringVar(&s.statusAddr, "status-addr", "", "this host's loopback holder status address")
|
|
local, _ := os.Hostname()
|
|
fs.StringVar(&s.serverID, "server-id", local, "this host's blockvolume server-id")
|
|
fs.DurationVar(&s.timeout, "timeout", 10*time.Second, "bound for each command")
|
|
fs.DurationVar(&s.maxClockSkew, "max-clock-skew", 20*time.Second, "master's configured workspace skew bound")
|
|
fs.StringVar(&command, "c", "", "one command instead of reading stdin")
|
|
if fs.Parse(args) != nil || fs.NArg() != 0 || s.master == "" || s.timeout <= 0 || s.maxClockSkew <= 0 {
|
|
fmt.Fprintln(stderr, "usage: sw-block shell -master ADDR [-filer ADDR] [-status-addr ADDR -server-id ID] [-c 'cluster.status -o json']")
|
|
return 2
|
|
}
|
|
if command != "" {
|
|
return s.execute(command, stdout, stderr)
|
|
}
|
|
interactive := false
|
|
if f, ok := input.(*os.File); ok {
|
|
if info, err := f.Stat(); err == nil {
|
|
interactive = info.Mode()&os.ModeCharDevice != 0
|
|
}
|
|
}
|
|
scanner := bufio.NewScanner(input)
|
|
scanner.Buffer(make([]byte, 4096), 64<<10)
|
|
code := 0
|
|
for {
|
|
if interactive {
|
|
fmt.Fprint(stdout, "sw-block> ")
|
|
}
|
|
if !scanner.Scan() {
|
|
break
|
|
}
|
|
line := strings.TrimSpace(scanner.Text())
|
|
if line == "quit" || line == "exit" {
|
|
break
|
|
}
|
|
if line == "" {
|
|
continue
|
|
}
|
|
if c := s.execute(line, stdout, stderr); c != 0 {
|
|
code = c
|
|
}
|
|
}
|
|
if err := scanner.Err(); err != nil {
|
|
fmt.Fprintln(stderr, err)
|
|
return 2
|
|
}
|
|
return code
|
|
}
|
|
|
|
// Shell words support quoting, but never variable expansion or command execution.
|
|
func inspectionWords(line string) ([]string, error) {
|
|
var words []string
|
|
var word strings.Builder
|
|
var quote rune
|
|
escaped, active := false, false
|
|
for _, r := range line {
|
|
if escaped {
|
|
word.WriteRune(r)
|
|
escaped = false
|
|
active = true
|
|
continue
|
|
}
|
|
if r == '\\' && quote != '\'' {
|
|
escaped = true
|
|
active = true
|
|
continue
|
|
}
|
|
if quote != 0 {
|
|
if r == quote {
|
|
quote = 0
|
|
} else {
|
|
word.WriteRune(r)
|
|
}
|
|
active = true
|
|
continue
|
|
}
|
|
if r == '\'' || r == '"' {
|
|
quote = r
|
|
active = true
|
|
continue
|
|
}
|
|
if r == ' ' || r == '\t' {
|
|
if active {
|
|
words = append(words, word.String())
|
|
word.Reset()
|
|
active = false
|
|
}
|
|
continue
|
|
}
|
|
word.WriteRune(r)
|
|
active = true
|
|
}
|
|
if quote != 0 || escaped {
|
|
return nil, errors.New("unfinished quote or escape")
|
|
}
|
|
if active {
|
|
words = append(words, word.String())
|
|
}
|
|
return words, nil
|
|
}
|
|
|
|
func (s inspectionShell) execute(line string, stdout, stderr io.Writer) int {
|
|
words, err := inspectionWords(line)
|
|
if err != nil {
|
|
fmt.Fprintln(stderr, err)
|
|
return 2
|
|
}
|
|
if len(words) == 0 {
|
|
return 0
|
|
}
|
|
name, args := words[0], words[1:]
|
|
if name == "log.collect" {
|
|
return s.collectLogs(args, stdout, stderr)
|
|
}
|
|
if name == "help" {
|
|
fmt.Fprintln(stdout, "cluster.status, cluster.check [volume], volume.list, volume.status <volume>, volume.explain <volume>, snapshot.list <volume>, store.show <volume>, store.usage <volume>, volume.verify <volume>, log.collect -out DIR; each supports -o json; quit")
|
|
return 0
|
|
}
|
|
format := "text"
|
|
var positional []string
|
|
for i := 0; i < len(args); i++ {
|
|
if args[i] == "-o" {
|
|
i++
|
|
if i == len(args) {
|
|
return 2
|
|
}
|
|
format = args[i]
|
|
} else {
|
|
positional = append(positional, args[i])
|
|
}
|
|
}
|
|
if format != "text" && format != "json" {
|
|
fmt.Fprintln(stderr, "output must be text or json")
|
|
return 2
|
|
}
|
|
if name == "cluster.check" {
|
|
if len(positional) > 1 {
|
|
fmt.Fprintln(stderr, "cluster.check accepts at most one volume")
|
|
return 2
|
|
}
|
|
volume := ""
|
|
if len(positional) == 1 {
|
|
volume = positional[0]
|
|
}
|
|
return s.check(volume, format, stdout, stderr)
|
|
}
|
|
if name == "snapshot.list" || name == "store.show" || name == "store.usage" || name == "volume.verify" {
|
|
if len(positional) != 1 || s.filer == "" {
|
|
fmt.Fprintf(stderr, "%s requires a volume and shell -filer\n", name)
|
|
return 2
|
|
}
|
|
run := map[string]func([]string, io.Writer, io.Writer) int{"snapshot.list": chunkSnapshots, "store.show": chunkShow, "store.usage": chunkUsage, "volume.verify": chunkVerify}[name]
|
|
return run([]string{"--master", s.master, "--chunk-filer", s.filer, "--chunk-path", s.chunkPath, "--timeout", s.timeout.String(), "--volume", positional[0]}, stdout, stderr)
|
|
}
|
|
if name != "cluster.status" && name != "volume.list" && name != "volume.status" && name != "volume.explain" {
|
|
fmt.Fprintf(stderr, "unknown inspection command %q; use help\n", name)
|
|
return 2
|
|
}
|
|
want := 0
|
|
if name == "volume.status" || name == "volume.explain" {
|
|
want = 1
|
|
}
|
|
if len(positional) != want {
|
|
fmt.Fprintf(stderr, "%s needs %d volume arguments\n", name, want)
|
|
return 2
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), s.timeout)
|
|
defer cancel()
|
|
cluster, err := readMasterClusterEvidence(ctx, s.master)
|
|
if err != nil {
|
|
fmt.Fprintf(stderr, "%s: %v\n", name, err)
|
|
return 1
|
|
}
|
|
var value any = cluster
|
|
text := ops.RenderClusterEvidenceText(cluster)
|
|
if name == "volume.list" {
|
|
value = cluster.Volumes
|
|
}
|
|
if want == 1 {
|
|
found := false
|
|
for _, v := range cluster.Volumes {
|
|
if v.VolumeID == positional[0] {
|
|
value = v
|
|
text = ops.RenderVolumeEvidenceText(v)
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
fmt.Fprintf(stderr, "volume %q not found\n", positional[0])
|
|
return 1
|
|
}
|
|
}
|
|
if format == "json" {
|
|
if err := json.NewEncoder(stdout).Encode(value); err != nil {
|
|
fmt.Fprintln(stderr, err)
|
|
return 1
|
|
}
|
|
} else {
|
|
fmt.Fprint(stdout, text)
|
|
}
|
|
return 0
|
|
}
|