package bidder import ( "context" "crypto/tls" "crypto/x509" "encoding/json" "errors" "io" "log/slog" "net/http" "os" "os/user" "path/filepath" "runtime" "strings" "time" "github.com/bluesky-social/indigo/atproto/identity" "github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1" "github.com/johnandersen777/socialweb-computer-kcp/internal/app/compose" "github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/market" "github.com/johnandersen777/socialweb-computer-kcp/internal/buildstamp" contract "github.com/johnandersen777/socialweb-computer-kcp/internal/contract/release" "github.com/johnandersen777/socialweb-computer-kcp/internal/controllers/runtime/kube" "github.com/johnandersen777/socialweb-computer-kcp/internal/install" "github.com/johnandersen777/socialweb-computer-kcp/internal/kit/selfexe" "github.com/johnandersen777/socialweb-computer-kcp/internal/releasefollow" ) const releaseChannelWatch = compose.ReleaseChannelWatch func releaseClient(n *v1alpha1.MarketNetworkValue) (*http.Client, identity.Directory) { client := &http.Client{Timeout: releaseReadTimeout} if n != nil && n.CABundle != "" { pool, err := x509.SystemCertPool() if err != nil || pool == nil { pool = x509.NewCertPool() } pool.AppendCertsFromPEM([]byte(n.CABundle)) client.Transport = &http.Transport{Proxy: http.ProxyFromEnvironment, TLSClientConfig: &tls.Config{RootCAs: pool}} } plc := identity.DefaultPLCURL if n != nil && strings.TrimSpace(n.PLCDirectoryURL) != "" { plc = strings.TrimRight(strings.TrimSpace(n.PLCDirectoryURL), "/") } return client, &identity.BaseDirectory{PLCURL: plc, HTTPClient: *client, PLCClient: client, SkipHandleVerification: true} } const ( releaseReadTimeout = 60 * time.Second releaseDownloadTimeout = 10 * time.Minute releaseRetry = 5 * time.Minute ) func followRelease(ctx context.Context, k *Kcp, stateDir, releaseChannel string, network *v1alpha1.MarketNetworkValue, log *slog.Logger) { version, _ := buildstamp.Identity() if buildstamp.Dev() || version == "" { log.Info("releasefollow_off", "reason", "a dev build follows no release channel") return } if _, err := os.Stat(filepath.Join(stateDir, "bin", "current")); err != nil { log.Info("releasefollow_off", "reason", "the core does not run from an install (no bin/current)") return } u, err := user.Current() if err != nil { log.Warn("releasefollow_off", "reason", err.Error()) return } channel := strings.TrimSpace(releaseChannel) if channel == "" { channel = "stable" if strings.Contains(version, "-rc") { channel = "rc" } } sum := "" if pin, err := selfexe.Sum(); err == nil { sum = pin.Sha256 } rkey := "socialweb-computer:" + channel client, dir := releaseClient(network) reader := market.NewHTTPReader(dir) reader.Client = client download := &http.Client{Transport: client.Transport, Timeout: releaseDownloadTimeout} f := releasefollow.New(releasefollow.Config{Running: version, RunningSHA256: sum, OS: runtime.GOOS, Arch: runtime.GOARCH, Dir: filepath.Join(stateDir, "bin", ".incoming"), Reader: reader, HTTP: download, Log: log, Upgrade: func(ctx context.Context, binary, sum string) error { return install.Upgrade(ctx, install.UpgradeOptions{StateDir: stateDir, Home: u.HomeDir, Account: u.Username, RuntimeDir: install.OwnRuntimeDir(), Binary: binary, SHA256: sum, Out: io.Discard, SelfCheck: install.SelfCheck, Restart: install.RestartOwnUnits}) }}) log.Info("releasefollow_on", "channel", channel, "running", version) seen := "" for ctx.Err() == nil { _, err := kube.Await(ctx, k.dyn.Resource(v1alpha1.AtprotoRecordWatchGVR), releaseChannelWatch, releaseRetry, func(o *v1alpha1.AtprotoRecordWatch) (bool, error) { if o == nil { return false, nil } for _, r := range o.Status.Records { if !strings.HasSuffix(r.URI, "/"+rkey) || r.Cid == seen { continue } var value map[string]any if err := json.Unmarshal(r.Value, &value); err != nil { log.Warn("releasefollow_failed", "channel", r.URI, "err", err.Error()) continue } out, err := f.Follow(ctx, contract.Channel{URI: r.URI, CID: r.Cid, Value: value}) if err != nil { log.Warn("releasefollow_failed", "channel", r.URI, "err", err.Error()) continue } seen = r.Cid log.Info("releasefollow_seen", "channel", r.URI, "version", out.Version, "installed", out.Installed) } return false, nil }) if err != nil && ctx.Err() == nil && !errors.Is(err, kube.ErrAwaitTimeout) { log.Warn("releasefollow_watch_failed", "watch", releaseChannelWatch, "err", err.Error()) select { case <-ctx.Done(): case <-time.After(releaseRetry): } } } }