| 1 | //go:build linux && rootrunner |
| 2 | |
| 3 | package host |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "github.com/carlosframework/platform/internal/hostbox" |
| 8 | "github.com/carlosframework/platform/internal/hostguard" |
| 9 | "github.com/carlosframework/platform/internal/release" |
| 10 | "github.com/carlosframework/platform/internal/rootparticipation" |
| 11 | "github.com/carlosframework/platform/internal/rootstate" |
| 12 | "os/exec" |
| 13 | "testing" |
| 14 | ) |
| 15 | |
| 16 | func lifecycleActivationDiagnostic(t *testing.T, ctx context.Context) { |
| 17 | err := participationRelease.With(ctx, func(ctx context.Context) error { |
| 18 | store, err := rootstate.Open(release.RootGenerationsDir, false, false) |
| 19 | if err != nil { |
| 20 | return err |
| 21 | } |
| 22 | defer store.Close() |
| 23 | ledger := &release.RootGenerations{Store: store} |
| 24 | rs, err := ledger.Reservations(ctx) |
| 25 | t.Log("diagnostic reservations", len(rs), err) |
| 26 | if err != nil { |
| 27 | return err |
| 28 | } |
| 29 | inv, err := generationInventoryAt(ctx, rootparticipation.Namespace, "/var/lib/carlos-claims/authority/fences", "/var/lib/carlos-claims/journals.db") |
| 30 | t.Log("diagnostic inventory", len(inv.Installations), len(inv.Launches), len(inv.Payloads), err) |
| 31 | inv.Handovers, err = productionGenerationHandovers(ctx) |
| 32 | t.Log("diagnostic handovers", len(inv.Handovers), err) |
| 33 | inv.Retirements, err = productionGenerationRetirements(ctx) |
| 34 | t.Log("diagnostic retirements", len(inv.Retirements), err) |
| 35 | inv, err = generationLocalObligations(ctx, inv, productionGenerationCreation) |
| 36 | t.Log("diagnostic obligations", err) |
| 37 | _, err = productionGenerationRegistryInventory(ctx, inv) |
| 38 | t.Log("diagnostic registry", err) |
| 39 | if err != nil { |
| 40 | t.Logf("registry snapshot: %+v", inv) |
| 41 | } |
| 42 | lifecycleInventoryDiagnostic(t, ctx, inv, rs) |
| 43 | v := generationReconciler{read: productionLocalGenerationInventory, kernel: productionGenerationKernelCensus} |
| 44 | _, err = v.reconcile(ctx, rs) |
| 45 | return err |
| 46 | }) |
| 47 | t.Log("diagnostic certificate", err) |
| 48 | } |
| 49 | |
| 50 | func lifecycleInventoryDiagnostic(t *testing.T, ctx context.Context, inv generationInventory, rs []release.GenerationReservation) { |
| 51 | physical, err := generationPhysicalPayloads(ctx, inv) |
| 52 | t.Log("physical payload map", err) |
| 53 | if err == nil { |
| 54 | for _, p := range physical { |
| 55 | t.Log("physical payload", p.Inventory.Host, p.Inventory.InstallationStamp, generationPayloadFiles(ctx, dataDir(), []hostbox.GenerationPayload{p})) |
| 56 | } |
| 57 | } |
| 58 | boot, k, err := productionGenerationKernelCensus(context.WithValue(ctx, generationCensusReservationsKey{}, rs)) |
| 59 | t.Log("kernel census", boot, k, err) |
| 60 | for _, v := range inv.Installations { |
| 61 | t.Logf("installation %+v", v) |
| 62 | } |
| 63 | for _, r := range rs { |
| 64 | t.Logf("reservation %+v", r) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func lifecycleRetirementDiagnostic(t *testing.T, ctx context.Context, g *release.RootGenerations) { |
| 69 | records, err := g.Reservations(ctx) |
| 70 | t.Log("retirement records", err) |
| 71 | ctx = context.WithValue(ctx, generationCensusReservationsKey{}, records) |
| 72 | out, lockErr := exec.Command("lslocks", "--json", "--output", "PID,COMMAND,TYPE,PATH").CombinedOutput() |
| 73 | t.Logf("retirement locks: %s %v", out, lockErr) |
| 74 | s, err := rootstate.Open("/var/lib/carlos-claims/authority/fences", false, false) |
| 75 | if err != nil { |
| 76 | t.Log(err) |
| 77 | return |
| 78 | } |
| 79 | defer s.Close() |
| 80 | j, err := openGenerationLaunchInventory("/var/lib/carlos-claims/journals.db") |
| 81 | if err != nil { |
| 82 | t.Log(err) |
| 83 | return |
| 84 | } |
| 85 | defer j.Close() |
| 86 | v := generationRetirement{guard: hostguard.Guard{Root: hostguard.NewRootAuthority(s)}, journal: j, kernel: productionGenerationAbsence} |
| 87 | for _, r := range records { |
| 88 | t.Log("retirement ledger", r.Binding.Target.Host, r.Binding.Target.Stamp, r.Closed, r.Retirement) |
| 89 | closed, e := v.closure(ctx, r) |
| 90 | t.Log("retirement closure", r.Binding.Target.Host, r.Binding.Target.Stamp, closed, e) |
| 91 | view, e := v.guard.Root.View(ctx, r.Binding.Target.Host, hostguard.Captured{Incarnation: r.Binding.Target.Incarnation, Stamp: r.Binding.Target.Stamp}) |
| 92 | if e == nil && closed { |
| 93 | _, e = productionRetiredGenerationAbsence(ctx, r, view) |
| 94 | t.Log("retirement absence", r.Binding.Target.Host, r.Binding.Target.Stamp, e) |
| 95 | t.Log("retirement replacement", (generationKernel{proc: "/proc", cgroups: "/sys/fs/cgroup", contained: records}).replacedGroup(ctx, r)) |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |