Skip to content

Exec: inject stdin, capture stdout and stderr #1620

Closed

Description

@shykes

Overview

It would be useful to allow injecting data into a command's standard input, and capture its standard output and error streams.
Unlike an earlier proposal, data would be buffered and not streamed.

Proposed design:

// dagger.io/dagger/engine
package engine

#Exec: {
  stdin?: string
  stdout?: string
  stdout?: string
  ...
}
// universe.dagger.io/docker
package docker

#Run: {
  stdin?: string
  stdout?: string
  stdout?: string
  ...
}

Activity

  1. added this to the milestone on Feb 15, 2022
  2. talentedmrjones commented on Feb 16, 2022

    @talentedmrjones

    I asked Slack #buildkit and tonistiigi said:

    in llb.Run you should write to and read from actual files that you can move around with mounts. we have an interactive api as well that does give stdio access but that is not for build. mostly it is for debugging

    (emphasis mine)

    I'm looking into ways to form a CUE based convenience to > /output.txt

  3. jlongtine commented on Feb 16, 2022

    @jlongtine
    Contributor

    @talentedmrjones As an FYI, the interactive API that Tonis mentions is
    frontend/gateway/client#Client.NewContainer. The difficulty with that API is that you can't generate State from a Container. But it does allow direct access to stdio, as he mentions.

  4. talentedmrjones commented on Feb 17, 2022

    @talentedmrjones

    After digging into this, and chatting with Tonis, I see that the very "philosophy" of buildkit is to use files and mounts for IO operations rather than traditional stdio. So we need a way to launch the given command in a wrapper of some kind that can handle proxying stdin and capturing stdout/err in a convenient way.

  5. helderco commented on Feb 17, 2022

    @helderco
    Contributor

    Reminds me of cronic 🙂

  6. talentedmrjones commented on Mar 14, 2022

    @talentedmrjones

    @jlongtine @helderco Any ideas to create a pure CUE convenience abstraction as a wrapper around output: files: "./output.txt": _ ? The tricky bit is that many (most?) commands are not going to parse stdio operators, so passing > ./output.txt is really only guaranteed to work when using sh as the command.

    One idea stems from the fact that mixins are easy with CUE:

    #Stdio: {
      stdin: docker.#Image
      stdout: export.files."/output.txt"
      script: string
      export: files: "/output.txt": _
      command: {
        name: "sh"
        args: ["> /output.txt"]
        if stdin != _|_ {
          flags: "-c": "cat /output.txt | \(script)"
        }
        if stdin == _|_ {
          flags: "-c": script
        }
      }
    }
    
    
    // Get output from one command
    foo: docker.#Run & #Stdio & {
      script: 'echo "Hello World!"'
    }
    
    // Do something with output as string
    bar: foo.stdout & {...}
    
    // Pass input to another command
    baz: docker.#Run & #Stdio {
      stdin: foo.output
    }
  7. shykes commented on Mar 28, 2022

    @shykes
    ContributorAuthor

    Flagging this for prioritization @aluzzardi , it has been requested for a pilot implementation.

  8. helderco commented on Mar 29, 2022

    @helderco
    Contributor

    I think it's simpler to add an entrypoint that handles that:

    #!/bin/bash
    
    set -euo pipefail
    
    if [ -f "stdin.log" ]; then
        exec "$@" <(cat stdin.log) 1> >(tee stdout.log) 2> >(tee stderr.log >&2)
    fi
    
    exec "$@" 1> >(tee stdout.log) 2> >(tee stderr.log >&2)

    The downsides:

    • Overrides image's own entrypoint, or conflicts with user's entrypoint setting;
    • Requires a shell (script needs to be ported to sh, needs to work in busybox too for example);

    Solution:

    • Create a simple go program to do this;
    • Copy it inside the input FS;
    • Prepend it to the list of args inside the Exec action;

    Pros:

    • No other dependencies;
    • No overriding entrypoint;
    • Pretty basic CUE implementation;
  9. helderco commented on May 2, 2022

    @helderco
    Contributor
  10. sipsma commented on May 2, 2022

    @sipsma
    Contributor

    I am curious what the specific use cases for this are if they can be shared. In general, stdio processing is something users might default to because it's a very generic mechanism, but I do want to make sure there aren't better ways to solve their problems.


    Either way, support for async execs (core.#Start and core.#Stop) has just been added, and while I didn't include stdin/out/err support there, it uses the NewContainer API mentioned above, so we can support it quite easily if needed. I would caution that we should be careful how we provide this to users. The async execs are meant for non-deterministic "sidecar" services like a test DB, so I don't want users to try using the output of one of them to feed into cacheable core.Exec tasks, which can create non-determinism.

    If we want to support these for normal core.#Exec, @helderco's suggestion seems like it should work. A few notes:

    1. We can just bind-mount the wrapper program and stdin.log inside the container instead of copying
    2. We can also write the stdout/err files to a separate mount, which can be retrieved after the exec (using the GetMount API, which I'm not sure we have used yet in Dagger).

    In general, I can't think of any reason why we couldn't support at least the stdin part of this upstream, so then we could just provide the stdin buffer to use for an ExecOp as part of the LLB. The concern in general is making sure that we don't introduce a lot of non-determinism and/or parameters that can't be cached, but providing a stdin buffer is easily cached as part of the ExecOp's definition.

    Upstream support for stdout/err is a little trickier because it's not completely obvious what the interface would be, but I can't imagine any technical reason why we couldn't have Buildkit just optionally tee the stdout/err to a file on a separate mount, which would create the same end effect as the stdio tee program. This should fit into the cache model well as far as I can tell. It's probably more a matter of convincing upstream to take on this complication when there is a mostly viable approach here.

  11. shykes commented on May 9, 2022

    @shykes
    ContributorAuthor

    I am curious what the specific use cases for this are if they can be shared. In general, stdio processing is something users might default to because it's a very generic mechanism, but I do want to make sure there aren't better ways to solve their problems.

    It's pretty common, when developing a new package, to find yourself wanting to expose the output of a command-line tool directly as an output field in your CUE definition. Since there is no API to do that, you have to 1) wrap the command in a shell script which redirects the output to a file 2) export the file 3) reference the contents of the exported file in your CUE field.

    At best, it's verbose and adds friction ("what filename am I supposed to use? Do filenames have a meaning? Am I doing this right?"). At worst, it can add additional headaches, for example if you previously executed the command directly, and now are forced to add the indirection layer of the shell script.

    Beyond that, there's a more general argument that, well - if a shell script can do it, and cue cmd can do it (they have a CUE API for this), then why couldn't I do it in Dagger? This one is less about an urgent need, and more about giving our developers more creative freedom to build fun and powerful things with Dagger.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions