Files
core/solutions/cli/doctor.go
T

194 lines
5.0 KiB
Go
Raw Normal View History

package cli
import (
"context"
"errors"
"fmt"
"net"
"os"
"runtime"
"strings"
"syscall"
"time"
"yetano.dev/yetano/core/solutions/engine"
)
// doctor runs a state's requirement checks on this machine (DR-01/03).
// Only kinds it can really check get PASS/WARNING/ERROR; everything else
// stays NOT_CHECKED with the command to run -- never a fake PASS.
func (a *app) doctor(args []string) error {
fs := a.flags("solution doctor")
f := a.instFlags(fs)
offline := fs.Bool("offline", false, "skip checks that need the network (DNS)")
if err := a.parse(fs, args); err != nil {
return err
}
_, st, err := a.stateOf(fs, f)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(a.ctx, 30*time.Second)
defer cancel()
worst := 0
for i := range st.Checks {
c := &st.Checks[i]
if c.Status == engine.CheckNotApplicable {
continue
}
runCheck(ctx, c, *offline)
if c.Status == engine.CheckError || c.Status == engine.CheckBlocked {
worst = 1
}
}
if a.jsonOut {
if err := a.printJSON(map[string]any{"solution": st.Solution.ID, "instance": st.Instance, "checks": st.Checks}); err != nil {
return err
}
} else {
for _, c := range st.Checks {
a.say("%-14s %s\n", c.Status, c.Title)
if c.Note != "" {
a.say("%-14s %s\n", "", wrap(c.Note, 62, strings.Repeat(" ", 15)))
}
if c.Status == engine.CheckNotChecked && c.Run != "" {
a.say("%-14s run: %s\n", "", c.Run)
}
if (c.Status == engine.CheckWarning || c.Status == engine.CheckError) && c.Hint != "" {
a.say("%-14s hint: %s\n", "", wrap(c.Hint, 60, strings.Repeat(" ", 21)))
}
}
}
if worst > 0 {
return exitErr{ExitError, errors.New("")}
}
return nil
}
func sevStatus(sev string) string {
switch sev {
case "warning":
return engine.CheckWarning
case "info":
return engine.CheckInfo
}
return engine.CheckError
}
func runCheck(ctx context.Context, c *engine.CheckResult, offline bool) {
p := c.Params
switch c.Kind {
case "os":
want := strings.Split(p["want"], ",")
for _, w := range want {
if w == runtime.GOOS {
c.Status, c.Note = engine.CheckPass, runtime.GOOS
return
}
}
c.Status, c.Note = sevStatus(c.Severity), runtime.GOOS+" is not supported"
case "port":
var busy, denied []string
for _, port := range strings.Split(p["port"], ",") {
l, err := net.Listen("tcp", ":"+strings.TrimSpace(port))
if err == nil {
l.Close()
continue
}
if errors.Is(err, syscall.EACCES) || errors.Is(err, os.ErrPermission) {
denied = append(denied, port)
} else {
busy = append(busy, port)
}
}
switch {
case len(busy) > 0:
c.Status, c.Note = sevStatus(c.Severity), "in use: "+strings.Join(busy, ", ")
case len(denied) > 0:
c.Status, c.Note = engine.CheckNotChecked, "no permission to test port(s) "+strings.Join(denied, ", ")+" as this user"
default:
c.Status = engine.CheckPass
}
case "file":
var missing []string
for _, f := range strings.Split(p["paths"], ",") {
if _, err := os.Stat(strings.TrimSpace(f)); err != nil {
missing = append(missing, f)
}
}
if len(missing) > 0 {
c.Status, c.Note = sevStatus(c.Severity), "missing: "+strings.Join(missing, ", ")
} else {
c.Status = engine.CheckPass
}
case "dns":
if offline {
c.Status, c.Note = engine.CheckNotChecked, "offline"
return
}
dnsCheck(ctx, c)
case "manual":
c.Status = engine.CheckInfo
c.Note = "check this yourself"
default:
c.Status = engine.CheckNotChecked
}
}
func dnsCheck(ctx context.Context, c *engine.CheckResult) {
p := c.Params
r := net.DefaultResolver
var problems []string
names := strings.Split(p["names"], ",")
for _, name := range names {
name = p["prefix"] + strings.TrimSpace(name)
if name == "" {
continue
}
qctx, cancel := context.WithTimeout(ctx, 5*time.Second)
switch strings.ToUpper(p["type"]) {
case "MX":
mx, err := r.LookupMX(qctx, name)
if err != nil || len(mx) == 0 {
problems = append(problems, name+": no MX record")
break
}
if want := strings.TrimSuffix(p["want"], "."); want != "" {
ok := false
for _, m := range mx {
ok = ok || strings.EqualFold(strings.TrimSuffix(m.Host, "."), want)
}
if !ok {
problems = append(problems, fmt.Sprintf("%s: MX does not point to %s", name, want))
}
}
case "A":
if ips, err := r.LookupIPAddr(qctx, name); err != nil || len(ips) == 0 {
problems = append(problems, name+": does not resolve")
}
case "TXT":
txt, err := r.LookupTXT(qctx, name)
found := false
for _, t := range txt {
found = found || strings.Contains(t, p["contains"])
}
if err != nil || !found {
problems = append(problems, fmt.Sprintf("%s: no TXT record with %q", name, p["contains"]))
}
default:
c.Status = engine.CheckNotChecked
cancel()
return
}
cancel()
}
if ctx.Err() != nil {
c.Status, c.Note = engine.CheckNotChecked, "timed out"
return
}
if len(problems) > 0 {
c.Status, c.Note = sevStatus(c.Severity), strings.Join(problems, "; ")
return
}
c.Status = engine.CheckPass
}