170 lines
4.3 KiB
Go
170 lines
4.3 KiB
Go
package app
|
|
|
|
import (
|
|
"archive/zip"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/driver/reims"
|
|
"github.com/Nwflower/reims-go/internal/host"
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
rt "github.com/Nwflower/reims-go/internal/runtime"
|
|
)
|
|
|
|
type diagnosticsBundle struct {
|
|
HostReport host.Report
|
|
Optimizations []host.Optimization
|
|
Runtime *rt.Installed
|
|
VMDetail *reims.VMDetail
|
|
SerialLog string
|
|
QEMULog string
|
|
AppLogs []string
|
|
PluginDiagnostics []plugin.DiagnosticsEntry
|
|
}
|
|
|
|
func writeDiagnosticsZip(zipPath string, b diagnosticsBundle) error {
|
|
if err := os.MkdirAll(filepath.Dir(zipPath), 0o755); err != nil {
|
|
return err
|
|
}
|
|
f, err := os.Create(zipPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
zw := zip.NewWriter(f)
|
|
addFile := func(name, content string) error {
|
|
w, err := zw.Create(name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = w.Write([]byte(reims.RedactText(content)))
|
|
return err
|
|
}
|
|
addJSON := func(name string, v any) error {
|
|
raw, err := json.MarshalIndent(v, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return addFile(name, string(raw)+"\n")
|
|
}
|
|
|
|
hostSummary := map[string]any{
|
|
"generated_at": time.Now().UTC().Format(time.RFC3339),
|
|
"host": b.HostReport,
|
|
"optimizations": b.Optimizations,
|
|
}
|
|
if err := addJSON("host-report.json", hostSummary); err != nil {
|
|
_ = zw.Close()
|
|
_ = f.Close()
|
|
return err
|
|
}
|
|
if b.Runtime != nil {
|
|
if err := addJSON("runtime-manifest.json", b.Runtime.Manifest); err != nil {
|
|
_ = zw.Close()
|
|
_ = f.Close()
|
|
return err
|
|
}
|
|
}
|
|
if b.VMDetail != nil {
|
|
sanitized := *b.VMDetail
|
|
sanitized.NetworkSummary = reims.RedactLogLine(sanitized.NetworkSummary)
|
|
sanitized.ExtraArgs = reims.RedactLogLine(sanitized.ExtraArgs)
|
|
if err := addJSON("vm-detail.json", sanitized); err != nil {
|
|
_ = zw.Close()
|
|
_ = f.Close()
|
|
return err
|
|
}
|
|
}
|
|
if strings.TrimSpace(b.SerialLog) != "" {
|
|
if err := addFile("serial.log", b.SerialLog); err != nil {
|
|
_ = zw.Close()
|
|
_ = f.Close()
|
|
return err
|
|
}
|
|
}
|
|
if strings.TrimSpace(b.QEMULog) != "" {
|
|
if err := addFile("qemu.log", b.QEMULog); err != nil {
|
|
_ = zw.Close()
|
|
_ = f.Close()
|
|
return err
|
|
}
|
|
}
|
|
if len(b.AppLogs) > 0 {
|
|
if err := addFile("app-logs.txt", strings.Join(b.AppLogs, "\n")+"\n"); err != nil {
|
|
_ = zw.Close()
|
|
_ = f.Close()
|
|
return err
|
|
}
|
|
}
|
|
if len(b.PluginDiagnostics) > 0 {
|
|
if err := addJSON("plugin-diagnostics.json", b.PluginDiagnostics); err != nil {
|
|
_ = zw.Close()
|
|
_ = f.Close()
|
|
return err
|
|
}
|
|
}
|
|
if err := zw.Close(); err != nil {
|
|
_ = f.Close()
|
|
return err
|
|
}
|
|
return f.Close()
|
|
}
|
|
|
|
// ExportDiagnostics collects a redacted troubleshooting zip bundle (host probe,
|
|
// runtime manifest, VM detail, serial/QEMU logs, and recent app logs) into
|
|
// `<DataRoot>/reports/` and opens the folder in File Explorer.
|
|
func (a *App) ExportDiagnostics(name string) (string, error) {
|
|
a.mu.Lock()
|
|
cfgCopy := *a.cfg
|
|
appLogs := append([]string(nil), a.recentLogs...)
|
|
a.mu.Unlock()
|
|
|
|
rep := host.Inspect(cfgCopy.DataRoot)
|
|
var active *rt.Installed
|
|
var patchNames []string
|
|
if inst, err := rt.ResolveActive(&cfgCopy); err == nil {
|
|
active = inst
|
|
for _, p := range inst.Manifest.Patches {
|
|
patchNames = append(patchNames, p.Name)
|
|
}
|
|
}
|
|
bundle := diagnosticsBundle{
|
|
HostReport: rep,
|
|
Optimizations: host.EvaluateOptimizations(rep, patchNames),
|
|
Runtime: active,
|
|
AppLogs: appLogs,
|
|
PluginDiagnostics: a.collectPluginDiagnostics(context.Background(), name),
|
|
}
|
|
|
|
label := "host"
|
|
if strings.TrimSpace(name) != "" {
|
|
label = name
|
|
if c, err := a.client(); err == nil {
|
|
ctx := context.Background()
|
|
if d, derr := a.GetVMDetail(name); derr == nil {
|
|
bundle.VMDetail = &d
|
|
}
|
|
if serial, serr := c.ReadLogs(ctx, name, true); serr == nil {
|
|
bundle.SerialLog = serial
|
|
}
|
|
if qemuLog, qerr := c.ReadLogs(ctx, name, false); qerr == nil {
|
|
bundle.QEMULog = qemuLog
|
|
}
|
|
}
|
|
}
|
|
|
|
reportsDir := filepath.Join(cfgCopy.DataRoot, "reports")
|
|
zipName := fmt.Sprintf("reims-go-diag-%s-%s.zip", label, time.Now().Format("20060102-150405"))
|
|
zipPath := filepath.Join(reportsDir, zipName)
|
|
if err := writeDiagnosticsZip(zipPath, bundle); err != nil {
|
|
return "", err
|
|
}
|
|
a.emitLog(label, "Diagnostics exported: "+zipPath)
|
|
_ = host.OpenFolder(reportsDir)
|
|
return zipPath, nil
|
|
}
|