Skip to content

Question: --input and environment  #50

Closed

Description

@dubo-dubon-duponey

The current --input mechanism is very satisfying - flexible and powerful.

Although, injecting environment variables is a bit cumbersome and requires some serious shell gymnastic.

Typically, if I have a cue file like this:

myenv: {
  foo: string | *"foo"
  bar: string | *"bar"
  baz: string | *"baz"
  ble: string | *"ble"
  lol: string | *"lol"
}


foo: dagger: #compute: {
   // lalalala
}

And if I want to be able to override "myenv" with corresponding environment variables, I will have to write a shell script in the line of:

envs=()
while read -r line; do
  if [ "${!line+x}" ]; then
    # mind the step!
    envs+=(--input "myenv: $line: \"$(printf "%s" "${!line}" | sed -E 's/"/\\"/')\"")
  fi
done <<<"foo
bar
baz
ble
lol"

dagger compute gros_cue "${envs[@]}" "$@"

Sure, it's "doable", but:

  • mere mortals will likely find it challenging to come up with the above (you have to: 1. check if the variable is defined or not, then 2. evaluate it, and 3. escape it properly, which is not that trivial)
  • if you want something nice like here and maintainable, you have to basically duplicate the list of "vars" you are interested in, on the cue side, and inside a wrapper script (I'm certainly not going to type the above ^^^ on the command line every time I want to run my stuff)

So, would you be open considering some additional sugar to ease things up?

Maybe it could be something like:

dagger compute cue --bind-env 'myenv'

The behavior would be exactly the same I have above ^:

  • for each property listed on the cue side under myenv, check if an environment variable by that name is defined or not
  • if it's defined, escape it and inject it into cue land under myenv

This proposal seem to alleviate both:

  • duplication / cost of maintenance
  • newcomer bear trap

wdyt?

Activity

  1. dubo-dubon-duponey commented on Jan 19, 2021

    @dubo-dubon-duponey
    ContributorAuthor

    By the way, my wrapper script above will fail if none of the environment variables are defined (kind of proving my point that it is challenging to get it right, even for jedi-master-extraordinaire-shell-hackers like me :D :D :D)

  2. dubo-dubon-duponey commented on Jan 19, 2021

    @dubo-dubon-duponey
    ContributorAuthor

    Note that none of the above works if "myenv" properties have default values (see #51 )

  3. dubo-dubon-duponey commented on Jan 19, 2021

    @dubo-dubon-duponey
    ContributorAuthor

    Ok, this is the complete form of what you have to do right now to get environment nicely merged into a cue struct that has default values (accounting for #51)

    Cue:

    env: {
      inject: {
        foo: string | *"default foo \"\n"
        bar: string | *""
      }
    }
    

    Script:

    readonly root="$(cd "$(dirname "${BASH_SOURCE[0]:-$PWD}")" 2>/dev/null 1>&2 && pwd)"
    readonly cuepack="$root"/dagger
    
    # Prepare arguments
    args=(compute "$cuepack")
    
    # Extract properties from cue under the "env" struct, and iterate
    readonly CUE_ENV_KEY="env"
    while read -r key; do
      # If there is a corresponding environment variable by that name that is defined
      if [ "${!key+x}" ]; then
        # Evaluate and escape it
        args+=(--input "$(printf "%s" "$CUE_ENV_KEY" | tr "." ":"): resolve: $key: \"$(printf "%s" "${!key}" | sed -E 's/"/\\"/')\"")
      else
        # If not there, grab the resolved default from cue
        override="$(cd "$cuepack"; cue eval --expression "env.inject.$key" --out json)"
        args+=(--input "$(printf "%s" "$CUE_ENV_KEY" | tr "." ":"): resolve: $key: $override")
      fi
    done < <(cd "$cuepack"; cue eval --expression "${CUE_ENV_KEY}.inject" --out json | jq -rc 'keys | .[]')
    
    # Add remaining command-line arguments
    args+=("$@")
    
    dagger "${args[@]}"
    

    Now, you have what you want under "env: resolve:"

    Not the friendliest thing ever :p

    Alternatively, in the spirit of doing everything inside dagger, one could just --input "env: \"$(env | base64)\"" on the shell script side and do all the donkey dancing inside the cue file with an exec.

    UPDATE: actually, this does not seem to work - because input is handled too late, you cannot use any of env.resolve in your script...

  4. shykes commented on Jan 30, 2021

    @shykes
    Contributor

    I think it’s reasonable for client scripts to have an easy way to pass a pre-existing key-value map without having to enumerate each key in the command-line args passed to dagger. Did I summarize the requirement correctly?

    I think the cleanest way to do this would be with an env file. For example dagger compute —input-envfile TARGET=PATH could parse a standard envfile into a cue map.

    For example:

    $ cat prod.env
    PROD=1
    SSL=ENABLED
    ANALYTICS_KEY=BLABLABLA
    $ dagger compute . —input-envfile api.env=./prod.env —input-string api.env.USER=dubondubonney
    

    In this example, the resulting input would be:

    api: env: {
      PROD: “1”
      SSL: “ENABLED”
      ANALYTICS_KEY: “BLABLABLA
      USER: “dubondubonney”
    }
    

    The best part is you can source the env file straight from your shell script to avoid duplication.

    I like this approach because it allows us to not deal with environment variables at all. In my experience once you make environment variables a part of your API and UI, the user experience is made worse by the inevitable appearance of side effects which are hard to detect, painful to reproduce, and impossible to prevent.

  5. shykes commented on Apr 29, 2021

    @shykes
    Contributor

    Closing this since it’s not directly actionable. Happy to resume the conversation in a github discussion if needed.

  6. added a commit that references this issue on May 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions