Vagrant Virtual Machine with Puppet for Rails

When I first started trying to learn Ruby on Rails, I noticed that there seemed to be a lot of steps, apt-gets, and other hoops and obstacles I had to tackle prior to getting my first “rails server” command to successfully fire up localhost:3000 with my rails application. I remember thinking to myself, if this is how hard it is to set up a workstation to run rails, then I’m not too sure it’s going to be all that pleasant to work with.

In particular, I realized that in a team setting, having to configure multiple workstations for each developer would be less than awesome. Yuck!

Enter VirtualBox and Vagrant. VirtualBox is a free Oracle product that allows developers to add virtual machine instances to their local operating system install. Vagrant is essentially a way to download an instance of an operating system, such as Precise32, and fire up a VirtualBox virtual machine on the fly in accordance to a specified config file.

But that’s not all. Throw in Puppet, which is an an automated administrative engine for your specified operation system, and you have all the ingredients to create virtual machines to host rails applications by just typing one simple command into the terminal:

vagrant up

Now I don’t want to get into the details of how to install VirtualBox and Vagrant, but on a Linux box, VirtualBox can be installed via the sofware center or, for more detailed instructions, visit the VirtualBox website.

Vagrant is pretty easy to install as well. For direction, visit the Vagrant website.

You won’t need to worry about installing puppet on your local workstation, Vagrant will handle utilizing puppet itself when creating your virtual machine.

So, without further adieu, here are some directions to get your first vagrant box set up for your rails app. Note: I’m assuming a Linux workstation, if you are using something else, I can’t 100% guarantee everything will work exactly as I have it, but that’s why there’s google ūüėČ

1) In a terminal, cd into a directory you’d like to store your RailsDevBox and Rails applications that will utilize the box.

2) Hopefully you have git installed on your local workstation, but if you haven’t, run the following command in the terminal:
$ apt-get install git
3) Clone rails-dev-box which is an awesome, open source, pre-defined vagrant/puppet package that will set up everything you’ll need to get your vagrant development virtual machine up and running:
host $ git clone https://github.com/rails/rails-dev-box.git
host $ cd rails-dev-box

4) You should see an Vagrantfile within the directory. Open it, and paste in the following if the text doesn’t already exist:

Vagrant::Config.run do |config|
  config.vm.box       = 'precise32'
  config.vm.box_url   = 'http://files.vagrantup.com/precise32.box'
  config.vm.host_name = 'rails-dev-box'
  config.vm.network :bridged

  config.vm.forward_port 3000, 3000
  config.vm.forward_port 8982, 8982

  config.vm.provision :puppet,
    :manifests_path => 'puppet/manifests',
    :module_path    => 'puppet/modules'
end

5) Some notes on the configuration above, the vm.box can be named whatever you want, but you can probably see I’m naming it after the type of operation systems I’m going to use, which is specified by the vm.box_url, or in our case, a precise32 linux install. You can go here for some other box options.

Also note we are telling Vagrant where to find our puppet manifests and modules, which consist of the configurations we’d like to set and installs we’d like to perform on our new box after it’s created. This will all be done dynamically through the puppet scripts we cloned from the rails-dev-box package.

The vm.network :bridged line is useful because your vagrant virtual machine will be on your network if you have one. (Note: during the vagrant install on step 7, you may need to select between multiple networks, any should work)

Finally, note the port forwarding. We are specifying the we’d like the host’s port 3000 (the rails server port) to be forwarded to the Virtual Box port 3000. That will allow us to simply fire up a browser with localhost:3000 and run our rails app just like we’d do if we were hosting it truly locally. The other port I added for solr, which is a search tool I highly recommend and use in my personal rails¬†applications. I’ll blog about solr more in future posts.

6) Open puppet/manifests/default.pp. You should see lot’s of great stuff, but¬†unfortunately¬†rails-dev-box doesn’t install a version of java that we can use for selenium tests and solr by default, both of which I love. Nor does it create the dev and test mysql databases and an¬†appropriate¬†user if you plan to use mysql as your database server, which I do for my applications.

To install java, add the following to the default.pp file:

# Java runtime
package {  jdk:
   ensure => installed,
   name => "openjdk-6-jdk",
}
To handle the mysql additions, find the¬†# — MySQL section of the file, and update the subsequent code with the following:
# --- MySQL --------------------------------------------------------------------

class install_mysql {
  class { 'mysql': }

  class { 'mysql::server':
    config_hash => { 'root_password' => '' }
  }

  database_user { 'rails@localhost':
    ensure  => present,
    password_hash => mysql_password('yourpassword'),
    require => Class['mysql::server']
  }

  database { 'yourapp_development':
    ensure  => 'present',
    charset => 'utf8',
  }

  database { 'yourapp_test':
    ensure  => 'present',
    charset => 'utf8',
  }

  database_grant { ['rails@localhost/yourapp_development', 'rails@localhost/yourapp_test']:
    privileges => ['all'],
    require    => Database_user['rails@localhost']
  }

  package { 'libmysqlclient15-dev':
    ensure => installed
  }
}
class { 'install_mysql': }
——-

Of ¬†course, make sure to set the following values (italicized¬†in the code above) to whatever you’re using in your rails application:

yourapp_development
yourapp_test
rails@localhost
yourpassword

7) There you go! Should be set. Simply run the following command from your terminal and watch the magic happen:

$ vagrant up

8) This could take a while, since your downloading an entire virtual machine and installing lots of packages to it. But after the first time, it won’t take that long the next time you vagrant up.

9) From your host computer, you can now add a rails directory (or whatever you want to call the directory with your rails applications) within your rails dev box directory. Basically, this directory will be your new rails projects directory that will be hosted up by the vagrant server. The directory structure should like like this

-YourRailsDevBoxDir
      -RailsApplicationDirectory
            -App1
            -App2
      -Vagrantfile
      -puppet
            -manifests
            -modules
10) Run the following to ssh into the vagrant rails box:
$ vagrant ssh

11) Browse to your rails application from the share in the vagrant server

$ cd /vagrant/YourRailsDevBoxDir/App1

12) Bundle up your rails app

$ bundle install

13) Fire up the rails sever

$ rails server

14) From the host, open a browser and browse to localhost:3000

That’s it, your done! Now, you can commit your rails dev box to git, and any new developer can simply clone it, run vagrant up, and they should be good to go.

Enjoy, and feel free to comment if you need clarification on any of the steps, or find something I need to update or correct.

Advertisements
Tagged , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: