Start SASS/Compass on Vagrant reload

I use SASS & Compass on most of my projects, as well as Vagrant. I normally have my puppet provisioning scripts start a compass watcher to compile my SASS files into CSS, but this no longer works with Vagrant 1.3.0+ as provision scripts aren’t run on vagrant up or vagrant reload (only on the first vagrant up). If you reload your VM, the compass watcher won’t be running.

It turned out to be pretty tricky to fix. Running a script at start-up isn’t that difficult, but the Vagrant shared folders won’t be mounted at that point, so Compass will exit. Luckily Vagrant has an Upstart event called vagrant-mounted that is fired once the shared folders have been mounted (this hook doesn’t seem to be documented).

I created an Upstart job (my VMs run on Ubuntu), /etc/init/compass.conf that looks like this:

# Compass Watch Service
description "Compass Watcher"
author "Brandon Wamboldt <[email protected]>"
start on vagrant-mounted
exec /usr/local/bin/compass watch --poll /var/www/html/projectname > /tmp/compass.log 2>&1
respawn
respawn limit 15 5

And that’s it. My Puppet manifest looks like this:

# Start Compass watching for changes to SASS files so we can compile to CSS
file { '/etc/init/compass.conf':
  ensure => 'file',
  source => '/vagrant/vagrant/puppet/init/compass.conf',
  owner => 'root',
  group => 'root',
}

service { 'compass':
  ensure => 'running',
  enable => true,
  require => File['/etc/init/compass.conf'],
}

It took a while to figure this out, so hopefully I can save someone else the trouble.

2 Replies to “Start SASS/Compass on Vagrant reload”

  1. I tried to use this upstart job with ansible provision. And problem is because it doesn’t work on first vagrant up, and I have no idea why. Do you have some idea what can be wrong? This is how my compass role looks like:


    – name: Install Ruby
    sudo: yes
    apt: name=ruby state=latest

    – name: Install RubyGems
    sudo: yes
    apt: name=rubygems state=latest

    – name: Install Sass
    sudo: yes
    command: gem install sass

    – name: Install compass
    sudo: yes
    command: sudo gem install compass

    – name: Starting ‘compass watch’ in background on reload
    sudo: yes
    template: src=compass.conf.js dest=/etc/init/compass.conf owner=root group=root mode=0644

    – name: Starting ‘compass watch’ in background
    sudo: yes
    shell: /usr/local/bin/compass watch /var/www/nano/public/static/sass-main –poll &

  2. // easier with a ansible rule
    // while the provision

    – name: Install required gem packages
    shell: bundle install /var/www/{{ deploy_identifier }}/Gemfile
    sudo: no
    tags: packages tools frontend gem bundle ruby

    – name: starting ‘compass watch’ in background
    shell: compass watch /var/www/{{ deploy_identifier }} –poll &
    sudo: no
    tags: packages tools frontend gem bundle ruby

Leave a Reply