Vagrant and Propel – external configuration and tabs

I’m currently using Vagrant with a client to help automate their development build processes. We want to try as much as possible to make the dev environment match the final production environment. So I can set up the provision.sh script to use environmental variable overrides so we can easily go from an ‘all-in-one’ development deployment to ‘spread-out’ production deployment without having to make special case scripts.

One area I found interesting was that we needed to wire up Propel and generate the required config file, whilst keeping all the Tabs that YAML format files love. If you are also pulling your hair out over this, use the following bash function.

template(){
 # usage: template file.tpl
 old_IFS=$IFS # save the field separator 
 IFS=$'\n' 
 while read -r line ; do
 line=${line//\"/\\\"}
 line=${line//\`/\\\`}
 line=${line//\$/\\\$}
 line=${line//\\\${/\${}
 eval "echo \"$line\""; 
 done < ${1}
 IFS=$old_IFS
}

template /vagrant/prov/propel.tpl > /vagrant/config/propel.yaml

A right little life saver, it looks like magic – but it does the job. Just refer to the environmental vars using the normal ${VAR} format.