1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
30 "git.curoverse.com/arvados.git/lib/crunchstat"
31 "git.curoverse.com/arvados.git/sdk/go/arvados"
32 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
33 "git.curoverse.com/arvados.git/sdk/go/keepclient"
34 "git.curoverse.com/arvados.git/sdk/go/manifest"
35 "golang.org/x/net/context"
37 dockertypes "github.com/docker/docker/api/types"
38 dockercontainer "github.com/docker/docker/api/types/container"
39 dockernetwork "github.com/docker/docker/api/types/network"
40 dockerclient "github.com/docker/docker/client"
45 // IArvadosClient is the minimal Arvados API methods used by crunch-run.
46 type IArvadosClient interface {
47 Create(resourceType string, parameters arvadosclient.Dict, output interface{}) error
48 Get(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) error
49 Update(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) error
50 Call(method, resourceType, uuid, action string, parameters arvadosclient.Dict, output interface{}) error
51 CallRaw(method string, resourceType string, uuid string, action string, parameters arvadosclient.Dict) (reader io.ReadCloser, err error)
52 Discovery(key string) (interface{}, error)
55 // ErrCancelled is the error returned when the container is cancelled.
56 var ErrCancelled = errors.New("Cancelled")
58 // IKeepClient is the minimal Keep API methods used by crunch-run.
59 type IKeepClient interface {
60 PutHB(hash string, buf []byte) (string, int, error)
61 ManifestFileReader(m manifest.Manifest, filename string) (arvados.File, error)
65 // NewLogWriter is a factory function to create a new log writer.
66 type NewLogWriter func(name string) io.WriteCloser
68 type RunArvMount func(args []string, tok string) (*exec.Cmd, error)
70 type MkTempDir func(string, string) (string, error)
72 // ThinDockerClient is the minimal Docker client interface used by crunch-run.
73 type ThinDockerClient interface {
74 ContainerAttach(ctx context.Context, container string, options dockertypes.ContainerAttachOptions) (dockertypes.HijackedResponse, error)
75 ContainerCreate(ctx context.Context, config *dockercontainer.Config, hostConfig *dockercontainer.HostConfig,
76 networkingConfig *dockernetwork.NetworkingConfig, containerName string) (dockercontainer.ContainerCreateCreatedBody, error)
77 ContainerStart(ctx context.Context, container string, options dockertypes.ContainerStartOptions) error
78 ContainerRemove(ctx context.Context, container string, options dockertypes.ContainerRemoveOptions) error
79 ContainerWait(ctx context.Context, container string, condition dockercontainer.WaitCondition) (<-chan dockercontainer.ContainerWaitOKBody, <-chan error)
80 ImageInspectWithRaw(ctx context.Context, image string) (dockertypes.ImageInspect, []byte, error)
81 ImageLoad(ctx context.Context, input io.Reader, quiet bool) (dockertypes.ImageLoadResponse, error)
82 ImageRemove(ctx context.Context, image string, options dockertypes.ImageRemoveOptions) ([]dockertypes.ImageDeleteResponseItem, error)
85 // ContainerRunner is the main stateful struct used for a single execution of a
87 type ContainerRunner struct {
88 Docker ThinDockerClient
89 ArvClient IArvadosClient
92 ContainerConfig dockercontainer.Config
93 dockercontainer.HostConfig
99 CrunchLog *ThrottledLogger
100 Stdout io.WriteCloser
101 Stderr io.WriteCloser
102 LogCollection *CollectionWriter
110 Volumes map[string]struct{}
112 SigChan chan os.Signal
113 ArvMountExit chan error
114 SecretMounts map[string]arvados.Mount
118 statLogger io.WriteCloser
119 statReporter *crunchstat.Reporter
120 hoststatLogger io.WriteCloser
121 hoststatReporter *crunchstat.Reporter
122 statInterval time.Duration
124 // What we expect the container's cgroup parent to be.
125 expectCgroupParent string
126 // What we tell docker to use as the container's cgroup
127 // parent. Note: Ideally we would use the same field for both
128 // expectCgroupParent and setCgroupParent, and just make it
129 // default to "docker". However, when using docker < 1.10 with
130 // systemd, specifying a non-empty cgroup parent (even the
131 // default value "docker") hits a docker bug
132 // (https://github.com/docker/docker/issues/17126). Using two
133 // separate fields makes it possible to use the "expect cgroup
134 // parent to be X" feature even on sites where the "specify
135 // cgroup parent" feature breaks.
136 setCgroupParent string
138 cStateLock sync.Mutex
139 cCancelled bool // StopContainer() invoked
141 enableNetwork string // one of "default" or "always"
142 networkMode string // passed through to HostConfig.NetworkMode
143 arvMountLog *ThrottledLogger
146 // setupSignals sets up signal handling to gracefully terminate the underlying
147 // Docker container and update state when receiving a TERM, INT or QUIT signal.
148 func (runner *ContainerRunner) setupSignals() {
149 runner.SigChan = make(chan os.Signal, 1)
150 signal.Notify(runner.SigChan, syscall.SIGTERM)
151 signal.Notify(runner.SigChan, syscall.SIGINT)
152 signal.Notify(runner.SigChan, syscall.SIGQUIT)
154 go func(sig chan os.Signal) {
161 // stop the underlying Docker container.
162 func (runner *ContainerRunner) stop(sig os.Signal) {
163 runner.cStateLock.Lock()
164 defer runner.cStateLock.Unlock()
166 runner.CrunchLog.Printf("caught signal: %v", sig)
168 if runner.ContainerID == "" {
171 runner.cCancelled = true
172 runner.CrunchLog.Printf("removing container")
173 err := runner.Docker.ContainerRemove(context.TODO(), runner.ContainerID, dockertypes.ContainerRemoveOptions{Force: true})
175 runner.CrunchLog.Printf("error removing container: %s", err)
179 var errorBlacklist = []string{
180 "(?ms).*[Cc]annot connect to the Docker daemon.*",
181 "(?ms).*oci runtime error.*starting container process.*container init.*mounting.*to rootfs.*no such file or directory.*",
183 var brokenNodeHook *string = flag.String("broken-node-hook", "", "Script to run if node is detected to be broken (for example, Docker daemon is not running)")
185 func (runner *ContainerRunner) checkBrokenNode(goterr error) bool {
186 for _, d := range errorBlacklist {
187 if m, e := regexp.MatchString(d, goterr.Error()); m && e == nil {
188 runner.CrunchLog.Printf("Error suggests node is unable to run containers: %v", goterr)
189 if *brokenNodeHook == "" {
190 runner.CrunchLog.Printf("No broken node hook provided, cannot mark node as broken.")
192 runner.CrunchLog.Printf("Running broken node hook %q", *brokenNodeHook)
194 c := exec.Command(*brokenNodeHook)
195 c.Stdout = runner.CrunchLog
196 c.Stderr = runner.CrunchLog
199 runner.CrunchLog.Printf("Error running broken node hook: %v", err)
208 // LoadImage determines the docker image id from the container record and
209 // checks if it is available in the local Docker image store. If not, it loads
210 // the image from Keep.
211 func (runner *ContainerRunner) LoadImage() (err error) {
213 runner.CrunchLog.Printf("Fetching Docker image from collection '%s'", runner.Container.ContainerImage)
215 var collection arvados.Collection
216 err = runner.ArvClient.Get("collections", runner.Container.ContainerImage, nil, &collection)
218 return fmt.Errorf("While getting container image collection: %v", err)
220 manifest := manifest.Manifest{Text: collection.ManifestText}
221 var img, imageID string
222 for ms := range manifest.StreamIter() {
223 img = ms.FileStreamSegments[0].Name
224 if !strings.HasSuffix(img, ".tar") {
225 return fmt.Errorf("First file in the container image collection does not end in .tar")
227 imageID = img[:len(img)-4]
230 runner.CrunchLog.Printf("Using Docker image id '%s'", imageID)
232 _, _, err = runner.Docker.ImageInspectWithRaw(context.TODO(), imageID)
234 runner.CrunchLog.Print("Loading Docker image from keep")
236 var readCloser io.ReadCloser
237 readCloser, err = runner.Kc.ManifestFileReader(manifest, img)
239 return fmt.Errorf("While creating ManifestFileReader for container image: %v", err)
242 response, err := runner.Docker.ImageLoad(context.TODO(), readCloser, true)
244 return fmt.Errorf("While loading container image into Docker: %v", err)
247 defer response.Body.Close()
248 rbody, err := ioutil.ReadAll(response.Body)
250 return fmt.Errorf("Reading response to image load: %v", err)
252 runner.CrunchLog.Printf("Docker response: %s", rbody)
254 runner.CrunchLog.Print("Docker image is available")
257 runner.ContainerConfig.Image = imageID
259 runner.Kc.ClearBlockCache()
264 func (runner *ContainerRunner) ArvMountCmd(arvMountCmd []string, token string) (c *exec.Cmd, err error) {
265 c = exec.Command("arv-mount", arvMountCmd...)
267 // Copy our environment, but override ARVADOS_API_TOKEN with
268 // the container auth token.
270 for _, s := range os.Environ() {
271 if !strings.HasPrefix(s, "ARVADOS_API_TOKEN=") {
272 c.Env = append(c.Env, s)
275 c.Env = append(c.Env, "ARVADOS_API_TOKEN="+token)
277 runner.arvMountLog = NewThrottledLogger(runner.NewLogWriter("arv-mount"))
278 c.Stdout = runner.arvMountLog
279 c.Stderr = runner.arvMountLog
281 runner.CrunchLog.Printf("Running %v", c.Args)
288 statReadme := make(chan bool)
289 runner.ArvMountExit = make(chan error)
294 time.Sleep(100 * time.Millisecond)
295 _, err = os.Stat(fmt.Sprintf("%s/by_id/README", runner.ArvMountPoint))
307 runner.CrunchLog.Printf("Arv-mount exit error: %v", mnterr)
309 runner.ArvMountExit <- mnterr
310 close(runner.ArvMountExit)
316 case err := <-runner.ArvMountExit:
317 runner.ArvMount = nil
325 func (runner *ContainerRunner) SetupArvMountPoint(prefix string) (err error) {
326 if runner.ArvMountPoint == "" {
327 runner.ArvMountPoint, err = runner.MkTempDir(runner.parentTemp, prefix)
332 func copyfile(src string, dst string) (err error) {
333 srcfile, err := os.Open(src)
338 os.MkdirAll(path.Dir(dst), 0777)
340 dstfile, err := os.Create(dst)
344 _, err = io.Copy(dstfile, srcfile)
349 err = srcfile.Close()
350 err2 := dstfile.Close()
363 func (runner *ContainerRunner) SetupMounts() (err error) {
364 err = runner.SetupArvMountPoint("keep")
366 return fmt.Errorf("While creating keep mount temp dir: %v", err)
369 token, err := runner.ContainerToken()
371 return fmt.Errorf("could not get container token: %s", err)
376 arvMountCmd := []string{
380 fmt.Sprintf("--crunchstat-interval=%v", runner.statInterval.Seconds())}
382 if runner.Container.RuntimeConstraints.KeepCacheRAM > 0 {
383 arvMountCmd = append(arvMountCmd, "--file-cache", fmt.Sprintf("%d", runner.Container.RuntimeConstraints.KeepCacheRAM))
386 collectionPaths := []string{}
388 runner.Volumes = make(map[string]struct{})
389 needCertMount := true
390 type copyFile struct {
394 var copyFiles []copyFile
397 for bind := range runner.Container.Mounts {
398 binds = append(binds, bind)
400 for bind := range runner.SecretMounts {
401 if runner.SecretMounts[bind].Kind != "json" &&
402 runner.SecretMounts[bind].Kind != "text" {
403 return fmt.Errorf("Secret mount %q type is %q but only 'json' and 'text' are permitted.",
404 bind, runner.SecretMounts[bind].Kind)
406 binds = append(binds, bind)
410 for _, bind := range binds {
411 mnt, ok := runner.Container.Mounts[bind]
413 mnt = runner.SecretMounts[bind]
415 if bind == "stdout" || bind == "stderr" {
416 // Is it a "file" mount kind?
417 if mnt.Kind != "file" {
418 return fmt.Errorf("Unsupported mount kind '%s' for %s. Only 'file' is supported.", mnt.Kind, bind)
421 // Does path start with OutputPath?
422 prefix := runner.Container.OutputPath
423 if !strings.HasSuffix(prefix, "/") {
426 if !strings.HasPrefix(mnt.Path, prefix) {
427 return fmt.Errorf("%s path does not start with OutputPath: %s, %s", strings.Title(bind), mnt.Path, prefix)
432 // Is it a "collection" mount kind?
433 if mnt.Kind != "collection" && mnt.Kind != "json" {
434 return fmt.Errorf("Unsupported mount kind '%s' for stdin. Only 'collection' or 'json' are supported.", mnt.Kind)
438 if bind == "/etc/arvados/ca-certificates.crt" {
439 needCertMount = false
442 if strings.HasPrefix(bind, runner.Container.OutputPath+"/") && bind != runner.Container.OutputPath+"/" {
443 if mnt.Kind != "collection" && mnt.Kind != "text" && mnt.Kind != "json" {
444 return fmt.Errorf("Only mount points of kind 'collection', 'text' or 'json' are supported underneath the output_path for %q, was %q", bind, mnt.Kind)
449 case mnt.Kind == "collection" && bind != "stdin":
451 if mnt.UUID != "" && mnt.PortableDataHash != "" {
452 return fmt.Errorf("Cannot specify both 'uuid' and 'portable_data_hash' for a collection mount")
456 return fmt.Errorf("Writing to existing collections currently not permitted.")
459 src = fmt.Sprintf("%s/by_id/%s", runner.ArvMountPoint, mnt.UUID)
460 } else if mnt.PortableDataHash != "" {
461 if mnt.Writable && !strings.HasPrefix(bind, runner.Container.OutputPath+"/") {
462 return fmt.Errorf("Can never write to a collection specified by portable data hash")
464 idx := strings.Index(mnt.PortableDataHash, "/")
466 mnt.Path = path.Clean(mnt.PortableDataHash[idx:])
467 mnt.PortableDataHash = mnt.PortableDataHash[0:idx]
468 runner.Container.Mounts[bind] = mnt
470 src = fmt.Sprintf("%s/by_id/%s", runner.ArvMountPoint, mnt.PortableDataHash)
471 if mnt.Path != "" && mnt.Path != "." {
472 if strings.HasPrefix(mnt.Path, "./") {
473 mnt.Path = mnt.Path[2:]
474 } else if strings.HasPrefix(mnt.Path, "/") {
475 mnt.Path = mnt.Path[1:]
477 src += "/" + mnt.Path
480 src = fmt.Sprintf("%s/tmp%d", runner.ArvMountPoint, tmpcount)
481 arvMountCmd = append(arvMountCmd, "--mount-tmp")
482 arvMountCmd = append(arvMountCmd, fmt.Sprintf("tmp%d", tmpcount))
486 if bind == runner.Container.OutputPath {
487 runner.HostOutputDir = src
488 runner.Binds = append(runner.Binds, fmt.Sprintf("%s:%s", src, bind))
489 } else if strings.HasPrefix(bind, runner.Container.OutputPath+"/") {
490 copyFiles = append(copyFiles, copyFile{src, runner.HostOutputDir + bind[len(runner.Container.OutputPath):]})
492 runner.Binds = append(runner.Binds, fmt.Sprintf("%s:%s", src, bind))
495 runner.Binds = append(runner.Binds, fmt.Sprintf("%s:%s:ro", src, bind))
497 collectionPaths = append(collectionPaths, src)
499 case mnt.Kind == "tmp":
501 tmpdir, err = runner.MkTempDir(runner.parentTemp, "tmp")
503 return fmt.Errorf("While creating mount temp dir: %v", err)
505 st, staterr := os.Stat(tmpdir)
507 return fmt.Errorf("While Stat on temp dir: %v", staterr)
509 err = os.Chmod(tmpdir, st.Mode()|os.ModeSetgid|0777)
511 return fmt.Errorf("While Chmod temp dir: %v", err)
513 runner.Binds = append(runner.Binds, fmt.Sprintf("%s:%s", tmpdir, bind))
514 if bind == runner.Container.OutputPath {
515 runner.HostOutputDir = tmpdir
518 case mnt.Kind == "json" || mnt.Kind == "text":
520 if mnt.Kind == "json" {
521 filedata, err = json.Marshal(mnt.Content)
523 return fmt.Errorf("encoding json data: %v", err)
526 text, ok := mnt.Content.(string)
528 return fmt.Errorf("content for mount %q must be a string", bind)
530 filedata = []byte(text)
533 tmpdir, err := runner.MkTempDir(runner.parentTemp, mnt.Kind)
535 return fmt.Errorf("creating temp dir: %v", err)
537 tmpfn := filepath.Join(tmpdir, "mountdata."+mnt.Kind)
538 err = ioutil.WriteFile(tmpfn, filedata, 0444)
540 return fmt.Errorf("writing temp file: %v", err)
542 if strings.HasPrefix(bind, runner.Container.OutputPath+"/") {
543 copyFiles = append(copyFiles, copyFile{tmpfn, runner.HostOutputDir + bind[len(runner.Container.OutputPath):]})
545 runner.Binds = append(runner.Binds, fmt.Sprintf("%s:%s:ro", tmpfn, bind))
548 case mnt.Kind == "git_tree":
549 tmpdir, err := runner.MkTempDir(runner.parentTemp, "git_tree")
551 return fmt.Errorf("creating temp dir: %v", err)
553 err = gitMount(mnt).extractTree(runner.ArvClient, tmpdir, token)
557 runner.Binds = append(runner.Binds, tmpdir+":"+bind+":ro")
561 if runner.HostOutputDir == "" {
562 return fmt.Errorf("Output path does not correspond to a writable mount point")
565 if wantAPI := runner.Container.RuntimeConstraints.API; needCertMount && wantAPI != nil && *wantAPI {
566 for _, certfile := range arvadosclient.CertFiles {
567 _, err := os.Stat(certfile)
569 runner.Binds = append(runner.Binds, fmt.Sprintf("%s:/etc/arvados/ca-certificates.crt:ro", certfile))
576 arvMountCmd = append(arvMountCmd, "--mount-by-pdh", "by_id")
578 arvMountCmd = append(arvMountCmd, "--mount-by-id", "by_id")
580 arvMountCmd = append(arvMountCmd, runner.ArvMountPoint)
582 runner.ArvMount, err = runner.RunArvMount(arvMountCmd, token)
584 return fmt.Errorf("While trying to start arv-mount: %v", err)
587 for _, p := range collectionPaths {
590 return fmt.Errorf("While checking that input files exist: %v", err)
594 for _, cp := range copyFiles {
595 st, err := os.Stat(cp.src)
597 return fmt.Errorf("While staging writable file from %q to %q: %v", cp.src, cp.bind, err)
600 err = filepath.Walk(cp.src, func(walkpath string, walkinfo os.FileInfo, walkerr error) error {
604 target := path.Join(cp.bind, walkpath[len(cp.src):])
605 if walkinfo.Mode().IsRegular() {
606 copyerr := copyfile(walkpath, target)
610 return os.Chmod(target, walkinfo.Mode()|0777)
611 } else if walkinfo.Mode().IsDir() {
612 mkerr := os.MkdirAll(target, 0777)
616 return os.Chmod(target, walkinfo.Mode()|os.ModeSetgid|0777)
618 return fmt.Errorf("Source %q is not a regular file or directory", cp.src)
621 } else if st.Mode().IsRegular() {
622 err = copyfile(cp.src, cp.bind)
624 err = os.Chmod(cp.bind, st.Mode()|0777)
628 return fmt.Errorf("While staging writable file from %q to %q: %v", cp.src, cp.bind, err)
635 func (runner *ContainerRunner) ProcessDockerAttach(containerReader io.Reader) {
636 // Handle docker log protocol
637 // https://docs.docker.com/engine/reference/api/docker_remote_api_v1.15/#attach-to-a-container
638 defer close(runner.loggingDone)
640 header := make([]byte, 8)
643 _, err = io.ReadAtLeast(containerReader, header, 8)
650 readsize := int64(header[7]) | (int64(header[6]) << 8) | (int64(header[5]) << 16) | (int64(header[4]) << 24)
653 _, err = io.CopyN(runner.Stdout, containerReader, readsize)
656 _, err = io.CopyN(runner.Stderr, containerReader, readsize)
661 runner.CrunchLog.Printf("error reading docker logs: %v", err)
664 err = runner.Stdout.Close()
666 runner.CrunchLog.Printf("error closing stdout logs: %v", err)
669 err = runner.Stderr.Close()
671 runner.CrunchLog.Printf("error closing stderr logs: %v", err)
674 if runner.statReporter != nil {
675 runner.statReporter.Stop()
676 err = runner.statLogger.Close()
678 runner.CrunchLog.Printf("error closing crunchstat logs: %v", err)
683 func (runner *ContainerRunner) stopHoststat() error {
684 if runner.hoststatReporter == nil {
687 runner.hoststatReporter.Stop()
688 err := runner.hoststatLogger.Close()
690 return fmt.Errorf("error closing hoststat logs: %v", err)
695 func (runner *ContainerRunner) startHoststat() {
696 runner.hoststatLogger = NewThrottledLogger(runner.NewLogWriter("hoststat"))
697 runner.hoststatReporter = &crunchstat.Reporter{
698 Logger: log.New(runner.hoststatLogger, "", 0),
699 CgroupRoot: runner.cgroupRoot,
700 PollPeriod: runner.statInterval,
702 runner.hoststatReporter.Start()
705 func (runner *ContainerRunner) startCrunchstat() {
706 runner.statLogger = NewThrottledLogger(runner.NewLogWriter("crunchstat"))
707 runner.statReporter = &crunchstat.Reporter{
708 CID: runner.ContainerID,
709 Logger: log.New(runner.statLogger, "", 0),
710 CgroupParent: runner.expectCgroupParent,
711 CgroupRoot: runner.cgroupRoot,
712 PollPeriod: runner.statInterval,
714 runner.statReporter.Start()
717 type infoCommand struct {
722 // LogHostInfo logs info about the current host, for debugging and
723 // accounting purposes. Although it's logged as "node-info", this is
724 // about the environment where crunch-run is actually running, which
725 // might differ from what's described in the node record (see
727 func (runner *ContainerRunner) LogHostInfo() (err error) {
728 w := runner.NewLogWriter("node-info")
730 commands := []infoCommand{
732 label: "Host Information",
733 cmd: []string{"uname", "-a"},
736 label: "CPU Information",
737 cmd: []string{"cat", "/proc/cpuinfo"},
740 label: "Memory Information",
741 cmd: []string{"cat", "/proc/meminfo"},
745 cmd: []string{"df", "-m", "/", os.TempDir()},
748 label: "Disk INodes",
749 cmd: []string{"df", "-i", "/", os.TempDir()},
753 // Run commands with informational output to be logged.
754 for _, command := range commands {
755 fmt.Fprintln(w, command.label)
756 cmd := exec.Command(command.cmd[0], command.cmd[1:]...)
759 if err := cmd.Run(); err != nil {
760 err = fmt.Errorf("While running command %q: %v", command.cmd, err)
769 return fmt.Errorf("While closing node-info logs: %v", err)
774 // LogContainerRecord gets and saves the raw JSON container record from the API server
775 func (runner *ContainerRunner) LogContainerRecord() error {
776 logged, err := runner.logAPIResponse("container", "containers", map[string]interface{}{"filters": [][]string{{"uuid", "=", runner.Container.UUID}}}, nil)
777 if !logged && err == nil {
778 err = fmt.Errorf("error: no container record found for %s", runner.Container.UUID)
783 // LogNodeRecord logs arvados#node record corresponding to the current host.
784 func (runner *ContainerRunner) LogNodeRecord() error {
785 hostname := os.Getenv("SLURMD_NODENAME")
787 hostname, _ = os.Hostname()
789 _, err := runner.logAPIResponse("node", "nodes", map[string]interface{}{"filters": [][]string{{"hostname", "=", hostname}}}, func(resp interface{}) {
790 // The "info" field has admin-only info when obtained
791 // with a privileged token, and should not be logged.
792 node, ok := resp.(map[string]interface{})
800 func (runner *ContainerRunner) logAPIResponse(label, path string, params map[string]interface{}, munge func(interface{})) (logged bool, err error) {
802 ArvClient: runner.ArvClient,
803 UUID: runner.Container.UUID,
804 loggingStream: label,
805 writeCloser: runner.LogCollection.Open(label + ".json"),
808 reader, err := runner.ArvClient.CallRaw("GET", path, "", "", arvadosclient.Dict(params))
810 return false, fmt.Errorf("error getting %s record: %v", label, err)
814 dec := json.NewDecoder(reader)
816 var resp map[string]interface{}
817 if err = dec.Decode(&resp); err != nil {
818 return false, fmt.Errorf("error decoding %s list response: %v", label, err)
820 items, ok := resp["items"].([]interface{})
822 return false, fmt.Errorf("error decoding %s list response: no \"items\" key in API list response", label)
823 } else if len(items) < 1 {
829 // Re-encode it using indentation to improve readability
830 enc := json.NewEncoder(w)
831 enc.SetIndent("", " ")
832 if err = enc.Encode(items[0]); err != nil {
833 return false, fmt.Errorf("error logging %s record: %v", label, err)
837 return false, fmt.Errorf("error closing %s.json in log collection: %v", label, err)
842 // AttachStreams connects the docker container stdin, stdout and stderr logs
843 // to the Arvados logger which logs to Keep and the API server logs table.
844 func (runner *ContainerRunner) AttachStreams() (err error) {
846 runner.CrunchLog.Print("Attaching container streams")
848 // If stdin mount is provided, attach it to the docker container
849 var stdinRdr arvados.File
851 if stdinMnt, ok := runner.Container.Mounts["stdin"]; ok {
852 if stdinMnt.Kind == "collection" {
853 var stdinColl arvados.Collection
854 collId := stdinMnt.UUID
856 collId = stdinMnt.PortableDataHash
858 err = runner.ArvClient.Get("collections", collId, nil, &stdinColl)
860 return fmt.Errorf("While getting stding collection: %v", err)
863 stdinRdr, err = runner.Kc.ManifestFileReader(manifest.Manifest{Text: stdinColl.ManifestText}, stdinMnt.Path)
864 if os.IsNotExist(err) {
865 return fmt.Errorf("stdin collection path not found: %v", stdinMnt.Path)
866 } else if err != nil {
867 return fmt.Errorf("While getting stdin collection path %v: %v", stdinMnt.Path, err)
869 } else if stdinMnt.Kind == "json" {
870 stdinJson, err = json.Marshal(stdinMnt.Content)
872 return fmt.Errorf("While encoding stdin json data: %v", err)
877 stdinUsed := stdinRdr != nil || len(stdinJson) != 0
878 response, err := runner.Docker.ContainerAttach(context.TODO(), runner.ContainerID,
879 dockertypes.ContainerAttachOptions{Stream: true, Stdin: stdinUsed, Stdout: true, Stderr: true})
881 return fmt.Errorf("While attaching container stdout/stderr streams: %v", err)
884 runner.loggingDone = make(chan bool)
886 if stdoutMnt, ok := runner.Container.Mounts["stdout"]; ok {
887 stdoutFile, err := runner.getStdoutFile(stdoutMnt.Path)
891 runner.Stdout = stdoutFile
893 runner.Stdout = NewThrottledLogger(runner.NewLogWriter("stdout"))
896 if stderrMnt, ok := runner.Container.Mounts["stderr"]; ok {
897 stderrFile, err := runner.getStdoutFile(stderrMnt.Path)
901 runner.Stderr = stderrFile
903 runner.Stderr = NewThrottledLogger(runner.NewLogWriter("stderr"))
908 _, err := io.Copy(response.Conn, stdinRdr)
910 runner.CrunchLog.Print("While writing stdin collection to docker container %q", err)
914 response.CloseWrite()
916 } else if len(stdinJson) != 0 {
918 _, err := io.Copy(response.Conn, bytes.NewReader(stdinJson))
920 runner.CrunchLog.Print("While writing stdin json to docker container %q", err)
923 response.CloseWrite()
927 go runner.ProcessDockerAttach(response.Reader)
932 func (runner *ContainerRunner) getStdoutFile(mntPath string) (*os.File, error) {
933 stdoutPath := mntPath[len(runner.Container.OutputPath):]
934 index := strings.LastIndex(stdoutPath, "/")
936 subdirs := stdoutPath[:index]
938 st, err := os.Stat(runner.HostOutputDir)
940 return nil, fmt.Errorf("While Stat on temp dir: %v", err)
942 stdoutPath := filepath.Join(runner.HostOutputDir, subdirs)
943 err = os.MkdirAll(stdoutPath, st.Mode()|os.ModeSetgid|0777)
945 return nil, fmt.Errorf("While MkdirAll %q: %v", stdoutPath, err)
949 stdoutFile, err := os.Create(filepath.Join(runner.HostOutputDir, stdoutPath))
951 return nil, fmt.Errorf("While creating file %q: %v", stdoutPath, err)
954 return stdoutFile, nil
957 // CreateContainer creates the docker container.
958 func (runner *ContainerRunner) CreateContainer() error {
959 runner.CrunchLog.Print("Creating Docker container")
961 runner.ContainerConfig.Cmd = runner.Container.Command
962 if runner.Container.Cwd != "." {
963 runner.ContainerConfig.WorkingDir = runner.Container.Cwd
966 for k, v := range runner.Container.Environment {
967 runner.ContainerConfig.Env = append(runner.ContainerConfig.Env, k+"="+v)
970 runner.ContainerConfig.Volumes = runner.Volumes
972 maxRAM := int64(runner.Container.RuntimeConstraints.RAM)
973 runner.HostConfig = dockercontainer.HostConfig{
975 LogConfig: dockercontainer.LogConfig{
978 Resources: dockercontainer.Resources{
979 CgroupParent: runner.setCgroupParent,
980 NanoCPUs: int64(runner.Container.RuntimeConstraints.VCPUs) * 1000000000,
981 Memory: maxRAM, // RAM
982 MemorySwap: maxRAM, // RAM+swap
983 KernelMemory: maxRAM, // kernel portion
987 if wantAPI := runner.Container.RuntimeConstraints.API; wantAPI != nil && *wantAPI {
988 tok, err := runner.ContainerToken()
992 runner.ContainerConfig.Env = append(runner.ContainerConfig.Env,
993 "ARVADOS_API_TOKEN="+tok,
994 "ARVADOS_API_HOST="+os.Getenv("ARVADOS_API_HOST"),
995 "ARVADOS_API_HOST_INSECURE="+os.Getenv("ARVADOS_API_HOST_INSECURE"),
997 runner.HostConfig.NetworkMode = dockercontainer.NetworkMode(runner.networkMode)
999 if runner.enableNetwork == "always" {
1000 runner.HostConfig.NetworkMode = dockercontainer.NetworkMode(runner.networkMode)
1002 runner.HostConfig.NetworkMode = dockercontainer.NetworkMode("none")
1006 _, stdinUsed := runner.Container.Mounts["stdin"]
1007 runner.ContainerConfig.OpenStdin = stdinUsed
1008 runner.ContainerConfig.StdinOnce = stdinUsed
1009 runner.ContainerConfig.AttachStdin = stdinUsed
1010 runner.ContainerConfig.AttachStdout = true
1011 runner.ContainerConfig.AttachStderr = true
1013 createdBody, err := runner.Docker.ContainerCreate(context.TODO(), &runner.ContainerConfig, &runner.HostConfig, nil, runner.Container.UUID)
1015 return fmt.Errorf("While creating container: %v", err)
1018 runner.ContainerID = createdBody.ID
1020 return runner.AttachStreams()
1023 // StartContainer starts the docker container created by CreateContainer.
1024 func (runner *ContainerRunner) StartContainer() error {
1025 runner.CrunchLog.Printf("Starting Docker container id '%s'", runner.ContainerID)
1026 runner.cStateLock.Lock()
1027 defer runner.cStateLock.Unlock()
1028 if runner.cCancelled {
1031 err := runner.Docker.ContainerStart(context.TODO(), runner.ContainerID,
1032 dockertypes.ContainerStartOptions{})
1035 if m, e := regexp.MatchString("(?ms).*(exec|System error).*(no such file or directory|file not found).*", err.Error()); m && e == nil {
1036 advice = fmt.Sprintf("\nPossible causes: command %q is missing, the interpreter given in #! is missing, or script has Windows line endings.", runner.Container.Command[0])
1038 return fmt.Errorf("could not start container: %v%s", err, advice)
1043 // WaitFinish waits for the container to terminate, capture the exit code, and
1044 // close the stdout/stderr logging.
1045 func (runner *ContainerRunner) WaitFinish() error {
1046 runner.CrunchLog.Print("Waiting for container to finish")
1048 waitOk, waitErr := runner.Docker.ContainerWait(context.TODO(), runner.ContainerID, dockercontainer.WaitConditionNotRunning)
1049 arvMountExit := runner.ArvMountExit
1052 case waitBody := <-waitOk:
1053 runner.CrunchLog.Printf("Container exited with code: %v", waitBody.StatusCode)
1054 code := int(waitBody.StatusCode)
1055 runner.ExitCode = &code
1057 // wait for stdout/stderr to complete
1058 <-runner.loggingDone
1061 case err := <-waitErr:
1062 return fmt.Errorf("container wait: %v", err)
1064 case <-arvMountExit:
1065 runner.CrunchLog.Printf("arv-mount exited while container is still running. Stopping container.")
1067 // arvMountExit will always be ready now that
1068 // it's closed, but that doesn't interest us.
1074 var ErrNotInOutputDir = fmt.Errorf("Must point to path within the output directory")
1076 func (runner *ContainerRunner) derefOutputSymlink(path string, startinfo os.FileInfo) (tgt string, readlinktgt string, info os.FileInfo, err error) {
1077 // Follow symlinks if necessary
1082 for followed := 0; info.Mode()&os.ModeSymlink != 0; followed++ {
1083 if followed >= limitFollowSymlinks {
1084 // Got stuck in a loop or just a pathological number of links, give up.
1085 err = fmt.Errorf("Followed more than %v symlinks from path %q", limitFollowSymlinks, path)
1089 readlinktgt, err = os.Readlink(nextlink)
1095 if !strings.HasPrefix(tgt, "/") {
1096 // Relative symlink, resolve it to host path
1097 tgt = filepath.Join(filepath.Dir(path), tgt)
1099 if strings.HasPrefix(tgt, runner.Container.OutputPath+"/") && !strings.HasPrefix(tgt, runner.HostOutputDir+"/") {
1100 // Absolute symlink to container output path, adjust it to host output path.
1101 tgt = filepath.Join(runner.HostOutputDir, tgt[len(runner.Container.OutputPath):])
1103 if !strings.HasPrefix(tgt, runner.HostOutputDir+"/") {
1104 // After dereferencing, symlink target must either be
1105 // within output directory, or must point to a
1106 // collection mount.
1107 err = ErrNotInOutputDir
1111 info, err = os.Lstat(tgt)
1114 err = fmt.Errorf("Symlink in output %q points to invalid location %q: %v",
1115 path[len(runner.HostOutputDir):], readlinktgt, err)
1125 var limitFollowSymlinks = 10
1127 // UploadFile uploads files within the output directory, with special handling
1128 // for symlinks. If the symlink leads to a keep mount, copy the manifest text
1129 // from the keep mount into the output manifestText. Ensure that whether
1130 // symlinks are relative or absolute, every symlink target (even targets that
1131 // are symlinks themselves) must point to a path in either the output directory
1132 // or a collection mount.
1134 // Assumes initial value of "path" is absolute, and located within runner.HostOutputDir.
1135 func (runner *ContainerRunner) UploadOutputFile(
1140 walkUpload *WalkUpload,
1141 relocateFrom string,
1143 followed int) (manifestText string, err error) {
1149 if info.Mode().IsDir() {
1150 // if empty, need to create a .keep file
1151 dir, direrr := os.Open(path)
1156 names, eof := dir.Readdirnames(1)
1157 if len(names) == 0 && eof == io.EOF && path != runner.HostOutputDir {
1158 containerPath := runner.OutputPath + path[len(runner.HostOutputDir):]
1159 for _, bind := range binds {
1160 mnt := runner.Container.Mounts[bind]
1161 // Check if there is a bind for this
1162 // directory, in which case assume we don't need .keep
1163 if (containerPath == bind || strings.HasPrefix(containerPath, bind+"/")) && mnt.PortableDataHash != "d41d8cd98f00b204e9800998ecf8427e+0" {
1167 outputSuffix := path[len(runner.HostOutputDir)+1:]
1168 return fmt.Sprintf("./%v d41d8cd98f00b204e9800998ecf8427e+0 0:0:.keep\n", outputSuffix), nil
1173 if followed >= limitFollowSymlinks {
1174 // Got stuck in a loop or just a pathological number of
1175 // directory links, give up.
1176 err = fmt.Errorf("Followed more than %v symlinks from path %q", limitFollowSymlinks, path)
1180 // "path" is the actual path we are visiting
1181 // "tgt" is the target of "path" (a non-symlink) after following symlinks
1182 // "relocated" is the path in the output manifest where the file should be placed,
1183 // but has HostOutputDir as a prefix.
1185 // The destination path in the output manifest may need to be
1186 // logically relocated to some other path in order to appear
1187 // in the correct location as a result of following a symlink.
1188 // Remove the relocateFrom prefix and replace it with
1190 relocated := relocateTo + path[len(relocateFrom):]
1192 tgt, readlinktgt, info, derefErr := runner.derefOutputSymlink(path, info)
1193 if derefErr != nil && derefErr != ErrNotInOutputDir {
1197 // go through mounts and try reverse map to collection reference
1198 for _, bind := range binds {
1199 mnt := runner.Container.Mounts[bind]
1200 if (tgt == bind || strings.HasPrefix(tgt, bind+"/")) && !mnt.Writable {
1201 // get path relative to bind
1202 targetSuffix := tgt[len(bind):]
1204 // Copy mount and adjust the path to add path relative to the bind
1205 adjustedMount := mnt
1206 adjustedMount.Path = filepath.Join(adjustedMount.Path, targetSuffix)
1208 // Terminates in this keep mount, so add the
1209 // manifest text at appropriate location.
1210 outputSuffix := relocated[len(runner.HostOutputDir):]
1211 manifestText, err = runner.getCollectionManifestForPath(adjustedMount, outputSuffix)
1216 // If target is not a collection mount, it must be located within the
1217 // output directory, otherwise it is an error.
1218 if derefErr == ErrNotInOutputDir {
1219 err = fmt.Errorf("Symlink in output %q points to invalid location %q, must point to path within the output directory.",
1220 path[len(runner.HostOutputDir):], readlinktgt)
1224 if info.Mode().IsRegular() {
1225 return "", walkUpload.UploadFile(relocated, tgt)
1228 if info.Mode().IsDir() {
1229 // Symlink leads to directory. Walk() doesn't follow
1230 // directory symlinks, so we walk the target directory
1231 // instead. Within the walk, file paths are relocated
1232 // so they appear under the original symlink path.
1233 err = filepath.Walk(tgt, func(walkpath string, walkinfo os.FileInfo, walkerr error) error {
1235 m, walkerr = runner.UploadOutputFile(walkpath, walkinfo, walkerr,
1236 binds, walkUpload, tgt, relocated, followed+1)
1238 manifestText = manifestText + m
1248 // HandleOutput sets the output, unmounts the FUSE mount, and deletes temporary directories
1249 func (runner *ContainerRunner) CaptureOutput() error {
1250 if wantAPI := runner.Container.RuntimeConstraints.API; wantAPI != nil && *wantAPI {
1251 // Output may have been set directly by the container, so
1252 // refresh the container record to check.
1253 err := runner.ArvClient.Get("containers", runner.Container.UUID,
1254 nil, &runner.Container)
1258 if runner.Container.Output != "" {
1259 // Container output is already set.
1260 runner.OutputPDH = &runner.Container.Output
1265 if runner.HostOutputDir == "" {
1269 _, err := os.Stat(runner.HostOutputDir)
1271 return fmt.Errorf("While checking host output path: %v", err)
1274 // Pre-populate output from the configured mount points
1276 for bind, mnt := range runner.Container.Mounts {
1277 if mnt.Kind == "collection" {
1278 binds = append(binds, bind)
1283 // Delete secret mounts so they don't get saved to the output collection.
1284 for bind := range runner.SecretMounts {
1285 if strings.HasPrefix(bind, runner.Container.OutputPath+"/") {
1286 err = os.Remove(runner.HostOutputDir + bind[len(runner.Container.OutputPath):])
1288 return fmt.Errorf("Unable to remove secret mount: %v", err)
1293 var manifestText string
1295 collectionMetafile := fmt.Sprintf("%s/.arvados#collection", runner.HostOutputDir)
1296 _, err = os.Stat(collectionMetafile)
1298 // Regular directory
1300 cw := CollectionWriter{0, runner.Kc, nil, nil, sync.Mutex{}}
1301 walkUpload := cw.BeginUpload(runner.HostOutputDir, runner.CrunchLog.Logger)
1304 err = filepath.Walk(runner.HostOutputDir, func(path string, info os.FileInfo, err error) error {
1305 m, err = runner.UploadOutputFile(path, info, err, binds, walkUpload, "", "", 0)
1307 manifestText = manifestText + m
1312 cw.EndUpload(walkUpload)
1315 return fmt.Errorf("While uploading output files: %v", err)
1318 m, err = cw.ManifestText()
1319 manifestText = manifestText + m
1321 return fmt.Errorf("While uploading output files: %v", err)
1324 // FUSE mount directory
1325 file, openerr := os.Open(collectionMetafile)
1327 return fmt.Errorf("While opening FUSE metafile: %v", err)
1331 var rec arvados.Collection
1332 err = json.NewDecoder(file).Decode(&rec)
1334 return fmt.Errorf("While reading FUSE metafile: %v", err)
1336 manifestText = rec.ManifestText
1339 for _, bind := range binds {
1340 mnt := runner.Container.Mounts[bind]
1342 bindSuffix := strings.TrimPrefix(bind, runner.Container.OutputPath)
1344 if bindSuffix == bind || len(bindSuffix) <= 0 {
1345 // either does not start with OutputPath or is OutputPath itself
1349 if mnt.ExcludeFromOutput == true || mnt.Writable {
1353 // append to manifest_text
1354 m, err := runner.getCollectionManifestForPath(mnt, bindSuffix)
1359 manifestText = manifestText + m
1363 var response arvados.Collection
1364 manifest := manifest.Manifest{Text: manifestText}
1365 manifestText = manifest.Extract(".", ".").Text
1366 err = runner.ArvClient.Create("collections",
1368 "ensure_unique_name": true,
1369 "collection": arvadosclient.Dict{
1371 "name": "output for " + runner.Container.UUID,
1372 "manifest_text": manifestText}},
1375 return fmt.Errorf("While creating output collection: %v", err)
1377 runner.OutputPDH = &response.PortableDataHash
1381 var outputCollections = make(map[string]arvados.Collection)
1383 // Fetch the collection for the mnt.PortableDataHash
1384 // Return the manifest_text fragment corresponding to the specified mnt.Path
1385 // after making any required updates.
1387 // If mnt.Path is not specified,
1388 // return the entire manifest_text after replacing any "." with bindSuffix
1389 // If mnt.Path corresponds to one stream,
1390 // return the manifest_text for that stream after replacing that stream name with bindSuffix
1391 // Otherwise, check if a filename in any one stream is being sought. Return the manifest_text
1392 // for that stream after replacing stream name with bindSuffix minus the last word
1393 // and the file name with last word of the bindSuffix
1394 // Allowed path examples:
1396 // "path":"/subdir1"
1397 // "path":"/subdir1/subdir2"
1398 // "path":"/subdir/filename" etc
1399 func (runner *ContainerRunner) getCollectionManifestForPath(mnt arvados.Mount, bindSuffix string) (string, error) {
1400 collection := outputCollections[mnt.PortableDataHash]
1401 if collection.PortableDataHash == "" {
1402 err := runner.ArvClient.Get("collections", mnt.PortableDataHash, nil, &collection)
1404 return "", fmt.Errorf("While getting collection for %v: %v", mnt.PortableDataHash, err)
1406 outputCollections[mnt.PortableDataHash] = collection
1409 if collection.ManifestText == "" {
1410 runner.CrunchLog.Printf("No manifest text for collection %v", collection.PortableDataHash)
1414 mft := manifest.Manifest{Text: collection.ManifestText}
1415 extracted := mft.Extract(mnt.Path, bindSuffix)
1416 if extracted.Err != nil {
1417 return "", fmt.Errorf("Error parsing manifest for %v: %v", mnt.PortableDataHash, extracted.Err.Error())
1419 return extracted.Text, nil
1422 func (runner *ContainerRunner) CleanupDirs() {
1423 if runner.ArvMount != nil {
1425 umount := exec.Command("arv-mount", fmt.Sprintf("--unmount-timeout=%d", delay), "--unmount", runner.ArvMountPoint)
1426 umount.Stdout = runner.CrunchLog
1427 umount.Stderr = runner.CrunchLog
1428 runner.CrunchLog.Printf("Running %v", umount.Args)
1429 umnterr := umount.Start()
1432 runner.CrunchLog.Printf("Error unmounting: %v", umnterr)
1434 // If arv-mount --unmount gets stuck for any reason, we
1435 // don't want to wait for it forever. Do Wait() in a goroutine
1436 // so it doesn't block crunch-run.
1437 umountExit := make(chan error)
1439 mnterr := umount.Wait()
1441 runner.CrunchLog.Printf("Error unmounting: %v", mnterr)
1443 umountExit <- mnterr
1446 for again := true; again; {
1452 case <-runner.ArvMountExit:
1454 case <-time.After(time.Duration((delay + 1) * int64(time.Second))):
1455 runner.CrunchLog.Printf("Timed out waiting for unmount")
1457 umount.Process.Kill()
1459 runner.ArvMount.Process.Kill()
1465 if runner.ArvMountPoint != "" {
1466 if rmerr := os.Remove(runner.ArvMountPoint); rmerr != nil {
1467 runner.CrunchLog.Printf("While cleaning up arv-mount directory %s: %v", runner.ArvMountPoint, rmerr)
1471 if rmerr := os.RemoveAll(runner.parentTemp); rmerr != nil {
1472 runner.CrunchLog.Printf("While cleaning up temporary directory %s: %v", runner.parentTemp, rmerr)
1476 // CommitLogs posts the collection containing the final container logs.
1477 func (runner *ContainerRunner) CommitLogs() error {
1479 // Hold cStateLock to prevent races on CrunchLog (e.g., stop()).
1480 runner.cStateLock.Lock()
1481 defer runner.cStateLock.Unlock()
1483 runner.CrunchLog.Print(runner.finalState)
1485 if runner.arvMountLog != nil {
1486 runner.arvMountLog.Close()
1488 runner.CrunchLog.Close()
1490 // Closing CrunchLog above allows them to be committed to Keep at this
1491 // point, but re-open crunch log with ArvClient in case there are any
1492 // other further errors (such as failing to write the log to Keep!)
1493 // while shutting down
1494 runner.CrunchLog = NewThrottledLogger(&ArvLogWriter{ArvClient: runner.ArvClient,
1495 UUID: runner.Container.UUID, loggingStream: "crunch-run", writeCloser: nil})
1496 runner.CrunchLog.Immediate = log.New(os.Stderr, runner.Container.UUID+" ", 0)
1499 if runner.LogsPDH != nil {
1500 // If we have already assigned something to LogsPDH,
1501 // we must be closing the re-opened log, which won't
1502 // end up getting attached to the container record and
1503 // therefore doesn't need to be saved as a collection
1504 // -- it exists only to send logs to other channels.
1508 mt, err := runner.LogCollection.ManifestText()
1510 return fmt.Errorf("While creating log manifest: %v", err)
1513 var response arvados.Collection
1514 err = runner.ArvClient.Create("collections",
1516 "ensure_unique_name": true,
1517 "collection": arvadosclient.Dict{
1519 "name": "logs for " + runner.Container.UUID,
1520 "manifest_text": mt}},
1523 return fmt.Errorf("While creating log collection: %v", err)
1525 runner.LogsPDH = &response.PortableDataHash
1529 // UpdateContainerRunning updates the container state to "Running"
1530 func (runner *ContainerRunner) UpdateContainerRunning() error {
1531 runner.cStateLock.Lock()
1532 defer runner.cStateLock.Unlock()
1533 if runner.cCancelled {
1536 return runner.ArvClient.Update("containers", runner.Container.UUID,
1537 arvadosclient.Dict{"container": arvadosclient.Dict{"state": "Running"}}, nil)
1540 // ContainerToken returns the api_token the container (and any
1541 // arv-mount processes) are allowed to use.
1542 func (runner *ContainerRunner) ContainerToken() (string, error) {
1543 if runner.token != "" {
1544 return runner.token, nil
1547 var auth arvados.APIClientAuthorization
1548 err := runner.ArvClient.Call("GET", "containers", runner.Container.UUID, "auth", nil, &auth)
1552 runner.token = auth.APIToken
1553 return runner.token, nil
1556 // UpdateContainerComplete updates the container record state on API
1557 // server to "Complete" or "Cancelled"
1558 func (runner *ContainerRunner) UpdateContainerFinal() error {
1559 update := arvadosclient.Dict{}
1560 update["state"] = runner.finalState
1561 if runner.LogsPDH != nil {
1562 update["log"] = *runner.LogsPDH
1564 if runner.finalState == "Complete" {
1565 if runner.ExitCode != nil {
1566 update["exit_code"] = *runner.ExitCode
1568 if runner.OutputPDH != nil {
1569 update["output"] = *runner.OutputPDH
1572 return runner.ArvClient.Update("containers", runner.Container.UUID, arvadosclient.Dict{"container": update}, nil)
1575 // IsCancelled returns the value of Cancelled, with goroutine safety.
1576 func (runner *ContainerRunner) IsCancelled() bool {
1577 runner.cStateLock.Lock()
1578 defer runner.cStateLock.Unlock()
1579 return runner.cCancelled
1582 // NewArvLogWriter creates an ArvLogWriter
1583 func (runner *ContainerRunner) NewArvLogWriter(name string) io.WriteCloser {
1584 return &ArvLogWriter{
1585 ArvClient: runner.ArvClient,
1586 UUID: runner.Container.UUID,
1587 loggingStream: name,
1588 writeCloser: runner.LogCollection.Open(name + ".txt")}
1591 // Run the full container lifecycle.
1592 func (runner *ContainerRunner) Run() (err error) {
1593 runner.CrunchLog.Printf("crunch-run %s started", version)
1594 runner.CrunchLog.Printf("Executing container '%s'", runner.Container.UUID)
1596 hostname, hosterr := os.Hostname()
1598 runner.CrunchLog.Printf("Error getting hostname '%v'", hosterr)
1600 runner.CrunchLog.Printf("Executing on host '%s'", hostname)
1603 runner.finalState = "Queued"
1606 runner.CleanupDirs()
1608 runner.CrunchLog.Printf("crunch-run finished")
1609 runner.CrunchLog.Close()
1613 // checkErr prints e (unless it's nil) and sets err to
1614 // e (unless err is already non-nil). Thus, if err
1615 // hasn't already been assigned when Run() returns,
1616 // this cleanup func will cause Run() to return the
1617 // first non-nil error that is passed to checkErr().
1618 checkErr := func(e error) {
1622 runner.CrunchLog.Print(e)
1626 if runner.finalState == "Complete" {
1627 // There was an error in the finalization.
1628 runner.finalState = "Cancelled"
1632 // Log the error encountered in Run(), if any
1635 if runner.finalState == "Queued" {
1636 runner.UpdateContainerFinal()
1640 if runner.IsCancelled() {
1641 runner.finalState = "Cancelled"
1642 // but don't return yet -- we still want to
1643 // capture partial output and write logs
1646 checkErr(runner.CaptureOutput())
1647 checkErr(runner.stopHoststat())
1648 checkErr(runner.CommitLogs())
1649 checkErr(runner.UpdateContainerFinal())
1652 err = runner.fetchContainerRecord()
1656 runner.setupSignals()
1657 runner.startHoststat()
1659 // check for and/or load image
1660 err = runner.LoadImage()
1662 if !runner.checkBrokenNode(err) {
1663 // Failed to load image but not due to a "broken node"
1664 // condition, probably user error.
1665 runner.finalState = "Cancelled"
1667 err = fmt.Errorf("While loading container image: %v", err)
1671 // set up FUSE mount and binds
1672 err = runner.SetupMounts()
1674 runner.finalState = "Cancelled"
1675 err = fmt.Errorf("While setting up mounts: %v", err)
1679 err = runner.CreateContainer()
1683 err = runner.LogHostInfo()
1687 err = runner.LogNodeRecord()
1691 err = runner.LogContainerRecord()
1696 if runner.IsCancelled() {
1700 err = runner.UpdateContainerRunning()
1704 runner.finalState = "Cancelled"
1706 runner.startCrunchstat()
1708 err = runner.StartContainer()
1710 runner.checkBrokenNode(err)
1714 err = runner.WaitFinish()
1715 if err == nil && !runner.IsCancelled() {
1716 runner.finalState = "Complete"
1721 // Fetch the current container record (uuid = runner.Container.UUID)
1722 // into runner.Container.
1723 func (runner *ContainerRunner) fetchContainerRecord() error {
1724 reader, err := runner.ArvClient.CallRaw("GET", "containers", runner.Container.UUID, "", nil)
1726 return fmt.Errorf("error fetching container record: %v", err)
1728 defer reader.Close()
1730 dec := json.NewDecoder(reader)
1732 err = dec.Decode(&runner.Container)
1734 return fmt.Errorf("error decoding container record: %v", err)
1738 SecretMounts map[string]arvados.Mount `json:"secret_mounts"`
1741 err = runner.ArvClient.Call("GET", "containers", runner.Container.UUID, "secret_mounts", nil, &sm)
1743 if apierr, ok := err.(arvadosclient.APIServerError); !ok || apierr.HttpStatusCode != 404 {
1744 return fmt.Errorf("error fetching secret_mounts: %v", err)
1746 // ok && apierr.HttpStatusCode == 404, which means
1747 // secret_mounts isn't supported by this API server.
1749 runner.SecretMounts = sm.SecretMounts
1754 // NewContainerRunner creates a new container runner.
1755 func NewContainerRunner(api IArvadosClient,
1757 docker ThinDockerClient,
1758 containerUUID string) *ContainerRunner {
1760 cr := &ContainerRunner{ArvClient: api, Kc: kc, Docker: docker}
1761 cr.NewLogWriter = cr.NewArvLogWriter
1762 cr.RunArvMount = cr.ArvMountCmd
1763 cr.MkTempDir = ioutil.TempDir
1764 cr.LogCollection = &CollectionWriter{0, kc, nil, nil, sync.Mutex{}}
1765 cr.Container.UUID = containerUUID
1766 cr.CrunchLog = NewThrottledLogger(cr.NewLogWriter("crunch-run"))
1767 cr.CrunchLog.Immediate = log.New(os.Stderr, containerUUID+" ", 0)
1769 loadLogThrottleParams(api)
1775 statInterval := flag.Duration("crunchstat-interval", 10*time.Second, "sampling period for periodic resource usage reporting")
1776 cgroupRoot := flag.String("cgroup-root", "/sys/fs/cgroup", "path to sysfs cgroup tree")
1777 cgroupParent := flag.String("cgroup-parent", "docker", "name of container's parent cgroup (ignored if -cgroup-parent-subsystem is used)")
1778 cgroupParentSubsystem := flag.String("cgroup-parent-subsystem", "", "use current cgroup for given subsystem as parent cgroup for container")
1779 caCertsPath := flag.String("ca-certs", "", "Path to TLS root certificates")
1780 enableNetwork := flag.String("container-enable-networking", "default",
1781 `Specify if networking should be enabled for container. One of 'default', 'always':
1782 default: only enable networking if container requests it.
1783 always: containers always have networking enabled
1785 networkMode := flag.String("container-network-mode", "default",
1786 `Set networking mode for container. Corresponds to Docker network mode (--net).
1788 memprofile := flag.String("memprofile", "", "write memory profile to `file` after running container")
1789 getVersion := flag.Bool("version", false, "Print version information and exit.")
1792 // Print version information if requested
1794 fmt.Printf("crunch-run %s\n", version)
1798 log.Printf("crunch-run %s started", version)
1800 containerId := flag.Arg(0)
1802 if *caCertsPath != "" {
1803 arvadosclient.CertFiles = []string{*caCertsPath}
1806 api, err := arvadosclient.MakeArvadosClient()
1808 log.Fatalf("%s: %v", containerId, err)
1812 kc, kcerr := keepclient.MakeKeepClient(api)
1814 log.Fatalf("%s: %v", containerId, kcerr)
1816 kc.BlockCache = &keepclient.BlockCache{MaxBlocks: 2}
1819 // API version 1.21 corresponds to Docker 1.9, which is currently the
1820 // minimum version we want to support.
1821 docker, dockererr := dockerclient.NewClient(dockerclient.DefaultDockerHost, "1.21", nil, nil)
1823 cr := NewContainerRunner(api, kc, docker, containerId)
1824 if dockererr != nil {
1825 cr.CrunchLog.Printf("%s: %v", containerId, dockererr)
1826 cr.checkBrokenNode(dockererr)
1827 cr.CrunchLog.Close()
1831 parentTemp, tmperr := cr.MkTempDir("", "crunch-run."+containerId+".")
1833 log.Fatalf("%s: %v", containerId, tmperr)
1836 cr.parentTemp = parentTemp
1837 cr.statInterval = *statInterval
1838 cr.cgroupRoot = *cgroupRoot
1839 cr.expectCgroupParent = *cgroupParent
1840 cr.enableNetwork = *enableNetwork
1841 cr.networkMode = *networkMode
1842 if *cgroupParentSubsystem != "" {
1843 p := findCgroup(*cgroupParentSubsystem)
1844 cr.setCgroupParent = p
1845 cr.expectCgroupParent = p
1850 if *memprofile != "" {
1851 f, err := os.Create(*memprofile)
1853 log.Printf("could not create memory profile: ", err)
1855 runtime.GC() // get up-to-date statistics
1856 if err := pprof.WriteHeapProfile(f); err != nil {
1857 log.Printf("could not write memory profile: ", err)
1859 closeerr := f.Close()
1860 if closeerr != nil {
1861 log.Printf("closing memprofile file: ", err)
1866 log.Fatalf("%s: %v", containerId, runerr)