Skip to content

Allow calling core functions from the CLI #6947

Closed

Description

@shykes

Problem

There is no way to call core functions from the CLI

Solution

Allow calling core functions from the CLI.

See https://daggerverse.dev/mod/github.com/shykes/core for a userland implementation.

Activity

  1. shykes commented on Mar 26, 2024

    @shykes
    ContributorAuthor

    FYI @sipsma, this may be a duplicate, after a quick search I couldn't find a direct equivalent, but may not have found it.

  2. added
    area/cli
    on Apr 12, 2024
  3. helderco commented on May 3, 2024

    @helderco
    Contributor

    Would you use -m core to select it? If so, we need to reserve that name:

    • Forbid creating a module with name "core"
    • Forbid installing a dependency with name "core"
  4. shykes commented on May 3, 2024

    @shykes
    ContributorAuthor

    Would you use -m core to select it? If so, we need to reserve that name:

    • Forbid creating a module with name "core"
    • Forbid installing a dependency with name "core"

    Good question... any thoughts @sipsma @vito @aluzzardi @jedevc ?

  5. jedevc commented on May 7, 2024

    @jedevc
    Contributor

    Good point - I like the idea of needing to explicitly select it, so yes, we'd probably need to reserve this ASAP.

  6. vito commented on May 7, 2024

    @vito
    Contributor

    Reserving core sounds fine to me - it's short and sweet, and I can't think of anything that would conflict with it (in a way that's legitimate and not misleading).

  7. self-assigned this
    on May 7, 2024
  8. helderco commented on May 7, 2024

    @helderco
    Contributor

    I have a working POC:

    Screenshot 2024-05-07 at 17 08 30

    Just needs the name reservation and a bit of polish to reduce what's available:

    ❯ dagger-dev -m core functions
    Name                                   Description
    blob                                   Retrieves a content-addressed blob.
    builtin-container                      Retrieves a container builtin to the engine.
    cache-volume                           Constructs a cache volume for a given cache key.
    check-version-compatibility            Checks if the current Dagger Engine is compatible with an SDK's required version.
    container                              Creates a scratch container.
    current-function-call                  The FunctionCall context that the SDK caller is currently executing in.
    current-module                         The module currently being served in the session, if any.
    current-type-defs                      The TypeDef representations of the objects currently being served in the session.
    default-platform                       The default platform of the engine.
    directory                              Creates an empty directory.
    file                                   -
    function                               Creates a function.
    generated-code                         Create a code generation result, given a directory containing the generated code.
    git                                    Queries a Git repository.
    host                                   Queries the host environment.
    http                                   Returns a file containing an http remote url content.
    load-cache-volume-from-id              Load a CacheVolume from its ID.
    load-container-from-id                 Load a Container from its ID.
    load-current-module-from-id            Load a CurrentModule from its ID.
    load-directory-from-id                 Load a Directory from its ID.
    load-env-variable-from-id              Load a EnvVariable from its ID.
    load-field-type-def-from-id            Load a FieldTypeDef from its ID.
    load-file-from-id                      Load a File from its ID.
    load-function-arg-from-id              Load a FunctionArg from its ID.
    load-function-call-arg-value-from-id   Load a FunctionCallArgValue from its ID.
    load-function-call-from-id             Load a FunctionCall from its ID.
    load-function-from-id                  Load a Function from its ID.
    load-generated-code-from-id            Load a GeneratedCode from its ID.
    load-git-module-source-from-id         Load a GitModuleSource from its ID.
    load-git-ref-from-id                   Load a GitRef from its ID.
    load-git-repository-from-id            Load a GitRepository from its ID.
    load-host-from-id                      Load a Host from its ID.
    load-input-type-def-from-id            Load a InputTypeDef from its ID.
    load-interface-type-def-from-id        Load a InterfaceTypeDef from its ID.
    load-label-from-id                     Load a Label from its ID.
    load-list-type-def-from-id             Load a ListTypeDef from its ID.
    load-local-module-source-from-id       Load a LocalModuleSource from its ID.
    load-module-dependency-from-id         Load a ModuleDependency from its ID.
    load-module-from-id                    Load a Module from its ID.
    load-module-source-from-id             Load a ModuleSource from its ID.
    load-module-source-view-from-id        Load a ModuleSourceView from its ID.
    load-object-type-def-from-id           Load a ObjectTypeDef from its ID.
    load-port-from-id                      Load a Port from its ID.
    load-scalar-type-def-from-id           Load a ScalarTypeDef from its ID.
    load-secret-from-id                    Load a Secret from its ID.
    load-service-from-id                   Load a Service from its ID.
    load-socket-from-id                    Load a Socket from its ID.
    load-terminal-from-id                  Load a Terminal from its ID.
    load-type-def-from-id                  Load a TypeDef from its ID.
    module                                 Create a new module.
    module-dependency                      Create a new module dependency configuration from a module source and name
    module-source                          Create a new module source instance from a source ref string.
    pipeline                               Creates a named sub-pipeline.
    secret                                 Reference a secret by name.
    set-secret                             Sets a secret given a user defined name to its plaintext and returns the secret.
    socket                                 Loads a socket by its ID.
    type-def                               Create a new TypeDef.
    version                                Get the current Dagger Engine version.

    Including skipping optional flags that aren't supported rather than error.

  9. shykes commented on May 7, 2024

    @shykes
    ContributorAuthor

    Would it break github.com/shykes/core? 😭

  10. helderco commented on May 7, 2024

    @helderco
    Contributor

    Would it break github.com/shykes/core? 😭

    Actually it doesn't. Just need to choose a different name if installing so that you can't use -m core. Even if just checked out locally you could use -m ./core (or an absolute path).

    Basically, only the exact -m core needs to be reserved, it's not about the module's name really. Or we can use something else that doesn't clash with anything like -m @. To be clear that syntax is just to prove a point, not an endorsement 😄

  11. helderco commented on May 7, 2024

    @helderco
    Contributor

    Wdyt of -m dag?

  12. shykes commented on May 8, 2024

    @shykes
    ContributorAuthor

    I think -m core is more straightforward than -m dag.

    the downside of both options, is that the module name doesn't map to the codegen namespace.

    • install .../docker: dag.Docker()
    • install .../python: dag.Python()
    • install core: dag.Git(), dag.Container()... 🤔

    I know that you don't actually have to install core, but conceptually it is preinstalled. So the mismatch is potentially confusing. Also if we ever want to support multiple -m in the same command , which seems a bit hard to do now, but I kind of wish we had discussed that ise case earlier...

  13. helderco commented on May 8, 2024

    @helderco
    Contributor

    Let’s see, the main difference when loading core vs a module named “core” is this:

    • -m ./core → preselects query{core{}}, available functions are from Core object
    • -m core → preselects query{}, available functions are from Query object

    Just brain dumping here to make a point…

    The default for -m is ".", so we could use an empty string to mean core. However that doesn’t look like good ergonomics, and defaulting to the current directory is still a better default than running core. Example:

    • dagger -m "" call container ...
    • dagger -m - call container ...
    • dagger -m _ call container ...

    Those would avoid a naming conflict, but are they intuitive? The thing is, I don’t think anyone is going to “guess” to use -m core so you need documentation anyway. The question then is, do they have good ergonomics or do they feel weird? Is there something else that doesn’t feel weird, doesn’t cause a naming conflict and is better at conveying consistency? I think that -m "" fixes the last two but not the first (i.e., weird). If I had to pick one, it would probably be -m - as the - is sometimes used in CLIs to mean something special, although it’s usually for stdin.

    End brain dump

    As for using multiple -m, I remember us talking about it at some point but at the moment I’m not seeing how that could be used. You have a chain that has to start somewhere (ultimately from Query). If it’s because of multiple queries in one command, how would you separate the chains? With --?

    Just trying to see if there’s value in trying to keep this as a possibility. But to be clear, I don’t think supporting calling core functions hurts this ability.

  14. vito commented on May 8, 2024

    @vito
    Contributor

    Throwing another option into the mix: -m query. That would bring it into alignment with regular module invocation, at least as far as the target type is concerned (Query).

    Might be a little odd but it'd at least be leaning on pre-existing GraphQL terminology that we already have to reckon with, as opposed to "core" which makes sense but comes with the cost of claiming another word.

    Maybe that could even work with multiple -m flags, assuming the first N modules are loaded and the last -m determines the target of the call:

    dagger -m github.com/vito/daggerverse/apko -m query call apko --help
    
    # equivalent to:
    dagger -m github.com/vito/daggerverse/apko call --help

    Though it's not obvious what utility that has, since it's not like modules can monkey-patch anymore.

  15. shykes commented on May 9, 2024

    @shykes
    ContributorAuthor

    What about -m git, -m container, -m http, -m directory? It would map to the top-level graphql namespace.

  16. 26 remaining items

  17. shykes commented on May 29, 2024

    @shykes
    ContributorAuthor

    Another problem with full parity: how much of the graphql API can I query without running any containers? For example, if I run the CLI on a mac without a working container runtime: how much of the CLI can I actually use?

  18. aluzzardi commented on May 30, 2024

    @aluzzardi
    Contributor

    Not an argument for or against, just sharing some (potential?) context.

    I'm imagining in the near future the CLI exposing more features.

    For example:

    • Run Garbage Collect (for storage drivers)
    • Other storage-related commands. No idea what but it's possible something will need to be exposed
    • Serve a "storage service". Don't know what it is, but if the storage architecture takes us somewhere centralized (e.g. database backed layer metadata storage), then we may need to run a service, and we may want to bake it into the CLI
    • Control Plane: maybe in the future the CLI will be able to listen to GitHub etc events directly. We may need a command to listen for such events

    With that being said, is a 1:1 mapping going to help (easier to extend the CLI) or get in the way (trying to fit things into modules that shouldn't, making us slower), or neither?

  19. sipsma commented on May 30, 2024

    @sipsma
    Contributor

    One thing to be clear about up front: I don't think we should make some 100% set-in-stone rule that "Thou shalt only implement top-level CLI commands as API calls". It's a nice and appealing approach for a number of reasons but we would probably want to approach migrating existing commands iteratively and also leave the door open for pure CLI implementations if/when needed

    • But if we have a single binary this all becomes a bit more moot like @shykes said

    Run Garbage Collect (for storage drivers)
    Other storage-related commands. No idea what but it's possible something will need to be exposed

    @aluzzardi Yes I've been thinking about the same recently alongside other refactoring work and these specifically are examples of commands that we almost certainly do want to be in the top-level graphql API, just due to the fact that they require close integration with all the solver+cache stuff that happens in the engine container. So I think it would align with this idea.

    Serve a "storage service". Don't know what it is, but if the storage architecture takes us somewhere centralized (e.g. database backed layer metadata storage), then we may need to run a service, and we may want to bake it into the CLI
    Control Plane: maybe in the future the CLI will be able to listen to GitHub etc events directly. We may need a command to listen for such events

    Makes sense too, but at minimum the storage parts is also something where we almost certainly need close integration with the solver+cache in the engine container, so no matter what quite a bit of the logic needs to live there.

    As for the "serve on the CLI-side" part, this is where all our existing functionality around sessions will help us out enormously. We already have the ability to tunnel traffic on arbitrary ports back and forth between the CLI and the engine container (was added for service proxying/tunneling, but @vito's implementation is very generic and can be re-used out-of-the-box for things like this).

    • We can also easily add variations on this that don't require localhost to be involved and instead just plumb e.g. github events to the CLI directly over a socket, which probably makes more sense for those specifically

    With that being said, is a 1:1 mapping going to help (easier to extend the CLI) or get in the way (trying to fit things into modules that shouldn't, making us slower), or neither?

    One thing, just to be extra clear, we don't have to implement all this as modules (though that's an option). They can be core API calls as desired, which reduces potential sources of overhead when that matters.

    Also worth mentioning, in addition to the tunneling session functionality I mentioned above, we also already have the ability to read/write/stat arbitrary files on the CLI's host from the engine container, read auth, etc. It's straightforward to add more functionality as needed too. So I don't think it would get in our way in that sense.

    The impact on performance is the main thing to look out for though I agree. There's a couple things that lessen my concern somewhat:

    1. We already have to do all this "session callback to client" stuff for tons of really basic functionality, so if we do need to tune performance somewhere, it's likely going to benefit everything rather than just be a thorn to deal with to support the idea in this issue.
    2. All the refactoring I've been doing has ended up entangling simplifications that reduce the overhead of all this (e.g. less layers of gRPC tunneling.), and it's also making it easier for us to iterate on all of this more as needed.

    Another problem with full parity: how much of the graphql API can I query without running any containers? For example, if I run the CLI on a mac without a working container runtime: how much of the CLI can I actually use?

    If you're asking about what parts of the current core API are "theoretically" usable if they were running directly on macos/windows w/out container support, then the main things hard excluded at the moment would be:

    • withExec + services
    • modules (since they currently rely on running in containers)

    There's a lot of functionality around just pulling images, performing file operations on them, etc. that don't need any containers per-se, though it's going to be hit or miss which parts of some of those currently compile on non-Linux.

    • There has actually been a ton of work in buildkit on native windows support (and some on native mac os support), so it would mostly be a matter of whack-a-mole on using go build tags in the right places so code actually compiles such that it's using the right underlying syscalls.
    • But for other gql API calls that don't involve syscalls at all, or only minimally (e.g. checkVersionCompatibility comes to mind first) this shouldn't be a problem

    Not sure if that answers your question, let me know.

  20. added
    area/cli
    and removed
    area/cli
    on May 31, 2024
  21. helderco commented on Jun 14, 2024

    @helderco
    Contributor

    So, options are:

    1. 🎉 dagger call -c container ... (boolean -c, --core flag, mutually exclusive with -m, --mod)
    2. 🚀 dagger container ... (top-level commands; hand picked)
    3. 👀 dagger core container ...

    Do you mind voting with the emojis to see where everyone's at?

  22. shykes commented on Jun 14, 2024

    @shykes
    ContributorAuthor

    @helderco there is an option 3 which is dagger core ...

  23. MikaelElkiaer commented on Jun 16, 2024

    @MikaelElkiaer

    @helderco Do I understand 1) correctly?
    The long format would be dagger --core core container <...>
    Does this mean there are more core modules than 'core'?

  24. helderco commented on Jun 16, 2024

    @helderco
    Contributor

    Does this mean there are more core modules than 'core'?

    Appologies, that was edited while on phone. Fixed it. Long format: dagger call --core container <...>.

  25. helderco commented on Jul 23, 2024

    @helderco
    Contributor

    @helderco there is an option 3 which is dagger core ...

    After some discussions, I've reimplemented, to use the top level dagger core:

    I do find @sipsma's idea of making core functions be top-level commands (like dagger container) interesting, in particular allowing us to implement some commands via the API. But rather than making the jump on that now, putting the core functions under core is a stepping stone, to flush out the design more.

  26. aluzzardi commented on Aug 1, 2024

    @aluzzardi
    Contributor

    After some discussions, I've reimplemented, to use the top level dagger core:

    To everyone that had opinions on this (@jedevc @sipsma @vito @MikaelElkiaer @wingyplus @shykes): are you ok with the syntax of #7310? Can we move forward and merge it?

  27. samalba commented on Aug 1, 2024

    @samalba
    Contributor

    Sharing my opinion a bit late (non-blocking, feel free to move forward with any option): dagger core ... breaks the semantic, it uses a noun (core) in place of a verb (call) for calling a function.

    I'd vote for dagger call --core <func> to make it clear that you're calling a builtin function from the core, it's still a function call.

  28. helderco commented on Aug 9, 2024

    @helderco
    Contributor

    @samalba, @aluzzardi, @shykes. Since I'm going away on PTO, I left a quick PR to give you a little more time to consider the DX before next release:

    So that decision is no longer blocking the feature. Feel free to merge or close.

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

Metadata

Metadata

Assignees

Labels

area/cli

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions