[mapserver-users] Parametrizing a MapCache config file

Schmid Andreas Andreas.Schmid at bd.so.ch
Fri Dec 18 02:02:19 PST 2020


Thank you, Dave, for sharing your solution. So as the differences between my config files are small, I guess I'm going to solve it with a sed script that reads the actual values from environment variables. And the templating language approach for sure would be the way to go for more complex cases.

I'm actually using this in a MapCache Docker image too, that should include the config file for convenience.

Best regards,
Andy


> -----Ursprüngliche Nachricht-----
> Von: mapserver-users <mapserver-users-bounces at lists.osgeo.org> Im Auftrag
> von David Hoese
> Gesendet: Donnerstag, 17. Dezember 2020 16:48
> An: mapserver-users at lists.osgeo.org
> Betreff: Re: [mapserver-users] Parametrizing a MapCache config file
> 
> Hi,
> 
> I had a similar need where I was running MapCache in a docker container
> and needed the config to change based on some user-provided environment
> variables. I ended up writing a python script that uses the Jinja
> templating language. That way I could write a really complex config
> "template" and run my python script when the docker container started and
> produce the desired output config. It was extra nice for me because my
> jinja template allowed me to do "for" loops and in my case I had a lot of
> the same grids and tilesets that were only different by one or two
> parameters.
> 
> It would be nice if there was something builtin, but I think a sed script
> or something like my Jinja templating are the easiest solutions.
> 
> Dave
> 
> Example
> -------
> 
>      <!--    Overlays -->
>      <source name="{{ plat }}_{{ inst }}_borders" type="wms">
>          <getmap>
>              <params>
>                  <FORMAT>rgba</FORMAT>
>                  <LAYERS>bw_map</LAYERS>
>              </params>
>          </getmap>
>          <http>
>              <url>http://{{ wms_host }}:{{ wms_port }}/wms/borders</url>
>              <connection_timeout>10</connection_timeout>
>              <timeout>15</timeout>
>          </http>
>      </source>
> 
> Python (trimmed down)
> ---------------------
> 
>      tmpl_vars = {'wms_host': 'my_host', 'wms_port': 8888, 'plat':
> 'test', 'inst': 'example'}
>      with open(args.template_fn, 'r') as tmpl_file:
>          template = jinja2.Template(tmpl_file.read())
>          print(template.render(tmpl_vars))
> 
> I then run it like:
> 
> python render.py > /path/to/output_config.xml
> 
> On 12/17/20 7:26 AM, Schmid Andreas wrote:
> > Hi list
> >
> > I'm looking for a way how I could parametrize a MapCache XML config
> file. Up to now, I have three config files: One that I use for development
> purposes, one for running MapCache in the test environment, and one for
> the production environment. The files differ only in the URL of the
> service metadata, the URL of the http-source, and whether the demo service
> is enabled. So when I change something in the file, I always need to make
> sure that I make the same change in the other files. Which I don't really
> like because it's error-prone.
> >
> > One solution that I can think of is keeping only one file and putting
> placeholders in the appropriate places. And then running a sed-Script over
> it before deploying it.
> >
> > But maybe someone knows of a smarter idea for this?
> >
> > Thanks a lot,
> > Andy
> >
> > _______________________________________________
> > mapserver-users mailing list
> > mapserver-users at lists.osgeo.org
> > https://lists.osgeo.org/mailman/listinfo/mapserver-users
> >
> _______________________________________________
> mapserver-users mailing list
> mapserver-users at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/mapserver-users


More information about the mapserver-users mailing list