Document every exported type, function and method in the migration, storage and cephfsnative packages and add package comments for migration and the cephmig command, following the commentary rules of Effective Go. Comments on unexported helpers now start with the name they describe. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
105 lines
2.7 KiB
Go
105 lines
2.7 KiB
Go
package migration
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
// Executor runs an external command, writing its standard output to the
|
|
// given writer.
|
|
type Executor interface {
|
|
Exec(context.Context, io.Writer, string, ...string) error
|
|
}
|
|
|
|
// Process is the Executor used in production. It runs commands in their own
|
|
// process group and mirrors their standard error to Stderr, if set.
|
|
type Process struct{ Stderr io.Writer }
|
|
|
|
// ProcessError reports a failed external command. Diagnostic holds the tail
|
|
// of its standard error; the arguments are deliberately omitted.
|
|
type ProcessError struct {
|
|
Name string
|
|
Err error
|
|
Diagnostic string
|
|
}
|
|
|
|
// Error implements the error interface.
|
|
func (e *ProcessError) Error() string { return fmt.Sprintf("%s failed: %v", e.Name, e.Err) }
|
|
|
|
// Unwrap returns the underlying error.
|
|
func (e *ProcessError) Unwrap() error { return e.Err }
|
|
|
|
type tailBuffer struct{ data []byte }
|
|
|
|
func (b *tailBuffer) Write(p []byte) (int, error) {
|
|
const limit = 4096
|
|
n := len(p)
|
|
if n >= limit {
|
|
b.data = append(b.data[:0], p[n-limit:]...)
|
|
return n, nil
|
|
}
|
|
if len(b.data)+n > limit {
|
|
b.data = append(b.data[:0], b.data[len(b.data)+n-limit:]...)
|
|
}
|
|
b.data = append(b.data, p...)
|
|
return n, nil
|
|
}
|
|
|
|
// Exec runs name with args under ctx. On cancellation the whole process
|
|
// group receives SIGTERM.
|
|
func (p Process) Exec(ctx context.Context, out io.Writer, name string, args ...string) error {
|
|
cmd := exec.CommandContext(ctx, name, args...)
|
|
cmd.Stdout = out
|
|
var diagnostic tailBuffer
|
|
cmd.Stderr = &diagnostic
|
|
if p.Stderr != nil {
|
|
cmd.Stderr = io.MultiWriter(p.Stderr, &diagnostic)
|
|
}
|
|
cmd.Env = append(os.Environ(), "LC_ALL=C")
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
|
cmd.Cancel = func() error {
|
|
err := syscall.Kill(-cmd.Process.Pid, syscall.SIGTERM)
|
|
if errors.Is(err, syscall.ESRCH) {
|
|
return os.ErrProcessDone
|
|
}
|
|
return err
|
|
}
|
|
cmd.WaitDelay = 5 * time.Second
|
|
if err := cmd.Run(); err != nil {
|
|
if ctx.Err() != nil {
|
|
return ctx.Err()
|
|
}
|
|
// Do not print arguments: Ceph metadata values may be confidential.
|
|
return &ProcessError{Name: name, Err: err, Diagnostic: strings.TrimSpace(string(diagnostic.data))}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type limitedBuffer struct {
|
|
bytes.Buffer
|
|
limit int
|
|
}
|
|
|
|
func (b *limitedBuffer) Write(p []byte) (int, error) {
|
|
if len(p) > b.limit-b.Len() {
|
|
return 0, errors.New("command output exceeds configured limit")
|
|
}
|
|
return b.Buffer.Write(p)
|
|
}
|
|
|
|
func output(ctx context.Context, x Executor, name string, args ...string) ([]byte, error) {
|
|
b := &limitedBuffer{limit: 16 << 20}
|
|
if err := x.Exec(ctx, b, name, args...); err != nil {
|
|
return nil, err
|
|
}
|
|
return b.Bytes(), nil
|
|
}
|