Skip to content

Instantly share code, notes, and snippets.

@stevage stevage/gist:9061001
Created Feb 17, 2014

Embed
What would you like to do?
I just noticed the absurdity of this code I wrote yesterday. Javascript inside Python inside Bash inside Jinja inside YAML, and perilously close to a layer of regex too.
configure_osrmweb:
cmd.run:
- cwd: /usr/share/nginx/www/osrm/WebContent
- name: |
python <<EOF
import os, sys, re
os.rename('OSRM.config.js', 'OSRM.config.js.orig')
# In the process we convert line endings for some reason. :/
with open('OSRM.config.js.orig', 'r') as fin, open('OSRM.config.js', 'w') as fout:
data = fin.read()
data = re.sub(r'(ROUTING_ENGINES: \[).*?(\s+\],)', r'\1\n' +
{% for instance in tm_osrminstances %}
'{\n' +
' url: "http://{{ grains.fqdn }}:{{ instance.port}}/viaroute",\n' +
' timestamp: "http://{{ grains.fqdn }}:{{ instance.port}}/timestamp",\n' +
' metric: 1,\n' +
' label: "{{ instance.name }}",\n'
'}, \n' +
{% endfor %}
r'\2\n', data, flags=re.DOTALL)
fout.write(data)
EOF
echo
if [ "`diff OSRM.config.js OSRM.config.js.orig`" ]; then echo "changed=yes"; else echo "changed=no"; fi
- stateful: True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.