Automating vagrant boxes on OS X

  • Reading time: 4 min
  • Published 5 years ago

A while ago, I said I would be writing about how to automate Laravel Homestead (or any other Vagrant box for that matter). As it turns out, I did not have to reboot my system for a very long stretch of time which in turn meant that I did not feel the need to automate something I wasn't even doing manually in the first place. But enough already with the non goal-oriented writing. I love to automate tedious everyday or not so everyday tasks in order to

  1. gain more time doing productive work and
  2. worry less about system management.

To tackle the actual problem at hand, I tried to find out what exactly I wanted to automate. There are many parts of a Vagrant setup that can be automated (essentially almost all of them) but once, a few hundred days ago, there was an excellent xkcd that I try to keep in mind whenever I set out to automate some part of my workflow.

Ideally for me, Homestead should…

  • autostart after booting
  • suspend when on battery power and no network signal is available
  • resume once the above holds no longer true
  • halt before a system power down.

The part where I don't want it running with no network signal and the system on battery is a fail save compensating for my machine being rather old which translates to not having that gorgeous battery life of the newer generation MacBooks. Halting the box before a system power down is not strictly necessary since even if some part of the box gets corrupted Vagrant will just redeploy it and you won't even notice. But redeploying consumes avoidable time and system resources.

Anyway, that's a lot of requirements and looking back at the above xkcd, I decided that I would actually be very satisfied with just having my Homestead environment start automagically whenever I have to reboot the system for now.

Fortunately, OS X provides several great instruments for automating workflows. The most powerful one is certainly the system's launch daemon launchd which is OS X's replacement for both cron and init.d like programs which you might know from other Unixes or Linuxes. As with most Apple software, launchd reads configuration files in the so-called property list format which is essentially a barebones dictionary representation for XML that feels really really ugly for first-time viewers. (Don't worry, it will continue to feel strange.)

If you are new to launchd, you might want to go checkout this a little dated but wonderful primer.

Launchd can do all kinds of things based on all kinds of conditions but the most practical one for me is automatically starting software I want to be running in the background. To start Homebrew's Vagrant box is easily done with something like the following configuration.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//E"">
<plist version="1.0">
      <false />

Save the above at ~/Library/LaunchAgents/com.meanderingsoul.homestead-up but don't forget to change '/your/homestead/path' to the path to your Homestead configuration. Paths in launchd-configurations should always be absolute unless you specify the EnableGlobbing key. After saving the file, remember that these configurations must be executable, ergo chmod +x ~/Library/LaunchAgents/com.meanderingsoul.homestead-up. This will start Homestead after logging (e.g. rebooting the system.) Thankfully, vagrant will not up an environment if it is already running, thus a lot of security checks to avoid this can be omitted.

A note on starting for the first time

Just saving the Property List in the Launch Agents directory will not load it into the system. Eventhough it should be automatically loaded after the next reboot, you can do so manually with launchctl load ~/Library/LaunchAgents/com.meanderingsoul.homestead-up.plist. This will register the agent with the system. In order to directly run it, just type launchctl start com.meanderingsoul.homestead-up.