Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116package namespace
import ( _ "embed" "fmt" "path" "regexp" "strings"
"tangled.org/core/spindle/engines/namespace/nsapi" "tangled.org/core/spindle/models")
const ( defaultCacheSizeGB = 20 exitFile = "/tangled/.exit")
var ( //go:embed scripts/restore-cache.sh restoreCache string)
type cacheOptions struct { entries []models.CacheEntry sizeMB int64 nix bool images bool}
func (c cacheOptions) any() bool { return len(c.entries) > 0 || c.nix || c.images }
func (c cacheOptions) keepAfter(o outcome) bool { switch o { case succeeded: return true case failed: for _, e := range c.entries { if e.When != "always" { return false } } return true } return false}
func (p plan) attachCaches(spec *nsapi.InstanceSpec, repoDID, workflow string) { c := p.cache if !c.any() { return } repo := path.Join("tangled", tagPart(repoDID)) run := path.Join(repo, tagPart(strings.TrimSuffix(workflow, path.Ext(workflow))), p.shape.OS.String()+"-"+p.shape.Arch.String()) var vols []nsapi.VolumeSpec if len(c.entries) > 0 { vols = append(vols, nsapi.VolumeSpec{MountPoint: p.host.cacheMount, Tag: path.Join(run, "paths"), SizeMB: c.sizeMB}) } if c.nix { vols = append(vols, nsapi.VolumeSpec{MountPoint: p.host.nixVolume, Tag: path.Join(run, "nix"), SizeMB: c.sizeMB}) } if c.images { spec.ImageCache = &nsapi.VolumeSpec{Tag: path.Join(repo, "images", p.shape.Arch.String()), SizeMB: c.sizeMB} } if spec.Container == nil { spec.Volumes = vols return } spec.Container.Volumes = vols spec.Container.Job = true spec.Container.Entrypoint = jobEntrypoint()}
var notTagChar = regexp.MustCompile(`[^a-z0-9._-]+`)
func tagPart(s string) string { return strings.Trim(notTagChar.ReplaceAllString(strings.ToLower(s), "-"), "-")}
func restoreStep(entries []models.CacheEntry, mount string) Step { lines := []string{restoreCache} for _, e := range entries { name := "-" if len(e.Hash) > 0 { name = `"$(cat ` + quoteAll(e.Hash) + ` 2>/dev/null | digest)"` } lines = append(lines, fmt.Sprintf("restore %s %s %t %s", shellQuote(e.Key), name, e.Fallback, quoteAll(e.Paths))) } return Step{ name: "Restore caches", kind: models.StepKindSystem, command: strings.Join(lines, "\n"), environment: map[string]string{"TANGLED_CACHE": mount}, }}
func quoteAll(words []string) string { quoted := make([]string, len(words)) for i, w := range words { quoted[i] = shellQuote(w) } return strings.Join(quoted, " ")}
func jobEntrypoint() []string { return []string{"bash", "-c", "until [ -e " + exitFile + " ]; do sleep 1; done; exit \"$(cat " + exitFile + ")\""}}
func (h host) endJob(keep bool) nsapi.Command { code := "1" if keep { code = "0" } return h.command("mkdir -p "+path.Dir(exitFile)+" && echo "+code+" > "+exitFile, nil, "/", true)}