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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"maps"
"net/url"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"time"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/app/compose"
"github.com/johnandersen777/socialweb-computer-kcp/internal/app/marketbidder"
"github.com/johnandersen777/socialweb-computer-kcp/internal/app/marketnetwork"
"github.com/johnandersen777/socialweb-computer-kcp/internal/appcli"
"github.com/johnandersen777/socialweb-computer-kcp/internal/bootstrap"
"github.com/johnandersen777/socialweb-computer-kcp/internal/bootstrap/table"
"github.com/johnandersen777/socialweb-computer-kcp/internal/buildstamp"
"github.com/johnandersen777/socialweb-computer-kcp/internal/clicfg"
"github.com/johnandersen777/socialweb-computer-kcp/internal/computevm"
"github.com/johnandersen777/socialweb-computer-kcp/internal/devinstance"
"github.com/johnandersen777/socialweb-computer-kcp/internal/devsup"
"github.com/johnandersen777/socialweb-computer-kcp/internal/guestimage"
"github.com/johnandersen777/socialweb-computer-kcp/internal/guestimagerelease"
"github.com/johnandersen777/socialweb-computer-kcp/internal/hostsvc"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/fsx"
"github.com/johnandersen777/socialweb-computer-kcp/internal/progress"
"github.com/johnandersen777/socialweb-computer-kcp/internal/stack/bidder"
"github.com/johnandersen777/socialweb-computer-kcp/internal/stack/request"
"github.com/johnandersen777/socialweb-computer-kcp/internal/vmruntime"
)
const bidderIdentityWait = 5 * time.Minute
var bidderOptions = slices.Concat([]string{backendOpt, "profile", "network", "name", "instance", "state-dir", "fg", "ready-timeout",
"network-timeout", "market-network-file", "bidder-policy", "bidder-policy-args", "bidder-operator-signin", "bidder-options", "kcp-path", "kine-path", "bao-path", "openbao-plugin-dir", "asset-cache", "unit-prefix", "kcp-front-address", "guest-image-release-base", "no-guest-lan-udp", "guest-host-udp", "compute-kinds"},
devVMOptions)
type bidderRun struct {
a *app
vals clicfg.Values
backend hostsvc.Backend
profile string
handle string
network string
policy string
options []v1alpha1.ApplicationOption
value *v1alpha1.MarketNetworkValue
ingress *v1alpha1.IngressConfig
comms *v1alpha1.MarketComms
guestNetwork *v1alpha1.GuestNetworkOptions
pdsURL string
identityMode v1alpha1.MarketIdentityMode
password string
readyTimeout time.Duration
networkTimeout time.Duration
computeKinds []string
}
type bidderSummary struct {
Profile string `json:"profile"`
Handle string `json:"handle,omitempty"`
Network string `json:"network,omitempty"`
Name string `json:"name,omitempty"`
Instance string `json:"instance,omitempty"`
KcpURL string `json:"kcpUrl,omitempty"`
Did string `json:"did,omitempty"`
Phase v1alpha1.ApplicationPhase `json:"phase,omitempty"`
Message string `json:"message,omitempty"`
Listeners []v1alpha1.ApplicationListener `json:"listeners,omitempty"`
Comps []v1alpha1.ApplicationComponent `json:"components,omitempty"`
Seconds float64 `json:"seconds"`
Identity *v1alpha1.MarketIdentityStatus `json:"identity,omitempty"`
}
func (r *bidderRun) log(msg string, args ...any) {
r.a.log.Info(msg, append([]any{"verb", "run bidder", "profile", r.profile, "utc", time.Now().UTC().Format(time.RFC3339)}, args...)...)
}
func (r *bidderRun) guestNet() *v1alpha1.GuestNetworkOptions {
if r.guestNetwork == nil {
r.guestNetwork = &v1alpha1.GuestNetworkOptions{}
}
return r.guestNetwork
}
func hostUDPValue(v string) (*bool, error) {
switch strings.ToLower(strings.TrimSpace(v)) {
case "", "auto":
return nil, nil
case "true":
on := true
return &on, nil
case "false":
off := false
return &off, nil
}
return nil, fmt.Errorf("%q: want true, false or auto", v)
}
func guestNetworkFlags(vals clicfg.Values) (*v1alpha1.GuestNetworkOptions, error) {
host, err := hostUDPValue(vals.String("guest-host-udp"))
if err != nil {
return nil, fmt.Errorf("--guest-host-udp %w", err)
}
lan := !vals.Bool("no-guest-lan-udp")
return &v1alpha1.GuestNetworkOptions{LANUDP: &lan, HostUDP: host}, nil
}
func (r *bidderRun) passthrough(args []string) error {
parsed, err := compose.ParseAppArgs(marketbidder.BidderApp, args)
if err != nil {
return err
}
var nv v1alpha1.MarketNetworkValue
nvSet := false
for _, p := range parsed {
if r.network != "" && strings.HasPrefix(p.To, compose.MapToNetwork) {
return fmt.Errorf("--%s maps to %s, which the market network sets for its child applications", p.Name, p.To)
}
switch {
case p.To == compose.MapToOptions && p.Name == "atproto-oauth":
r.identityMode = v1alpha1.MarketIdentityModeOAuthBrowser
case p.To == compose.MapToOptions && p.Name == "atproto-oauth-qr":
r.identityMode = v1alpha1.MarketIdentityModeOAuthQr
case p.To == compose.MapToOptions:
r.options = append(r.options, v1alpha1.ApplicationOption{Name: p.Name, Value: p.Value})
case p.To == "spec.policy":
r.policy = p.Value
case p.To == "spec.guestNetwork.lanUDP":
on, err := strconv.ParseBool(p.Value)
if err != nil {
return fmt.Errorf("--%s %q: want true or false", p.Name, p.Value)
}
r.guestNet().LANUDP = &on
case p.To == "spec.guestNetwork.hostUDP":
on, err := hostUDPValue(p.Value)
if err != nil {
return fmt.Errorf("--%s: %w", p.Name, err)
}
r.guestNet().HostUDP = on
case p.To == "spec.identity.mode":
r.identityMode = v1alpha1.MarketIdentityMode(p.Value)
case p.To == "spec.identity.pdsUrl":
r.pdsURL = p.Value
case p.To == compose.MapToState && p.Name == "atproto-password":
r.password = p.Value
case p.To == "spec.identity.handle":
if r.handle != "" && r.handle != p.Value {
return fmt.Errorf("--atproto-handle %s and --handle %s name two accounts", p.Value, r.handle)
}
r.handle = p.Value
case strings.HasPrefix(p.To, "spec.firecracker"):
return marketbidder.ErrFirecrackerInPod
case strings.HasPrefix(p.To, "spec.ingress"):
var applied bool
var err error
if r.ingress, applied, err = applyIngressOption(r.ingress, p.Name, p.Value); err != nil {
return err
}
if !applied {
return fmt.Errorf("--%s maps to %s, which run bidder does not take", p.Name, p.To)
}
case strings.HasPrefix(p.To, "spec.comms"):
var applied bool
var err error
if r.comms, applied, err = applyCommsOption(r.comms, p.Name, p.Value); err != nil {
return err
}
if !applied {
return fmt.Errorf("--%s maps to %s, which run bidder does not take", p.Name, p.To)
}
case strings.HasPrefix(p.To, compose.MapToNetwork):
nvSet = true
switch p.To {
case "network.plcDirectoryUrl":
nv.PLCDirectoryURL = p.Value
case "network.ingressProxyHost":
nv.IngressProxyHost = p.Value
case "network.qrOrigin":
nv.QrOrigin = p.Value
case "network.guestTlsUrl":
port := strings.TrimSpace(p.Value)
if u, err := url.Parse(port); err == nil && u.Port() != "" {
port = u.Port()
}
if _, err := strconv.Atoi(port); err != nil {
return fmt.Errorf("--%s %q: want the dispatcher's TLS port", p.Name, p.Value)
}
nv.Hosts = append(nv.Hosts, v1alpha1.NetworkHost{Name: v1alpha1.NetworkDispatcherHost, Address: "127.0.0.1:" + port})
case "network.hosts":
for _, h := range splitList(p.Value) {
name, addr, ok := strings.Cut(h, "=")
if !ok {
return fmt.Errorf("--network-hosts %q: want name=address,...", p.Value)
}
nv.Hosts = append(nv.Hosts, v1alpha1.NetworkHost{Name: name, Address: addr})
}
}
default:
return fmt.Errorf("--%s maps to %s, which run bidder does not take", p.Name, p.To)
}
}
if nvSet {
r.value = &nv
}
return nil
}
func (r *bidderRun) configOptions(words string) error {
parsed, err := compose.ParseAppArgs(marketbidder.BidderApp, strings.Fields(words))
if err != nil {
return err
}
for _, p := range parsed {
if p.To != compose.MapToOptions {
return fmt.Errorf("--%s maps to %s; bidder-options carries only the options entries of %s", p.Name, p.To, compose.OptionMapPath)
}
if !slices.ContainsFunc(r.options, func(o v1alpha1.ApplicationOption) bool { return o.Name == p.Name }) {
r.options = append(r.options, v1alpha1.ApplicationOption{Name: p.Name, Value: p.Value})
}
}
return nil
}
func runBidder(ctx context.Context, a *app, vals clicfg.Values, rest []string) int {
pos, pass := splitWords(rest)
if len(pos) > 0 {
return a.fail(exitRefused, "run bidder: unexpected %q; hono-bidder options go after --", strings.Join(pos, " "))
}
backend, err := hostsvc.ParseBackend(vals.String(backendOpt))
if err != nil {
return a.fail(exitRefused, "run bidder: %v", err)
}
r := &bidderRun{a: a, vals: vals, backend: backend, profile: vals.String("profile"), handle: strings.ToLower(vals.String("handle")),
network: vals.String("network")}
if r.profile == "" {
r.profile = marketbidder.BidderProfileProdQr
if r.network != "" || vals.String("market-network-file") != "" {
r.profile = marketbidder.BidderProfileNetwork
}
}
if !slices.Contains(marketbidder.BidderProfiles, r.profile) {
return a.fail(exitRefused, "run bidder: --profile %q: want one of %s", r.profile, strings.Join(marketbidder.BidderProfiles, ", "))
}
if r.readyTimeout, err = time.ParseDuration(vals.String("ready-timeout")); err != nil {
return a.fail(exitRefused, "run bidder: ready-timeout: %v", err)
}
if r.readyTimeout < 0 {
return a.fail(exitRefused, "run bidder: ready-timeout: %s is negative", r.readyTimeout)
}
if r.networkTimeout, err = time.ParseDuration(vals.String("network-timeout")); err != nil {
return a.fail(exitRefused, "run bidder: network-timeout: %v", err)
}
if err := r.passthrough(pass); err != nil {
return a.fail(exitRefused, "run bidder: %v", err)
}
if err := r.configOptions(vals.String("bidder-options")); err != nil {
return a.fail(exitRefused, "run bidder: bidder-options: %v", err)
}
flags, err := guestNetworkFlags(vals)
if err != nil {
return a.fail(exitRefused, "run bidder: %v", err)
}
r.guestNetwork = r.guestNetwork.WithDefaults(flags)
if r.password != "" {
if err := os.Setenv(compose.EnvPdsAccountPassword, r.password); err != nil {
return a.fail(exitRefused, "run bidder: --atproto-password: %v", err)
}
}
if r.computeKinds, err = computeKindsFlag(vals); err != nil {
return a.fail(exitRefused, "run bidder: %v", err)
}
if len(r.computeKinds) == 0 {
r.computeKinds = []string{string(v1alpha1.ComputeKindVM)}
}
if r.policy == "" {
r.policy = vals.String("bidder-policy")
}
if args := vals.String("bidder-policy-args"); args != "" && !slices.ContainsFunc(r.options, func(o v1alpha1.ApplicationOption) bool { return o.Name == "policy-args" }) {
r.options = append(r.options, v1alpha1.ApplicationOption{Name: "policy-args", Value: args})
}
if account := vals.String("bidder-operator-signin"); account != "" && !slices.ContainsFunc(r.options, func(o v1alpha1.ApplicationOption) bool { return o.Name == compose.OperatorSignInOption }) {
r.options = append(r.options, v1alpha1.ApplicationOption{Name: compose.OperatorSignInOption, Value: account})
}
switch {
case r.network != "" && r.profile != marketbidder.BidderProfileNetwork:
return a.fail(exitRefused, "run bidder: --network places the bidder in a market network, whose profile is %s, not %s", marketbidder.BidderProfileNetwork, r.profile)
case r.network == "" && r.profile == marketbidder.BidderProfileNetwork && r.vals.String("market-network-file") == "":
return a.fail(exitRefused, "run bidder: --profile %s needs --network <market network instance> (dev up --profile market-network), or --market-network-file for an installed bidder account", r.profile)
case r.network != "" && r.vals.String("market-network-file") != "":
return a.fail(exitRefused, "run bidder: --network places the bidder in the market network, which hands it the network value; --market-network-file is for a bidder outside it")
case r.network != "" && backend != hostsvc.BackendProc:
return a.fail(exitRefused, "run bidder: --network: the market network places its children under its own devsup; pass --host-backend=proc")
case r.network == "" && r.handle == "":
return a.fail(exitRefused, "run bidder: --handle is required: it names the bidder's account and owns its ComputeVMs")
}
if r.network == "" && backend == hostsvc.BackendProc && r.servesVM() {
if err := a.vmHostPreflight(ctx, r.vals); err != nil {
return a.fail(exitRefused, "run bidder: %v", err)
}
}
if !r.servesVM() {
return r.runDenoOnly(ctx, backend)
}
if r.vals.String("guest-image-source") == "" {
if backend == hostsvc.BackendSystemdUser && cachedGuestImage(r.vals.String("state-dir")) {
r.a.log.Info("the pinned guest image is already in the store, so no source is asked of the published artifacts")
} else {
src, err := publishedGuestImageSource(ctx, r.vals.String("guest-image-release-base"))
if err != nil {
return a.fail(exitRefused, "run bidder: %v", err)
}
r.vals = r.vals.With("guest-image-source", src)
}
}
if r.handle != "" {
if err := bidder.CheckHandle(r.handle); err != nil {
return a.fail(exitRefused, "run bidder: %v", err)
}
}
if p := r.vals.String("market-network-file"); p != "" {
if err := r.readMarketNetwork(p); err != nil {
return a.fail(exitRefused, "run bidder: --market-network-file: %v", err)
}
}
switch {
case r.network != "":
return r.inNetwork(ctx)
case backend == hostsvc.BackendSystemdUser:
return r.installedCore(ctx)
}
return r.standalone(ctx)
}
func (r *bidderRun) readMarketNetwork(path string) error {
if r.profile != marketbidder.BidderProfileNetwork {
return fmt.Errorf("it names a market network, whose profile is %s, not %s", marketbidder.BidderProfileNetwork, r.profile)
}
if r.value != nil {
return errors.New("it and the --network-* options both name the network value")
}
b, err := os.ReadFile(path)
if err != nil {
return err
}
var st v1alpha1.MarketNetworkStatus
if err := json.Unmarshal(b, &st); err != nil {
return fmt.Errorf("%s is not a MarketNetwork status: %w", path, err)
}
if st.Network == nil || st.BidderPdsURL == "" {
return fmt.Errorf("%s names no network value or bidderPdsUrl (dev status --instance <n> | jq .network.status, once the network is Ready)", path)
}
if strings.TrimSpace(st.Network.CABundle) == "" {
return fmt.Errorf("%s names no network CA bundle", path)
}
r.value, r.pdsURL = st.Network, st.BidderPdsURL
return nil
}
func cachedGuestImage(stateDir string) bool {
if stateDir == "" {
return false
}
pin, err := guestimage.PinFor(vmruntime.ImageTarget())
if err != nil || pin.Valid() != nil {
return false
}
dir := guestimage.Store{Root: filepath.Join(stateDir, "images")}.Dir(pin)
for _, name := range []string{guestimage.ManifestName, guestimage.KernelName, guestimage.RootfsName} {
st, err := os.Stat(filepath.Join(dir, name))
if err != nil || !st.Mode().IsRegular() || st.Size() != pin.Files[name].Bytes {
return false
}
}
return true
}
func publishedGuestImageSource(ctx context.Context, base string) (string, error) {
version, commit := buildstamp.Identity()
target := vmruntime.ImageTarget()
pin, err := guestimage.PinFor(target)
if err != nil {
return "", err
}
src, err := guestimagerelease.Source(ctx, guestimagerelease.Options{
Base: base,
Target: target,
Version: version,
Commit: commit,
ManifestSHA256: pin.Files[guestimage.ManifestName].SHA256,
})
if err != nil {
return "", fmt.Errorf("%w (pass --guest-image-source file:///<dir>, or point --guest-image-release-base at the published artifacts)", err)
}
return src, nil
}
func (r *bidderRun) kcpProvider() *v1alpha1.KcpProvider {
return &v1alpha1.KcpProvider{Handle: r.handle, GuestImageSource: r.vals.String("guest-image-source"),
VmHostBackend: r.vals.String("vm-host-backend")}
}
func (r *bidderRun) spec() (v1alpha1.MarketBidderSpec, error) {
var kcp *v1alpha1.KcpProvider
if r.servesVM() {
kcp = r.kcpProvider()
}
return bidder.Spec(bidder.SpecInput{Profile: r.profile, Handle: r.handle, Policy: r.policy, Network: r.value, PdsUrl: r.pdsURL,
Mode: r.identityMode, Kcp: kcp, Ingress: r.ingress, Comms: r.comms, Options: r.options, GuestNetwork: r.guestNetwork,
ComputeKinds: r.computeKinds})
}
func computeKindsFlag(vals clicfg.Values) ([]string, error) {
var kinds []string
for _, k := range splitList(vals.String("compute-kinds")) {
switch v1alpha1.ComputeKind(k) {
case v1alpha1.ComputeKindVM, v1alpha1.ComputeKindDeno:
default:
return nil, fmt.Errorf("--compute-kinds %q: want vm or deno", k)
}
if !slices.Contains(kinds, k) {
kinds = append(kinds, k)
}
}
return kinds, nil
}
func (r *bidderRun) servesVM() bool {
for _, k := range r.computeKinds {
if k == string(v1alpha1.ComputeKindVM) {
return true
}
}
return false
}
func (r *bidderRun) runDenoOnly(ctx context.Context, backend hostsvc.Backend) int {
if backend != hostsvc.BackendProc {
return r.a.fail(exitRefused, "run bidder: --compute-kinds without vm runs in the foreground under devsup; pass --host-backend=proc (or --fg)")
}
if !r.vals.Bool("fg") {
return r.a.fail(exitRefused, "run bidder: --compute-kinds without vm runs in the foreground; pass --fg so the sandbox units die with this process")
}
return r.standalone(ctx)
}
func (r *bidderRun) marketNetwork() (devinstance.Layout, devinstance.Record, error) {
l, err := r.a.layoutOf(r.network, r.vals)
if err != nil {
return l, devinstance.Record{}, err
}
var rec devinstance.Record
if err := fsx.ReadJSON(l.Record, &rec); err != nil {
return l, rec, fmt.Errorf("market network %s has no instance record (dev up --profile market-network first): %w", r.network, err)
}
if rec.Profile != bootstrap.ProfileMarketNetwork || rec.AdminKubeconfig == "" {
return l, rec, fmt.Errorf("instance %s is not a market network (profile %s)", r.network, rec.Profile)
}
return l, rec, nil
}
func (r *bidderRun) inNetwork(ctx context.Context) int {
started := time.Now()
_, netRec, err := r.marketNetwork()
if err != nil {
return r.a.fail(exitRefused, "run bidder: %v", err)
}
name := r.vals.String("name")
if name == "" && r.handle != "" {
name = bidder.AppName(r.handle)
}
if name == "" {
name = "bidder-" + strings.ToLower(time.Now().UTC().Format("150405"))
}
if err := request.CheckName(name); err != nil {
return r.a.fail(exitRefused, "run bidder: --name: %v", err)
}
app, err := marketnetwork.NetworkBidderApp(name, nil, marketnetwork.BidderVMDefaults{GuestImageSource: r.vals.String("guest-image-source"),
VmHostBackend: r.vals.String("vm-host-backend"), Handle: r.handle, ComputeKinds: r.computeKinds})
if err != nil {
return r.a.fail(exitRefused, "run bidder: %v", err)
}
app.Profile, app.Options, app.GuestNetwork = r.profile, r.options, r.guestNetwork
if r.policy != "" {
app.Policy = r.policy
}
cur, found, err := request.NetworkApp(ctx, netRec.AdminKubeconfig, name)
switch {
case err != nil:
return r.a.fail(exitFailed, "run bidder: %v", err)
case !found:
if err := request.AddToNetwork(ctx, netRec.AdminKubeconfig, app); err != nil {
return r.a.fail(exitFailed, "run bidder: %v", err)
}
r.log("the market network places the bidder in its workspace of the network's kcp", "network", r.network, "name", name, "computeVMHandle", marketnetwork.KcpHandle(app))
case cur.Message == marketnetwork.StoppedMessage:
if err := request.SetStopped(ctx, netRec.AdminKubeconfig, name, false); err != nil {
return r.a.fail(exitFailed, "run bidder: %v", err)
}
r.log("the market network starts the bidder's kcp again from its kept state", "name", name)
default:
r.log("the market network already runs this bidder; waiting for it to be Ready", "name", name, "phase", string(cur.Phase))
}
st, err := request.WaitNetworkApp(ctx, netRec.AdminKubeconfig, name, r.networkTimeout, func(a v1alpha1.NetworkAppStatus) bool {
return a.Phase == v1alpha1.ApplicationPhaseReady && a.Did != ""
}, func(a v1alpha1.NetworkAppStatus) {
r.log("network child", "name", name, "phase", string(a.Phase), "message", a.Message)
})
sum := bidderSummary{Profile: r.profile, Handle: name + "." + marketnetwork.BidderAccountDomain, Network: r.network, Name: name, Instance: st.Instance,
KcpURL: st.KcpURL, Did: st.Did, Phase: st.Phase, Message: st.Message, Listeners: st.Listeners, Seconds: time.Since(started).Seconds()}
r.a.printJSON(sum)
if err != nil {
return r.a.fail(exitFailed, "run bidder: %v", err)
}
if !r.vals.Bool("fg") {
return exitOK
}
r.log("run bidder --fg: the bidder lives as long as this process; Ctrl-C removes it from the market network", "name", name)
<-ctx.Done()
rctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), networkDownTimeout)
defer cancel()
if err := request.RemoveFromNetwork(rctx, netRec.AdminKubeconfig, name); err != nil {
return r.a.fail(exitFailed, "run bidder: removing %s from the market network: %v", name, err)
}
return exitOK
}
func (r *bidderRun) standalone(ctx context.Context) int {
started := time.Now()
spec, err := r.spec()
if err != nil {
return r.a.fail(exitRefused, "run bidder: %v", err)
}
instance := r.vals.String("instance")
if instance == "" {
instance = bidder.InstanceID(r.handle)
}
vmHandle := r.handle
if !r.servesVM() {
vmHandle = ""
}
vals := r.vals.With("instance", instance).With("profile", bootstrap.ProfileQuick).With("handle", vmHandle).With(appRoleOpt, table.RoleBidder)
up, code := bringUp(ctx, r.a, vals, false)
if code != exitOK {
return code
}
code = r.serve(ctx, up, spec, started)
if !r.vals.Bool("fg") {
return code
}
if ctx.Err() != nil {
code = exitOK
}
dctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), fgDownTimeout)
defer cancel()
d := downInstance(dctx, r.a, up.layout, true, false)
r.a.printJSON(d)
if len(d.Remaining) > 0 {
return exitFailed
}
return code
}
func (r *bidderRun) serve(ctx context.Context, up upReport, spec v1alpha1.MarketBidderSpec, started time.Time) int {
k, err := bidder.OpenKcp(up.AdminKubeconfig)
if err != nil {
return r.a.fail(exitFailed, "run bidder: %v", err)
}
deadline := time.Now().Add(3 * time.Minute)
delay := 2 * time.Second
var applyErr error
for {
if applyErr = k.ApplyRoot(ctx, spec); applyErr == nil {
break
}
left := time.Until(deadline)
if left <= 0 {
return r.a.fail(exitFailed, "run bidder: applying the MarketBidder root object: %v", applyErr)
}
again := time.NewTimer(min(delay, left))
select {
case <-ctx.Done():
again.Stop()
return r.a.fail(exitFailed, "run bidder: applying the MarketBidder root object: %v", ctx.Err())
case <-again.C:
}
delay = min(2*delay, bidder.RootRetryMax)
}
r.log("the MarketBidder root object is applied into the handle's own kcp", "instance", up.Instance, "kcp", up.KcpURL)
sum := bidderSummary{Profile: r.profile, Handle: r.handle, Instance: up.Instance, KcpURL: up.KcpURL}
id, err := r.signIn(ctx, k, spec)
if id != nil {
s := id.Status
sum.Identity = &s
}
if err != nil {
r.a.printJSON(sum)
return r.a.fail(exitFailed, "run bidder: %v", err)
}
o, err := k.WaitReady(ctx, r.networkTimeout, func(st v1alpha1.ApplicationStatus) {
r.log("bidder", "phase", string(st.Phase), "message", st.Message)
})
if o != nil {
sum.Did, sum.Phase, sum.Message, sum.Listeners, sum.Comps = o.Status.Did, o.Status.Phase, o.Status.Message, o.Status.Listeners, o.Status.Components
}
sum.Seconds = time.Since(started).Seconds()
r.a.printJSON(sum)
if err != nil {
return r.a.fail(exitFailed, "run bidder: %v", err)
}
if !r.vals.Bool("fg") {
return exitOK
}
r.log("run bidder --fg: the bidder lives as long as this process; Ctrl-C stops its instance and keeps its state", "instance", up.Instance)
<-ctx.Done()
return exitOK
}
func (r *bidderRun) signIn(ctx context.Context, k *bidder.Kcp, spec v1alpha1.MarketBidderSpec) (*v1alpha1.MarketIdentity, error) {
if err := k.WaitIdentityObject(ctx, r.networkTimeout); err != nil {
return nil, err
}
ids, err := k.Identities()
if err != nil {
return nil, err
}
var in io.Reader
if fi, err := os.Stdin.Stat(); err == nil && fi.Mode()&os.ModeCharDevice != 0 {
in = os.Stdin
}
wctx, cancel := ctx, context.CancelFunc(func() {})
if !r.vals.Bool("fg") {
wctx, cancel = context.WithTimeout(ctx, bidderIdentityWait)
} else {
r.log("run bidder --fg: waiting for the bidder identity without a deadline (a QR sign-in, or the association of a new DID); Ctrl-C stops", "identity", marketbidder.BidderIdentity)
}
defer cancel()
id, err := appcli.IdentityRenderer{Client: ids, Watch: k.AwaitIdentity, Out: r.a.stderr, In: in}.WaitReady(wctx, marketbidder.BidderIdentity)
if err != nil {
return id, err
}
if err := bidder.CheckSignIn(spec, id); err != nil {
return id, fmt.Errorf("the sign-in is refused; the core removes the session and shows a new QR: %w", err)
}
return id, nil
}
func (r *bidderRun) installedCore(ctx context.Context) int {
const verb = "run bidder"
state := r.vals.String("state-dir")
if !filepath.IsAbs(state) {
return r.a.fail(exitRefused, "%s: host-backend=systemd-user: state-dir %q is not absolute (install bidder writes it into config.json)", verb, state)
}
if err := bidder.CheckAccount(r.backend, r.handle, "/"); err != nil {
return r.a.fail(exitRefused, "%s: %v", verb, err)
}
spec, err := r.spec()
if err != nil {
return r.a.fail(exitRefused, "%s: %v", verb, err)
}
prefix := r.vals.String("unit-prefix")
exes := map[string]devinstance.Executable{}
for _, x := range []struct{ name, option string }{{bidder.ExeKcp, "kcp-path"}, {bidder.ExeKine, "kine-path"}, {bidder.ExeBao, "bao-path"}} {
e, err := r.a.optionalExe(ctx, r.vals, x.name, x.option)
if err != nil {
return r.a.fail(exitRefused, "%s: %v", verb, err)
}
exes[x.name] = e
}
plugins, err := r.a.openBaoPluginExes(r.vals)
if err != nil {
return r.a.fail(exitRefused, "%s: %v", verb, err)
}
maps.Copy(exes, plugins)
downloads, err := r.a.guestDownloads(r.vals)
if err != nil {
return r.a.fail(exitRefused, "%s: the guest downloads asset %s: %v", verb, marketbidder.GuestDownloadsExecutable, err)
}
if downloads.Path != "" {
exes[marketbidder.GuestDownloadsExecutable] = downloads
}
exe, err := selfExe()
if err != nil {
return r.a.fail(exitFailed, "%s: %v", verb, err)
}
exes[bidder.ExeCore] = devinstance.Executable{Path: exe}
grace, err := vmStopGrace(r.vals)
if err != nil {
return r.a.fail(exitRefused, "%s: %v", verb, err)
}
runtimeDir := r.vals.String("vm-runtime-dir")
if !filepath.IsAbs(runtimeDir) {
return r.a.fail(exitRefused, "%s: vm-runtime-dir %q must be absolute (the account's XDG_RUNTIME_DIR)", verb, runtimeDir)
}
vm := devinstance.VMRuntime{Handle: r.handle, Backend: hostsvc.BackendSystemdUser, ImageSource: r.vals.String("guest-image-source"),
StopGrace: grace.String(), RuntimeDir: runtimeDir, Executables: map[string]string{}}
var vexe vmruntime.Executables
for _, x := range []struct {
dst *string
name, option string
}{{&vexe.Stage, "swc-userns-stage", "userns-stage-path"}, {&vexe.Pasta, "pasta", "pasta-path"},
{&vexe.VMExec, "socialweb-vmexec", "vmexec-path"}, {&vexe.Firecracker, "firecracker", "firecracker-path"}} {
p, err := r.a.vmExe(r.vals, x.name, x.option)
if err != nil {
return r.a.fail(exitRefused, "%s: %v", verb, err)
}
*x.dst, vm.Executables[x.name] = p, p
}
m, err := hostsvc.Open(ctx, hostsvc.Config{Backend: hostsvc.BackendSystemdUser, UnitPrefix: prefix})
if err != nil {
return r.a.fail(exitFailed, "%s: the systemd-user backend: %v", verb, err)
}
defer m.Close()
rt, err := vmruntime.ForHost(vmruntime.Config{Manager: m, StateDir: state, RuntimeDir: runtimeDir, Exec: vexe,
ImageSource: vm.ImageSource, StopGrace: grace, Binaries: installedBinaries(state),
ImageOut: r.a.stderr, ImageTTY: progress.IsTTYWriter(r.a.stderr)})
if err != nil {
return r.a.fail(exitFailed, "%s: the VM runtime: %v", verb, err)
}
unit, _ := r.a.env.LookupEnv(devsup.EnvUnit)
cfg := bidder.CoreConfig{StateDir: state, Prefix: prefix, Handle: r.handle, Executables: exes, Spec: spec, VM: vm,
ComputeVM: computevm.Config{Handle: r.handle, Runtime: rt, Manager: m, Log: r.a.log}, FrontAddress: r.vals.String("kcp-front-address"),
ReadyTimeout: r.readyTimeout, Unit: unit, Log: r.a.log}
if err := bidder.RunCore(ctx, cfg, m); err != nil {
if errors.Is(err, context.Canceled) {
return exitOK
}
return r.a.fail(exitFailed, "%s: %v", verb, err)
}
return exitOK
}