Skip to content
waynegraham edited this page Jul 21, 2011 · 4 revisions

I usually also create Rakefile to help handle most of the management of the Vagrant and Chef stuff. I'll start including them here. If this gets out of hand, I'll probably break this page up at some point.

Ruby on Rails

Here's a Rakefile I created while working through the RoR tutorial.

require 'vagrant'
require 'fileutils'

task :default => :usage

task :usage do
  puts "You forgot to tell the computer what to do; try one of these commands:"
  system("rake -T")
end


desc 'Initializes the environment.'
task :init do
  system('git clone https://github.com/opscode/cookbooks.git')
  system('git clone https://github.com/scholarslab/cookbooks.git slab-cookbooks')
  puts 'vagrant up'
  env = Vagrant::Environment.new
  env.cli('up')
end

desc 'Cleans everything out of the environment.'
task :clobber do
  FileUtils.rmtree %w{cookbooks slab-cookbooks}, :verbose => true
  puts 'vagrant destroy'
  env = Vagrant::Environment.new
  env.cli('destroy')
end

desc 'Do a safe halt on the VM.'
task :halt do
  env = Vagrant::Environment.new
  puts 'vagrant ssh sudo halt'
  env.primary_vm.ssh.execute do |ssh|
    ssh.exec!('sudo halt') do |channel, stream, data|
      print data
      $stdout.flush
    end
  end
end

desc "cat /tmp/vagrant-chef/chef-stacktrace.out."
task :chefst do
  env = Vagrant::Environment.new
  raise "Must run `vagrant up`" if !env.primary_vm.created?
  raise "Must be running!" if !env.primary_vm.vm.running?
  puts "Getting chef stacktrace."
  env.primary_vm.ssh.execute do |ssh|
    ssh.exec!("cat /tmp/vagrant-chef/chef-stacktrace.out") do |channel, stream, data|
      puts data
    end
  end
end

namespace :rails do

  desc 'Placeholder.'
  task :noop do
  end

end

And here's the Vagrantfile that goes with it:

require 'fileutils'

if File.directory?('cookbooks/windows')
  FileUtils.remove_dir('cookbooks/windows', true)
end

Vagrant::Config.run do |config|
  # All Vagrant configuration is done here. The most common configuration
  # options are documented and commented below. For a complete reference,
  # please see the online documentation at vagrantup.com.

  # Every Vagrant virtual environment requires a box to build off of.
  config.vm.box = "ror-tutorial"

  # The url from where the 'config.vm.box' box will be fetched if it
  # doesn't already exist on the user's system.
  config.vm.box_url = "lucid32"

  # Forward a port from the guest to the host, which allows for outside
  # computers to access the VM, whereas host only networking does not.
  config.vm.forward_port "http", 3000, 3000

  # Enable provisioning with chef solo, specifying a cookbooks path (relative
  # to this Vagrantfile), and adding some recipes and/or roles.
  #
  config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = ['cookbooks', 'slab-cookbooks']

    chef.add_recipe 'sqlite3dev'
    chef.add_recipe 'rails'
    chef.add_recipe 'tmux'
    chef.add_recipe 'vim'
 
    chef.json.merge!({
      :rails => {
        :gems => %w{sqlite3}
      },
      :vim => {
        :extra_packages => %w{vim-scripts exuberant-ctags}
      },
      :domain => [],
      :openldap => {}
    })
  end

end
Clone this wiki locally