Skip to content

Instantly share code, notes, and snippets.

@stevage
Created February 17, 2014 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevage/9061001 to your computer and use it in GitHub Desktop.
Save stevage/9061001 to your computer and use it in GitHub Desktop.
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