dockerize icon indicating copy to clipboard operation
dockerize copied to clipboard

Additional functions in template

Open psemeniuk opened this issue 7 years ago • 4 comments

Hi. I created some functions that were helpful in creation custom images. For example we can pass multiple datasources by environment variables to tomcat xml configuration.

`

WEB-INF/web.xml ${catalina.base}/conf/web.xml
{{ range $value := envSlice "TOMCAT_CONTEXT_RESOURCE" }}
<Resource name="{{ requiredEmpty $value.TOMCAT_CONTEXT_RESOURCE_NAME }}"
          auth="{{ defaultEmpty $value.TOMCAT_CONTEXT_RESOURCE_AUTH "Container" }}"
          type="{{ defaultEmpty $value.TOMCAT_CONTEXT_RESOURCE_TYPE "javax.sql.DataSource" }}"
          maxTotal="{{ defaultEmpty $value.TOMCAT_CONTEXT_RESOURCE_MAXTOTAL "100" }}"
          maxIdle="{{ defaultEmpty $value.TOMCAT_CONTEXT_RESOURCE_MAXIDLE "30" }}"
          maxWaitMillis="{{ defaultEmpty $value.TOMCAT_CONTEXT_RESOURCE_MAXWAITMILLIS "10000" }}"
          username="{{ requiredEmpty $value.TOMCAT_CONTEXT_RESOURCE_USERNAME }}"
          password="{{ requiredEmpty $value.TOMCAT_CONTEXT_RESOURCE_PASSWORD }}"
          driverClassName="{{ requiredEmpty $value.TOMCAT_CONTEXT_RESOURCE_DRIVERCLASSNAME }}"
          url="{{ requiredEmpty $value.TOMCAT_CONTEXT_RESOURCE_URL }}"
/>
{{end}}

`

psemeniuk avatar Jun 23 '17 17:06 psemeniuk

I'd love to see more utility methods in the templating, this is a place where confd really shines by comparison.

chrismccracken avatar May 03 '18 17:05 chrismccracken

These functions are very useful indeed. @jwilder please merge this. This addresses some pain points.

arunvelsriram avatar Aug 14 '18 13:08 arunvelsriram

Maintenance and development now happens in fork https://github.com/powerman/dockerize.

  • defaultEmpty $var $default - Returns a default value for one that does not exist or is empty in case of string. {{ default .Env.VERSION "0.1.2" }}

default already works this way, except order of params:

$ unset UNSET; EMPTY="" SPACE=" " dockerize -template /dev/stdin <<'EOF'
[{{ default "unset" .Env.UNSET }}]
[{{ default "empty" .Env.EMPTY }}]
[{{ default "space" .Env.SPACE }}] 
EOF                               
[unset]
[empty]
[ ]
  • required $val Returns a value passed as argument or error if value does not exist. {{ required .Env.VERSION }}
  • requiredEmpty $val Returns a value passed as argument or error if value does not exist or is empty in case of string. {{ requiredEmpty .Env.VERSION }}

These can be added, but to me it looks like right way to get error because of non-existing environment variable is to enable Option("missingkey=error") - this way it'll work automatically for all environment variables used in template, and you don't have to do anything manually. I plan to add new flag -template-strict which will enable that option.

Also current version provides empty and fail functions which you may find useful too.

Feel free to open PR at https://github.com/powerman/dockerize if you need something else and proposed flag and empty function doesn't do what you need (but please first review existing functions provided by Sprig).

  • envSlice - Returns the array filled with maps of environment variables passed as collection (variables that contains numeric suffix) and matched to $prefix {{ envSlice "VARIABLE_NAME" }}

This one is unique enough, but there are other functions which may do similar things:

  • splitList, split and regexSplit let you get multiple values from single env var if you can use some unique separator between values.
  • jsonQuery is much more powerful if you can store JSON inside env variable.

If proposed alternatives won't work for you then we can add envSlice at https://github.com/powerman/dockerize.

powerman avatar Nov 30 '18 22:11 powerman

Sorry for off-topic, but I have a concern against extending template functions. Ideally we need support of custom template functions loaded from user-provided files like j2cli supports (refer to --filters command line parameter).

Almost every time when one fills templates, he needs to think about quoting (to support whitespaces and to prevent configuration injection) and escaping (because of configuration file format and because of quoting). This sort of transformation can be complex. Here is an example of escaping for JBoss EAP / Wildlfy configuration - mabrarov/dockerfile-test/hollow-image/src/main/resources/rootfs/app/bin/filters.py.

So my personal preference would be to implement feature like j2cli provides - defining and loading external user-defined functions which could be used in dockerize template engine.

mabrarov avatar Nov 11 '20 14:11 mabrarov