Files
Reims-Go/diagnostics_flow.go
T
Nwflower fc284e99ab feat: complete M3.4-M3.7, M4 CI/smoke pipeline, and fix Guest Tools UTF-8 clipboard
- Fix macOS Guest Tools LaunchAgent UTF-8 encoding (__CF_USER_TEXT_ENCODING=0x08000100 + LANG/LC_ALL=en_US.UTF-8) so pbpaste/pbcopy never outputs GBK mojibake on Chinese macOS, plus server-side UTF-8 validation
- M3.4: Auto-upgrade legacy AHCI to NVMe on Exit recovery mode + one-click Upgrade to NVMe banner + Windows USB/external drive (IOCTL_STORAGE_QUERY_PROPERTY BusTypeUsb) and high-RAM diagnostics
- M3.5: One-click redacted diagnostics .zip export (host report, runtime manifest, VM detail, serial/QEMU/app logs with MAC, serial, SMC OSK, and Bearer token redaction)
- M3.6: Automatic qemu-img convert for .vmdk/.raw disk imports in CreateWizard
- M3.7: Instant CoW VM derivation (CloneVMCoW + CloneDialog) preserving golden backing image untouched
- M4.2/M4.3: Add GitHub Actions CI & candidate runtime build workflows + scripts/smoke-runtime.ps1
2026-11-28 08:50:16 -07:00

160 lines
3.9 KiB
Go

package main
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"
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
}
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 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,
}
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
}