Next: , Up: Docker Entrypoint   [Contents][Index]


7.1 Expanding Environment Variables in GNU m4

Configuration preprocessing (see Preprocessor) can be used to conditionally enable parts of the pies.conf file, depending on the value of an environment variable. The technique described below assumes that you use GNU m4 as preprocessor.

Define the following two M4 macros:

M4 macro: CF_WITH_ENVAR name text

Expands the environment variable name within text. The macro does so by temporarily redefining the symbol name to the value of the environment variable name and expanding text.

The definition of the macro is:

m4_define(`CF_WITH_ENVAR',m4_dnl
`m4_pushdef(`$1',m4_esyscmd(printf "$`$1'"))m4_dnl
$2`'m4_dnl
m4_popdef(`$1')m4_dnl
')

This macro allows you to use environment expansion where it is not normally supported. Consider, for example, this fragment:

component {
  CF_WITH_ENVAR(`WORKDIR', `chdir "WORKDIR";')
  ...
}  

If you set WORKDIR=/var/wd prior to invoking pies, it will actually expand to

component {
  chdir "/var/wd";
  ...
}  

See chdir, for details about the chdir statement.

M4 macro: CF_IF_ENVAR name if-set if-unset

If the environment variable name is defined and has a non-empty value, expand if-set, otherwise expand if-unset. Expand each occurrence of name in if-set to the actual value of the environment variable.

Following is the definition of this macro:

m4_define(`CF_IF_ENVAR',m4_dnl
`CF_WITH_ENVAR(`$1',`m4_ifelse($1,`',$3,$2)')')

This macro makes it possible to conditionally enable configuration file fragments depending on whether some environment variable is defined. E.g.:

CF_IF_ENVAR(`LOGHOST',`
component logger { 
  command "syslogd -n -R LOGHOST;
}
')

Place both macros in a single file and include it at the top of your pies.conf using the m4_include command (see m4).


Next: , Up: Docker Entrypoint   [Contents][Index]