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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package table
import (
"cmp"
"errors"
"fmt"
"regexp"
"slices"
"github.com/johnandersen777/socialweb-computer-kcp/internal/hostsvc"
)
type Kind string
const (
KindStore Kind = "store"
KindKine Kind = "kine"
KindFront Kind = "front"
KindSlot Kind = "slot"
KindCore Kind = "core"
)
const (
RoleGateway = "gateway"
RoleBidder = "bidder"
RoleRequester = "requester"
StoreSqlite = "kine-sqlite"
StorePostgres = "kine-postgres"
GatewayKey = "gw"
GatewayCore = "computer-gateway" + hostsvc.ServiceSuffix
BidderCore = "computer-bidder@"
RequesterCore = "computer-requester@"
RolePostgres = "pg"
RoleFront = "front"
RoleCore = "core"
SlotA = "a"
SlotB = "b"
)
var (
keyRE = regexp.MustCompile(`^[a-z0-9]([a-z0-9-]{0,22}[a-z0-9])?$`)
slotRE = regexp.MustCompile(`^[a-z]$`)
)
type App struct {
Role string
Key string
Handle string
Name string
Store string
Slots []string
DevCore bool
Prefix string
}
type Entry struct {
Kind Kind
Role string
Unit string
Slot string
After []string
Wants []string
}
type Names struct {
Prefix string
Key string
}
func Gateway() App {
return App{Role: RoleGateway, Key: GatewayKey, Store: StorePostgres, Slots: []string{SlotA, SlotB}}
}
func Bidder(handle, hid string) App {
return App{Role: RoleBidder, Key: hid, Handle: handle, Store: StoreSqlite, Slots: []string{SlotA}}
}
func Requester(name, key string) App {
return App{Role: RoleRequester, Key: key, Name: name, Store: StoreSqlite, Slots: []string{SlotA}}
}
func (a App) SingleSlot() bool { return a.Role == RoleBidder || a.Role == RoleRequester }
func Dev(key, store string) App {
slots := []string{SlotA}
if store == StorePostgres {
slots = []string{SlotA, SlotB}
}
return App{Role: RoleGateway, Key: key, Store: store, Slots: slots, DevCore: true}
}
func DevNames(key string) Names { return Names{Prefix: hostsvc.NamePrefix, Key: key} }
func (a App) Names() Names {
p := a.Prefix
p = cmp.Or(p, hostsvc.NamePrefix)
return Names{Prefix: p, Key: a.Key}
}
func (n Names) base() string { return n.Prefix + "kcp-" + n.Key }
func (n Names) Store() string { return n.base() + "-pg" + hostsvc.ServiceSuffix }
func (n Names) Kine(slot string) string { return n.base() + "-kine@" + slot + hostsvc.ServiceSuffix }
func (n Names) Front() string { return n.base() + "-front" + hostsvc.ServiceSuffix }
func (n Names) Slot(slot string) string { return n.base() + "@" + slot + hostsvc.ServiceSuffix }
func (n Names) Slice() string { return n.base() + ".slice" }
func (n Names) UnitPrefix() string { return n.base() }
func (n Names) DevCore() string { return n.Prefix + "core-" + n.Key + "-dev" + hostsvc.ServiceSuffix }
func (n Names) HostProcessPrefix() string { return n.Prefix + "hp-" + n.Key + "-" }
func (n Names) CorePrefix() string { return n.Prefix + "core-" + n.Key + "-" }
func (n Names) CoreSlice() string { return n.Prefix + "core-" + n.Key + ".slice" }
func (n Names) StandIn(role string) string { return n.base() + "-" + role + hostsvc.ServiceSuffix }
func KineRole(slot string) string { return "kine-" + slot }
func SlotRole(slot string) string { return "kcp-" + slot }
func (a App) Validate() error {
if a.Role != RoleGateway && a.Role != RoleBidder && a.Role != RoleRequester {
return fmt.Errorf("bootstrap: application role %q: want %s, %s or %s", a.Role, RoleGateway, RoleBidder, RoleRequester)
}
if !keyRE.MatchString(a.Key) {
return fmt.Errorf("bootstrap: application key %q: want lower-case letters, digits and dashes, at most 24", a.Key)
}
if a.Store != StoreSqlite && a.Store != StorePostgres {
return fmt.Errorf("bootstrap: store %q: want %s or %s", a.Store, StoreSqlite, StorePostgres)
}
if a.Role == RoleBidder {
if a.Store != StoreSqlite {
return errors.New("bootstrap: a bidder's kcp is one slot on kine-sqlite; it never has a postgres")
}
if a.Handle == "" && !a.DevCore {
return errors.New("bootstrap: a bidder application names its handle")
}
}
if a.Role == RoleRequester {
if a.Store != StoreSqlite {
return errors.New("bootstrap: a requester's kcp is one slot on kine-sqlite; it never has a postgres")
}
if a.Name == "" {
return errors.New("bootstrap: a requester application names its instance")
}
if len(a.Slots) != 1 {
return errors.New("bootstrap: a requester runs one kcp slot")
}
}
if len(a.Slots) == 0 || len(a.Slots) > 2 {
return fmt.Errorf("bootstrap: %d kcp slots: want one or two", len(a.Slots))
}
if a.Role == RoleBidder && len(a.Slots) != 1 {
return errors.New("bootstrap: a bidder runs one kcp slot")
}
for i, s := range a.Slots {
if !slotRE.MatchString(s) || slices.Contains(a.Slots[:i], s) {
return fmt.Errorf("bootstrap: slot %q: want distinct single letters", s)
}
}
return nil
}
func (a App) KineSlots() []string {
if a.Store == StorePostgres {
return slices.Clone(a.Slots)
}
return []string{a.Slots[0]}
}
func (a App) KineOf(slot string) string {
if a.Store == StorePostgres && slices.Contains(a.Slots, slot) {
return slot
}
return a.Slots[0]
}
func (a App) SlotEntry(slot string) Entry {
n := a.Names()
kine := n.Kine(a.KineOf(slot))
return Entry{Kind: KindSlot, Role: SlotRole(slot), Unit: n.Slot(slot), Slot: slot, After: []string{kine, n.Front()}, Wants: []string{kine}}
}
func (a App) CoreUnit() string {
switch {
case a.DevCore:
return a.Names().DevCore()
case a.Role == RoleBidder:
return a.Names().Prefix + BidderCore + a.Handle + hostsvc.ServiceSuffix
case a.Role == RoleRequester:
return a.Names().Prefix + RequesterCore + a.Name + hostsvc.ServiceSuffix
}
return a.Names().Prefix + GatewayCore
}
func CoreTemplate(prefix, role string) string {
prefix = cmp.Or(prefix, hostsvc.NamePrefix)
switch role {
case RoleBidder:
return prefix + BidderCore + hostsvc.ServiceSuffix
case RoleRequester:
return prefix + RequesterCore + hostsvc.ServiceSuffix
}
return prefix + GatewayCore
}
func Table(a App) ([]Entry, error) {
if err := a.Validate(); err != nil {
return nil, err
}
n := a.Names()
var out []Entry
var store []string
if a.Store == StorePostgres {
store = []string{n.Store()}
out = append(out, Entry{Kind: KindStore, Role: RolePostgres, Unit: n.Store()})
}
for _, s := range a.KineSlots() {
out = append(out, Entry{Kind: KindKine, Role: KineRole(s), Unit: n.Kine(s), Slot: s, After: store, Wants: store})
}
out = append(out, Entry{Kind: KindFront, Role: RoleFront, Unit: n.Front()})
for _, s := range a.Slots {
out = append(out, a.SlotEntry(s))
}
var after, wants []string
for _, e := range out {
after = append(after, e.Unit)
if e.Kind != KindSlot {
wants = append(wants, e.Unit)
}
}
out = append(out, Entry{Kind: KindCore, Role: RoleCore, Unit: a.CoreUnit(), After: after, Wants: wants})
return out, nil
}
func Order() []Kind { return []Kind{KindStore, KindKine, KindFront, KindSlot, KindCore} }