carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package hostbox
2
3import (
4 "context"
5 "encoding/json"
6 "github.com/carlosframework/platform/internal/hostguard"
7 "github.com/carlosframework/platform/internal/registry"
8)
9
10type InventoryExpectation struct {
11 Revision uint64
12 RecordKey, RecordETag string
13 Target hostguard.FenceTarget
14}
15
16func (d *StopInventoryDB) Snapshot(ctx context.Context, host string, captured hostguard.Captured) (StopInventory, uint64, error) {
17 if d == nil {
18 return StopInventory{}, 0, ErrUnsupported
19 }
20 target := hostguard.FenceTarget{Host: host, Incarnation: captured.Incarnation, Stamp: captured.Stamp}
21 var out JournalResponse
22 var err error
23 if d.remote != nil {
24 out, err = journalCall(ctx, d.remote, JournalRequest{Op: "inventory-version", Target: target})
25 } else {
26 out, err = d.inventoryVersion(ctx, target)
27 }
28 return out.Inventory, out.Revision, err
29}
30
31type TimerMutation struct {
32 Timer registry.Timer
33 Expected string
34 Remove bool
35}
36
37func (d *StopInventoryDB) WriteTimerExpected(ctx context.Context, in StopInventory, expected InventoryExpectation, timer TimerMutation) (uint64, error) {
38 return d.writeExpected(ctx, in, expected, &timer)
39}
40func (d *StopInventoryDB) WriteExpected(ctx context.Context, in StopInventory, expected InventoryExpectation) (uint64, error) {
41 return d.writeExpected(ctx, in, expected, nil)
42}
43func (d *StopInventoryDB) writeExpected(ctx context.Context, in StopInventory, expected InventoryExpectation, timer *TimerMutation) (uint64, error) {
44 if d == nil || d.remote == nil || expected.Target.Host != in.Host || expected.Target.Incarnation != in.Incarnation || expected.Target.Stamp != in.InstallationStamp {
45 return 0, ErrTypedInventory
46 }
47 out, err := journalCall(ctx, d.remote, JournalRequest{Timer: timer, Op: "inventory-write", Inventory: in, Revision: expected.Revision, Target: expected.Target, RecordKey: expected.RecordKey, RecordETag: expected.RecordETag})
48 if err != nil {
49 return 0, err
50 }
51 if out.Revision != expected.Revision+1 {
52 return 0, ErrChanged
53 }
54 return out.Revision, nil
55}
56
57func (d *StopInventoryDB) Remote() bool { return d != nil && d.remote != nil }
58
59func (d *StopInventoryDB) SnapshotRequest(ctx context.Context, target hostguard.FenceTarget) (JournalResponse, error) {
60 if d == nil {
61 return JournalResponse{}, ErrUnsupported
62 }
63 if d.remote != nil {
64 return journalCall(ctx, d.remote, JournalRequest{Op: "inventory-version", Target: target})
65 }
66 return d.inventoryVersion(ctx, target)
67}
68
69func (m TimerMutation) MarshalJSON() ([]byte, error) {
70 type wire TimerMutation
71 return json.Marshal(struct {
72 wire
73 Registration string
74 }{wire(m), m.Timer.Registration})
75}
76func (m *TimerMutation) UnmarshalJSON(raw []byte) error {
77 type wire TimerMutation
78 var v struct {
79 wire
80 Registration string
81 }
82 if err := json.Unmarshal(raw, &v); err != nil {
83 return err
84 }
85 *m = TimerMutation(v.wire)
86 m.Timer.Registration = v.Registration
87 return nil
88}
89