Cover image from Vagrant and Puppet setup

Vagrant and Puppet setup

October 15, 2014
Puppet logo

This summer during my holidays in Romania I looked into Vagrant. I had some time during the due to the heat, undertaking my own siesta without the sleep.

My path to to Vagrant was slightly oblique, as I had been looking to test out Puppet. I had been using VirtualBox virtual machines as an integral part of my development and testing process for some time. The promise of making this an automated and scripted process for my development environment appealed to me. In the end I chose an Ubuntu base box, as opposed to my usually preferred OpenSuse. I couldn’t find an Ubuntu base box with Puppet setup on the Vagrant website.

MySQL logo

One issue that constantly niggles me though with these setups is how to manage databases. Migrating/mirroring databases is always a pain in the neck when using these setups. I decided to investigate another way to approach this. So I installed the OSX version of MySQL on the host machine. The benefit of this is that the databases are shared between all development boxes. One small issue I encountered with this though is that I need to launch my MySQL instance and my Vagrant box separately.

Enter a little bash script to save the day. Below you can see the script I used to do this. Unfortunately admin password entry is required, but, c’est la vie.

#!/bin/bash

SCRIPT=\`basename ${BASH\_SOURCE\[0\]}\`

#Initialize variables to default values. RUN\_CMD='start'

#Set fonts for Help.

#Help function function HELP { echo -e \\n"Help documentation for ${SCRIPT}."\\n echo -e "Basic usage: $SCRIPT {start|stop|restart}"\\n echo "Command line switches are optional. The following switches are recognized." echo "start --Start Vagrant and MySQL" echo "stop --Stop Vagrant and MySQL" echo "restart --Restart Vagrant and MySQL" echo -e "-h --Displays this help message"\\n echo -e "Example: $SCRIPT start"\\n exit 1 }

#Check the number of arguments. If none are passed, print help and exit. NUMARGS=$# if \[ ! $NUMARGS -eq 1 \]; then HELP fi

RUN\_CMD="$1"

case $RUN\_CMD in "start") echo "Starting Vagrant and MySQL" ;; "stop") echo "Stopping Vagrant and MySQL" ;; "restart") echo "Restarting Vagrant and MySQL" ;; \*) HELP esac

current\_dir=$(pwd)

cd ~/Documents/vagrant/ubuntu

sudo /usr/local/mysql/support-files/mysql.server $RUN\_CMD

case $RUN\_CMD in "start") # This should be empty if there is no save present IS\_SAVED\_CHECK=$(vagrant status | grep '^default.\*saved.\*$') if \[\[ -z "$IS\_SAVED\_CHECK" \]\]; then vagrant up else vagrant resume fi ;; "stop") vagrant suspend ;; "restart") vagrant reload ;; esac

\# Go back to DIR we started in cd "$current\_dir"