1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
30 "git.arvados.org/arvados.git/lib/cmd"
31 "git.arvados.org/arvados.git/lib/crunchstat"
32 "git.arvados.org/arvados.git/sdk/go/arvados"
33 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
34 "git.arvados.org/arvados.git/sdk/go/keepclient"
35 "git.arvados.org/arvados.git/sdk/go/manifest"
36 "golang.org/x/crypto/ssh"
37 "golang.org/x/net/context"
39 dockertypes "github.com/docker/docker/api/types"
40 dockercontainer "github.com/docker/docker/api/types/container"
41 dockernetwork "github.com/docker/docker/api/types/network"
42 dockerclient "github.com/docker/docker/client"
47 var Command = command{}
49 // IArvadosClient is the minimal Arvados API methods used by crunch-run.
50 type IArvadosClient interface {
51 Create(resourceType string, parameters arvadosclient.Dict, output interface{}) error
52 Get(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) error
53 Update(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) error
54 Call(method, resourceType, uuid, action string, parameters arvadosclient.Dict, output interface{}) error
55 CallRaw(method string, resourceType string, uuid string, action string, parameters arvadosclient.Dict) (reader io.ReadCloser, err error)
56 Discovery(key string) (interface{}, error)
59 // ErrCancelled is the error returned when the container is cancelled.
60 var ErrCancelled = errors.New("Cancelled")
62 // IKeepClient is the minimal Keep API methods used by crunch-run.
63 type IKeepClient interface {
64 PutB(buf []byte) (string, int, error)
65 ReadAt(locator string, p []byte, off int) (int, error)
66 ManifestFileReader(m manifest.Manifest, filename string) (arvados.File, error)
67 LocalLocator(locator string) (string, error)
71 // NewLogWriter is a factory function to create a new log writer.
72 type NewLogWriter func(name string) (io.WriteCloser, error)
74 type RunArvMount func(args []string, tok string) (*exec.Cmd, error)
76 type MkTempDir func(string, string) (string, error)
78 // ThinDockerClient is the minimal Docker client interface used by crunch-run.
79 type ThinDockerClient interface {
80 ContainerAttach(ctx context.Context, container string, options dockertypes.ContainerAttachOptions) (dockertypes.HijackedResponse, error)
81 ContainerCreate(ctx context.Context, config *dockercontainer.Config, hostConfig *dockercontainer.HostConfig,
82 networkingConfig *dockernetwork.NetworkingConfig, containerName string) (dockercontainer.ContainerCreateCreatedBody, error)
83 ContainerStart(ctx context.Context, container string, options dockertypes.ContainerStartOptions) error
84 ContainerRemove(ctx context.Context, container string, options dockertypes.ContainerRemoveOptions) error
85 ContainerWait(ctx context.Context, container string, condition dockercontainer.WaitCondition) (<-chan dockercontainer.ContainerWaitOKBody, <-chan error)
86 ContainerInspect(ctx context.Context, id string) (dockertypes.ContainerJSON, error)
87 ImageInspectWithRaw(ctx context.Context, image string) (dockertypes.ImageInspect, []byte, error)
88 ImageLoad(ctx context.Context, input io.Reader, quiet bool) (dockertypes.ImageLoadResponse, error)
89 ImageRemove(ctx context.Context, image string, options dockertypes.ImageRemoveOptions) ([]dockertypes.ImageDeleteResponseItem, error)
92 type PsProcess interface {
93 CmdlineSlice() ([]string, error)
96 // ContainerRunner is the main stateful struct used for a single execution of a
98 type ContainerRunner struct {
99 Docker ThinDockerClient
101 // Dispatcher client is initialized with the Dispatcher token.
102 // This is a privileged token used to manage container status
105 // We have both dispatcherClient and DispatcherArvClient
106 // because there are two different incompatible Arvados Go
107 // SDKs and we have to use both (hopefully this gets fixed in
109 dispatcherClient *arvados.Client
110 DispatcherArvClient IArvadosClient
111 DispatcherKeepClient IKeepClient
113 // Container client is initialized with the Container token
114 // This token controls the permissions of the container, and
115 // must be used for operations such as reading collections.
117 // Same comment as above applies to
118 // containerClient/ContainerArvClient.
119 containerClient *arvados.Client
120 ContainerArvClient IArvadosClient
121 ContainerKeepClient IKeepClient
123 Container arvados.Container
124 ContainerConfig dockercontainer.Config
125 HostConfig dockercontainer.HostConfig
129 NewLogWriter NewLogWriter
130 loggingDone chan bool
131 CrunchLog *ThrottledLogger
132 Stdout io.WriteCloser
133 Stderr io.WriteCloser
136 LogCollection arvados.CollectionFileSystem
138 RunArvMount RunArvMount
144 Volumes map[string]struct{}
146 SigChan chan os.Signal
147 ArvMountExit chan error
148 SecretMounts map[string]arvados.Mount
149 MkArvClient func(token string) (IArvadosClient, IKeepClient, *arvados.Client, error)
153 statLogger io.WriteCloser
154 statReporter *crunchstat.Reporter
155 hoststatLogger io.WriteCloser
156 hoststatReporter *crunchstat.Reporter
157 statInterval time.Duration
159 // What we expect the container's cgroup parent to be.
160 expectCgroupParent string
161 // What we tell docker to use as the container's cgroup
162 // parent. Note: Ideally we would use the same field for both
163 // expectCgroupParent and setCgroupParent, and just make it
164 // default to "docker". However, when using docker < 1.10 with
165 // systemd, specifying a non-empty cgroup parent (even the
166 // default value "docker") hits a docker bug
167 // (https://github.com/docker/docker/issues/17126). Using two
168 // separate fields makes it possible to use the "expect cgroup
169 // parent to be X" feature even on sites where the "specify
170 // cgroup parent" feature breaks.
171 setCgroupParent string
173 cStateLock sync.Mutex
174 cCancelled bool // StopContainer() invoked
175 cRemoved bool // docker confirmed the container no longer exists
177 enableNetwork string // one of "default" or "always"
178 networkMode string // passed through to HostConfig.NetworkMode
179 arvMountLog *ThrottledLogger
181 containerWatchdogInterval time.Duration
183 gatewayAddress string
184 gatewaySSHConfig *ssh.ServerConfig
185 gatewayAuthSecret string
188 // setupSignals sets up signal handling to gracefully terminate the underlying
189 // Docker container and update state when receiving a TERM, INT or QUIT signal.
190 func (runner *ContainerRunner) setupSignals() {
191 runner.SigChan = make(chan os.Signal, 1)
192 signal.Notify(runner.SigChan, syscall.SIGTERM)
193 signal.Notify(runner.SigChan, syscall.SIGINT)
194 signal.Notify(runner.SigChan, syscall.SIGQUIT)
196 go func(sig chan os.Signal) {
203 // stop the underlying Docker container.
204 func (runner *ContainerRunner) stop(sig os.Signal) {
205 runner.cStateLock.Lock()
206 defer runner.cStateLock.Unlock()
208 runner.CrunchLog.Printf("caught signal: %v", sig)
210 if runner.ContainerID == "" {
213 runner.cCancelled = true
214 runner.CrunchLog.Printf("removing container")
215 err := runner.Docker.ContainerRemove(context.TODO(), runner.ContainerID, dockertypes.ContainerRemoveOptions{Force: true})
217 runner.CrunchLog.Printf("error removing container: %s", err)
219 if err == nil || strings.Contains(err.Error(), "No such container: "+runner.ContainerID) {
220 runner.cRemoved = true
224 var errorBlacklist = []string{
225 "(?ms).*[Cc]annot connect to the Docker daemon.*",
226 "(?ms).*oci runtime error.*starting container process.*container init.*mounting.*to rootfs.*no such file or directory.*",
227 "(?ms).*grpc: the connection is unavailable.*",
229 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)")
231 func (runner *ContainerRunner) runBrokenNodeHook() {
232 if *brokenNodeHook == "" {
233 path := filepath.Join(lockdir, brokenfile)
234 runner.CrunchLog.Printf("Writing %s to mark node as broken", path)
235 f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0700)
237 runner.CrunchLog.Printf("Error writing %s: %s", path, err)
242 runner.CrunchLog.Printf("Running broken node hook %q", *brokenNodeHook)
244 c := exec.Command(*brokenNodeHook)
245 c.Stdout = runner.CrunchLog
246 c.Stderr = runner.CrunchLog
249 runner.CrunchLog.Printf("Error running broken node hook: %v", err)
254 func (runner *ContainerRunner) checkBrokenNode(goterr error) bool {
255 for _, d := range errorBlacklist {
256 if m, e := regexp.MatchString(d, goterr.Error()); m && e == nil {
257 runner.CrunchLog.Printf("Error suggests node is unable to run containers: %v", goterr)
258 runner.runBrokenNodeHook()
265 // LoadImage determines the docker image id from the container record and
266 // checks if it is available in the local Docker image store. If not, it loads
267 // the image from Keep.
268 func (runner *ContainerRunner) LoadImage() (err error) {
270 runner.CrunchLog.Printf("Fetching Docker image from collection '%s'", runner.Container.ContainerImage)
272 var collection arvados.Collection
273 err = runner.ContainerArvClient.Get("collections", runner.Container.ContainerImage, nil, &collection)
275 return fmt.Errorf("While getting container image collection: %v", err)
277 manifest := manifest.Manifest{Text: collection.ManifestText}
278 var img, imageID string
279 for ms := range manifest.StreamIter() {
280 img = ms.FileStreamSegments[0].Name
281 if !strings.HasSuffix(img, ".tar") {
282 return fmt.Errorf("First file in the container image collection does not end in .tar")
284 imageID = img[:len(img)-4]
287 runner.CrunchLog.Printf("Using Docker image id '%s'", imageID)
289 _, _, err = runner.Docker.ImageInspectWithRaw(context.TODO(), imageID)
291 runner.CrunchLog.Print("Loading Docker image from keep")
293 var readCloser io.ReadCloser
294 readCloser, err = runner.ContainerKeepClient.ManifestFileReader(manifest, img)
296 return fmt.Errorf("While creating ManifestFileReader for container image: %v", err)
299 response, err := runner.Docker.ImageLoad(context.TODO(), readCloser, true)
301 return fmt.Errorf("While loading container image into Docker: %v", err)
304 defer response.Body.Close()
305 rbody, err := ioutil.ReadAll(response.Body)
307 return fmt.Errorf("Reading response to image load: %v", err)
309 runner.CrunchLog.Printf("Docker response: %s", rbody)
311 runner.CrunchLog.Print("Docker image is available")
314 runner.ContainerConfig.Image = imageID
316 runner.ContainerKeepClient.ClearBlockCache()
321 func (runner *ContainerRunner) ArvMountCmd(arvMountCmd []string, token string) (c *exec.Cmd, err error) {
322 c = exec.Command("arv-mount", arvMountCmd...)
324 // Copy our environment, but override ARVADOS_API_TOKEN with
325 // the container auth token.
327 for _, s := range os.Environ() {
328 if !strings.HasPrefix(s, "ARVADOS_API_TOKEN=") {
329 c.Env = append(c.Env, s)
332 c.Env = append(c.Env, "ARVADOS_API_TOKEN="+token)
334 w, err := runner.NewLogWriter("arv-mount")
338 runner.arvMountLog = NewThrottledLogger(w)
339 c.Stdout = runner.arvMountLog
340 c.Stderr = runner.arvMountLog
342 runner.CrunchLog.Printf("Running %v", c.Args)
349 statReadme := make(chan bool)
350 runner.ArvMountExit = make(chan error)
355 time.Sleep(100 * time.Millisecond)
356 _, err = os.Stat(fmt.Sprintf("%s/by_id/README", runner.ArvMountPoint))
368 runner.CrunchLog.Printf("Arv-mount exit error: %v", mnterr)
370 runner.ArvMountExit <- mnterr
371 close(runner.ArvMountExit)
377 case err := <-runner.ArvMountExit:
378 runner.ArvMount = nil
386 func (runner *ContainerRunner) SetupArvMountPoint(prefix string) (err error) {
387 if runner.ArvMountPoint == "" {
388 runner.ArvMountPoint, err = runner.MkTempDir(runner.parentTemp, prefix)
393 func copyfile(src string, dst string) (err error) {
394 srcfile, err := os.Open(src)
399 os.MkdirAll(path.Dir(dst), 0777)
401 dstfile, err := os.Create(dst)
405 _, err = io.Copy(dstfile, srcfile)
410 err = srcfile.Close()
411 err2 := dstfile.Close()
424 func (runner *ContainerRunner) SetupMounts() (err error) {
425 err = runner.SetupArvMountPoint("keep")
427 return fmt.Errorf("While creating keep mount temp dir: %v", err)
430 token, err := runner.ContainerToken()
432 return fmt.Errorf("could not get container token: %s", err)
437 arvMountCmd := []string{
441 fmt.Sprintf("--crunchstat-interval=%v", runner.statInterval.Seconds())}
443 if runner.Container.RuntimeConstraints.KeepCacheRAM > 0 {
444 arvMountCmd = append(arvMountCmd, "--file-cache", fmt.Sprintf("%d", runner.Container.RuntimeConstraints.KeepCacheRAM))
447 collectionPaths := []string{}
449 runner.Volumes = make(map[string]struct{})
450 needCertMount := true
451 type copyFile struct {
455 var copyFiles []copyFile
458 for bind := range runner.Container.Mounts {
459 binds = append(binds, bind)
461 for bind := range runner.SecretMounts {
462 if _, ok := runner.Container.Mounts[bind]; ok {
463 return fmt.Errorf("secret mount %q conflicts with regular mount", bind)
465 if runner.SecretMounts[bind].Kind != "json" &&
466 runner.SecretMounts[bind].Kind != "text" {
467 return fmt.Errorf("secret mount %q type is %q but only 'json' and 'text' are permitted",
468 bind, runner.SecretMounts[bind].Kind)
470 binds = append(binds, bind)
474 for _, bind := range binds {
475 mnt, ok := runner.Container.Mounts[bind]
477 mnt = runner.SecretMounts[bind]
479 if bind == "stdout" || bind == "stderr" {
480 // Is it a "file" mount kind?
481 if mnt.Kind != "file" {
482 return fmt.Errorf("unsupported mount kind '%s' for %s: only 'file' is supported", mnt.Kind, bind)
485 // Does path start with OutputPath?
486 prefix := runner.Container.OutputPath
487 if !strings.HasSuffix(prefix, "/") {
490 if !strings.HasPrefix(mnt.Path, prefix) {
491 return fmt.Errorf("%s path does not start with OutputPath: %s, %s", strings.Title(bind), mnt.Path, prefix)
496 // Is it a "collection" mount kind?
497 if mnt.Kind != "collection" && mnt.Kind != "json" {
498 return fmt.Errorf("unsupported mount kind '%s' for stdin: only 'collection' and 'json' are supported", mnt.Kind)
502 if bind == "/etc/arvados/ca-certificates.crt" {
503 needCertMount = false
506 if strings.HasPrefix(bind, runner.Container.OutputPath+"/") && bind != runner.Container.OutputPath+"/" {
507 if mnt.Kind != "collection" && mnt.Kind != "text" && mnt.Kind != "json" {
508 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)
513 case mnt.Kind == "collection" && bind != "stdin":
515 if mnt.UUID != "" && mnt.PortableDataHash != "" {
516 return fmt.Errorf("cannot specify both 'uuid' and 'portable_data_hash' for a collection mount")
520 return fmt.Errorf("writing to existing collections currently not permitted")
523 src = fmt.Sprintf("%s/by_id/%s", runner.ArvMountPoint, mnt.UUID)
524 } else if mnt.PortableDataHash != "" {
525 if mnt.Writable && !strings.HasPrefix(bind, runner.Container.OutputPath+"/") {
526 return fmt.Errorf("can never write to a collection specified by portable data hash")
528 idx := strings.Index(mnt.PortableDataHash, "/")
530 mnt.Path = path.Clean(mnt.PortableDataHash[idx:])
531 mnt.PortableDataHash = mnt.PortableDataHash[0:idx]
532 runner.Container.Mounts[bind] = mnt
534 src = fmt.Sprintf("%s/by_id/%s", runner.ArvMountPoint, mnt.PortableDataHash)
535 if mnt.Path != "" && mnt.Path != "." {
536 if strings.HasPrefix(mnt.Path, "./") {
537 mnt.Path = mnt.Path[2:]
538 } else if strings.HasPrefix(mnt.Path, "/") {
539 mnt.Path = mnt.Path[1:]
541 src += "/" + mnt.Path
544 src = fmt.Sprintf("%s/tmp%d", runner.ArvMountPoint, tmpcount)
545 arvMountCmd = append(arvMountCmd, "--mount-tmp")
546 arvMountCmd = append(arvMountCmd, fmt.Sprintf("tmp%d", tmpcount))
550 if bind == runner.Container.OutputPath {
551 runner.HostOutputDir = src
552 runner.Binds = append(runner.Binds, fmt.Sprintf("%s:%s", src, bind))
553 } else if strings.HasPrefix(bind, runner.Container.OutputPath+"/") {
554 copyFiles = append(copyFiles, copyFile{src, runner.HostOutputDir + bind[len(runner.Container.OutputPath):]})
556 runner.Binds = append(runner.Binds, fmt.Sprintf("%s:%s", src, bind))
559 runner.Binds = append(runner.Binds, fmt.Sprintf("%s:%s:ro", src, bind))
561 collectionPaths = append(collectionPaths, src)
563 case mnt.Kind == "tmp":
565 tmpdir, err = runner.MkTempDir(runner.parentTemp, "tmp")
567 return fmt.Errorf("while creating mount temp dir: %v", err)
569 st, staterr := os.Stat(tmpdir)
571 return fmt.Errorf("while Stat on temp dir: %v", staterr)
573 err = os.Chmod(tmpdir, st.Mode()|os.ModeSetgid|0777)
575 return fmt.Errorf("while Chmod temp dir: %v", err)
577 runner.Binds = append(runner.Binds, fmt.Sprintf("%s:%s", tmpdir, bind))
578 if bind == runner.Container.OutputPath {
579 runner.HostOutputDir = tmpdir
582 case mnt.Kind == "json" || mnt.Kind == "text":
584 if mnt.Kind == "json" {
585 filedata, err = json.Marshal(mnt.Content)
587 return fmt.Errorf("encoding json data: %v", err)
590 text, ok := mnt.Content.(string)
592 return fmt.Errorf("content for mount %q must be a string", bind)
594 filedata = []byte(text)
597 tmpdir, err := runner.MkTempDir(runner.parentTemp, mnt.Kind)
599 return fmt.Errorf("creating temp dir: %v", err)
601 tmpfn := filepath.Join(tmpdir, "mountdata."+mnt.Kind)
602 err = ioutil.WriteFile(tmpfn, filedata, 0444)
604 return fmt.Errorf("writing temp file: %v", err)
606 if strings.HasPrefix(bind, runner.Container.OutputPath+"/") {
607 copyFiles = append(copyFiles, copyFile{tmpfn, runner.HostOutputDir + bind[len(runner.Container.OutputPath):]})
609 runner.Binds = append(runner.Binds, fmt.Sprintf("%s:%s:ro", tmpfn, bind))
612 case mnt.Kind == "git_tree":
613 tmpdir, err := runner.MkTempDir(runner.parentTemp, "git_tree")
615 return fmt.Errorf("creating temp dir: %v", err)
617 err = gitMount(mnt).extractTree(runner.ContainerArvClient, tmpdir, token)
621 runner.Binds = append(runner.Binds, tmpdir+":"+bind+":ro")
625 if runner.HostOutputDir == "" {
626 return fmt.Errorf("output path does not correspond to a writable mount point")
629 if wantAPI := runner.Container.RuntimeConstraints.API; needCertMount && wantAPI != nil && *wantAPI {
630 for _, certfile := range arvadosclient.CertFiles {
631 _, err := os.Stat(certfile)
633 runner.Binds = append(runner.Binds, fmt.Sprintf("%s:/etc/arvados/ca-certificates.crt:ro", certfile))
640 arvMountCmd = append(arvMountCmd, "--mount-by-pdh", "by_id")
642 arvMountCmd = append(arvMountCmd, "--mount-by-id", "by_id")
644 arvMountCmd = append(arvMountCmd, runner.ArvMountPoint)
646 runner.ArvMount, err = runner.RunArvMount(arvMountCmd, token)
648 return fmt.Errorf("while trying to start arv-mount: %v", err)
651 for _, p := range collectionPaths {
654 return fmt.Errorf("while checking that input files exist: %v", err)
658 for _, cp := range copyFiles {
659 st, err := os.Stat(cp.src)
661 return fmt.Errorf("while staging writable file from %q to %q: %v", cp.src, cp.bind, err)
664 err = filepath.Walk(cp.src, func(walkpath string, walkinfo os.FileInfo, walkerr error) error {
668 target := path.Join(cp.bind, walkpath[len(cp.src):])
669 if walkinfo.Mode().IsRegular() {
670 copyerr := copyfile(walkpath, target)
674 return os.Chmod(target, walkinfo.Mode()|0777)
675 } else if walkinfo.Mode().IsDir() {
676 mkerr := os.MkdirAll(target, 0777)
680 return os.Chmod(target, walkinfo.Mode()|os.ModeSetgid|0777)
682 return fmt.Errorf("source %q is not a regular file or directory", cp.src)
685 } else if st.Mode().IsRegular() {
686 err = copyfile(cp.src, cp.bind)
688 err = os.Chmod(cp.bind, st.Mode()|0777)
692 return fmt.Errorf("while staging writable file from %q to %q: %v", cp.src, cp.bind, err)
699 func (runner *ContainerRunner) ProcessDockerAttach(containerReader io.Reader) {
700 // Handle docker log protocol
701 // https://docs.docker.com/engine/reference/api/docker_remote_api_v1.15/#attach-to-a-container
702 defer close(runner.loggingDone)
704 header := make([]byte, 8)
707 _, err = io.ReadAtLeast(containerReader, header, 8)
714 readsize := int64(header[7]) | (int64(header[6]) << 8) | (int64(header[5]) << 16) | (int64(header[4]) << 24)
717 _, err = io.CopyN(runner.Stdout, containerReader, readsize)
720 _, err = io.CopyN(runner.Stderr, containerReader, readsize)
725 runner.CrunchLog.Printf("error reading docker logs: %v", err)
728 err = runner.Stdout.Close()
730 runner.CrunchLog.Printf("error closing stdout logs: %v", err)
733 err = runner.Stderr.Close()
735 runner.CrunchLog.Printf("error closing stderr logs: %v", err)
738 if runner.statReporter != nil {
739 runner.statReporter.Stop()
740 err = runner.statLogger.Close()
742 runner.CrunchLog.Printf("error closing crunchstat logs: %v", err)
747 func (runner *ContainerRunner) stopHoststat() error {
748 if runner.hoststatReporter == nil {
751 runner.hoststatReporter.Stop()
752 err := runner.hoststatLogger.Close()
754 return fmt.Errorf("error closing hoststat logs: %v", err)
759 func (runner *ContainerRunner) startHoststat() error {
760 w, err := runner.NewLogWriter("hoststat")
764 runner.hoststatLogger = NewThrottledLogger(w)
765 runner.hoststatReporter = &crunchstat.Reporter{
766 Logger: log.New(runner.hoststatLogger, "", 0),
767 CgroupRoot: runner.cgroupRoot,
768 PollPeriod: runner.statInterval,
770 runner.hoststatReporter.Start()
774 func (runner *ContainerRunner) startCrunchstat() error {
775 w, err := runner.NewLogWriter("crunchstat")
779 runner.statLogger = NewThrottledLogger(w)
780 runner.statReporter = &crunchstat.Reporter{
781 CID: runner.ContainerID,
782 Logger: log.New(runner.statLogger, "", 0),
783 CgroupParent: runner.expectCgroupParent,
784 CgroupRoot: runner.cgroupRoot,
785 PollPeriod: runner.statInterval,
786 TempDir: runner.parentTemp,
788 runner.statReporter.Start()
792 type infoCommand struct {
797 // LogHostInfo logs info about the current host, for debugging and
798 // accounting purposes. Although it's logged as "node-info", this is
799 // about the environment where crunch-run is actually running, which
800 // might differ from what's described in the node record (see
802 func (runner *ContainerRunner) LogHostInfo() (err error) {
803 w, err := runner.NewLogWriter("node-info")
808 commands := []infoCommand{
810 label: "Host Information",
811 cmd: []string{"uname", "-a"},
814 label: "CPU Information",
815 cmd: []string{"cat", "/proc/cpuinfo"},
818 label: "Memory Information",
819 cmd: []string{"cat", "/proc/meminfo"},
823 cmd: []string{"df", "-m", "/", os.TempDir()},
826 label: "Disk INodes",
827 cmd: []string{"df", "-i", "/", os.TempDir()},
831 // Run commands with informational output to be logged.
832 for _, command := range commands {
833 fmt.Fprintln(w, command.label)
834 cmd := exec.Command(command.cmd[0], command.cmd[1:]...)
837 if err := cmd.Run(); err != nil {
838 err = fmt.Errorf("While running command %q: %v", command.cmd, err)
847 return fmt.Errorf("While closing node-info logs: %v", err)
852 // LogContainerRecord gets and saves the raw JSON container record from the API server
853 func (runner *ContainerRunner) LogContainerRecord() error {
854 logged, err := runner.logAPIResponse("container", "containers", map[string]interface{}{"filters": [][]string{{"uuid", "=", runner.Container.UUID}}}, nil)
855 if !logged && err == nil {
856 err = fmt.Errorf("error: no container record found for %s", runner.Container.UUID)
861 // LogNodeRecord logs the current host's InstanceType config entry (or
862 // the arvados#node record, if running via crunch-dispatch-slurm).
863 func (runner *ContainerRunner) LogNodeRecord() error {
864 if it := os.Getenv("InstanceType"); it != "" {
865 // Dispatched via arvados-dispatch-cloud. Save
866 // InstanceType config fragment received from
867 // dispatcher on stdin.
868 w, err := runner.LogCollection.OpenFile("node.json", os.O_CREATE|os.O_WRONLY, 0666)
873 _, err = io.WriteString(w, it)
879 // Dispatched via crunch-dispatch-slurm. Look up
880 // apiserver's node record corresponding to
882 hostname := os.Getenv("SLURMD_NODENAME")
884 hostname, _ = os.Hostname()
886 _, err := runner.logAPIResponse("node", "nodes", map[string]interface{}{"filters": [][]string{{"hostname", "=", hostname}}}, func(resp interface{}) {
887 // The "info" field has admin-only info when
888 // obtained with a privileged token, and
889 // should not be logged.
890 node, ok := resp.(map[string]interface{})
898 func (runner *ContainerRunner) logAPIResponse(label, path string, params map[string]interface{}, munge func(interface{})) (logged bool, err error) {
899 writer, err := runner.LogCollection.OpenFile(label+".json", os.O_CREATE|os.O_WRONLY, 0666)
904 ArvClient: runner.DispatcherArvClient,
905 UUID: runner.Container.UUID,
906 loggingStream: label,
910 reader, err := runner.DispatcherArvClient.CallRaw("GET", path, "", "", arvadosclient.Dict(params))
912 return false, fmt.Errorf("error getting %s record: %v", label, err)
916 dec := json.NewDecoder(reader)
918 var resp map[string]interface{}
919 if err = dec.Decode(&resp); err != nil {
920 return false, fmt.Errorf("error decoding %s list response: %v", label, err)
922 items, ok := resp["items"].([]interface{})
924 return false, fmt.Errorf("error decoding %s list response: no \"items\" key in API list response", label)
925 } else if len(items) < 1 {
931 // Re-encode it using indentation to improve readability
932 enc := json.NewEncoder(w)
933 enc.SetIndent("", " ")
934 if err = enc.Encode(items[0]); err != nil {
935 return false, fmt.Errorf("error logging %s record: %v", label, err)
939 return false, fmt.Errorf("error closing %s.json in log collection: %v", label, err)
944 // AttachStreams connects the docker container stdin, stdout and stderr logs
945 // to the Arvados logger which logs to Keep and the API server logs table.
946 func (runner *ContainerRunner) AttachStreams() (err error) {
948 runner.CrunchLog.Print("Attaching container streams")
950 // If stdin mount is provided, attach it to the docker container
951 var stdinRdr arvados.File
953 if stdinMnt, ok := runner.Container.Mounts["stdin"]; ok {
954 if stdinMnt.Kind == "collection" {
955 var stdinColl arvados.Collection
956 collID := stdinMnt.UUID
958 collID = stdinMnt.PortableDataHash
960 err = runner.ContainerArvClient.Get("collections", collID, nil, &stdinColl)
962 return fmt.Errorf("While getting stdin collection: %v", err)
965 stdinRdr, err = runner.ContainerKeepClient.ManifestFileReader(
966 manifest.Manifest{Text: stdinColl.ManifestText},
968 if os.IsNotExist(err) {
969 return fmt.Errorf("stdin collection path not found: %v", stdinMnt.Path)
970 } else if err != nil {
971 return fmt.Errorf("While getting stdin collection path %v: %v", stdinMnt.Path, err)
973 } else if stdinMnt.Kind == "json" {
974 stdinJSON, err = json.Marshal(stdinMnt.Content)
976 return fmt.Errorf("While encoding stdin json data: %v", err)
981 stdinUsed := stdinRdr != nil || len(stdinJSON) != 0
982 response, err := runner.Docker.ContainerAttach(context.TODO(), runner.ContainerID,
983 dockertypes.ContainerAttachOptions{Stream: true, Stdin: stdinUsed, Stdout: true, Stderr: true})
985 return fmt.Errorf("While attaching container stdout/stderr streams: %v", err)
988 runner.loggingDone = make(chan bool)
990 if stdoutMnt, ok := runner.Container.Mounts["stdout"]; ok {
991 stdoutFile, err := runner.getStdoutFile(stdoutMnt.Path)
995 runner.Stdout = stdoutFile
996 } else if w, err := runner.NewLogWriter("stdout"); err != nil {
999 runner.Stdout = NewThrottledLogger(w)
1002 if stderrMnt, ok := runner.Container.Mounts["stderr"]; ok {
1003 stderrFile, err := runner.getStdoutFile(stderrMnt.Path)
1007 runner.Stderr = stderrFile
1008 } else if w, err := runner.NewLogWriter("stderr"); err != nil {
1011 runner.Stderr = NewThrottledLogger(w)
1014 if stdinRdr != nil {
1016 _, err := io.Copy(response.Conn, stdinRdr)
1018 runner.CrunchLog.Printf("While writing stdin collection to docker container: %v", err)
1022 response.CloseWrite()
1024 } else if len(stdinJSON) != 0 {
1026 _, err := io.Copy(response.Conn, bytes.NewReader(stdinJSON))
1028 runner.CrunchLog.Printf("While writing stdin json to docker container: %v", err)
1031 response.CloseWrite()
1035 go runner.ProcessDockerAttach(response.Reader)
1040 func (runner *ContainerRunner) getStdoutFile(mntPath string) (*os.File, error) {
1041 stdoutPath := mntPath[len(runner.Container.OutputPath):]
1042 index := strings.LastIndex(stdoutPath, "/")
1044 subdirs := stdoutPath[:index]
1046 st, err := os.Stat(runner.HostOutputDir)
1048 return nil, fmt.Errorf("While Stat on temp dir: %v", err)
1050 stdoutPath := filepath.Join(runner.HostOutputDir, subdirs)
1051 err = os.MkdirAll(stdoutPath, st.Mode()|os.ModeSetgid|0777)
1053 return nil, fmt.Errorf("While MkdirAll %q: %v", stdoutPath, err)
1057 stdoutFile, err := os.Create(filepath.Join(runner.HostOutputDir, stdoutPath))
1059 return nil, fmt.Errorf("While creating file %q: %v", stdoutPath, err)
1062 return stdoutFile, nil
1065 // CreateContainer creates the docker container.
1066 func (runner *ContainerRunner) CreateContainer() error {
1067 runner.CrunchLog.Print("Creating Docker container")
1069 runner.ContainerConfig.Cmd = runner.Container.Command
1070 if runner.Container.Cwd != "." {
1071 runner.ContainerConfig.WorkingDir = runner.Container.Cwd
1074 for k, v := range runner.Container.Environment {
1075 runner.ContainerConfig.Env = append(runner.ContainerConfig.Env, k+"="+v)
1078 runner.ContainerConfig.Volumes = runner.Volumes
1080 maxRAM := int64(runner.Container.RuntimeConstraints.RAM)
1081 minDockerRAM := int64(16)
1082 if maxRAM < minDockerRAM*1024*1024 {
1083 // Docker daemon won't let you set a limit less than ~10 MiB
1084 maxRAM = minDockerRAM * 1024 * 1024
1086 runner.HostConfig = dockercontainer.HostConfig{
1087 Binds: runner.Binds,
1088 LogConfig: dockercontainer.LogConfig{
1091 Resources: dockercontainer.Resources{
1092 CgroupParent: runner.setCgroupParent,
1093 NanoCPUs: int64(runner.Container.RuntimeConstraints.VCPUs) * 1000000000,
1094 Memory: maxRAM, // RAM
1095 MemorySwap: maxRAM, // RAM+swap
1096 KernelMemory: maxRAM, // kernel portion
1100 if wantAPI := runner.Container.RuntimeConstraints.API; wantAPI != nil && *wantAPI {
1101 tok, err := runner.ContainerToken()
1105 runner.ContainerConfig.Env = append(runner.ContainerConfig.Env,
1106 "ARVADOS_API_TOKEN="+tok,
1107 "ARVADOS_API_HOST="+os.Getenv("ARVADOS_API_HOST"),
1108 "ARVADOS_API_HOST_INSECURE="+os.Getenv("ARVADOS_API_HOST_INSECURE"),
1110 runner.HostConfig.NetworkMode = dockercontainer.NetworkMode(runner.networkMode)
1112 if runner.enableNetwork == "always" {
1113 runner.HostConfig.NetworkMode = dockercontainer.NetworkMode(runner.networkMode)
1115 runner.HostConfig.NetworkMode = dockercontainer.NetworkMode("none")
1119 _, stdinUsed := runner.Container.Mounts["stdin"]
1120 runner.ContainerConfig.OpenStdin = stdinUsed
1121 runner.ContainerConfig.StdinOnce = stdinUsed
1122 runner.ContainerConfig.AttachStdin = stdinUsed
1123 runner.ContainerConfig.AttachStdout = true
1124 runner.ContainerConfig.AttachStderr = true
1126 createdBody, err := runner.Docker.ContainerCreate(context.TODO(), &runner.ContainerConfig, &runner.HostConfig, nil, runner.Container.UUID)
1128 return fmt.Errorf("While creating container: %v", err)
1131 runner.ContainerID = createdBody.ID
1133 return runner.AttachStreams()
1136 // StartContainer starts the docker container created by CreateContainer.
1137 func (runner *ContainerRunner) StartContainer() error {
1138 runner.CrunchLog.Printf("Starting Docker container id '%s'", runner.ContainerID)
1139 runner.cStateLock.Lock()
1140 defer runner.cStateLock.Unlock()
1141 if runner.cCancelled {
1144 err := runner.Docker.ContainerStart(context.TODO(), runner.ContainerID,
1145 dockertypes.ContainerStartOptions{})
1148 if m, e := regexp.MatchString("(?ms).*(exec|System error).*(no such file or directory|file not found).*", err.Error()); m && e == nil {
1149 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])
1151 return fmt.Errorf("could not start container: %v%s", err, advice)
1156 // WaitFinish waits for the container to terminate, capture the exit code, and
1157 // close the stdout/stderr logging.
1158 func (runner *ContainerRunner) WaitFinish() error {
1159 var runTimeExceeded <-chan time.Time
1160 runner.CrunchLog.Print("Waiting for container to finish")
1162 waitOk, waitErr := runner.Docker.ContainerWait(context.TODO(), runner.ContainerID, dockercontainer.WaitConditionNotRunning)
1163 arvMountExit := runner.ArvMountExit
1164 if timeout := runner.Container.SchedulingParameters.MaxRunTime; timeout > 0 {
1165 runTimeExceeded = time.After(time.Duration(timeout) * time.Second)
1168 containerGone := make(chan struct{})
1170 defer close(containerGone)
1171 if runner.containerWatchdogInterval < 1 {
1172 runner.containerWatchdogInterval = time.Minute
1174 for range time.NewTicker(runner.containerWatchdogInterval).C {
1175 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(runner.containerWatchdogInterval))
1176 ctr, err := runner.Docker.ContainerInspect(ctx, runner.ContainerID)
1178 runner.cStateLock.Lock()
1179 done := runner.cRemoved || runner.ExitCode != nil
1180 runner.cStateLock.Unlock()
1183 } else if err != nil {
1184 runner.CrunchLog.Printf("Error inspecting container: %s", err)
1185 runner.checkBrokenNode(err)
1187 } else if ctr.State == nil || !(ctr.State.Running || ctr.State.Status == "created") {
1188 runner.CrunchLog.Printf("Container is not running: State=%v", ctr.State)
1196 case waitBody := <-waitOk:
1197 runner.CrunchLog.Printf("Container exited with code: %v", waitBody.StatusCode)
1198 code := int(waitBody.StatusCode)
1199 runner.ExitCode = &code
1201 // wait for stdout/stderr to complete
1202 <-runner.loggingDone
1205 case err := <-waitErr:
1206 return fmt.Errorf("container wait: %v", err)
1208 case <-arvMountExit:
1209 runner.CrunchLog.Printf("arv-mount exited while container is still running. Stopping container.")
1211 // arvMountExit will always be ready now that
1212 // it's closed, but that doesn't interest us.
1215 case <-runTimeExceeded:
1216 runner.CrunchLog.Printf("maximum run time exceeded. Stopping container.")
1218 runTimeExceeded = nil
1220 case <-containerGone:
1221 return errors.New("docker client never returned status")
1226 func (runner *ContainerRunner) updateLogs() {
1227 ticker := time.NewTicker(crunchLogUpdatePeriod / 360)
1230 sigusr1 := make(chan os.Signal, 1)
1231 signal.Notify(sigusr1, syscall.SIGUSR1)
1232 defer signal.Stop(sigusr1)
1234 saveAtTime := time.Now().Add(crunchLogUpdatePeriod)
1235 saveAtSize := crunchLogUpdateSize
1241 saveAtTime = time.Now()
1243 runner.logMtx.Lock()
1244 done := runner.LogsPDH != nil
1245 runner.logMtx.Unlock()
1249 size := runner.LogCollection.Size()
1250 if size == savedSize || (time.Now().Before(saveAtTime) && size < saveAtSize) {
1253 saveAtTime = time.Now().Add(crunchLogUpdatePeriod)
1254 saveAtSize = runner.LogCollection.Size() + crunchLogUpdateSize
1255 saved, err := runner.saveLogCollection(false)
1257 runner.CrunchLog.Printf("error updating log collection: %s", err)
1261 var updated arvados.Container
1262 err = runner.DispatcherArvClient.Update("containers", runner.Container.UUID, arvadosclient.Dict{
1263 "container": arvadosclient.Dict{"log": saved.PortableDataHash},
1266 runner.CrunchLog.Printf("error updating container log to %s: %s", saved.PortableDataHash, err)
1274 // CaptureOutput saves data from the container's output directory if
1275 // needed, and updates the container output accordingly.
1276 func (runner *ContainerRunner) CaptureOutput() error {
1277 if wantAPI := runner.Container.RuntimeConstraints.API; wantAPI != nil && *wantAPI {
1278 // Output may have been set directly by the container, so
1279 // refresh the container record to check.
1280 err := runner.DispatcherArvClient.Get("containers", runner.Container.UUID,
1281 nil, &runner.Container)
1285 if runner.Container.Output != "" {
1286 // Container output is already set.
1287 runner.OutputPDH = &runner.Container.Output
1292 txt, err := (&copier{
1293 client: runner.containerClient,
1294 arvClient: runner.ContainerArvClient,
1295 keepClient: runner.ContainerKeepClient,
1296 hostOutputDir: runner.HostOutputDir,
1297 ctrOutputDir: runner.Container.OutputPath,
1298 binds: runner.Binds,
1299 mounts: runner.Container.Mounts,
1300 secretMounts: runner.SecretMounts,
1301 logger: runner.CrunchLog,
1306 if n := len(regexp.MustCompile(` [0-9a-f]+\+\S*\+R`).FindAllStringIndex(txt, -1)); n > 0 {
1307 runner.CrunchLog.Printf("Copying %d data blocks from remote input collections...", n)
1308 fs, err := (&arvados.Collection{ManifestText: txt}).FileSystem(runner.containerClient, runner.ContainerKeepClient)
1312 txt, err = fs.MarshalManifest(".")
1317 var resp arvados.Collection
1318 err = runner.ContainerArvClient.Create("collections", arvadosclient.Dict{
1319 "ensure_unique_name": true,
1320 "collection": arvadosclient.Dict{
1322 "name": "output for " + runner.Container.UUID,
1323 "manifest_text": txt,
1327 return fmt.Errorf("error creating output collection: %v", err)
1329 runner.OutputPDH = &resp.PortableDataHash
1333 func (runner *ContainerRunner) CleanupDirs() {
1334 if runner.ArvMount != nil {
1336 umount := exec.Command("arv-mount", fmt.Sprintf("--unmount-timeout=%d", delay), "--unmount", runner.ArvMountPoint)
1337 umount.Stdout = runner.CrunchLog
1338 umount.Stderr = runner.CrunchLog
1339 runner.CrunchLog.Printf("Running %v", umount.Args)
1340 umnterr := umount.Start()
1343 runner.CrunchLog.Printf("Error unmounting: %v", umnterr)
1345 // If arv-mount --unmount gets stuck for any reason, we
1346 // don't want to wait for it forever. Do Wait() in a goroutine
1347 // so it doesn't block crunch-run.
1348 umountExit := make(chan error)
1350 mnterr := umount.Wait()
1352 runner.CrunchLog.Printf("Error unmounting: %v", mnterr)
1354 umountExit <- mnterr
1357 for again := true; again; {
1363 case <-runner.ArvMountExit:
1365 case <-time.After(time.Duration((delay + 1) * int64(time.Second))):
1366 runner.CrunchLog.Printf("Timed out waiting for unmount")
1368 umount.Process.Kill()
1370 runner.ArvMount.Process.Kill()
1376 if runner.ArvMountPoint != "" {
1377 if rmerr := os.Remove(runner.ArvMountPoint); rmerr != nil {
1378 runner.CrunchLog.Printf("While cleaning up arv-mount directory %s: %v", runner.ArvMountPoint, rmerr)
1382 if rmerr := os.RemoveAll(runner.parentTemp); rmerr != nil {
1383 runner.CrunchLog.Printf("While cleaning up temporary directory %s: %v", runner.parentTemp, rmerr)
1387 // CommitLogs posts the collection containing the final container logs.
1388 func (runner *ContainerRunner) CommitLogs() error {
1390 // Hold cStateLock to prevent races on CrunchLog (e.g., stop()).
1391 runner.cStateLock.Lock()
1392 defer runner.cStateLock.Unlock()
1394 runner.CrunchLog.Print(runner.finalState)
1396 if runner.arvMountLog != nil {
1397 runner.arvMountLog.Close()
1399 runner.CrunchLog.Close()
1401 // Closing CrunchLog above allows them to be committed to Keep at this
1402 // point, but re-open crunch log with ArvClient in case there are any
1403 // other further errors (such as failing to write the log to Keep!)
1404 // while shutting down
1405 runner.CrunchLog = NewThrottledLogger(&ArvLogWriter{
1406 ArvClient: runner.DispatcherArvClient,
1407 UUID: runner.Container.UUID,
1408 loggingStream: "crunch-run",
1411 runner.CrunchLog.Immediate = log.New(os.Stderr, runner.Container.UUID+" ", 0)
1414 if runner.LogsPDH != nil {
1415 // If we have already assigned something to LogsPDH,
1416 // we must be closing the re-opened log, which won't
1417 // end up getting attached to the container record and
1418 // therefore doesn't need to be saved as a collection
1419 // -- it exists only to send logs to other channels.
1422 saved, err := runner.saveLogCollection(true)
1424 return fmt.Errorf("error saving log collection: %s", err)
1426 runner.logMtx.Lock()
1427 defer runner.logMtx.Unlock()
1428 runner.LogsPDH = &saved.PortableDataHash
1432 func (runner *ContainerRunner) saveLogCollection(final bool) (response arvados.Collection, err error) {
1433 runner.logMtx.Lock()
1434 defer runner.logMtx.Unlock()
1435 if runner.LogsPDH != nil {
1436 // Already finalized.
1439 mt, err := runner.LogCollection.MarshalManifest(".")
1441 err = fmt.Errorf("error creating log manifest: %v", err)
1444 updates := arvadosclient.Dict{
1445 "name": "logs for " + runner.Container.UUID,
1446 "manifest_text": mt,
1449 updates["is_trashed"] = true
1451 exp := time.Now().Add(crunchLogUpdatePeriod * 24)
1452 updates["trash_at"] = exp
1453 updates["delete_at"] = exp
1455 reqBody := arvadosclient.Dict{"collection": updates}
1456 if runner.logUUID == "" {
1457 reqBody["ensure_unique_name"] = true
1458 err = runner.DispatcherArvClient.Create("collections", reqBody, &response)
1460 err = runner.DispatcherArvClient.Update("collections", runner.logUUID, reqBody, &response)
1465 runner.logUUID = response.UUID
1469 // UpdateContainerRunning updates the container state to "Running"
1470 func (runner *ContainerRunner) UpdateContainerRunning() error {
1471 runner.cStateLock.Lock()
1472 defer runner.cStateLock.Unlock()
1473 if runner.cCancelled {
1476 return runner.DispatcherArvClient.Update("containers", runner.Container.UUID,
1477 arvadosclient.Dict{"container": arvadosclient.Dict{"state": "Running", "gateway_address": runner.gatewayAddress}}, nil)
1480 // ContainerToken returns the api_token the container (and any
1481 // arv-mount processes) are allowed to use.
1482 func (runner *ContainerRunner) ContainerToken() (string, error) {
1483 if runner.token != "" {
1484 return runner.token, nil
1487 var auth arvados.APIClientAuthorization
1488 err := runner.DispatcherArvClient.Call("GET", "containers", runner.Container.UUID, "auth", nil, &auth)
1492 runner.token = fmt.Sprintf("v2/%s/%s/%s", auth.UUID, auth.APIToken, runner.Container.UUID)
1493 return runner.token, nil
1496 // UpdateContainerFinal updates the container record state on API
1497 // server to "Complete" or "Cancelled"
1498 func (runner *ContainerRunner) UpdateContainerFinal() error {
1499 update := arvadosclient.Dict{}
1500 update["state"] = runner.finalState
1501 if runner.LogsPDH != nil {
1502 update["log"] = *runner.LogsPDH
1504 if runner.finalState == "Complete" {
1505 if runner.ExitCode != nil {
1506 update["exit_code"] = *runner.ExitCode
1508 if runner.OutputPDH != nil {
1509 update["output"] = *runner.OutputPDH
1512 return runner.DispatcherArvClient.Update("containers", runner.Container.UUID, arvadosclient.Dict{"container": update}, nil)
1515 // IsCancelled returns the value of Cancelled, with goroutine safety.
1516 func (runner *ContainerRunner) IsCancelled() bool {
1517 runner.cStateLock.Lock()
1518 defer runner.cStateLock.Unlock()
1519 return runner.cCancelled
1522 // NewArvLogWriter creates an ArvLogWriter
1523 func (runner *ContainerRunner) NewArvLogWriter(name string) (io.WriteCloser, error) {
1524 writer, err := runner.LogCollection.OpenFile(name+".txt", os.O_CREATE|os.O_WRONLY, 0666)
1528 return &ArvLogWriter{
1529 ArvClient: runner.DispatcherArvClient,
1530 UUID: runner.Container.UUID,
1531 loggingStream: name,
1532 writeCloser: writer,
1536 // Run the full container lifecycle.
1537 func (runner *ContainerRunner) Run() (err error) {
1538 runner.CrunchLog.Printf("crunch-run %s started", cmd.Version.String())
1539 runner.CrunchLog.Printf("Executing container '%s'", runner.Container.UUID)
1541 hostname, hosterr := os.Hostname()
1543 runner.CrunchLog.Printf("Error getting hostname '%v'", hosterr)
1545 runner.CrunchLog.Printf("Executing on host '%s'", hostname)
1548 runner.finalState = "Queued"
1551 runner.CleanupDirs()
1553 runner.CrunchLog.Printf("crunch-run finished")
1554 runner.CrunchLog.Close()
1557 err = runner.fetchContainerRecord()
1561 if runner.Container.State != "Locked" {
1562 return fmt.Errorf("dispatch error detected: container %q has state %q", runner.Container.UUID, runner.Container.State)
1566 // checkErr prints e (unless it's nil) and sets err to
1567 // e (unless err is already non-nil). Thus, if err
1568 // hasn't already been assigned when Run() returns,
1569 // this cleanup func will cause Run() to return the
1570 // first non-nil error that is passed to checkErr().
1571 checkErr := func(errorIn string, e error) {
1575 runner.CrunchLog.Printf("error in %s: %v", errorIn, e)
1579 if runner.finalState == "Complete" {
1580 // There was an error in the finalization.
1581 runner.finalState = "Cancelled"
1585 // Log the error encountered in Run(), if any
1586 checkErr("Run", err)
1588 if runner.finalState == "Queued" {
1589 runner.UpdateContainerFinal()
1593 if runner.IsCancelled() {
1594 runner.finalState = "Cancelled"
1595 // but don't return yet -- we still want to
1596 // capture partial output and write logs
1599 checkErr("CaptureOutput", runner.CaptureOutput())
1600 checkErr("stopHoststat", runner.stopHoststat())
1601 checkErr("CommitLogs", runner.CommitLogs())
1602 checkErr("UpdateContainerFinal", runner.UpdateContainerFinal())
1605 runner.setupSignals()
1606 err = runner.startHoststat()
1611 // check for and/or load image
1612 err = runner.LoadImage()
1614 if !runner.checkBrokenNode(err) {
1615 // Failed to load image but not due to a "broken node"
1616 // condition, probably user error.
1617 runner.finalState = "Cancelled"
1619 err = fmt.Errorf("While loading container image: %v", err)
1623 // set up FUSE mount and binds
1624 err = runner.SetupMounts()
1626 runner.finalState = "Cancelled"
1627 err = fmt.Errorf("While setting up mounts: %v", err)
1631 err = runner.CreateContainer()
1635 err = runner.LogHostInfo()
1639 err = runner.LogNodeRecord()
1643 err = runner.LogContainerRecord()
1648 if runner.IsCancelled() {
1652 err = runner.UpdateContainerRunning()
1656 runner.finalState = "Cancelled"
1658 err = runner.startCrunchstat()
1663 err = runner.StartContainer()
1665 runner.checkBrokenNode(err)
1669 err = runner.WaitFinish()
1670 if err == nil && !runner.IsCancelled() {
1671 runner.finalState = "Complete"
1676 // Fetch the current container record (uuid = runner.Container.UUID)
1677 // into runner.Container.
1678 func (runner *ContainerRunner) fetchContainerRecord() error {
1679 reader, err := runner.DispatcherArvClient.CallRaw("GET", "containers", runner.Container.UUID, "", nil)
1681 return fmt.Errorf("error fetching container record: %v", err)
1683 defer reader.Close()
1685 dec := json.NewDecoder(reader)
1687 err = dec.Decode(&runner.Container)
1689 return fmt.Errorf("error decoding container record: %v", err)
1693 SecretMounts map[string]arvados.Mount `json:"secret_mounts"`
1696 containerToken, err := runner.ContainerToken()
1698 return fmt.Errorf("error getting container token: %v", err)
1701 runner.ContainerArvClient, runner.ContainerKeepClient,
1702 runner.containerClient, err = runner.MkArvClient(containerToken)
1704 return fmt.Errorf("error creating container API client: %v", err)
1707 err = runner.ContainerArvClient.Call("GET", "containers", runner.Container.UUID, "secret_mounts", nil, &sm)
1709 if apierr, ok := err.(arvadosclient.APIServerError); !ok || apierr.HttpStatusCode != 404 {
1710 return fmt.Errorf("error fetching secret_mounts: %v", err)
1712 // ok && apierr.HttpStatusCode == 404, which means
1713 // secret_mounts isn't supported by this API server.
1715 runner.SecretMounts = sm.SecretMounts
1720 // NewContainerRunner creates a new container runner.
1721 func NewContainerRunner(dispatcherClient *arvados.Client,
1722 dispatcherArvClient IArvadosClient,
1723 dispatcherKeepClient IKeepClient,
1724 docker ThinDockerClient,
1725 containerUUID string) (*ContainerRunner, error) {
1727 cr := &ContainerRunner{
1728 dispatcherClient: dispatcherClient,
1729 DispatcherArvClient: dispatcherArvClient,
1730 DispatcherKeepClient: dispatcherKeepClient,
1733 cr.NewLogWriter = cr.NewArvLogWriter
1734 cr.RunArvMount = cr.ArvMountCmd
1735 cr.MkTempDir = ioutil.TempDir
1736 cr.MkArvClient = func(token string) (IArvadosClient, IKeepClient, *arvados.Client, error) {
1737 cl, err := arvadosclient.MakeArvadosClient()
1739 return nil, nil, nil, err
1742 kc, err := keepclient.MakeKeepClient(cl)
1744 return nil, nil, nil, err
1746 c2 := arvados.NewClientFromEnv()
1747 c2.AuthToken = token
1748 return cl, kc, c2, nil
1751 cr.LogCollection, err = (&arvados.Collection{}).FileSystem(cr.dispatcherClient, cr.DispatcherKeepClient)
1755 cr.Container.UUID = containerUUID
1756 w, err := cr.NewLogWriter("crunch-run")
1760 cr.CrunchLog = NewThrottledLogger(w)
1761 cr.CrunchLog.Immediate = log.New(os.Stderr, containerUUID+" ", 0)
1763 loadLogThrottleParams(dispatcherArvClient)
1769 func (command) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
1770 flags := flag.NewFlagSet(prog, flag.ContinueOnError)
1771 statInterval := flags.Duration("crunchstat-interval", 10*time.Second, "sampling period for periodic resource usage reporting")
1772 cgroupRoot := flags.String("cgroup-root", "/sys/fs/cgroup", "path to sysfs cgroup tree")
1773 cgroupParent := flags.String("cgroup-parent", "docker", "name of container's parent cgroup (ignored if -cgroup-parent-subsystem is used)")
1774 cgroupParentSubsystem := flags.String("cgroup-parent-subsystem", "", "use current cgroup for given subsystem as parent cgroup for container")
1775 caCertsPath := flags.String("ca-certs", "", "Path to TLS root certificates")
1776 detach := flags.Bool("detach", false, "Detach from parent process and run in the background")
1777 stdinEnv := flags.Bool("stdin-env", false, "Load environment variables from JSON message on stdin")
1778 sleep := flags.Duration("sleep", 0, "Delay before starting (testing use only)")
1779 kill := flags.Int("kill", -1, "Send signal to an existing crunch-run process for given UUID")
1780 list := flags.Bool("list", false, "List UUIDs of existing crunch-run processes")
1781 enableNetwork := flags.String("container-enable-networking", "default",
1782 `Specify if networking should be enabled for container. One of 'default', 'always':
1783 default: only enable networking if container requests it.
1784 always: containers always have networking enabled
1786 networkMode := flags.String("container-network-mode", "default",
1787 `Set networking mode for container. Corresponds to Docker network mode (--net).
1789 memprofile := flags.String("memprofile", "", "write memory profile to `file` after running container")
1790 flags.Duration("check-containerd", 0, "Ignored. Exists for compatibility with older versions.")
1792 ignoreDetachFlag := false
1793 if len(args) > 0 && args[0] == "-no-detach" {
1794 // This process was invoked by a parent process, which
1795 // has passed along its own arguments, including
1796 // -detach, after the leading -no-detach flag. Strip
1797 // the leading -no-detach flag (it's not recognized by
1798 // flags.Parse()) and ignore the -detach flag that
1801 ignoreDetachFlag = true
1804 if err := flags.Parse(args); err == flag.ErrHelp {
1806 } else if err != nil {
1811 if *stdinEnv && !ignoreDetachFlag {
1812 // Load env vars on stdin if asked (but not in a
1813 // detached child process, in which case stdin is
1815 err := loadEnv(os.Stdin)
1822 containerID := flags.Arg(0)
1825 case *detach && !ignoreDetachFlag:
1826 return Detach(containerID, prog, args, os.Stdout, os.Stderr)
1828 return KillProcess(containerID, syscall.Signal(*kill), os.Stdout, os.Stderr)
1830 return ListProcesses(os.Stdout, os.Stderr)
1833 if containerID == "" {
1834 log.Printf("usage: %s [options] UUID", prog)
1838 log.Printf("crunch-run %s started", cmd.Version.String())
1841 if *caCertsPath != "" {
1842 arvadosclient.CertFiles = []string{*caCertsPath}
1845 api, err := arvadosclient.MakeArvadosClient()
1847 log.Printf("%s: %v", containerID, err)
1852 kc, kcerr := keepclient.MakeKeepClient(api)
1854 log.Printf("%s: %v", containerID, kcerr)
1857 kc.BlockCache = &keepclient.BlockCache{MaxBlocks: 2}
1860 // API version 1.21 corresponds to Docker 1.9, which is currently the
1861 // minimum version we want to support.
1862 docker, dockererr := dockerclient.NewClient(dockerclient.DefaultDockerHost, "1.21", nil, nil)
1864 cr, err := NewContainerRunner(arvados.NewClientFromEnv(), api, kc, docker, containerID)
1869 if dockererr != nil {
1870 cr.CrunchLog.Printf("%s: %v", containerID, dockererr)
1871 cr.checkBrokenNode(dockererr)
1872 cr.CrunchLog.Close()
1876 cr.gatewayAuthSecret = os.Getenv("GatewayAuthSecret")
1877 os.Unsetenv("GatewayAuthSecret")
1878 err = cr.startGatewayServer()
1880 log.Printf("error starting gateway server: %s", err)
1884 parentTemp, tmperr := cr.MkTempDir("", "crunch-run."+containerID+".")
1886 log.Printf("%s: %v", containerID, tmperr)
1890 cr.parentTemp = parentTemp
1891 cr.statInterval = *statInterval
1892 cr.cgroupRoot = *cgroupRoot
1893 cr.expectCgroupParent = *cgroupParent
1894 cr.enableNetwork = *enableNetwork
1895 cr.networkMode = *networkMode
1896 if *cgroupParentSubsystem != "" {
1897 p := findCgroup(*cgroupParentSubsystem)
1898 cr.setCgroupParent = p
1899 cr.expectCgroupParent = p
1904 if *memprofile != "" {
1905 f, err := os.Create(*memprofile)
1907 log.Printf("could not create memory profile: %s", err)
1909 runtime.GC() // get up-to-date statistics
1910 if err := pprof.WriteHeapProfile(f); err != nil {
1911 log.Printf("could not write memory profile: %s", err)
1913 closeerr := f.Close()
1914 if closeerr != nil {
1915 log.Printf("closing memprofile file: %s", err)
1920 log.Printf("%s: %v", containerID, runerr)
1926 func loadEnv(rdr io.Reader) error {
1927 buf, err := ioutil.ReadAll(rdr)
1929 return fmt.Errorf("read stdin: %s", err)
1931 var env map[string]string
1932 err = json.Unmarshal(buf, &env)
1934 return fmt.Errorf("decode stdin: %s", err)
1936 for k, v := range env {
1937 err = os.Setenv(k, v)
1939 return fmt.Errorf("setenv(%q): %s", k, err)