1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package install
import (
"bytes"
"encoding/json"
"errors"
"io/fs"
"os"
"path/filepath"
"slices"
"sort"
"github.com/johnandersen777/socialweb-computer-kcp/internal/hostcontract"
)
const (
OutcomeCreated = "created"
OutcomeFound = "found present"
OutcomeAdopted = "adopted"
OutcomeUpdated = "updated"
OutcomeNotNeeded = "not needed here"
OutcomeRefused = "refused"
)
const (
StepAccount = "account"
StepSubuid = "subuid"
StepSubgid = "subgid"
StepKVM = "kvm-group"
StepDelegation = "delegation-drop-in"
StepAppArmor = "apparmor-profile"
StepPassword = "node-password"
StepStateDir = "state-dir"
StepBinary = "binary"
StepConfig = "config"
StepUnitFiles = "unit-files"
StepLinger = "linger"
StepUnits = "core-units"
StepGuestImage = "guest-image"
StepBringUp = "bring-up"
StepCheckHost = "check-host"
StepRequesterRecord = "requester-record"
recordMode = 0o644
)
type Record struct {
Role hostcontract.Role `json:"role"`
Account string `json:"account"`
UID int `json:"uid"`
StateDir string `json:"stateDir"`
UnitPrefix string `json:"unitPrefix,omitempty"`
Handles []string `json:"handles,omitempty"`
Names []string `json:"names,omitempty"`
Steps map[string]string `json:"steps"`
SubidLines map[string]string `json:"subidLines,omitempty"`
CreatedDirs []string `json:"createdDirs,omitempty"`
UnitFiles []string `json:"unitFiles,omitempty"`
PrevOwner map[string][2]int `json:"previousOwner,omitempty"`
}
func loadRecord(path string) (*Record, error) {
b, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var r Record
if err := json.Unmarshal(b, &r); err != nil {
return nil, errors.Join(errors.New("install: the install record "+path+" is not valid JSON"), err)
}
if r.Steps == nil {
r.Steps = map[string]string{}
}
return &r, nil
}
func loadOrNewRecord(path string, role hostcontract.Role, account, state string) (*Record, bool, error) {
r, err := loadRecord(path)
if errors.Is(err, fs.ErrNotExist) {
return &Record{Role: role, Account: account, StateDir: state, UID: -1, Steps: map[string]string{}}, false, nil
}
return r, err == nil, err
}
func (r *Record) set(step, outcome string) {
switch r.Steps[step] {
case OutcomeCreated, OutcomeAdopted:
return
case OutcomeUpdated:
if outcome != OutcomeCreated {
return
}
}
r.Steps[step] = outcome
}
func (r *Record) created(step string) bool { return r.Steps[step] == OutcomeCreated }
func (r *Record) addDir(p string) {
if slices.Contains(r.CreatedDirs, p) {
return
}
r.CreatedDirs = append(r.CreatedDirs, p)
}
func (r *Record) addUnitFile(p string) {
if slices.Contains(r.UnitFiles, p) {
return
}
r.UnitFiles = append(r.UnitFiles, p)
sort.Strings(r.UnitFiles)
}
func (r *Record) Instances() []string {
if r.Role == hostcontract.RoleRequester {
return r.Names
}
return r.Handles
}
func (r *Record) addNames(ns []string) {
for _, n := range ns {
if !slices.Contains(r.Names, n) {
r.Names = append(r.Names, n)
}
}
sort.Strings(r.Names)
}
func (r *Record) addHandles(hs []string) {
seen := map[string]bool{}
for _, h := range append(append([]string(nil), r.Handles...), hs...) {
seen[h] = true
}
r.Handles = r.Handles[:0]
for h := range seen {
r.Handles = append(r.Handles, h)
}
sort.Strings(r.Handles)
}
func (r *Record) bytes() []byte {
b, _ := json.MarshalIndent(r, "", " ")
return append(b, '\n')
}
func sameBytes(path string, b []byte) bool {
cur, err := os.ReadFile(path)
return err == nil && bytes.Equal(cur, b)
}
func ReadRecord(root, account string) (*Record, error) {
return loadRecord(filepath.Join(root, RecordPath(account)))
}