[mapserver-users] Parametrizing a MapCache config file

Lars Schylberg lars.schylberg at blixtmail.se
Fri Dec 18 03:23:55 PST 2020


Hi,

I did a similar thing with mapfiles and substituted colors and other 
info with sed. I had the values in a csv file. Then read the values into 
a associative array in bash.

Finally I had a very long sed script to do the substitutitons.

You can find the example here: 
https://github.com/LarsSchy/SMAC-M/blob/master/chart-installation/generate_map_files/scripts/process_layer_colors.sh#L69 


The reading of values into the array, Line 71-77 and the sed part Line 
108-240.   The template mapfile had the variables to be substitued in 
curli brackets, ie {PATH} .

Just food for thought.

/Lars S.

Den 2020-12-18 kl. 11:02, skrev Schmid Andreas:
> 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
> _______________________________________________
> mapserver-users mailing list
> mapserver-users at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/mapserver-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/mapserver-users/attachments/20201218/2ab4ca19/attachment.html>


More information about the mapserver-users mailing list