Files
Reims-Go/internal/app/diagnostics_flow.go

174 lines
4.4 KiB
Go

package app
import (
"archive/zip"
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/Nwflower/reims-go/internal/driver"
"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 any // legacy test/export payload; production uses VMInfo
VMInfo *driver.VMInfo
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(host.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 code, detail := host.DiagnoseGPULog(b.QEMULog); code != "" {
hostSummary["gpu_diagnosis"] = map[string]string{
"code": code,
"detail": detail,
}
}
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 {
if err := addJSON("vm-detail.json", b.VMDetail); err != nil {
_ = zw.Close()
_ = f.Close()
return err
}
}
if b.VMInfo != nil {
if err := addJSON("vm-info.json", b.VMInfo); 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 through the owning driver.
func (a *App) ExportDiagnostics(driverID, name string) (string, error) {
var vm driver.VMInfo
var err error
if strings.TrimSpace(name) != "" {
vm, err = a.resolveVMInfo(driverID, name)
if err != nil {
return "", err
}
}
cfgCopy := a.runtimeConfig()
a.mu.Lock()
appLogs := append([]string(nil), a.recentLogs...)
a.mu.Unlock()
rep := host.Inspect(cfgCopy.DataRoot)
active, _ := rt.ResolveActive(&cfgCopy)
bundle := diagnosticsBundle{}
label := "host"
pluginKey := ""
var info *driver.VMInfo
if vm.Name != "" {
info = &vm
label = vm.Name
ref := vm.Ref()
pluginKey = ref.Key()
if ref.DriverID != driver.BuiltinReimsMacOSDriverID {
label = sshHostAlias(ref.DriverID, vm.Name)
}
if serial, err := a.ReadVMLogs(ref.DriverID, vm.Name, true); err == nil {
bundle.SerialLog = serial
}
if qemuLog, err := a.ReadVMLogs(ref.DriverID, vm.Name, false); err == nil {
bundle.QEMULog = qemuLog
}
}
bundle.HostReport, bundle.Optimizations, bundle.Runtime, bundle.AppLogs = rep, a.evaluateOptimizations(rep), active, appLogs
bundle.VMInfo, bundle.PluginDiagnostics = info, a.collectPluginDiagnostics(context.Background(), pluginKey)
reportsDir := filepath.Join(cfgCopy.DataRoot, "reports")
zipPath := filepath.Join(reportsDir, fmt.Sprintf("reims-go-diag-%s-%s.zip", label, time.Now().Format("20060102-150405")))
if err := writeDiagnosticsZip(zipPath, bundle); err != nil {
return "", err
}
a.emitLog(label, "Diagnostics exported: "+zipPath)
_ = host.OpenFolder(reportsDir)
return zipPath, nil
}