mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-09-08 15:41:15 +02:00
* shell: non-interactive mode exits non-zero when a command fails A failed command in a piped weed shell run printed 'error: ...' but the process still exited 0, so a CronJob wrapping e.g. echo 's3.lifecycle.run-shard -shards 0-15' | weed shell -master=... reported green while the run aborted partway (shards N+1..15 unwalked). An unknown command likewise exited 0. RunShell now returns the last command failure from the non-interactive stdin path (unknown commands included), and the shell command exits 2 on it. Interactive sessions are unchanged: errors are shown to the operator and the session continues, exiting 0 as before. * shell: route the piped-failure exit through main's shutdown path Review follow-up: os.Exit(2) inside the shell command skipped main's shutdown work. The command now records the status (SetCommandExitStatus) and returns normally; main applies it via setExitStatus before exit(). exit() itself now flushes sentry before os.Exit -- main's deferred sentry.Flush never ran on this path (os.Exit skips defers), so the existing 'flush buffered events before the program terminates' intent only worked for the autocomplete early-return. Exit status 2 on a failed piped run is preserved (verified: piped success exits 0, piped failing command exits 2). * shell: test the registered-command failure path Review follow-up: the error-propagation test only covered unknown commands. A fake registered command now drives processEachCmd's real dispatch path: a failing Do surfaces its exact error (errors.Is) and a succeeding one returns nil. The non-interactive exit status itself is main-level plumbing, verified end to end against the reproduction (piped failure exits 2). * shell: trim the comments added with the exit status Keep the non-obvious why -- why a piped run has to fail its wrapper, why the status is recorded instead of os.Exit'ed -- and drop the narration. Claude-Session: https://claude.ai/code/session_018DWwctzD4T2DmnczPRM47t * shell: fail a piped run with the status weed already uses for that weed.go spends 1 on a command that failed and 2 on a usage or syntax error, and runShell returns true precisely so the usage dump is skipped. Exiting 2 there told a wrapper the command line was wrong. Claude-Session: https://claude.ai/code/session_018DWwctzD4T2DmnczPRM47t --------- Co-authored-by: Carlos Leyva <carlos.leyva@idener.es>
80 lines
2.2 KiB
Go
80 lines
2.2 KiB
Go
package command
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/security"
|
|
"github.com/seaweedfs/seaweedfs/weed/shell"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
)
|
|
|
|
var (
|
|
shellOptions shell.ShellOptions
|
|
shellInitialFiler *string
|
|
shellCluster *string
|
|
shellDebug *bool
|
|
)
|
|
|
|
func init() {
|
|
cmdShell.Run = runShell // break init cycle
|
|
shellOptions.Masters = cmdShell.Flag.String("master", "", "comma-separated master servers, e.g. localhost:9333")
|
|
shellOptions.FilerGroup = cmdShell.Flag.String("filerGroup", "", "filerGroup for the filers")
|
|
shellInitialFiler = cmdShell.Flag.String("filer", "", "filer host and port for initial connection, e.g. localhost:8888")
|
|
shellCluster = cmdShell.Flag.String("cluster", "", "cluster defined in shell.toml")
|
|
shellDebug = cmdShell.Flag.Bool("debug", false, "print informational logs to stderr")
|
|
}
|
|
|
|
var cmdShell = &Command{
|
|
UsageLine: "shell",
|
|
Short: "run interactive administrative commands",
|
|
Long: `run interactive administrative commands.
|
|
|
|
Generate shell.toml via "weed scaffold -config=shell"
|
|
|
|
`,
|
|
}
|
|
|
|
func runShell(command *Command, args []string) bool {
|
|
|
|
util.LoadSecurityConfiguration()
|
|
shellOptions.GrpcDialOption = security.LoadClientTLS(util.GetViper(), "grpc.client")
|
|
shellOptions.Directory = "/"
|
|
|
|
util.LoadConfiguration("shell", false)
|
|
viper := util.GetViper()
|
|
cluster := viper.GetString("cluster.default")
|
|
if *shellCluster != "" {
|
|
cluster = *shellCluster
|
|
}
|
|
|
|
if *shellOptions.Masters == "" {
|
|
if cluster == "" {
|
|
*shellOptions.Masters = "localhost:9333"
|
|
} else {
|
|
*shellOptions.Masters = viper.GetString("cluster." + cluster + ".master")
|
|
}
|
|
}
|
|
|
|
filerAddress := *shellInitialFiler
|
|
if filerAddress == "" && cluster != "" {
|
|
filerAddress = viper.GetString("cluster." + cluster + ".filer")
|
|
}
|
|
shellOptions.FilerAddress = pb.ServerAddress(filerAddress)
|
|
shellOptions.Debug = *shellDebug
|
|
if shellOptions.Debug {
|
|
fmt.Fprintf(os.Stderr, "master: %s filer: %s\n", *shellOptions.Masters, shellOptions.FilerAddress)
|
|
}
|
|
|
|
if err := shell.RunShell(shellOptions); err != nil {
|
|
// The command already printed the error; a piped run has to fail the
|
|
// script wrapping it too.
|
|
SetCommandExitStatus(1)
|
|
}
|
|
|
|
return true
|
|
|
|
}
|