You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Exec: inject stdin, capture stdout and stderr #1620
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.
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
@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.
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.
@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.#Imagestdout: export.files."/output.txt"
script: stringexport: files: "/output.txt": _command: {
name: "sh"args: ["> /output.txt"]
ifstdin!=_|_ {
flags: "-c": "cat /output.txt | \(script)"
}
ifstdin==_|_ {
flags: "-c": script
}
}
}
// Get output from one commandfoo: docker.#Run&#Stdio& {
script: 'echo "Hello World!"'
}
// Do something with output as stringbar: foo.stdout& {...}
// Pass input to another commandbaz: docker.#Run&#Stdio {
stdin: foo.output
}
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:
We can just bind-mount the wrapper program and stdin.log inside the container instead of copying
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.
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.
Activity
talentedmrjones commentedon Feb 16, 2022
I asked Slack #buildkit and tonistiigi said:
(emphasis mine)
I'm looking into ways to form a CUE based convenience to
> /output.txtjlongtine commentedon Feb 16, 2022
@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 generateStatefrom aContainer. But it does allow direct access to stdio, as he mentions.talentedmrjones commentedon Feb 17, 2022
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 proxyingstdinand capturingstdout/errin a convenient way.helderco commentedon Feb 17, 2022
Reminds me of cronic 🙂
talentedmrjones commentedon Mar 14, 2022
@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.txtis really only guaranteed to work when usingshas the command.One idea stems from the fact that mixins are easy with CUE:
shykes commentedon Mar 28, 2022
Flagging this for prioritization @aluzzardi , it has been requested for a pilot implementation.
helderco commentedon Mar 29, 2022
I think it's simpler to add an entrypoint that handles that:
The downsides:
sh, needs to work in busybox too for example);Solution:
Execaction;Pros:
helderco commentedon May 2, 2022
/cc @sipsma
sipsma commentedon May 2, 2022
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.#Startandcore.#Stop) has just been added, and while I didn't include stdin/out/err support there, it uses theNewContainerAPI 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 cacheablecore.Exectasks, 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:GetMountAPI, 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.
shykes commentedon May 9, 2022
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 cmdcan 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.