1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
33 "git.arvados.org/arvados.git/lib/cmd"
34 "git.arvados.org/arvados.git/lib/crunchstat"
35 "git.arvados.org/arvados.git/sdk/go/arvados"
36 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
37 "git.arvados.org/arvados.git/sdk/go/keepclient"
38 "git.arvados.org/arvados.git/sdk/go/manifest"
43 var Command = command{}
45 // ConfigData contains environment variables and (when needed) cluster
46 // configuration, passed from dispatchcloud to crunch-run on stdin.
47 type ConfigData struct {
50 Cluster *arvados.Cluster
53 // IArvadosClient is the minimal Arvados API methods used by crunch-run.
54 type IArvadosClient interface {
55 Create(resourceType string, parameters arvadosclient.Dict, output interface{}) error
56 Get(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) error
57 Update(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) error
58 Call(method, resourceType, uuid, action string, parameters arvadosclient.Dict, output interface{}) error
59 CallRaw(method string, resourceType string, uuid string, action string, parameters arvadosclient.Dict) (reader io.ReadCloser, err error)
60 Discovery(key string) (interface{}, error)
63 // ErrCancelled is the error returned when the container is cancelled.
64 var ErrCancelled = errors.New("Cancelled")
66 // IKeepClient is the minimal Keep API methods used by crunch-run.
67 type IKeepClient interface {
68 BlockWrite(context.Context, arvados.BlockWriteOptions) (arvados.BlockWriteResponse, error)
69 ReadAt(locator string, p []byte, off int) (int, error)
70 ManifestFileReader(m manifest.Manifest, filename string) (arvados.File, error)
71 LocalLocator(locator string) (string, error)
73 SetStorageClasses(sc []string)
76 // NewLogWriter is a factory function to create a new log writer.
77 type NewLogWriter func(name string) (io.WriteCloser, error)
79 type RunArvMount func(cmdline []string, tok string) (*exec.Cmd, error)
81 type MkTempDir func(string, string) (string, error)
83 type PsProcess interface {
84 CmdlineSlice() ([]string, error)
87 // ContainerRunner is the main stateful struct used for a single execution of a
89 type ContainerRunner struct {
90 executor containerExecutor
91 executorStdin io.Closer
92 executorStdout io.Closer
93 executorStderr io.Closer
95 // Dispatcher client is initialized with the Dispatcher token.
96 // This is a privileged token used to manage container status
99 // We have both dispatcherClient and DispatcherArvClient
100 // because there are two different incompatible Arvados Go
101 // SDKs and we have to use both (hopefully this gets fixed in
103 dispatcherClient *arvados.Client
104 DispatcherArvClient IArvadosClient
105 DispatcherKeepClient IKeepClient
107 // Container client is initialized with the Container token
108 // This token controls the permissions of the container, and
109 // must be used for operations such as reading collections.
111 // Same comment as above applies to
112 // containerClient/ContainerArvClient.
113 containerClient *arvados.Client
114 ContainerArvClient IArvadosClient
115 ContainerKeepClient IKeepClient
117 Container arvados.Container
120 NewLogWriter NewLogWriter
121 CrunchLog *ThrottledLogger
124 LogCollection arvados.CollectionFileSystem
126 RunArvMount RunArvMount
131 Volumes map[string]struct{}
133 SigChan chan os.Signal
134 ArvMountExit chan error
135 SecretMounts map[string]arvados.Mount
136 MkArvClient func(token string) (IArvadosClient, IKeepClient, *arvados.Client, error)
140 statLogger io.WriteCloser
141 statReporter *crunchstat.Reporter
142 hoststatLogger io.WriteCloser
143 hoststatReporter *crunchstat.Reporter
144 statInterval time.Duration
146 // What we expect the container's cgroup parent to be.
147 expectCgroupParent string
148 // What we tell docker to use as the container's cgroup
149 // parent. Note: Ideally we would use the same field for both
150 // expectCgroupParent and setCgroupParent, and just make it
151 // default to "docker". However, when using docker < 1.10 with
152 // systemd, specifying a non-empty cgroup parent (even the
153 // default value "docker") hits a docker bug
154 // (https://github.com/docker/docker/issues/17126). Using two
155 // separate fields makes it possible to use the "expect cgroup
156 // parent to be X" feature even on sites where the "specify
157 // cgroup parent" feature breaks.
158 setCgroupParent string
160 cStateLock sync.Mutex
161 cCancelled bool // StopContainer() invoked
163 enableMemoryLimit bool
164 enableNetwork string // one of "default" or "always"
165 networkMode string // "none", "host", or "" -- passed through to executor
166 arvMountLog *ThrottledLogger
168 containerWatchdogInterval time.Duration
173 // setupSignals sets up signal handling to gracefully terminate the
174 // underlying container and update state when receiving a TERM, INT or
176 func (runner *ContainerRunner) setupSignals() {
177 runner.SigChan = make(chan os.Signal, 1)
178 signal.Notify(runner.SigChan, syscall.SIGTERM)
179 signal.Notify(runner.SigChan, syscall.SIGINT)
180 signal.Notify(runner.SigChan, syscall.SIGQUIT)
182 go func(sig chan os.Signal) {
189 // stop the underlying container.
190 func (runner *ContainerRunner) stop(sig os.Signal) {
191 runner.cStateLock.Lock()
192 defer runner.cStateLock.Unlock()
194 runner.CrunchLog.Printf("caught signal: %v", sig)
196 runner.cCancelled = true
197 runner.CrunchLog.Printf("stopping container")
198 err := runner.executor.Stop()
200 runner.CrunchLog.Printf("error stopping container: %s", err)
204 var errorBlacklist = []string{
205 "(?ms).*[Cc]annot connect to the Docker daemon.*",
206 "(?ms).*oci runtime error.*starting container process.*container init.*mounting.*to rootfs.*no such file or directory.*",
207 "(?ms).*grpc: the connection is unavailable.*",
209 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)")
211 func (runner *ContainerRunner) runBrokenNodeHook() {
212 if *brokenNodeHook == "" {
213 path := filepath.Join(lockdir, brokenfile)
214 runner.CrunchLog.Printf("Writing %s to mark node as broken", path)
215 f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0700)
217 runner.CrunchLog.Printf("Error writing %s: %s", path, err)
222 runner.CrunchLog.Printf("Running broken node hook %q", *brokenNodeHook)
224 c := exec.Command(*brokenNodeHook)
225 c.Stdout = runner.CrunchLog
226 c.Stderr = runner.CrunchLog
229 runner.CrunchLog.Printf("Error running broken node hook: %v", err)
234 func (runner *ContainerRunner) checkBrokenNode(goterr error) bool {
235 for _, d := range errorBlacklist {
236 if m, e := regexp.MatchString(d, goterr.Error()); m && e == nil {
237 runner.CrunchLog.Printf("Error suggests node is unable to run containers: %v", goterr)
238 runner.runBrokenNodeHook()
245 // LoadImage determines the docker image id from the container record and
246 // checks if it is available in the local Docker image store. If not, it loads
247 // the image from Keep.
248 func (runner *ContainerRunner) LoadImage() (string, error) {
249 runner.CrunchLog.Printf("Fetching Docker image from collection '%s'", runner.Container.ContainerImage)
251 d, err := os.Open(runner.ArvMountPoint + "/by_id/" + runner.Container.ContainerImage)
256 allfiles, err := d.Readdirnames(-1)
260 var tarfiles []string
261 for _, fnm := range allfiles {
262 if strings.HasSuffix(fnm, ".tar") {
263 tarfiles = append(tarfiles, fnm)
266 if len(tarfiles) == 0 {
267 return "", fmt.Errorf("image collection does not include a .tar image file")
269 if len(tarfiles) > 1 {
270 return "", fmt.Errorf("cannot choose from multiple tar files in image collection: %v", tarfiles)
272 imageID := tarfiles[0][:len(tarfiles[0])-4]
273 imageTarballPath := runner.ArvMountPoint + "/by_id/" + runner.Container.ContainerImage + "/" + imageID + ".tar"
274 runner.CrunchLog.Printf("Using Docker image id %q", imageID)
276 runner.CrunchLog.Print("Loading Docker image from keep")
277 err = runner.executor.LoadImage(imageID, imageTarballPath, runner.Container, runner.ArvMountPoint,
278 runner.containerClient)
286 func (runner *ContainerRunner) ArvMountCmd(cmdline []string, token string) (c *exec.Cmd, err error) {
287 c = exec.Command(cmdline[0], cmdline[1:]...)
289 // Copy our environment, but override ARVADOS_API_TOKEN with
290 // the container auth token.
292 for _, s := range os.Environ() {
293 if !strings.HasPrefix(s, "ARVADOS_API_TOKEN=") {
294 c.Env = append(c.Env, s)
297 c.Env = append(c.Env, "ARVADOS_API_TOKEN="+token)
299 w, err := runner.NewLogWriter("arv-mount")
303 runner.arvMountLog = NewThrottledLogger(w)
304 scanner := logScanner{
307 "Block not found error",
308 "Unhandled exception during FUSE operation",
310 ReportFunc: runner.reportArvMountWarning,
312 c.Stdout = runner.arvMountLog
313 c.Stderr = io.MultiWriter(runner.arvMountLog, os.Stderr, &scanner)
315 runner.CrunchLog.Printf("Running %v", c.Args)
322 statReadme := make(chan bool)
323 runner.ArvMountExit = make(chan error)
328 time.Sleep(100 * time.Millisecond)
329 _, err = os.Stat(fmt.Sprintf("%s/by_id/README", runner.ArvMountPoint))
341 runner.CrunchLog.Printf("Arv-mount exit error: %v", mnterr)
343 runner.ArvMountExit <- mnterr
344 close(runner.ArvMountExit)
350 case err := <-runner.ArvMountExit:
351 runner.ArvMount = nil
359 func (runner *ContainerRunner) SetupArvMountPoint(prefix string) (err error) {
360 if runner.ArvMountPoint == "" {
361 runner.ArvMountPoint, err = runner.MkTempDir(runner.parentTemp, prefix)
366 func copyfile(src string, dst string) (err error) {
367 srcfile, err := os.Open(src)
372 os.MkdirAll(path.Dir(dst), 0777)
374 dstfile, err := os.Create(dst)
378 _, err = io.Copy(dstfile, srcfile)
383 err = srcfile.Close()
384 err2 := dstfile.Close()
397 func (runner *ContainerRunner) SetupMounts() (map[string]bindmount, error) {
398 bindmounts := map[string]bindmount{}
399 err := runner.SetupArvMountPoint("keep")
401 return nil, fmt.Errorf("While creating keep mount temp dir: %v", err)
404 token, err := runner.ContainerToken()
406 return nil, fmt.Errorf("could not get container token: %s", err)
408 runner.CrunchLog.Printf("container token %q", token)
412 arvMountCmd := []string{
417 "--storage-classes", strings.Join(runner.Container.OutputStorageClasses, ","),
418 fmt.Sprintf("--crunchstat-interval=%v", runner.statInterval.Seconds())}
420 if runner.Container.RuntimeConstraints.KeepCacheRAM > 0 {
421 arvMountCmd = append(arvMountCmd, "--file-cache", fmt.Sprintf("%d", runner.Container.RuntimeConstraints.KeepCacheRAM))
424 collectionPaths := []string{}
425 needCertMount := true
426 type copyFile struct {
430 var copyFiles []copyFile
433 for bind := range runner.Container.Mounts {
434 binds = append(binds, bind)
436 for bind := range runner.SecretMounts {
437 if _, ok := runner.Container.Mounts[bind]; ok {
438 return nil, fmt.Errorf("secret mount %q conflicts with regular mount", bind)
440 if runner.SecretMounts[bind].Kind != "json" &&
441 runner.SecretMounts[bind].Kind != "text" {
442 return nil, fmt.Errorf("secret mount %q type is %q but only 'json' and 'text' are permitted",
443 bind, runner.SecretMounts[bind].Kind)
445 binds = append(binds, bind)
449 for _, bind := range binds {
450 mnt, ok := runner.Container.Mounts[bind]
452 mnt = runner.SecretMounts[bind]
454 if bind == "stdout" || bind == "stderr" {
455 // Is it a "file" mount kind?
456 if mnt.Kind != "file" {
457 return nil, fmt.Errorf("unsupported mount kind '%s' for %s: only 'file' is supported", mnt.Kind, bind)
460 // Does path start with OutputPath?
461 prefix := runner.Container.OutputPath
462 if !strings.HasSuffix(prefix, "/") {
465 if !strings.HasPrefix(mnt.Path, prefix) {
466 return nil, fmt.Errorf("%s path does not start with OutputPath: %s, %s", strings.Title(bind), mnt.Path, prefix)
471 // Is it a "collection" mount kind?
472 if mnt.Kind != "collection" && mnt.Kind != "json" {
473 return nil, fmt.Errorf("unsupported mount kind '%s' for stdin: only 'collection' and 'json' are supported", mnt.Kind)
477 if bind == "/etc/arvados/ca-certificates.crt" {
478 needCertMount = false
481 if strings.HasPrefix(bind, runner.Container.OutputPath+"/") && bind != runner.Container.OutputPath+"/" {
482 if mnt.Kind != "collection" && mnt.Kind != "text" && mnt.Kind != "json" {
483 return nil, fmt.Errorf("only mount points of kind 'collection', 'text' or 'json' are supported underneath the output_path for %q, was %q", bind, mnt.Kind)
488 case mnt.Kind == "collection" && bind != "stdin":
490 if mnt.UUID != "" && mnt.PortableDataHash != "" {
491 return nil, fmt.Errorf("cannot specify both 'uuid' and 'portable_data_hash' for a collection mount")
495 return nil, fmt.Errorf("writing to existing collections currently not permitted")
498 src = fmt.Sprintf("%s/by_id/%s", runner.ArvMountPoint, mnt.UUID)
499 } else if mnt.PortableDataHash != "" {
500 if mnt.Writable && !strings.HasPrefix(bind, runner.Container.OutputPath+"/") {
501 return nil, fmt.Errorf("can never write to a collection specified by portable data hash")
503 idx := strings.Index(mnt.PortableDataHash, "/")
505 mnt.Path = path.Clean(mnt.PortableDataHash[idx:])
506 mnt.PortableDataHash = mnt.PortableDataHash[0:idx]
507 runner.Container.Mounts[bind] = mnt
509 src = fmt.Sprintf("%s/by_id/%s", runner.ArvMountPoint, mnt.PortableDataHash)
510 if mnt.Path != "" && mnt.Path != "." {
511 if strings.HasPrefix(mnt.Path, "./") {
512 mnt.Path = mnt.Path[2:]
513 } else if strings.HasPrefix(mnt.Path, "/") {
514 mnt.Path = mnt.Path[1:]
516 src += "/" + mnt.Path
519 src = fmt.Sprintf("%s/tmp%d", runner.ArvMountPoint, tmpcount)
520 arvMountCmd = append(arvMountCmd, "--mount-tmp")
521 arvMountCmd = append(arvMountCmd, fmt.Sprintf("tmp%d", tmpcount))
525 if bind == runner.Container.OutputPath {
526 runner.HostOutputDir = src
527 bindmounts[bind] = bindmount{HostPath: src}
528 } else if strings.HasPrefix(bind, runner.Container.OutputPath+"/") {
529 copyFiles = append(copyFiles, copyFile{src, runner.HostOutputDir + bind[len(runner.Container.OutputPath):]})
531 bindmounts[bind] = bindmount{HostPath: src}
534 bindmounts[bind] = bindmount{HostPath: src, ReadOnly: true}
536 collectionPaths = append(collectionPaths, src)
538 case mnt.Kind == "tmp":
540 tmpdir, err = runner.MkTempDir(runner.parentTemp, "tmp")
542 return nil, fmt.Errorf("while creating mount temp dir: %v", err)
544 st, staterr := os.Stat(tmpdir)
546 return nil, fmt.Errorf("while Stat on temp dir: %v", staterr)
548 err = os.Chmod(tmpdir, st.Mode()|os.ModeSetgid|0777)
550 return nil, fmt.Errorf("while Chmod temp dir: %v", err)
552 bindmounts[bind] = bindmount{HostPath: tmpdir}
553 if bind == runner.Container.OutputPath {
554 runner.HostOutputDir = tmpdir
557 case mnt.Kind == "json" || mnt.Kind == "text":
559 if mnt.Kind == "json" {
560 filedata, err = json.Marshal(mnt.Content)
562 return nil, fmt.Errorf("encoding json data: %v", err)
565 text, ok := mnt.Content.(string)
567 return nil, fmt.Errorf("content for mount %q must be a string", bind)
569 filedata = []byte(text)
572 tmpdir, err := runner.MkTempDir(runner.parentTemp, mnt.Kind)
574 return nil, fmt.Errorf("creating temp dir: %v", err)
576 tmpfn := filepath.Join(tmpdir, "mountdata."+mnt.Kind)
577 err = ioutil.WriteFile(tmpfn, filedata, 0444)
579 return nil, fmt.Errorf("writing temp file: %v", err)
581 if strings.HasPrefix(bind, runner.Container.OutputPath+"/") {
582 copyFiles = append(copyFiles, copyFile{tmpfn, runner.HostOutputDir + bind[len(runner.Container.OutputPath):]})
584 bindmounts[bind] = bindmount{HostPath: tmpfn, ReadOnly: true}
587 case mnt.Kind == "git_tree":
588 tmpdir, err := runner.MkTempDir(runner.parentTemp, "git_tree")
590 return nil, fmt.Errorf("creating temp dir: %v", err)
592 err = gitMount(mnt).extractTree(runner.ContainerArvClient, tmpdir, token)
596 bindmounts[bind] = bindmount{HostPath: tmpdir, ReadOnly: true}
600 if runner.HostOutputDir == "" {
601 return nil, fmt.Errorf("output path does not correspond to a writable mount point")
604 if needCertMount && runner.Container.RuntimeConstraints.API {
605 for _, certfile := range arvadosclient.CertFiles {
606 _, err := os.Stat(certfile)
608 bindmounts["/etc/arvados/ca-certificates.crt"] = bindmount{HostPath: certfile, ReadOnly: true}
615 arvMountCmd = append(arvMountCmd, "--mount-by-pdh", "by_id")
617 arvMountCmd = append(arvMountCmd, "--mount-by-id", "by_id")
619 arvMountCmd = append(arvMountCmd, "--mount-by-id", "by_uuid")
620 arvMountCmd = append(arvMountCmd, runner.ArvMountPoint)
622 runner.ArvMount, err = runner.RunArvMount(arvMountCmd, token)
624 return nil, fmt.Errorf("while trying to start arv-mount: %v", err)
627 for _, p := range collectionPaths {
630 return nil, fmt.Errorf("while checking that input files exist: %v", err)
634 for _, cp := range copyFiles {
635 st, err := os.Stat(cp.src)
637 return nil, fmt.Errorf("while staging writable file from %q to %q: %v", cp.src, cp.bind, err)
640 err = filepath.Walk(cp.src, func(walkpath string, walkinfo os.FileInfo, walkerr error) error {
644 target := path.Join(cp.bind, walkpath[len(cp.src):])
645 if walkinfo.Mode().IsRegular() {
646 copyerr := copyfile(walkpath, target)
650 return os.Chmod(target, walkinfo.Mode()|0777)
651 } else if walkinfo.Mode().IsDir() {
652 mkerr := os.MkdirAll(target, 0777)
656 return os.Chmod(target, walkinfo.Mode()|os.ModeSetgid|0777)
658 return fmt.Errorf("source %q is not a regular file or directory", cp.src)
661 } else if st.Mode().IsRegular() {
662 err = copyfile(cp.src, cp.bind)
664 err = os.Chmod(cp.bind, st.Mode()|0777)
668 return nil, fmt.Errorf("while staging writable file from %q to %q: %v", cp.src, cp.bind, err)
672 return bindmounts, nil
675 func (runner *ContainerRunner) stopHoststat() error {
676 if runner.hoststatReporter == nil {
679 runner.hoststatReporter.Stop()
680 err := runner.hoststatLogger.Close()
682 return fmt.Errorf("error closing hoststat logs: %v", err)
687 func (runner *ContainerRunner) startHoststat() error {
688 w, err := runner.NewLogWriter("hoststat")
692 runner.hoststatLogger = NewThrottledLogger(w)
693 runner.hoststatReporter = &crunchstat.Reporter{
694 Logger: log.New(runner.hoststatLogger, "", 0),
695 CgroupRoot: runner.cgroupRoot,
696 PollPeriod: runner.statInterval,
698 runner.hoststatReporter.Start()
702 func (runner *ContainerRunner) startCrunchstat() error {
703 w, err := runner.NewLogWriter("crunchstat")
707 runner.statLogger = NewThrottledLogger(w)
708 runner.statReporter = &crunchstat.Reporter{
709 CID: runner.executor.CgroupID(),
710 Logger: log.New(runner.statLogger, "", 0),
711 CgroupParent: runner.expectCgroupParent,
712 CgroupRoot: runner.cgroupRoot,
713 PollPeriod: runner.statInterval,
714 TempDir: runner.parentTemp,
716 runner.statReporter.Start()
720 type infoCommand struct {
725 // LogHostInfo logs info about the current host, for debugging and
726 // accounting purposes. Although it's logged as "node-info", this is
727 // about the environment where crunch-run is actually running, which
728 // might differ from what's described in the node record (see
730 func (runner *ContainerRunner) LogHostInfo() (err error) {
731 w, err := runner.NewLogWriter("node-info")
736 commands := []infoCommand{
738 label: "Host Information",
739 cmd: []string{"uname", "-a"},
742 label: "CPU Information",
743 cmd: []string{"cat", "/proc/cpuinfo"},
746 label: "Memory Information",
747 cmd: []string{"cat", "/proc/meminfo"},
751 cmd: []string{"df", "-m", "/", os.TempDir()},
754 label: "Disk INodes",
755 cmd: []string{"df", "-i", "/", os.TempDir()},
759 // Run commands with informational output to be logged.
760 for _, command := range commands {
761 fmt.Fprintln(w, command.label)
762 cmd := exec.Command(command.cmd[0], command.cmd[1:]...)
765 if err := cmd.Run(); err != nil {
766 err = fmt.Errorf("While running command %q: %v", command.cmd, err)
775 return fmt.Errorf("While closing node-info logs: %v", err)
780 // LogContainerRecord gets and saves the raw JSON container record from the API server
781 func (runner *ContainerRunner) LogContainerRecord() error {
782 logged, err := runner.logAPIResponse("container", "containers", map[string]interface{}{"filters": [][]string{{"uuid", "=", runner.Container.UUID}}}, nil)
783 if !logged && err == nil {
784 err = fmt.Errorf("error: no container record found for %s", runner.Container.UUID)
789 // LogNodeRecord logs the current host's InstanceType config entry (or
790 // the arvados#node record, if running via crunch-dispatch-slurm).
791 func (runner *ContainerRunner) LogNodeRecord() error {
792 if it := os.Getenv("InstanceType"); it != "" {
793 // Dispatched via arvados-dispatch-cloud. Save
794 // InstanceType config fragment received from
795 // dispatcher on stdin.
796 w, err := runner.LogCollection.OpenFile("node.json", os.O_CREATE|os.O_WRONLY, 0666)
801 _, err = io.WriteString(w, it)
807 // Dispatched via crunch-dispatch-slurm. Look up
808 // apiserver's node record corresponding to
810 hostname := os.Getenv("SLURMD_NODENAME")
812 hostname, _ = os.Hostname()
814 _, err := runner.logAPIResponse("node", "nodes", map[string]interface{}{"filters": [][]string{{"hostname", "=", hostname}}}, func(resp interface{}) {
815 // The "info" field has admin-only info when
816 // obtained with a privileged token, and
817 // should not be logged.
818 node, ok := resp.(map[string]interface{})
826 func (runner *ContainerRunner) logAPIResponse(label, path string, params map[string]interface{}, munge func(interface{})) (logged bool, err error) {
827 writer, err := runner.LogCollection.OpenFile(label+".json", os.O_CREATE|os.O_WRONLY, 0666)
832 ArvClient: runner.DispatcherArvClient,
833 UUID: runner.Container.UUID,
834 loggingStream: label,
838 reader, err := runner.DispatcherArvClient.CallRaw("GET", path, "", "", arvadosclient.Dict(params))
840 return false, fmt.Errorf("error getting %s record: %v", label, err)
844 dec := json.NewDecoder(reader)
846 var resp map[string]interface{}
847 if err = dec.Decode(&resp); err != nil {
848 return false, fmt.Errorf("error decoding %s list response: %v", label, err)
850 items, ok := resp["items"].([]interface{})
852 return false, fmt.Errorf("error decoding %s list response: no \"items\" key in API list response", label)
853 } else if len(items) < 1 {
859 // Re-encode it using indentation to improve readability
860 enc := json.NewEncoder(w)
861 enc.SetIndent("", " ")
862 if err = enc.Encode(items[0]); err != nil {
863 return false, fmt.Errorf("error logging %s record: %v", label, err)
867 return false, fmt.Errorf("error closing %s.json in log collection: %v", label, err)
872 func (runner *ContainerRunner) getStdoutFile(mntPath string) (*os.File, error) {
873 stdoutPath := mntPath[len(runner.Container.OutputPath):]
874 index := strings.LastIndex(stdoutPath, "/")
876 subdirs := stdoutPath[:index]
878 st, err := os.Stat(runner.HostOutputDir)
880 return nil, fmt.Errorf("While Stat on temp dir: %v", err)
882 stdoutPath := filepath.Join(runner.HostOutputDir, subdirs)
883 err = os.MkdirAll(stdoutPath, st.Mode()|os.ModeSetgid|0777)
885 return nil, fmt.Errorf("While MkdirAll %q: %v", stdoutPath, err)
889 stdoutFile, err := os.Create(filepath.Join(runner.HostOutputDir, stdoutPath))
891 return nil, fmt.Errorf("While creating file %q: %v", stdoutPath, err)
894 return stdoutFile, nil
897 // CreateContainer creates the docker container.
898 func (runner *ContainerRunner) CreateContainer(imageID string, bindmounts map[string]bindmount) error {
899 var stdin io.ReadCloser = ioutil.NopCloser(bytes.NewReader(nil))
900 if mnt, ok := runner.Container.Mounts["stdin"]; ok {
907 collID = mnt.PortableDataHash
909 path := runner.ArvMountPoint + "/by_id/" + collID + "/" + mnt.Path
910 f, err := os.Open(path)
916 j, err := json.Marshal(mnt.Content)
918 return fmt.Errorf("error encoding stdin json data: %v", err)
920 stdin = ioutil.NopCloser(bytes.NewReader(j))
922 return fmt.Errorf("stdin mount has unsupported kind %q", mnt.Kind)
926 var stdout, stderr io.WriteCloser
927 if mnt, ok := runner.Container.Mounts["stdout"]; ok {
928 f, err := runner.getStdoutFile(mnt.Path)
933 } else if w, err := runner.NewLogWriter("stdout"); err != nil {
936 stdout = NewThrottledLogger(w)
939 if mnt, ok := runner.Container.Mounts["stderr"]; ok {
940 f, err := runner.getStdoutFile(mnt.Path)
945 } else if w, err := runner.NewLogWriter("stderr"); err != nil {
948 stderr = NewThrottledLogger(w)
951 env := runner.Container.Environment
952 enableNetwork := runner.enableNetwork == "always"
953 if runner.Container.RuntimeConstraints.API {
955 tok, err := runner.ContainerToken()
959 env = map[string]string{}
960 for k, v := range runner.Container.Environment {
963 env["ARVADOS_API_TOKEN"] = tok
964 env["ARVADOS_API_HOST"] = os.Getenv("ARVADOS_API_HOST")
965 env["ARVADOS_API_HOST_INSECURE"] = os.Getenv("ARVADOS_API_HOST_INSECURE")
967 workdir := runner.Container.Cwd
969 // both "" and "." mean default
972 ram := runner.Container.RuntimeConstraints.RAM
973 if !runner.enableMemoryLimit {
976 runner.executorStdin = stdin
977 runner.executorStdout = stdout
978 runner.executorStderr = stderr
979 return runner.executor.Create(containerSpec{
981 VCPUs: runner.Container.RuntimeConstraints.VCPUs,
985 BindMounts: bindmounts,
986 Command: runner.Container.Command,
987 EnableNetwork: enableNetwork,
988 NetworkMode: runner.networkMode,
989 CgroupParent: runner.setCgroupParent,
996 // StartContainer starts the docker container created by CreateContainer.
997 func (runner *ContainerRunner) StartContainer() error {
998 runner.CrunchLog.Printf("Starting container")
999 runner.cStateLock.Lock()
1000 defer runner.cStateLock.Unlock()
1001 if runner.cCancelled {
1004 err := runner.executor.Start()
1007 if m, e := regexp.MatchString("(?ms).*(exec|System error).*(no such file or directory|file not found).*", err.Error()); m && e == nil {
1008 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])
1010 return fmt.Errorf("could not start container: %v%s", err, advice)
1015 // WaitFinish waits for the container to terminate, capture the exit code, and
1016 // close the stdout/stderr logging.
1017 func (runner *ContainerRunner) WaitFinish() error {
1018 runner.CrunchLog.Print("Waiting for container to finish")
1019 var timeout <-chan time.Time
1020 if s := runner.Container.SchedulingParameters.MaxRunTime; s > 0 {
1021 timeout = time.After(time.Duration(s) * time.Second)
1023 ctx, cancel := context.WithCancel(context.Background())
1028 runner.CrunchLog.Printf("maximum run time exceeded. Stopping container.")
1030 case <-runner.ArvMountExit:
1031 runner.CrunchLog.Printf("arv-mount exited while container is still running. Stopping container.")
1036 exitcode, err := runner.executor.Wait(ctx)
1038 runner.checkBrokenNode(err)
1041 runner.ExitCode = &exitcode
1044 if err = runner.executorStdin.Close(); err != nil {
1045 err = fmt.Errorf("error closing container stdin: %s", err)
1046 runner.CrunchLog.Printf("%s", err)
1049 if err = runner.executorStdout.Close(); err != nil {
1050 err = fmt.Errorf("error closing container stdout: %s", err)
1051 runner.CrunchLog.Printf("%s", err)
1052 if returnErr == nil {
1056 if err = runner.executorStderr.Close(); err != nil {
1057 err = fmt.Errorf("error closing container stderr: %s", err)
1058 runner.CrunchLog.Printf("%s", err)
1059 if returnErr == nil {
1064 if runner.statReporter != nil {
1065 runner.statReporter.Stop()
1066 err = runner.statLogger.Close()
1068 runner.CrunchLog.Printf("error closing crunchstat logs: %v", err)
1074 func (runner *ContainerRunner) updateLogs() {
1075 ticker := time.NewTicker(crunchLogUpdatePeriod / 360)
1078 sigusr1 := make(chan os.Signal, 1)
1079 signal.Notify(sigusr1, syscall.SIGUSR1)
1080 defer signal.Stop(sigusr1)
1082 saveAtTime := time.Now().Add(crunchLogUpdatePeriod)
1083 saveAtSize := crunchLogUpdateSize
1089 saveAtTime = time.Now()
1091 runner.logMtx.Lock()
1092 done := runner.LogsPDH != nil
1093 runner.logMtx.Unlock()
1097 size := runner.LogCollection.Size()
1098 if size == savedSize || (time.Now().Before(saveAtTime) && size < saveAtSize) {
1101 saveAtTime = time.Now().Add(crunchLogUpdatePeriod)
1102 saveAtSize = runner.LogCollection.Size() + crunchLogUpdateSize
1103 saved, err := runner.saveLogCollection(false)
1105 runner.CrunchLog.Printf("error updating log collection: %s", err)
1109 var updated arvados.Container
1110 err = runner.DispatcherArvClient.Update("containers", runner.Container.UUID, arvadosclient.Dict{
1111 "container": arvadosclient.Dict{"log": saved.PortableDataHash},
1114 runner.CrunchLog.Printf("error updating container log to %s: %s", saved.PortableDataHash, err)
1122 func (runner *ContainerRunner) reportArvMountWarning(pattern, text string) {
1123 var updated arvados.Container
1124 err := runner.DispatcherArvClient.Update("containers", runner.Container.UUID, arvadosclient.Dict{
1125 "container": arvadosclient.Dict{
1126 "runtime_status": arvadosclient.Dict{
1127 "warning": "arv-mount: " + pattern,
1128 "warningDetail": text,
1133 runner.CrunchLog.Printf("error updating container runtime_status: %s", err)
1137 // CaptureOutput saves data from the container's output directory if
1138 // needed, and updates the container output accordingly.
1139 func (runner *ContainerRunner) CaptureOutput(bindmounts map[string]bindmount) error {
1140 if runner.Container.RuntimeConstraints.API {
1141 // Output may have been set directly by the container, so
1142 // refresh the container record to check.
1143 err := runner.DispatcherArvClient.Get("containers", runner.Container.UUID,
1144 nil, &runner.Container)
1148 if runner.Container.Output != "" {
1149 // Container output is already set.
1150 runner.OutputPDH = &runner.Container.Output
1155 txt, err := (&copier{
1156 client: runner.containerClient,
1157 arvClient: runner.ContainerArvClient,
1158 keepClient: runner.ContainerKeepClient,
1159 hostOutputDir: runner.HostOutputDir,
1160 ctrOutputDir: runner.Container.OutputPath,
1161 bindmounts: bindmounts,
1162 mounts: runner.Container.Mounts,
1163 secretMounts: runner.SecretMounts,
1164 logger: runner.CrunchLog,
1169 if n := len(regexp.MustCompile(` [0-9a-f]+\+\S*\+R`).FindAllStringIndex(txt, -1)); n > 0 {
1170 runner.CrunchLog.Printf("Copying %d data blocks from remote input collections...", n)
1171 fs, err := (&arvados.Collection{ManifestText: txt}).FileSystem(runner.containerClient, runner.ContainerKeepClient)
1175 txt, err = fs.MarshalManifest(".")
1180 var resp arvados.Collection
1181 err = runner.ContainerArvClient.Create("collections", arvadosclient.Dict{
1182 "ensure_unique_name": true,
1183 "collection": arvadosclient.Dict{
1185 "name": "output for " + runner.Container.UUID,
1186 "manifest_text": txt,
1190 return fmt.Errorf("error creating output collection: %v", err)
1192 runner.OutputPDH = &resp.PortableDataHash
1196 func (runner *ContainerRunner) CleanupDirs() {
1197 if runner.ArvMount != nil {
1199 umount := exec.Command("arv-mount", fmt.Sprintf("--unmount-timeout=%d", delay), "--unmount", runner.ArvMountPoint)
1200 umount.Stdout = runner.CrunchLog
1201 umount.Stderr = runner.CrunchLog
1202 runner.CrunchLog.Printf("Running %v", umount.Args)
1203 umnterr := umount.Start()
1206 runner.CrunchLog.Printf("Error unmounting: %v", umnterr)
1207 runner.ArvMount.Process.Kill()
1209 // If arv-mount --unmount gets stuck for any reason, we
1210 // don't want to wait for it forever. Do Wait() in a goroutine
1211 // so it doesn't block crunch-run.
1212 umountExit := make(chan error)
1214 mnterr := umount.Wait()
1216 runner.CrunchLog.Printf("Error unmounting: %v", mnterr)
1218 umountExit <- mnterr
1221 for again := true; again; {
1227 case <-runner.ArvMountExit:
1229 case <-time.After(time.Duration((delay + 1) * int64(time.Second))):
1230 runner.CrunchLog.Printf("Timed out waiting for unmount")
1232 umount.Process.Kill()
1234 runner.ArvMount.Process.Kill()
1238 runner.ArvMount = nil
1241 if runner.ArvMountPoint != "" {
1242 if rmerr := os.Remove(runner.ArvMountPoint); rmerr != nil {
1243 runner.CrunchLog.Printf("While cleaning up arv-mount directory %s: %v", runner.ArvMountPoint, rmerr)
1245 runner.ArvMountPoint = ""
1248 if rmerr := os.RemoveAll(runner.parentTemp); rmerr != nil {
1249 runner.CrunchLog.Printf("While cleaning up temporary directory %s: %v", runner.parentTemp, rmerr)
1253 // CommitLogs posts the collection containing the final container logs.
1254 func (runner *ContainerRunner) CommitLogs() error {
1256 // Hold cStateLock to prevent races on CrunchLog (e.g., stop()).
1257 runner.cStateLock.Lock()
1258 defer runner.cStateLock.Unlock()
1260 runner.CrunchLog.Print(runner.finalState)
1262 if runner.arvMountLog != nil {
1263 runner.arvMountLog.Close()
1265 runner.CrunchLog.Close()
1267 // Closing CrunchLog above allows them to be committed to Keep at this
1268 // point, but re-open crunch log with ArvClient in case there are any
1269 // other further errors (such as failing to write the log to Keep!)
1270 // while shutting down
1271 runner.CrunchLog = NewThrottledLogger(&ArvLogWriter{
1272 ArvClient: runner.DispatcherArvClient,
1273 UUID: runner.Container.UUID,
1274 loggingStream: "crunch-run",
1277 runner.CrunchLog.Immediate = log.New(os.Stderr, runner.Container.UUID+" ", 0)
1280 if runner.LogsPDH != nil {
1281 // If we have already assigned something to LogsPDH,
1282 // we must be closing the re-opened log, which won't
1283 // end up getting attached to the container record and
1284 // therefore doesn't need to be saved as a collection
1285 // -- it exists only to send logs to other channels.
1288 saved, err := runner.saveLogCollection(true)
1290 return fmt.Errorf("error saving log collection: %s", err)
1292 runner.logMtx.Lock()
1293 defer runner.logMtx.Unlock()
1294 runner.LogsPDH = &saved.PortableDataHash
1298 func (runner *ContainerRunner) saveLogCollection(final bool) (response arvados.Collection, err error) {
1299 runner.logMtx.Lock()
1300 defer runner.logMtx.Unlock()
1301 if runner.LogsPDH != nil {
1302 // Already finalized.
1305 updates := arvadosclient.Dict{
1306 "name": "logs for " + runner.Container.UUID,
1308 mt, err1 := runner.LogCollection.MarshalManifest(".")
1310 // Only send updated manifest text if there was no
1312 updates["manifest_text"] = mt
1315 // Even if flushing the manifest had an error, we still want
1316 // to update the log record, if possible, to push the trash_at
1317 // and delete_at times into the future. Details on bug
1320 updates["is_trashed"] = true
1322 exp := time.Now().Add(crunchLogUpdatePeriod * 24)
1323 updates["trash_at"] = exp
1324 updates["delete_at"] = exp
1326 reqBody := arvadosclient.Dict{"collection": updates}
1328 if runner.logUUID == "" {
1329 reqBody["ensure_unique_name"] = true
1330 err2 = runner.DispatcherArvClient.Create("collections", reqBody, &response)
1332 err2 = runner.DispatcherArvClient.Update("collections", runner.logUUID, reqBody, &response)
1335 runner.logUUID = response.UUID
1338 if err1 != nil || err2 != nil {
1339 err = fmt.Errorf("error recording logs: %q, %q", err1, err2)
1344 // UpdateContainerRunning updates the container state to "Running"
1345 func (runner *ContainerRunner) UpdateContainerRunning() error {
1346 runner.cStateLock.Lock()
1347 defer runner.cStateLock.Unlock()
1348 if runner.cCancelled {
1351 return runner.DispatcherArvClient.Update("containers", runner.Container.UUID,
1352 arvadosclient.Dict{"container": arvadosclient.Dict{"state": "Running", "gateway_address": runner.gateway.Address}}, nil)
1355 // ContainerToken returns the api_token the container (and any
1356 // arv-mount processes) are allowed to use.
1357 func (runner *ContainerRunner) ContainerToken() (string, error) {
1358 if runner.token != "" {
1359 return runner.token, nil
1362 var auth arvados.APIClientAuthorization
1363 err := runner.DispatcherArvClient.Call("GET", "containers", runner.Container.UUID, "auth", nil, &auth)
1367 runner.token = fmt.Sprintf("v2/%s/%s/%s", auth.UUID, auth.APIToken, runner.Container.UUID)
1368 return runner.token, nil
1371 // UpdateContainerFinal updates the container record state on API
1372 // server to "Complete" or "Cancelled"
1373 func (runner *ContainerRunner) UpdateContainerFinal() error {
1374 update := arvadosclient.Dict{}
1375 update["state"] = runner.finalState
1376 if runner.LogsPDH != nil {
1377 update["log"] = *runner.LogsPDH
1379 if runner.finalState == "Complete" {
1380 if runner.ExitCode != nil {
1381 update["exit_code"] = *runner.ExitCode
1383 if runner.OutputPDH != nil {
1384 update["output"] = *runner.OutputPDH
1387 return runner.DispatcherArvClient.Update("containers", runner.Container.UUID, arvadosclient.Dict{"container": update}, nil)
1390 // IsCancelled returns the value of Cancelled, with goroutine safety.
1391 func (runner *ContainerRunner) IsCancelled() bool {
1392 runner.cStateLock.Lock()
1393 defer runner.cStateLock.Unlock()
1394 return runner.cCancelled
1397 // NewArvLogWriter creates an ArvLogWriter
1398 func (runner *ContainerRunner) NewArvLogWriter(name string) (io.WriteCloser, error) {
1399 writer, err := runner.LogCollection.OpenFile(name+".txt", os.O_CREATE|os.O_WRONLY, 0666)
1403 return &ArvLogWriter{
1404 ArvClient: runner.DispatcherArvClient,
1405 UUID: runner.Container.UUID,
1406 loggingStream: name,
1407 writeCloser: writer,
1411 // Run the full container lifecycle.
1412 func (runner *ContainerRunner) Run() (err error) {
1413 runner.CrunchLog.Printf("crunch-run %s started", cmd.Version.String())
1414 runner.CrunchLog.Printf("Executing container '%s' using %s runtime", runner.Container.UUID, runner.executor.Runtime())
1416 hostname, hosterr := os.Hostname()
1418 runner.CrunchLog.Printf("Error getting hostname '%v'", hosterr)
1420 runner.CrunchLog.Printf("Executing on host '%s'", hostname)
1423 runner.finalState = "Queued"
1426 runner.CleanupDirs()
1428 runner.CrunchLog.Printf("crunch-run finished")
1429 runner.CrunchLog.Close()
1432 err = runner.fetchContainerRecord()
1436 if runner.Container.State != "Locked" {
1437 return fmt.Errorf("dispatch error detected: container %q has state %q", runner.Container.UUID, runner.Container.State)
1440 var bindmounts map[string]bindmount
1442 // checkErr prints e (unless it's nil) and sets err to
1443 // e (unless err is already non-nil). Thus, if err
1444 // hasn't already been assigned when Run() returns,
1445 // this cleanup func will cause Run() to return the
1446 // first non-nil error that is passed to checkErr().
1447 checkErr := func(errorIn string, e error) {
1451 runner.CrunchLog.Printf("error in %s: %v", errorIn, e)
1455 if runner.finalState == "Complete" {
1456 // There was an error in the finalization.
1457 runner.finalState = "Cancelled"
1461 // Log the error encountered in Run(), if any
1462 checkErr("Run", err)
1464 if runner.finalState == "Queued" {
1465 runner.UpdateContainerFinal()
1469 if runner.IsCancelled() {
1470 runner.finalState = "Cancelled"
1471 // but don't return yet -- we still want to
1472 // capture partial output and write logs
1475 if bindmounts != nil {
1476 checkErr("CaptureOutput", runner.CaptureOutput(bindmounts))
1478 checkErr("stopHoststat", runner.stopHoststat())
1479 checkErr("CommitLogs", runner.CommitLogs())
1480 runner.CleanupDirs()
1481 checkErr("UpdateContainerFinal", runner.UpdateContainerFinal())
1484 runner.setupSignals()
1485 err = runner.startHoststat()
1490 // set up FUSE mount and binds
1491 bindmounts, err = runner.SetupMounts()
1493 runner.finalState = "Cancelled"
1494 err = fmt.Errorf("While setting up mounts: %v", err)
1498 // check for and/or load image
1499 imageID, err := runner.LoadImage()
1501 if !runner.checkBrokenNode(err) {
1502 // Failed to load image but not due to a "broken node"
1503 // condition, probably user error.
1504 runner.finalState = "Cancelled"
1506 err = fmt.Errorf("While loading container image: %v", err)
1510 err = runner.CreateContainer(imageID, bindmounts)
1514 err = runner.LogHostInfo()
1518 err = runner.LogNodeRecord()
1522 err = runner.LogContainerRecord()
1527 if runner.IsCancelled() {
1531 err = runner.UpdateContainerRunning()
1535 runner.finalState = "Cancelled"
1537 err = runner.startCrunchstat()
1542 err = runner.StartContainer()
1544 runner.checkBrokenNode(err)
1548 err = runner.WaitFinish()
1549 if err == nil && !runner.IsCancelled() {
1550 runner.finalState = "Complete"
1555 // Fetch the current container record (uuid = runner.Container.UUID)
1556 // into runner.Container.
1557 func (runner *ContainerRunner) fetchContainerRecord() error {
1558 reader, err := runner.DispatcherArvClient.CallRaw("GET", "containers", runner.Container.UUID, "", nil)
1560 return fmt.Errorf("error fetching container record: %v", err)
1562 defer reader.Close()
1564 dec := json.NewDecoder(reader)
1566 err = dec.Decode(&runner.Container)
1568 return fmt.Errorf("error decoding container record: %v", err)
1572 SecretMounts map[string]arvados.Mount `json:"secret_mounts"`
1575 containerToken, err := runner.ContainerToken()
1577 return fmt.Errorf("error getting container token: %v", err)
1580 runner.ContainerArvClient, runner.ContainerKeepClient,
1581 runner.containerClient, err = runner.MkArvClient(containerToken)
1583 return fmt.Errorf("error creating container API client: %v", err)
1586 runner.ContainerKeepClient.SetStorageClasses(runner.Container.OutputStorageClasses)
1587 runner.DispatcherKeepClient.SetStorageClasses(runner.Container.OutputStorageClasses)
1589 err = runner.ContainerArvClient.Call("GET", "containers", runner.Container.UUID, "secret_mounts", nil, &sm)
1591 if apierr, ok := err.(arvadosclient.APIServerError); !ok || apierr.HttpStatusCode != 404 {
1592 return fmt.Errorf("error fetching secret_mounts: %v", err)
1594 // ok && apierr.HttpStatusCode == 404, which means
1595 // secret_mounts isn't supported by this API server.
1597 runner.SecretMounts = sm.SecretMounts
1602 // NewContainerRunner creates a new container runner.
1603 func NewContainerRunner(dispatcherClient *arvados.Client,
1604 dispatcherArvClient IArvadosClient,
1605 dispatcherKeepClient IKeepClient,
1606 containerUUID string) (*ContainerRunner, error) {
1608 cr := &ContainerRunner{
1609 dispatcherClient: dispatcherClient,
1610 DispatcherArvClient: dispatcherArvClient,
1611 DispatcherKeepClient: dispatcherKeepClient,
1613 cr.NewLogWriter = cr.NewArvLogWriter
1614 cr.RunArvMount = cr.ArvMountCmd
1615 cr.MkTempDir = ioutil.TempDir
1616 cr.MkArvClient = func(token string) (IArvadosClient, IKeepClient, *arvados.Client, error) {
1617 cl, err := arvadosclient.MakeArvadosClient()
1619 return nil, nil, nil, err
1622 kc, err := keepclient.MakeKeepClient(cl)
1624 return nil, nil, nil, err
1626 c2 := arvados.NewClientFromEnv()
1627 c2.AuthToken = token
1628 return cl, kc, c2, nil
1631 cr.LogCollection, err = (&arvados.Collection{}).FileSystem(cr.dispatcherClient, cr.DispatcherKeepClient)
1635 cr.Container.UUID = containerUUID
1636 w, err := cr.NewLogWriter("crunch-run")
1640 cr.CrunchLog = NewThrottledLogger(w)
1641 cr.CrunchLog.Immediate = log.New(os.Stderr, containerUUID+" ", 0)
1643 loadLogThrottleParams(dispatcherArvClient)
1649 func (command) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
1650 flags := flag.NewFlagSet(prog, flag.ContinueOnError)
1651 statInterval := flags.Duration("crunchstat-interval", 10*time.Second, "sampling period for periodic resource usage reporting")
1652 cgroupRoot := flags.String("cgroup-root", "/sys/fs/cgroup", "path to sysfs cgroup tree")
1653 cgroupParent := flags.String("cgroup-parent", "docker", "name of container's parent cgroup (ignored if -cgroup-parent-subsystem is used)")
1654 cgroupParentSubsystem := flags.String("cgroup-parent-subsystem", "", "use current cgroup for given subsystem as parent cgroup for container")
1655 caCertsPath := flags.String("ca-certs", "", "Path to TLS root certificates")
1656 detach := flags.Bool("detach", false, "Detach from parent process and run in the background")
1657 stdinConfig := flags.Bool("stdin-config", false, "Load config and environment variables from JSON message on stdin")
1658 sleep := flags.Duration("sleep", 0, "Delay before starting (testing use only)")
1659 kill := flags.Int("kill", -1, "Send signal to an existing crunch-run process for given UUID")
1660 list := flags.Bool("list", false, "List UUIDs of existing crunch-run processes")
1661 enableMemoryLimit := flags.Bool("enable-memory-limit", true, "tell container runtime to limit container's memory usage")
1662 enableNetwork := flags.String("container-enable-networking", "default", "enable networking \"always\" (for all containers) or \"default\" (for containers that request it)")
1663 networkMode := flags.String("container-network-mode", "default", `Docker network mode for container (use any argument valid for docker --net)`)
1664 memprofile := flags.String("memprofile", "", "write memory profile to `file` after running container")
1665 runtimeEngine := flags.String("runtime-engine", "docker", "container runtime: docker or singularity")
1666 flags.Duration("check-containerd", 0, "Ignored. Exists for compatibility with older versions.")
1668 ignoreDetachFlag := false
1669 if len(args) > 0 && args[0] == "-no-detach" {
1670 // This process was invoked by a parent process, which
1671 // has passed along its own arguments, including
1672 // -detach, after the leading -no-detach flag. Strip
1673 // the leading -no-detach flag (it's not recognized by
1674 // flags.Parse()) and ignore the -detach flag that
1677 ignoreDetachFlag = true
1680 if err := flags.Parse(args); err == flag.ErrHelp {
1682 } else if err != nil {
1687 containerUUID := flags.Arg(0)
1690 case *detach && !ignoreDetachFlag:
1691 return Detach(containerUUID, prog, args, os.Stdin, os.Stdout, os.Stderr)
1693 return KillProcess(containerUUID, syscall.Signal(*kill), os.Stdout, os.Stderr)
1695 return ListProcesses(os.Stdout, os.Stderr)
1698 if len(containerUUID) != 27 {
1699 log.Printf("usage: %s [options] UUID", prog)
1705 err := json.NewDecoder(os.Stdin).Decode(&conf)
1710 for k, v := range conf.Env {
1711 err = os.Setenv(k, v)
1713 log.Printf("setenv(%q): %s", k, err)
1717 if conf.Cluster != nil {
1718 // ClusterID is missing from the JSON
1719 // representation, but we need it to generate
1720 // a valid config file for keepstore, so we
1721 // fill it using the container UUID prefix.
1722 conf.Cluster.ClusterID = containerUUID[:5]
1726 log.Printf("crunch-run %s started", cmd.Version.String())
1729 if *caCertsPath != "" {
1730 arvadosclient.CertFiles = []string{*caCertsPath}
1733 var keepstoreLog bufThenWrite
1734 keepstore, err := startLocalKeepstore(conf, io.MultiWriter(&keepstoreLog, stderr))
1739 if keepstore != nil {
1740 defer keepstore.Process.Kill()
1743 api, err := arvadosclient.MakeArvadosClient()
1745 log.Printf("%s: %v", containerUUID, err)
1750 kc, kcerr := keepclient.MakeKeepClient(api)
1752 log.Printf("%s: %v", containerUUID, kcerr)
1755 kc.BlockCache = &keepclient.BlockCache{MaxBlocks: 2}
1758 cr, err := NewContainerRunner(arvados.NewClientFromEnv(), api, kc, containerUUID)
1764 if keepstore != nil {
1765 w, err := cr.NewLogWriter("keepstore")
1770 err = keepstoreLog.SetWriter(NewThrottledLogger(w))
1777 switch *runtimeEngine {
1779 cr.executor, err = newDockerExecutor(containerUUID, cr.CrunchLog.Printf, cr.containerWatchdogInterval)
1781 cr.executor, err = newSingularityExecutor(cr.CrunchLog.Printf)
1783 cr.CrunchLog.Printf("%s: unsupported RuntimeEngine %q", containerUUID, *runtimeEngine)
1784 cr.CrunchLog.Close()
1788 cr.CrunchLog.Printf("%s: %v", containerUUID, err)
1789 cr.checkBrokenNode(err)
1790 cr.CrunchLog.Close()
1793 defer cr.executor.Close()
1795 gwAuthSecret := os.Getenv("GatewayAuthSecret")
1796 os.Unsetenv("GatewayAuthSecret")
1797 if gwAuthSecret == "" {
1798 // not safe to run a gateway service without an auth
1800 cr.CrunchLog.Printf("Not starting a gateway server (GatewayAuthSecret was not provided by dispatcher)")
1801 } else if gwListen := os.Getenv("GatewayAddress"); gwListen == "" {
1802 // dispatcher did not tell us which external IP
1803 // address to advertise --> no gateway service
1804 cr.CrunchLog.Printf("Not starting a gateway server (GatewayAddress was not provided by dispatcher)")
1805 } else if de, ok := cr.executor.(*dockerExecutor); ok {
1806 cr.gateway = Gateway{
1808 AuthSecret: gwAuthSecret,
1809 ContainerUUID: containerUUID,
1810 DockerContainerID: &de.containerID,
1812 ContainerIPAddress: dockerContainerIPAddress(&de.containerID),
1814 err = cr.gateway.Start()
1816 log.Printf("error starting gateway server: %s", err)
1821 parentTemp, tmperr := cr.MkTempDir("", "crunch-run."+containerUUID+".")
1823 log.Printf("%s: %v", containerUUID, tmperr)
1827 cr.parentTemp = parentTemp
1828 cr.statInterval = *statInterval
1829 cr.cgroupRoot = *cgroupRoot
1830 cr.expectCgroupParent = *cgroupParent
1831 cr.enableMemoryLimit = *enableMemoryLimit
1832 cr.enableNetwork = *enableNetwork
1833 cr.networkMode = *networkMode
1834 if *cgroupParentSubsystem != "" {
1835 p := findCgroup(*cgroupParentSubsystem)
1836 cr.setCgroupParent = p
1837 cr.expectCgroupParent = p
1842 if *memprofile != "" {
1843 f, err := os.Create(*memprofile)
1845 log.Printf("could not create memory profile: %s", err)
1847 runtime.GC() // get up-to-date statistics
1848 if err := pprof.WriteHeapProfile(f); err != nil {
1849 log.Printf("could not write memory profile: %s", err)
1851 closeerr := f.Close()
1852 if closeerr != nil {
1853 log.Printf("closing memprofile file: %s", err)
1858 log.Printf("%s: %v", containerUUID, runerr)
1864 func startLocalKeepstore(configData ConfigData, logbuf io.Writer) (*exec.Cmd, error) {
1865 if configData.Cluster == nil || configData.KeepBuffers < 1 {
1869 // Rather than have an alternate way to tell keepstore how
1870 // many buffers to use when starting it this way, we just
1871 // modify the cluster configuration that we feed it on stdin.
1872 configData.Cluster.API.MaxKeepBlobBuffers = configData.KeepBuffers
1874 ln, err := net.Listen("tcp", "localhost:0")
1878 _, port, err := net.SplitHostPort(ln.Addr().String())
1884 url := "http://localhost:" + port
1886 fmt.Fprintf(logbuf, "starting keepstore on %s\n", url)
1888 var confJSON bytes.Buffer
1889 err = json.NewEncoder(&confJSON).Encode(arvados.Config{
1890 Clusters: map[string]arvados.Cluster{
1891 configData.Cluster.ClusterID: *configData.Cluster,
1897 cmd := exec.Command("/proc/self/exe", "keepstore", "-config=-")
1898 cmd.Stdin = &confJSON
1903 "ARVADOS_SERVICE_INTERNAL_URL=" + url,
1907 return nil, fmt.Errorf("error starting keepstore process: %w", err)
1909 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Second*10))
1911 poll := time.NewTicker(time.Second / 10)
1913 client := http.Client{}
1915 testReq, err := http.NewRequestWithContext(ctx, "GET", url, nil)
1919 resp, err := client.Do(testReq)
1921 // Success -- don't need to check the
1922 // response, we just need to know it's
1923 // accepting requests.
1927 if ctx.Err() != nil {
1928 return nil, fmt.Errorf("timed out waiting for new keepstore process to accept a request")
1931 os.Setenv("ARVADOS_KEEP_SERVICES", url)