8 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
9 "git.curoverse.com/arvados.git/sdk/go/keepclient"
10 "git.curoverse.com/arvados.git/sdk/go/manifest"
11 "github.com/curoverse/dockerclient"
24 // IArvadosClient is the minimal Arvados API methods used by crunch-run.
25 type IArvadosClient interface {
26 Create(resourceType string, parameters arvadosclient.Dict, output interface{}) error
27 Get(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) error
28 Update(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) (err error)
31 // ErrCancelled is the error returned when the container is cancelled.
32 var ErrCancelled = errors.New("Cancelled")
34 // IKeepClient is the minimal Keep API methods used by crunch-run.
35 type IKeepClient interface {
36 PutHB(hash string, buf []byte) (string, int, error)
37 ManifestFileReader(m manifest.Manifest, filename string) (keepclient.ReadCloserWithLen, error)
40 // Mount describes the mount points to create inside the container.
42 Kind string `json:"kind"`
43 Writable bool `json:"writable"`
44 PortableDataHash string `json:"portable_data_hash"`
45 UUID string `json:"uuid"`
46 DeviceType string `json:"device_type"`
49 // Collection record returned by the API server.
50 type CollectionRecord struct {
51 ManifestText string `json:"manifest_text"`
52 PortableDataHash string `json:"portable_data_hash"`
55 // ContainerRecord is the container record returned by the API server.
56 type ContainerRecord struct {
57 UUID string `json:"uuid"`
58 Command []string `json:"command"`
59 ContainerImage string `json:"container_image"`
60 Cwd string `json:"cwd"`
61 Environment map[string]string `json:"environment"`
62 Mounts map[string]Mount `json:"mounts"`
63 OutputPath string `json:"output_path"`
64 Priority int `json:"priority"`
65 RuntimeConstraints map[string]interface{} `json:"runtime_constraints"`
66 State string `json:"state"`
67 Output string `json:"output"`
70 // NewLogWriter is a factory function to create a new log writer.
71 type NewLogWriter func(name string) io.WriteCloser
73 type RunArvMount func([]string) (*exec.Cmd, error)
75 // ThinDockerClient is the minimal Docker client interface used by crunch-run.
76 type ThinDockerClient interface {
77 StopContainer(id string, timeout int) error
78 InspectImage(id string) (*dockerclient.ImageInfo, error)
79 LoadImage(reader io.Reader) error
80 CreateContainer(config *dockerclient.ContainerConfig, name string, authConfig *dockerclient.AuthConfig) (string, error)
81 StartContainer(id string, config *dockerclient.HostConfig) error
82 ContainerLogs(id string, options *dockerclient.LogOptions) (io.ReadCloser, error)
83 Wait(id string) <-chan dockerclient.WaitResult
84 RemoveImage(name string, force bool) ([]*dockerclient.ImageDelete, error)
87 // ContainerRunner is the main stateful struct used for a single execution of a
89 type ContainerRunner struct {
90 Docker ThinDockerClient
91 ArvClient IArvadosClient
94 dockerclient.ContainerConfig
99 CrunchLog *ThrottledLogger
100 Stdout *ThrottledLogger
101 Stderr *ThrottledLogger
102 LogCollection *CollectionWriter
110 CancelLock sync.Mutex
112 SigChan chan os.Signal
113 ArvMountExit chan error
117 // SetupSignals sets up signal handling to gracefully terminate the underlying
118 // Docker container and update state when receiving a TERM, INT or QUIT signal.
119 func (runner *ContainerRunner) SetupSignals() error {
120 runner.SigChan = make(chan os.Signal, 1)
121 signal.Notify(runner.SigChan, syscall.SIGTERM)
122 signal.Notify(runner.SigChan, syscall.SIGINT)
123 signal.Notify(runner.SigChan, syscall.SIGQUIT)
125 go func(sig <-chan os.Signal) {
127 if !runner.Cancelled {
128 runner.CancelLock.Lock()
129 runner.Cancelled = true
130 if runner.ContainerID != "" {
131 runner.Docker.StopContainer(runner.ContainerID, 10)
133 runner.CancelLock.Unlock()
141 // LoadImage determines the docker image id from the container record and
142 // checks if it is available in the local Docker image store. If not, it loads
143 // the image from Keep.
144 func (runner *ContainerRunner) LoadImage() (err error) {
146 runner.CrunchLog.Printf("Fetching Docker image from collection '%s'", runner.ContainerRecord.ContainerImage)
148 var collection CollectionRecord
149 err = runner.ArvClient.Get("collections", runner.ContainerRecord.ContainerImage, nil, &collection)
151 return fmt.Errorf("While getting container image collection: %v", err)
153 manifest := manifest.Manifest{Text: collection.ManifestText}
154 var img, imageID string
155 for ms := range manifest.StreamIter() {
156 img = ms.FileStreamSegments[0].Name
157 if !strings.HasSuffix(img, ".tar") {
158 return fmt.Errorf("First file in the container image collection does not end in .tar")
160 imageID = img[:len(img)-4]
163 runner.CrunchLog.Printf("Using Docker image id '%s'", imageID)
165 _, err = runner.Docker.InspectImage(imageID)
167 runner.CrunchLog.Print("Loading Docker image from keep")
169 var readCloser io.ReadCloser
170 readCloser, err = runner.Kc.ManifestFileReader(manifest, img)
172 return fmt.Errorf("While creating ManifestFileReader for container image: %v", err)
175 err = runner.Docker.LoadImage(readCloser)
177 return fmt.Errorf("While loading container image into Docker: %v", err)
180 runner.CrunchLog.Print("Docker image is available")
183 runner.ContainerConfig.Image = imageID
188 func (runner *ContainerRunner) ArvMountCmd(arvMountCmd []string) (c *exec.Cmd, err error) {
189 c = exec.Command("arv-mount", arvMountCmd...)
190 nt := NewThrottledLogger(runner.NewLogWriter("arv-mount"))
199 statReadme := make(chan bool)
200 runner.ArvMountExit = make(chan error)
205 time.Sleep(100 * time.Millisecond)
206 _, err = os.Stat(fmt.Sprintf("%s/by_id/README", runner.ArvMountPoint))
216 runner.ArvMountExit <- c.Wait()
217 close(runner.ArvMountExit)
223 case err := <-runner.ArvMountExit:
224 runner.ArvMount = nil
232 func (runner *ContainerRunner) SetupMounts() (err error) {
233 runner.ArvMountPoint, err = ioutil.TempDir("", "keep")
235 return fmt.Errorf("While creating keep mount temp dir: %v", err)
240 arvMountCmd := []string{"--foreground"}
241 collectionPaths := []string{}
243 for bind, mnt := range runner.ContainerRecord.Mounts {
244 if mnt.Kind == "collection" {
246 if mnt.UUID != "" && mnt.PortableDataHash != "" {
247 return fmt.Errorf("Cannot specify both 'uuid' and 'portable_data_hash' for a collection mount")
251 return fmt.Errorf("Writing to existing collections currently not permitted.")
254 src = fmt.Sprintf("%s/by_id/%s", runner.ArvMountPoint, mnt.UUID)
255 } else if mnt.PortableDataHash != "" {
257 return fmt.Errorf("Can never write to a collection specified by portable data hash")
259 src = fmt.Sprintf("%s/by_id/%s", runner.ArvMountPoint, mnt.PortableDataHash)
261 src = fmt.Sprintf("%s/tmp%i", runner.ArvMountPoint, tmpcount)
262 arvMountCmd = append(arvMountCmd, "--mount-tmp")
263 arvMountCmd = append(arvMountCmd, fmt.Sprintf("tmp%i", tmpcount))
267 if bind == runner.ContainerRecord.OutputPath {
268 runner.HostOutputDir = src
270 runner.Binds = append(runner.Binds, fmt.Sprintf("%s:%s", src, bind))
272 runner.Binds = append(runner.Binds, fmt.Sprintf("%s:%s:ro", src, bind))
274 collectionPaths = append(collectionPaths, src)
275 } else if mnt.Kind == "tmp" {
276 if bind == runner.ContainerRecord.OutputPath {
277 runner.HostOutputDir, err = ioutil.TempDir("", "")
279 return fmt.Errorf("While creating mount temp dir: %v", err)
282 runner.Binds = append(runner.Binds, fmt.Sprintf("%s:%s", runner.HostOutputDir, bind))
284 runner.Binds = append(runner.Binds, bind)
287 return fmt.Errorf("Unknown mount kind '%s'", mnt.Kind)
291 if runner.HostOutputDir == "" {
292 return fmt.Errorf("Output path does not correspond to a writable mount point")
296 arvMountCmd = append(arvMountCmd, "--mount-by-pdh", "by_id")
298 arvMountCmd = append(arvMountCmd, "--mount-by-id", "by_id")
300 arvMountCmd = append(arvMountCmd, runner.ArvMountPoint)
302 runner.ArvMount, err = runner.RunArvMount(arvMountCmd)
304 return fmt.Errorf("While trying to start arv-mount: %v", err)
307 for _, p := range collectionPaths {
310 return fmt.Errorf("While checking that input files exist: %v", err)
317 // StartContainer creates the container and runs it.
318 func (runner *ContainerRunner) StartContainer() (err error) {
319 runner.CrunchLog.Print("Creating Docker container")
321 runner.CancelLock.Lock()
322 defer runner.CancelLock.Unlock()
324 if runner.Cancelled {
328 runner.ContainerConfig.Cmd = runner.ContainerRecord.Command
329 if runner.ContainerRecord.Cwd != "." {
330 runner.ContainerConfig.WorkingDir = runner.ContainerRecord.Cwd
332 for k, v := range runner.ContainerRecord.Environment {
333 runner.ContainerConfig.Env = append(runner.ContainerConfig.Env, k+"="+v)
335 runner.ContainerConfig.NetworkDisabled = true
336 runner.ContainerID, err = runner.Docker.CreateContainer(&runner.ContainerConfig, "", nil)
338 return fmt.Errorf("While creating container: %v", err)
340 hostConfig := &dockerclient.HostConfig{Binds: runner.Binds}
342 runner.CrunchLog.Printf("Starting Docker container id '%s'", runner.ContainerID)
343 err = runner.Docker.StartContainer(runner.ContainerID, hostConfig)
345 return fmt.Errorf("While starting container: %v", err)
351 // AttachLogs connects the docker container stdout and stderr logs to the
352 // Arvados logger which logs to Keep and the API server logs table.
353 func (runner *ContainerRunner) AttachLogs() (err error) {
355 runner.CrunchLog.Print("Attaching container logs")
357 var stderrReader, stdoutReader io.Reader
358 stderrReader, err = runner.Docker.ContainerLogs(runner.ContainerID, &dockerclient.LogOptions{Follow: true, Stderr: true})
360 return fmt.Errorf("While getting container standard error: %v", err)
362 stdoutReader, err = runner.Docker.ContainerLogs(runner.ContainerID, &dockerclient.LogOptions{Follow: true, Stdout: true})
364 return fmt.Errorf("While getting container standard output: %v", err)
367 runner.loggingDone = make(chan bool)
369 runner.Stdout = NewThrottledLogger(runner.NewLogWriter("stdout"))
370 runner.Stderr = NewThrottledLogger(runner.NewLogWriter("stderr"))
371 go ReadWriteLines(stdoutReader, runner.Stdout, runner.loggingDone)
372 go ReadWriteLines(stderrReader, runner.Stderr, runner.loggingDone)
377 // WaitFinish waits for the container to terminate, capture the exit code, and
378 // close the stdout/stderr logging.
379 func (runner *ContainerRunner) WaitFinish() error {
380 result := runner.Docker.Wait(runner.ContainerID)
383 return fmt.Errorf("While waiting for container to finish: %v", wr.Error)
385 runner.ExitCode = &wr.ExitCode
387 // drain stdout/stderr
391 runner.Stdout.Close()
392 runner.Stderr.Close()
397 // HandleOutput sets the output and unmounts the FUSE mount.
398 func (runner *ContainerRunner) CaptureOutput() error {
399 if runner.ArvMount != nil {
401 umount := exec.Command("fusermount", "-z", "-u", runner.ArvMountPoint)
402 umnterr := umount.Run()
404 runner.CrunchLog.Print("While running fusermount: %v", umnterr)
407 mnterr := <-runner.ArvMountExit
409 runner.CrunchLog.Print("Arv-mount exit error: %v", mnterr)
414 if runner.finalState != "Complete" {
418 if runner.HostOutputDir == "" {
422 _, err := os.Stat(runner.HostOutputDir)
424 return fmt.Errorf("While checking host output path: %v", err)
427 var manifestText string
429 collectionMetafile := fmt.Sprintf("%s/.arvados#collection", runner.HostOutputDir)
430 _, err = os.Stat(collectionMetafile)
433 cw := CollectionWriter{runner.Kc, nil, sync.Mutex{}}
434 manifestText, err = cw.WriteTree(runner.HostOutputDir, runner.CrunchLog.Logger)
436 return fmt.Errorf("While uploading output files: %v", err)
439 // FUSE mount directory
440 file, openerr := os.Open(collectionMetafile)
442 return fmt.Errorf("While opening FUSE metafile: %v", err)
446 rec := CollectionRecord{}
447 err = json.NewDecoder(file).Decode(&rec)
449 return fmt.Errorf("While reading FUSE metafile: %v", err)
451 manifestText = rec.ManifestText
454 var response CollectionRecord
455 err = runner.ArvClient.Create("collections",
457 "collection": arvadosclient.Dict{
458 "manifest_text": manifestText}},
461 return fmt.Errorf("While creating output collection: %v", err)
464 runner.OutputPDH = new(string)
465 *runner.OutputPDH = response.PortableDataHash
470 // CommitLogs posts the collection containing the final container logs.
471 func (runner *ContainerRunner) CommitLogs() error {
472 runner.CrunchLog.Print(runner.finalState)
473 runner.CrunchLog.Close()
475 // Closing CrunchLog above allows it to be committed to Keep at this
476 // point, but re-open crunch log with ArvClient in case there are any
477 // other further (such as failing to write the log to Keep!) while
479 runner.CrunchLog = NewThrottledLogger(&ArvLogWriter{runner.ArvClient, runner.ContainerRecord.UUID,
482 mt, err := runner.LogCollection.ManifestText()
484 return fmt.Errorf("While creating log manifest: %v", err)
487 var response CollectionRecord
488 err = runner.ArvClient.Create("collections",
490 "collection": arvadosclient.Dict{
491 "name": "logs for " + runner.ContainerRecord.UUID,
492 "manifest_text": mt}},
495 return fmt.Errorf("While creating log collection: %v", err)
498 runner.LogsPDH = new(string)
499 *runner.LogsPDH = response.PortableDataHash
504 // UpdateContainerRecordRunning updates the container state to "Running"
505 func (runner *ContainerRunner) UpdateContainerRecordRunning() error {
506 return runner.ArvClient.Update("containers", runner.ContainerRecord.UUID,
507 arvadosclient.Dict{"container": arvadosclient.Dict{"state": "Running"}}, nil)
510 // UpdateContainerRecordComplete updates the container record state on API
511 // server to "Complete" or "Cancelled"
512 func (runner *ContainerRunner) UpdateContainerRecordComplete() error {
513 update := arvadosclient.Dict{}
514 if runner.LogsPDH != nil {
515 update["log"] = *runner.LogsPDH
517 if runner.ExitCode != nil {
518 update["exit_code"] = *runner.ExitCode
520 if runner.OutputPDH != nil {
521 update["output"] = runner.OutputPDH
524 update["state"] = runner.finalState
526 return runner.ArvClient.Update("containers", runner.ContainerRecord.UUID, arvadosclient.Dict{"container": update}, nil)
529 // NewArvLogWriter creates an ArvLogWriter
530 func (runner *ContainerRunner) NewArvLogWriter(name string) io.WriteCloser {
531 return &ArvLogWriter{runner.ArvClient, runner.ContainerRecord.UUID, name, runner.LogCollection.Open(name + ".txt")}
534 // Run the full container lifecycle.
535 func (runner *ContainerRunner) Run() (err error) {
536 runner.CrunchLog.Printf("Executing container '%s'", runner.ContainerRecord.UUID)
538 var runerr, waiterr error
542 runner.CrunchLog.Print(err)
545 if runner.Cancelled {
546 runner.finalState = "Cancelled"
548 runner.finalState = "Complete"
551 // (7) capture output
552 outputerr := runner.CaptureOutput()
553 if outputerr != nil {
554 runner.CrunchLog.Print(outputerr)
558 logerr := runner.CommitLogs()
560 runner.CrunchLog.Print(logerr)
563 // (9) update container record with results
564 updateerr := runner.UpdateContainerRecordComplete()
565 if updateerr != nil {
566 runner.CrunchLog.Print(updateerr)
569 runner.CrunchLog.Close()
574 } else if waiterr != nil {
576 } else if logerr != nil {
578 } else if updateerr != nil {
584 err = runner.ArvClient.Get("containers", runner.ContainerRecord.UUID, nil, &runner.ContainerRecord)
586 return fmt.Errorf("While getting container record: %v", err)
589 // (1) setup signal handling
590 err = runner.SetupSignals()
592 return fmt.Errorf("While setting up signal handling: %v", err)
595 // (2) check for and/or load image
596 err = runner.LoadImage()
598 return fmt.Errorf("While loading container image: %v", err)
601 // (3) set up FUSE mount and binds
602 err = runner.SetupMounts()
604 return fmt.Errorf("While setting up mounts: %v", err)
607 // (3) create and start container
608 err = runner.StartContainer()
610 if err == ErrCancelled {
616 // (4) update container record state
617 err = runner.UpdateContainerRecordRunning()
619 runner.CrunchLog.Print(err)
622 // (5) attach container logs
623 runerr = runner.AttachLogs()
625 runner.CrunchLog.Print(runerr)
628 // (6) wait for container to finish
629 waiterr = runner.WaitFinish()
634 // NewContainerRunner creates a new container runner.
635 func NewContainerRunner(api IArvadosClient,
637 docker ThinDockerClient,
638 containerUUID string) *ContainerRunner {
640 cr := &ContainerRunner{ArvClient: api, Kc: kc, Docker: docker}
641 cr.NewLogWriter = cr.NewArvLogWriter
642 cr.RunArvMount = cr.ArvMountCmd
643 cr.LogCollection = &CollectionWriter{kc, nil, sync.Mutex{}}
644 cr.ContainerRecord.UUID = containerUUID
645 cr.CrunchLog = NewThrottledLogger(cr.NewLogWriter("crunch-run"))
652 api, err := arvadosclient.MakeArvadosClient()
658 var kc *keepclient.KeepClient
659 kc, err = keepclient.MakeKeepClient(&api)
665 var docker *dockerclient.DockerClient
666 docker, err = dockerclient.NewDockerClient("unix:///var/run/docker.sock", nil)
671 cr := NewContainerRunner(api, kc, docker, flag.Arg(0))