vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
Put job execution behind an interface
de3eec162f93a697575c8bc756bd496fe4020376 · Keenan Nemetz · 2h ago · parent f784d769
Put job execution behind an interface Job execution was container-shaped all the way into the runner: ci/agent created the network, started the services and the job container, and held *container.Container and *container.Network values directly, and `vole workflow run` kept its own copy of the same orchestration. The only abstraction was Config.Runtime, a string naming one of three flag-compatible CLIs, which is enough for docker, podman and nerdctl and nothing else. ci/execute is that seam. An Executor prepares the job's image, starts its environment, runs one step while the caller keeps the lease alive, stops it, reclaims what a killed runner left behind, and bounds its own storage. A Sandbox is one job's environment: the job, the services beside it, and the network they share. Capabilities is what a backend can honour, so a job that asks for a network or for services is refused before it starts rather than quietly given less. ci/container implements it and takes over the orchestration that was in ci/agent and cmd/vole: the per-attempt network, the service containers and their log collection, the stale-container and stale-network reclamation, and the image build and prune coordination. The isolation flags are unchanged. This is the boundary a virtual-machine backend needs: a macOS guest behind Tart (#356), a Windows guest, or a Linux microVM disagree with a container about pids limits, user namespaces, tmpfs, capabilities and mounts, so they cannot be expressed as another runtime string. Nothing about the image or workflow schema changes here. closes: 01M8Z2M9PVKPNH923S3VJ5FR5E
ci/agent/agent.go +154−106
| @@ -30,7 +30,7 @@ import ( | |||
| 30 | 30 | ||
| 31 | 31 | "rodent.software/vole/ci" | |
| 32 | 32 | jobcache "rodent.software/vole/ci/cache" | |
| 33 | "rodent.software/vole/ci/container" | ||
| 33 | "rodent.software/vole/ci/execute" | ||
| 34 | 34 | "rodent.software/vole/ci/redact" | |
| 35 | 35 | "rodent.software/vole/ci/runs" | |
| 36 | 36 | "rodent.software/vole/client" | |
| @@ -57,13 +57,11 @@ const ( | |||
| 57 | 57 | reconnectMax = 30 * time.Second | |
| 58 | 58 | ) | |
| 59 | 59 | ||
| 60 | // errLeaseLost means the queue took the job away while the runner was still | ||
| 61 | // preparing its environment. There is nothing to report in that case. | ||
| 62 | var errLeaseLost = errors.New("lease lost") | ||
| 63 | |||
| 64 | // errCanceled means a maintainer asked this run to stop. The attempt is still worth | ||
| 65 | // reporting: a canceled run has a story, and the partial log and step timings are it. | ||
| 66 | var errCanceled = errors.New("run canceled") | ||
| 60 | // The two ways a lease ends mid-job are reported by execute.ErrLeaseLost and | ||
| 61 | // execute.ErrCanceled, because an executor is what has to stop when one of them | ||
| 62 | // happens. There is nothing to report for the first; the attempt is still worth | ||
| 63 | // reporting for the second, since a canceled run has a story and the partial log and | ||
| 64 | // step timings are it. | ||
| 67 | 65 | ||
| 68 | 66 | // errStopped says this slot was asked to stop while it held no job. The session was | |
| 69 | 67 | // hung up so a parked lease would answer instead of sitting out its wait, and nothing | |
| @@ -358,20 +356,23 @@ func Run(ctx context.Context, opts Options) error { | |||
| 358 | 356 | return err | |
| 359 | 357 | } | |
| 360 | 358 | ||
| 361 | box, err := containerOptions(opts, cache) | ||
| 359 | box, err := newRunnerBox(opts, cache) | ||
| 362 | 360 | if err != nil { | |
| 363 | 361 | return err | |
| 364 | 362 | } | |
| 365 | // The binary being on PATH is not the runtime being able to create a container. | ||
| 366 | // Probe before anything is leased: a machine that cannot run a container is not | ||
| 367 | // a degraded runner, and an attempt spent proving it is a commit blamed for the | ||
| 368 | // machine's problem. | ||
| 369 | if err := container.Probe(ctx, box.Config); err != nil { | ||
| 363 | // The backend being installed is not the backend being able to execute. Probe | ||
| 364 | // before anything is leased: a machine that cannot execute is not a degraded | ||
| 365 | // runner, and an attempt spent proving it is a commit blamed for the machine's | ||
| 366 | // problem. | ||
| 367 | if err := box.exec.Probe(ctx); err != nil { | ||
| 370 | 368 | return err | |
| 371 | 369 | } | |
| 372 | 370 | cleanupJobTrees(cache) | |
| 373 | cleanupStaleContainers(box) | ||
| 374 | cleanupStaleNetworks(box) | ||
| 371 | // What a killed predecessor left behind belongs to the backend: it knows how to | ||
| 372 | // name and find its own work, and this is the only place that has to say so. | ||
| 373 | if err := box.exec.Reconcile(ctx); err != nil { | ||
| 374 | return err | ||
| 375 | } | ||
| 375 | 376 | // Bound the saved checkouts before work starts, the same way the job cache is | |
| 376 | 377 | // bound below: a runner whose disk filled up while it was not watching should | |
| 377 | 378 | // not start by failing a job. | |
| @@ -384,13 +385,13 @@ func Run(ctx context.Context, opts Options) error { | |||
| 384 | 385 | // the runtime's own prune. Do it before the first lease for the reason the caches | |
| 385 | 386 | // above are bounded here: a host that filled up while nobody was watching should | |
| 386 | 387 | // reclaim before it fails a job, not after. | |
| 387 | imageTrim := newImageTrimmer(box, opts.ImageMaxAge) | ||
| 388 | imageTrim := newImageTrimmer(box.exec, opts.ImageMaxAge) | ||
| 388 | 389 | if imageTrim.enabled() { | |
| 389 | if box.Pull == "never" { | ||
| 390 | if box.cfg.Pull == "never" { | ||
| 390 | 391 | fmt.Fprintln(os.Stderr, "vole runner: --container-pull never keeps every tagged image; pruning dangling layers only") | |
| 391 | 392 | } | |
| 392 | imageTrim.trim(ctx, box) | ||
| 393 | go imageTrim.loop(ctx, box) | ||
| 393 | imageTrim.trim(ctx) | ||
| 394 | go imageTrim.loop(ctx) | ||
| 394 | 395 | } | |
| 395 | 396 | ||
| 396 | 397 | // The directory is mounted whether or not it is reused: an image that sets | |
| @@ -466,8 +467,8 @@ func Run(ctx context.Context, opts Options) error { | |||
| 466 | 467 | cacheRoot, format.Bytes(cacheMax)) | |
| 467 | 468 | } | |
| 468 | 469 | fmt.Fprintf(os.Stderr, "vole runner: %s\n", slots[0].describe(jobs)) | |
| 469 | fmt.Fprintf(os.Stderr, "vole runner: jobs run in the workflow's image via %s\n", box.Runtime) | ||
| 470 | if container.RunsAsRoot(box.Config) { | ||
| 470 | fmt.Fprintf(os.Stderr, "vole runner: jobs run in the workflow's image via %s\n", box.caps.Backend) | ||
| 471 | if box.caps.Root { | ||
| 471 | 472 | fmt.Fprintln(os.Stderr, "vole runner: warning: jobs run as root in their container, so a job can "+ | |
| 472 | 473 | "rewrite its own image; run the runner as an unprivileged user (or pass --container-user) "+ | |
| 473 | 474 | "to have the image's permissions keep it immutable") | |
| @@ -537,7 +538,7 @@ func (sl *slot) connect() error { | |||
| 537 | 538 | } | |
| 538 | 539 | sess.name = who.Name | |
| 539 | 540 | sess.repos, sess.labels = sl.opts.Repos, sl.opts.Labels | |
| 540 | sess.network = sl.box.Network | ||
| 541 | sess.network = sl.box.cfg.Network | ||
| 541 | 542 | sess.cacheRoot, sess.cacheReuse = sl.cacheRoot, !sl.opts.NoCache | |
| 542 | 543 | sess.diskRoot, sess.diskReserve = sl.cache, sl.opts.DiskReserve | |
| 543 | 544 | sl.sess, sl.who = sess, who | |
| @@ -658,7 +659,7 @@ func (sl *slot) serve(ctx context.Context) error { | |||
| 658 | 659 | if lease <= 0 { | |
| 659 | 660 | lease = 60 * time.Second | |
| 660 | 661 | } | |
| 661 | s.run(sl.opts.Remote, sl.cache, rep.Run, lease, sl.box) | ||
| 662 | s.run(ctx, sl.opts.Remote, sl.cache, rep.Run, lease, sl.box) | ||
| 662 | 663 | if sl.opts.Once { | |
| 663 | 664 | // This process is done; the next one bounds the cache at startup instead, | |
| 664 | 665 | // before it leases anything. | |
| @@ -918,7 +919,7 @@ func (s *runnerSession) close() { | |||
| 918 | 919 | ||
| 919 | 920 | // run executes a leased job and reports the outcome. A lost lease ends the job | |
| 920 | 921 | // without reporting a result: another runner owns it now. | |
| 921 | func (s *runnerSession) run(remote, cache string, job *runnerJob, lease time.Duration, box runnerBox) { | ||
| 922 | func (s *runnerSession) run(ctx context.Context, remote, cache string, job *runnerJob, lease time.Duration, box runnerBox) { | ||
| 922 | 923 | log, err := newSpool(MaxLogBytes) | |
| 923 | 924 | if err != nil { | |
| 924 | 925 | fmt.Fprintf(os.Stderr, "vole runner: %v\n", err) | |
| @@ -959,13 +960,16 @@ func (s *runnerSession) run(remote, cache string, job *runnerJob, lease time.Dur | |||
| 959 | 960 | }, box) | |
| 960 | 961 | return | |
| 961 | 962 | } | |
| 962 | jobBox := &jobSandbox{} | ||
| 963 | defer jobBox.Stop() | ||
| 963 | // The sandbox is created by the executor, which is what a second backend | ||
| 964 | // replaces. Until one exists there is nothing to stop, and every failure path | ||
| 965 | // below stops "the sandbox" without asking whether it was started. | ||
| 966 | jobBox := execute.Nothing() | ||
| 967 | defer func() { jobBox.Stop() }() | ||
| 964 | 968 | // The steps that ran, so a failure after them keeps what did run and a failure | |
| 965 | 969 | // before them can say that nothing did. | |
| 966 | 970 | var steps []runs.StepResult | |
| 967 | 971 | fail := func(format string, args ...any) { | |
| 968 | jobBox.StopWithLogs(log) | ||
| 972 | stopWithLogs(jobBox, log) | ||
| 969 | 973 | msg := mask.String(fmt.Sprintf(format, args...)) | |
| 970 | 974 | fmt.Fprintf(os.Stderr, "vole runner: %s\n", msg) | |
| 971 | 975 | fmt.Fprintf(log, "\n%s\n", msg) | |
| @@ -990,12 +994,12 @@ func (s *runnerSession) run(remote, cache string, job *runnerJob, lease time.Dur | |||
| 990 | 994 | // The job's network request is resolved before anything runs, so a runner that was | |
| 991 | 995 | // handed a job it cannot isolate refuses it instead of leaking the network into a | |
| 992 | 996 | // job that asked for none, or starving a job that asked for one. | |
| 993 | boxConfig, err := box.networkFor(job) | ||
| 997 | network, err := box.networkFor(job) | ||
| 994 | 998 | if err != nil { | |
| 995 | 999 | fail("%s", err) | |
| 996 | 1000 | return | |
| 997 | 1001 | } | |
| 998 | fmt.Fprintf(log, "environment: %s via %s, network %s\n", job.Image, box.Runtime, boxConfig.Network) | ||
| 1002 | fmt.Fprintf(log, "environment: %s via %s, network %s\n", job.Image, box.caps.Backend, network) | ||
| 999 | 1003 | // A declared secret the repository does not have is not a failure: the variable | |
| 1000 | 1004 | // is left unset and the log says which one, because a workflow is allowed an | |
| 1001 | 1005 | // optional secret and a required one is ${NAME:?} in the step. | |
| @@ -1008,23 +1012,31 @@ func (s *runnerSession) run(remote, cache string, job *runnerJob, lease time.Dur | |||
| 1008 | 1012 | fmt.Fprintf(log, "vole: %s is not set for this repository, so this job runs without it.\n", issue.Name) | |
| 1009 | 1013 | } | |
| 1010 | 1014 | ||
| 1011 | // Hold the image in use from before it is resolved until the job container is | ||
| 1012 | // up: a prune pass takes the same guard exclusively, so a tag this job is about to | ||
| 1013 | // run cannot disappear between the two. A running container already protects its | ||
| 1014 | // image from the runtime's own prune; this closes the gap before one exists. | ||
| 1015 | releaseImages := box.images.hold() | ||
| 1016 | defer func() { | ||
| 1015 | // The image is prepared through the executor, which holds it in use from before it | ||
| 1016 | // is resolved until the job's environment is up: a prune pass takes the same guard | ||
| 1017 | // exclusively, so an image this job is about to run cannot disappear between the | ||
| 1018 | // two. A running job already protects its image from the runtime's own prune; this | ||
| 1019 | // closes the gap before one exists. | ||
| 1020 | wanted := execute.Image{ | ||
| 1021 | File: job.ImageFile, | ||
| 1022 | Definition: job.ImageDefinition, | ||
| 1023 | Digest: job.ImageSHA, | ||
| 1024 | } | ||
| 1025 | if job.ImageFile == "" { | ||
| 1026 | // An image-file job also carries a `file:` reference in Image; it is the | ||
| 1027 | // definition that runs, so only a registry job names a reference. | ||
| 1028 | wanted.Ref = job.Image | ||
| 1029 | } | ||
| 1030 | image, releaseImages, err := box.exec.Prepare(ctx, wanted, log, jobRunner{s: s, runID: job.ID, lease: lease}) | ||
| 1031 | if err != nil { | ||
| 1017 | 1032 | if releaseImages != nil { | |
| 1018 | 1033 | releaseImages() | |
| 1019 | 1034 | } | |
| 1020 | }() | ||
| 1021 | image, err := s.resolveImage(job, box, log, lease) | ||
| 1022 | if err != nil { | ||
| 1023 | if errors.Is(err, errLeaseLost) { | ||
| 1035 | if errors.Is(err, execute.ErrLeaseLost) { | ||
| 1024 | 1036 | fmt.Fprintf(os.Stderr, "vole runner: lease lost while building %s; stopping\n", job.Image) | |
| 1025 | 1037 | return | |
| 1026 | 1038 | } | |
| 1027 | if errors.Is(err, errCanceled) { | ||
| 1039 | if errors.Is(err, execute.ErrCanceled) { | ||
| 1028 | 1040 | // The build was stopped on a heartbeat, and the log already says why. The | |
| 1029 | 1041 | // attempt still has a story: report it with the work that did happen. | |
| 1030 | 1042 | s.finish(remote, co, job, log, stream, attemptOutcome{Exit: 1}, box) | |
| @@ -1033,57 +1045,11 @@ func (s *runnerSession) run(remote, cache string, job *runnerJob, lease time.Dur | |||
| 1033 | 1045 | fail("%s", err) | |
| 1034 | 1046 | return | |
| 1035 | 1047 | } | |
| 1036 | // Services are containers beside the job on a network of their own. They start | ||
| 1037 | // after the image is resolved so a slow build does not leave a database running, | ||
| 1038 | // and before the job so their names resolve for the first step. | ||
| 1039 | if len(job.Services) > 0 { | ||
| 1040 | netName := serviceNetworkName(box, job) | ||
| 1041 | ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
| 1042 | net, err := container.CreateNetwork(ctx, box.Config, netName, box.labels(job)) | ||
| 1043 | cancel() | ||
| 1044 | if err != nil { | ||
| 1045 | fail("%s", err) | ||
| 1046 | return | ||
| 1047 | } | ||
| 1048 | jobBox.network = net | ||
| 1049 | serviceConfig := boxConfig | ||
| 1050 | serviceConfig.Network = netName | ||
| 1051 | boxConfig.Network = netName | ||
| 1052 | ids := make([]string, 0, len(job.Services)) | ||
| 1053 | for _, svc := range job.Services { | ||
| 1054 | c, err := container.StartService(serviceConfig, container.ServiceSpec{ | ||
| 1055 | Name: serviceContainerName(box, job, svc.ID), | ||
| 1056 | Alias: svc.ID, | ||
| 1057 | Image: svc.Image, | ||
| 1058 | Env: svc.Env, | ||
| 1059 | Labels: box.labels(job), | ||
| 1060 | }) | ||
| 1061 | if err != nil { | ||
| 1062 | fail("%s", err) | ||
| 1063 | return | ||
| 1064 | } | ||
| 1065 | jobBox.services = append(jobBox.services, c) | ||
| 1066 | jobBox.serviceIDs = append(jobBox.serviceIDs, svc.ID) | ||
| 1067 | ids = append(ids, svc.ID+" ("+svc.Image+")") | ||
| 1068 | } | ||
| 1069 | fmt.Fprintf(log, "services: %s\n", strings.Join(ids, ", ")) | ||
| 1070 | for i, c := range jobBox.services { | ||
| 1071 | running, err := c.Running(context.Background()) | ||
| 1072 | if err != nil { | ||
| 1073 | fail("service %s: %s", job.Services[i].ID, err) | ||
| 1074 | return | ||
| 1075 | } | ||
| 1076 | if !running { | ||
| 1077 | fail("service %s exited before the steps started", job.Services[i].ID) | ||
| 1078 | return | ||
| 1079 | } | ||
| 1080 | } | ||
| 1081 | } | ||
| 1082 | 1048 | // The run directory is mounted at /run/vole for the pinned event payload and for | |
| 1083 | 1049 | // the job's secrets. Both arrive as files rather than as interpolated commands: a | |
| 1084 | 1050 | // large issue body cannot make an argument oversized, and a secret never becomes | |
| 1085 | 1051 | // an argument at all. | |
| 1086 | mounts := cacheMounts | ||
| 1052 | shares := cacheMounts | ||
| 1087 | 1053 | env := job.Env | |
| 1088 | 1054 | runDir := "" | |
| 1089 | 1055 | if job.EventPayload != "" || len(job.Secrets) > 0 || job.Credential != nil { | |
| @@ -1093,7 +1059,7 @@ func (s *runnerSession) run(remote, cache string, job *runnerJob, lease time.Dur | |||
| 1093 | 1059 | return | |
| 1094 | 1060 | } | |
| 1095 | 1061 | defer os.RemoveAll(runDir) | |
| 1096 | mounts = append(mounts, container.Mount{Host: runDir, Path: "/run/vole"}) | ||
| 1062 | shares = append(shares, execute.Share{Host: runDir, Path: "/run/vole"}) | ||
| 1097 | 1063 | } | |
| 1098 | 1064 | if job.EventPayload != "" { | |
| 1099 | 1065 | if err := os.WriteFile(filepath.Join(runDir, "event.json"), []byte(job.EventPayload), 0o600); err != nil { | |
| @@ -1148,27 +1114,43 @@ func (s *runnerSession) run(remote, cache string, job *runnerJob, lease time.Dur | |||
| 1148 | 1114 | defer func() { | |
| 1149 | 1115 | // The namespace is held exclusively for this job and this defer runs before | |
| 1150 | 1116 | // the lock is released, so nothing else is inside it while it is emptied. | |
| 1151 | // Only the cache mount: the run directory is the job's own and is removed | ||
| 1117 | // Only the cache share: the run directory is the job's own and is removed | ||
| 1152 | 1118 | // by the defer above. | |
| 1153 | 1119 | for _, m := range cacheMounts { | |
| 1154 | 1120 | _ = os.RemoveAll(m.Host) | |
| 1155 | 1121 | } | |
| 1156 | 1122 | }() | |
| 1157 | 1123 | } | |
| 1158 | sandbox, err := container.Start(boxConfig, container.Spec{ | ||
| 1159 | Image: image, | ||
| 1124 | if len(job.Services) > 0 { | ||
| 1125 | ids := make([]string, 0, len(job.Services)) | ||
| 1126 | for _, svc := range job.Services { | ||
| 1127 | ids = append(ids, svc.ID+" ("+svc.Image+")") | ||
| 1128 | } | ||
| 1129 | fmt.Fprintf(log, "services: %s\n", strings.Join(ids, ", ")) | ||
| 1130 | } | ||
| 1131 | // The executor starts everything the attempt needs — the services, the network | ||
| 1132 | // they share, and the job itself — because what those are is a property of the | ||
| 1133 | // backend and not of the workflow. A backend that failed partway still hands back | ||
| 1134 | // a sandbox holding what it did start, so this failure path can put the services' | ||
| 1135 | // output in the attempt before stopping them. | ||
| 1136 | started, err := box.exec.Start(ctx, execute.Spec{ | ||
| 1160 | 1137 | Name: containerName(box, job), | |
| 1161 | Work: co.Work, | ||
| 1162 | Labels: box.labels(job), | ||
| 1163 | Mounts: mounts, | ||
| 1138 | Image: image, | ||
| 1139 | Work: execute.Share{Host: co.Work, Path: "/workspace"}, | ||
| 1140 | Shares: shares, | ||
| 1164 | 1141 | Env: env, | |
| 1165 | 1142 | SecretsFile: secretsFile, | |
| 1143 | Network: network, | ||
| 1144 | Memory: box.cfg.Memory, | ||
| 1145 | CPUs: box.cfg.CPUs, | ||
| 1146 | Services: servicesFor(job), | ||
| 1147 | Labels: box.labels(job), | ||
| 1166 | 1148 | }) | |
| 1149 | jobBox = started | ||
| 1167 | 1150 | if err != nil { | |
| 1168 | 1151 | fail("%s", err) | |
| 1169 | 1152 | return | |
| 1170 | 1153 | } | |
| 1171 | jobBox.job = sandbox | ||
| 1172 | 1154 | // The container now holds the image itself, so the prune pass may run again. | |
| 1173 | 1155 | releaseImages() | |
| 1174 | 1156 | releaseImages = nil | |
| @@ -1209,7 +1191,7 @@ func (s *runnerSession) run(remote, cache string, job *runnerJob, lease time.Dur | |||
| 1209 | 1191 | fmt.Fprintf(os.Stderr, "vole runner: lease lost before %q; stopping\n", step.Name) | |
| 1210 | 1192 | return | |
| 1211 | 1193 | } | |
| 1212 | code, lost, canceled = s.execStep(job.ID, sandbox, step, limit, lease, log) | ||
| 1194 | code, lost, canceled = s.execStep(job.ID, jobBox, step, limit, lease, log) | ||
| 1213 | 1195 | } | |
| 1214 | 1196 | // The mask holds back a few bytes so a value split across writes is caught; | |
| 1215 | 1197 | // flushing here keeps this step's byte range its own. | |
| @@ -1228,7 +1210,7 @@ func (s *runnerSession) run(remote, cache string, job *runnerJob, lease time.Dur | |||
| 1228 | 1210 | // the timings are what a reader asks for first after "what had it done". | |
| 1229 | 1211 | s.step(job, stepEventFor(job, i+1, runs.StatusCanceled, result)) | |
| 1230 | 1212 | steps = append(steps, result) | |
| 1231 | jobBox.StopWithLogs(log) | ||
| 1213 | stopWithLogs(jobBox, log) | ||
| 1232 | 1214 | s.finish(remote, co, job, log, stream, attemptOutcome{Exit: code, Steps: steps}, box) | |
| 1233 | 1215 | return | |
| 1234 | 1216 | } | |
| @@ -1238,7 +1220,7 @@ func (s *runnerSession) run(remote, cache string, job *runnerJob, lease time.Dur | |||
| 1238 | 1220 | result.Finished = time.Now().UTC() | |
| 1239 | 1221 | s.step(job, stepEventFor(job, i+1, runs.StatusFailed, result)) | |
| 1240 | 1222 | steps = append(steps, result) | |
| 1241 | jobBox.StopWithLogs(log) | ||
| 1223 | stopWithLogs(jobBox, log) | ||
| 1242 | 1224 | s.finish(remote, co, job, log, stream, attemptOutcome{Exit: code, Steps: steps}, box) | |
| 1243 | 1225 | return | |
| 1244 | 1226 | } | |
| @@ -1280,7 +1262,7 @@ func (s *runnerSession) run(remote, cache string, job *runnerJob, lease time.Dur | |||
| 1280 | 1262 | // namespace separates repositories from each other, and contributor branches from | |
| 1281 | 1263 | // everything else: a cache is shared mutable state, and a branch nobody has vouched | |
| 1282 | 1264 | // for must not be able to seed the one a protected branch builds into. | |
| 1283 | func (s *runnerSession) cacheFor(job *runnerJob) ([]container.Mount, func()) { | ||
| 1265 | func (s *runnerSession) cacheFor(job *runnerJob) ([]execute.Share, func()) { | ||
| 1284 | 1266 | if s.cacheRoot == "" { | |
| 1285 | 1267 | return nil, func() {} | |
| 1286 | 1268 | } | |
| @@ -1311,7 +1293,7 @@ func (s *runnerSession) cacheFor(job *runnerJob) ([]container.Mount, func()) { | |||
| 1311 | 1293 | fmt.Fprintf(os.Stderr, "vole runner: cannot prepare a cache for %s: %v\n", job.Repo, err) | |
| 1312 | 1294 | return nil, func() {} | |
| 1313 | 1295 | } | |
| 1314 | return []container.Mount{{Host: ns, Path: jobcache.Mount}}, release | ||
| 1296 | return []execute.Share{{Host: ns, Path: jobcache.Mount}}, release | ||
| 1315 | 1297 | } | |
| 1316 | 1298 | ||
| 1317 | 1299 | // trimJobCache bounds the job cache until the runner stops. Each pass skips a | |
| @@ -1401,7 +1383,7 @@ func (s *runnerSession) finish(remote string, co checkout, job *runnerJob, log * | |||
| 1401 | 1383 | OS: runtime.GOOS, Arch: runtime.GOARCH, | |
| 1402 | 1384 | } | |
| 1403 | 1385 | rec.Image, rec.ImageFile, rec.ImageSHA = job.Image, job.ImageFile, job.ImageSHA | |
| 1404 | rec.Runtime = box.Runtime | ||
| 1386 | rec.Runtime = box.caps.Backend | ||
| 1405 | 1387 | // The record says which version of each secret the job ran with, never the value. | |
| 1406 | 1388 | // The names are sorted so the same attempt reported twice produces the same bytes. | |
| 1407 | 1389 | for name, delivered := range job.Secrets { | |
| @@ -1775,7 +1757,7 @@ func artifactTarget(pattern, file string) string { | |||
| 1775 | 1757 | ||
| 1776 | 1758 | // execStep runs one shell step, heartbeating while it works. It returns the exit | |
| 1777 | 1759 | // code, whether the lease was lost, and whether a maintainer asked the run to stop. | |
| 1778 | func (s *runnerSession) execStep(runID string, sandbox *container.Container, step runnerStep, limit time.Duration, lease time.Duration, out io.Writer) (int, bool, bool) { | ||
| 1760 | func (s *runnerSession) execStep(runID string, sandbox execute.Sandbox, step runnerStep, limit time.Duration, lease time.Duration, out io.Writer) (int, bool, bool) { | ||
| 1779 | 1761 | // limit is already resolved: the step's own timeout, or what is left of the job's | |
| 1780 | 1762 | // budget. The caller never passes zero. | |
| 1781 | 1763 | ctx, cancel := context.WithTimeout(context.Background(), limit) | |
| @@ -1797,6 +1779,72 @@ func (s *runnerSession) execStep(runID string, sandbox *container.Container, ste | |||
| 1797 | 1779 | return code, lost, false | |
| 1798 | 1780 | } | |
| 1799 | 1781 | ||
| 1782 | // jobRunner is the lease, as an executor sees it. | ||
| 1783 | // | ||
| 1784 | // An executor performs operations the runner cannot measure: a build, a pull of a | ||
| 1785 | // large image, a boot. This is what those operations use to stay alive, and it is | ||
| 1786 | // why an executor never needs to know how a heartbeat is sent. | ||
| 1787 | type jobRunner struct { | ||
| 1788 | s *runnerSession | ||
| 1789 | runID string | ||
| 1790 | lease time.Duration | ||
| 1791 | } | ||
| 1792 | |||
| 1793 | // Run implements execute.Runner: the command runs with the lease kept alive, the | ||
| 1794 | // disk checked, and a cancellation turned into a killed process group. | ||
| 1795 | func (r jobRunner) Run(ctx context.Context, cmd *exec.Cmd, out io.Writer) (int, bool, bool) { | ||
| 1796 | return r.s.runCommand(ctx, cmd, r.runID, r.lease, nil, out) | ||
| 1797 | } | ||
| 1798 | |||
| 1799 | // Beat implements execute.Runner by waiting one lease interval and asking. It is | ||
| 1800 | // the waiter's version of the ticker inside runCommand: a backend waiting on | ||
| 1801 | // another slot's build has no command of its own to run. | ||
| 1802 | func (r jobRunner) Beat(out io.Writer) (bool, bool) { | ||
| 1803 | interval := r.lease / 3 | ||
| 1804 | if interval < time.Second { | ||
| 1805 | interval = time.Second | ||
| 1806 | } | ||
| 1807 | time.Sleep(interval) | ||
| 1808 | rep, err := r.s.call(runnerRequest{Op: "heartbeat", Run: r.runID}) | ||
| 1809 | if err != nil || !rep.OK { | ||
| 1810 | return false, false | ||
| 1811 | } | ||
| 1812 | if rep.Cancel != "" { | ||
| 1813 | fmt.Fprintf(out, "\nthe run was canceled by %s; stopping\n", rep.Cancel) | ||
| 1814 | return false, true | ||
| 1815 | } | ||
| 1816 | return true, false | ||
| 1817 | } | ||
| 1818 | |||
| 1819 | // servicesFor is the service list an executor starts beside the job. | ||
| 1820 | func servicesFor(job *runnerJob) []execute.Service { | ||
| 1821 | if len(job.Services) == 0 { | ||
| 1822 | return nil | ||
| 1823 | } | ||
| 1824 | out := make([]execute.Service, 0, len(job.Services)) | ||
| 1825 | for _, svc := range job.Services { | ||
| 1826 | out = append(out, execute.Service{ID: svc.ID, Image: svc.Image, Env: svc.Env}) | ||
| 1827 | } | ||
| 1828 | return out | ||
| 1829 | } | ||
| 1830 | |||
| 1831 | // stopWithLogs stops the sandbox and puts each service's last output into the | ||
| 1832 | // attempt under its own heading: a job that failed after a database refused to start | ||
| 1833 | // should not make the reader find the database's log in a second place. | ||
| 1834 | func stopWithLogs(sandbox execute.Sandbox, log io.Writer) { | ||
| 1835 | sandbox.StopJob() | ||
| 1836 | if log != nil { | ||
| 1837 | for _, svc := range sandbox.ServiceLogs(context.Background(), execute.MaxServiceLogBytes) { | ||
| 1838 | fmt.Fprintf(log, "\n=== service %s ===\n", svc.ID) | ||
| 1839 | _, _ = log.Write(svc.Data) | ||
| 1840 | if svc.Data[len(svc.Data)-1] != '\n' { | ||
| 1841 | fmt.Fprintln(log) | ||
| 1842 | } | ||
| 1843 | } | ||
| 1844 | } | ||
| 1845 | sandbox.Stop() | ||
| 1846 | } | ||
| 1847 | |||
| 1800 | 1848 | // runCommand starts one host-side command and heartbeats while it runs. It is used | |
| 1801 | 1849 | // for docker exec and for building a custom image; onTimeout is what closes the | |
| 1802 | 1850 | // sandbox when the command runs out of time. A stop request from the server rides a | |
| @@ -1897,12 +1945,12 @@ func secretValues(job *runnerJob) []string { | |||
| 1897 | 1945 | } | |
| 1898 | 1946 | ||
| 1899 | 1947 | // secretsScript renders a job's delivered values as the file every step sources. | |
| 1900 | // The rendering lives in ci/container because that is the package that sources it, | ||
| 1901 | // so a local run writes exactly the same file a runner does. | ||
| 1948 | // The rendering lives in ci/execute because every backend that runs shell steps | ||
| 1949 | // sources the same file, so a local run writes exactly the same file a runner does. | ||
| 1902 | 1950 | func secretsScript(secrets map[string]runnerSecret) ([]byte, error) { | |
| 1903 | 1951 | values := make(map[string]string, len(secrets)) | |
| 1904 | 1952 | for name, delivered := range secrets { | |
| 1905 | 1953 | values[name] = delivered.Value | |
| 1906 | 1954 | } | |
| 1907 | return container.SecretsEnv(values) | ||
| 1955 | return execute.SecretsEnv(values) | ||
| 1908 | 1956 | } | |