Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRYer VARs definition #1553

Open
maclarensg opened this issue Mar 16, 2024 · 0 comments
Open

DRYer VARs definition #1553

maclarensg opened this issue Mar 16, 2024 · 0 comments
Labels
state: needs triage Waiting to be triaged by a maintainer.

Comments

@maclarensg
Copy link

maclarensg commented Mar 16, 2024

For variables, i categorised them as 2 category:

  1. VARS that are set during before the runtime of a task. E.g. Env vars, env vars file and etc
  2. And VARS that are unknown until execution and runtime of task.

Since I can't define 2. in 1. then if I have several task asking for the same parameters. Wouldn't it be nice to be defined in some place and then reference by the tasks later.

example.

version: '3'
dotenv: ['setup.config']

tasks:
  shell:
    vars:
      ACCOUNT: '{{default "" .ACCOUNT}}'
      REGION: '{{default "ap-southeast-1" .REGION}}'
      GROUP: '{{default "" .GROUP}}'
      PATH: '$(case ${TIER} in 1) echo "{{.ACCOUNT}}" ;; 2) echo "{{.ACCOUNT}}/{{.REGION}}" ;; 3) echo "{{.ACCOUNT}}/{{.REGION}}/{{.GROUP}}" ;; *) echo "Unsupported tier" && exit 1 ;; esac)'
      COMMAND: '{{.CMD}} run --platform {{.PLATFORM}} --rm -it -e AWS_PROFILE=devops -v {{.HOME}}/.aws:/root/.aws -v {{.PWD}}/.gitconfig:/root/.gitconfig -v {{.PWD}}:/tf -v {{.PWD}}/local_modules:/tf/workspaces/{{.PATH}}/local_modules -w /tf/workspaces/{{.PATH}} --entrypoint \"\" {{.IMAGE}}'
    cmds:
      - |
        [ "{{.ACCOUNT}}" ] || ( echo "ACCOUNT is required."; exit 1 )
        [ "{{.TIER}}" -ge 2 ] && [ -z "{{.REGION}}" ] && echo "REGION is required." && exit 1 || true
        [ "{{.TIER}}" -eq 3 ] && [ -z "{{.GROUP}}" ] && echo "GROUP is required." && exit 1 || true
      - echo "{{.ACCOUNT}}"
      - echo "{{.REGION}}"
      - echo "{{.PATH}}"
      - eval "{{.COMMAND}}" /bin/sh

  scaffold:
    vars:
      ACCOUNT: '{{default "" .ACCOUNT}}'
      ACCOUNT_ID: '{{default "" .ACCOUNT_ID}}'
      REGION: '{{default "ap-southeast-1" .REGION}}'
      GROUP: '{{default "" .GROUP}}'
      PATH: '$(case ${TIER} in 1) echo "{{.ACCOUNT}}" ;; 2) echo "{{.ACCOUNT}}/{{.REGION}}" ;; 3) echo "{{.ACCOUNT}}/{{.REGION}}/{{.GROUP}}" ;; *) echo "Unsupported tier" && exit 1 ;; esac)'
    cmds:
      - |
        [ "{{.ACCOUNT}}" ]    || ( echo "ACCOUNT is required."; exit 1 )
        [ "{{.ACCOUNT_ID}}" ] || ( echo "ACCOUNT_ID is required."; exit 1 )
        [ "{{.TIER}}" -ge 2 ] && [ -z "{{.REGION}}" ] && echo "REGION is required." && exit 1 || true
        [ "{{.TIER}}" -eq 3 ] && [ -z "{{.GROUP}}" ] && echo "GROUP is required." && exit 1 || true

      - | 
        [ -d "workspaces/{{.PATH}}" ]                || mkdir -p workspaces/{{.PATH}}
        [ -d "workspaces/{{.PATH}}/local_modules" ]  || ( cd workspaces/{{.PATH}}; ln -s {{.BACK}}/local_modules . )
        [ -f "workspaces/{{.PATH}}/auto.tf" ]   	   || ( cd workspaces/{{.PATH}}; ln -s {{.BACK}}/base/auto.tf . )
        [ -f "workspaces/{{.PATH}}/main.tf" ]   	   || ( touch ./workspaces/{{.PATH}}/main.tf )
        [ -f "workspaces/{{.PATH}}/vars.tf" ]   	   || ( cp ./base/vars.tf ./workspaces/{{.PATH}}/ ) 
        [ -d "workspaces/{{.PATH}}/resources" ] 	   || ( mkdir -p workspaces/{{.PATH}}/resources; echo "{}" > workspaces/{{.PATH}}/resources/main.yaml; ) 
      - |
        echo "ACCOUNT: {{.ACCOUNT}}" > tmp/.config
        echo "ACCOUNT_ID: {{.ACCOUNT_ID}}" >> tmp/.config
        [ "{{.TIER}}" -ge 2 ] && echo "REGION: {{.REGION}}" >> tmp/.config
        [ "{{.TIER}}" -eq 3 ] && echo "GROUP: {{.GROUP}}" >> tmp/.config
        echo "PROJECT: {{.PROJECT}}" >> tmp/.config
        echo "DOMAIN: {{.DOMAIN}}" >> tmp/.config      

On the above example, variables that at in the cat1 section are store in setup.config which is a env file actually
You can see both shell and scaffold requires ACCOUNT(Mandatory), REGION and GROUP (optional) ....
And PATH is set during the runtime.

Now if there is a means to define it some place like

vars_template:
   template1:
      ACCOUNT: '{{default "" .ACCOUNT}}'
      REGION: '{{default "ap-southeast-1" .REGION}}'
      GROUP: '{{default "" .GROUP}}' 
      PATH: '$(case ${TIER} in 1) echo "{{.ACCOUNT}}" ;; 2) echo "{{.ACCOUNT}}/{{.REGION}}" ;; 3) echo "{{.ACCOUNT}}/{{.REGION}}/{{.GROUP}}" ;; *) echo "Unsupported tier" && exit 1 ;; esac)'

then later

tasks:
  shell:
    use_var_template:
      - template1
    vars:
      COMMAND: '{{.CMD}} run --platform {{.PLATFORM}} --rm -it -e AWS_PROFILE=devops -v {{.HOME}}/.aws:/root/.aws -v {{.PWD}}/.gitconfig:/root/.gitconfig -v {{.PWD}}:/tf -v {{.PWD}}/local_modules:/tf/workspaces/{{.PATH}}/local_modules -w /tf/workspaces/{{.PATH}} --entrypoint \"\" {{.IMAGE}}'
@task-bot task-bot added the state: needs triage Waiting to be triaged by a maintainer. label Mar 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: needs triage Waiting to be triaged by a maintainer.
Projects
None yet
Development

No branches or pull requests

2 participants