me@kevinbaugh.com
Kevin Baugh
> vagrant plugin install vagrant-vbguest
> vagrant plugin install vagrant-librarian-chefFirst, we're going to install some vagrant plugins to make our lives easier.
> cd project_name
> mkdir vagrant
First we're going to make the folder structure. Which is a project folder with a vagrant folder inside of it.
Then, cd into the vagrant folder, and run "vagrant init," which will make a Vagrantfile.
> cd vagrant
> vagrant initFinally, create a file called "Cheffile"
> touch CheffileFirst, we need to define where exactly all these chef files are going to be coming from.
site "http://community.opscode.com/api/v1"Now, what cookbooks are we going to need?
cookbook 'apt'
cookbook 'build-essential'
cookbook 'mysql', '5.5.3'
cookbook 'ruby_build'
cookbook 'nodejs', git: 'https://github.com/mdxp/nodejs-cookbook'
cookbook 'rbenv', git: 'https://github.com/RayMangum/chef-rbenv'
cookbook 'vim'
site "http://community.opscode.com/api/v1"
cookbook 'apt'
cookbook 'build-essential'
cookbook 'mysql', '5.5.3'
cookbook 'ruby_build'
cookbook 'nodejs', git: 'https://github.com/mdxp/nodejs-cookbook'
cookbook 'rbenv', git: 'https://github.com/RayMangum/chef-rbenv'
cookbook 'vim'
Vagrant creates a file that we can use to define our VirtualBox configuration.
They were kind enough to fill it with helpful comments and setup examples.
...Erase it all. We're doing this from scratch.
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
endEverything is going to go between the "do" and the "end"
# Use Ubuntu 14.04 Trusty Tahr 64-bit as our operating system
config.vm.box = "ubuntu/trusty64"
A base box has all of the bare bones setup you're going to need.
It shortcuts having to setup absolutely everything.
Let's up the memory a little bit on our box.
# Configurate the virtual machine to use 2GB of RAM
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "2048"]
end
Now we can match up port 3000 on the server to the same port on your computer so localhost:3000 will work the same (yay, Rails).
# Forward the Rails server default port to the host
config.vm.network :forwarded_port, guest: 3000, host: 3000
Now, we can mount our projects folder straight into the server.
# From where, to where?
config.vm.synced_folder "../", "/mnt"
This means, anything that's one folder up from your Vagrantfile location will be available on the server at "/mnt"
Now it's time to setup your Chef configs.
# Use Chef Solo to provision our virtual machine
config.vm.provision :chef_solo do |chef|
end
First, the wrapper.
Now, all our cookbooks (to add an individual recipe instead of the default, use ::)
# Use Chef Solo to provision our virtual machine
config.vm.provision :chef_solo do |chef|
chef.cookbooks_path = ["cookbooks", "site-cookbooks"]
chef.add_recipe "apt"
chef.add_recipe "nodejs"
chef.add_recipe "ruby_build"
chef.add_recipe "rbenv::user"
chef.add_recipe "rbenv::vagrant"
chef.add_recipe "vim"
chef.add_recipe "mysql::server"
chef.add_recipe "mysql::client"
end
Finally, we tell Chef what exactly we want installed. Which is:
chef.add_recipe "mysql::client"
# Install Ruby 2.1.2 and Bundler
# Set an empty root password for MySQL to make things simple
chef.json = {
rbenv: {
user_installs: [{
user: 'vagrant',
rubies: ["2.1.2"],
global: "2.1.2",
gems: {
"2.1.2" => [
{ name: "bundler" },
{ name: "rails" }
]
}
}]
},
mysql: {
server_root_password: ''
}
}
end
That's really it when it comes to writing stuff.
Now that we have everything defined, we need to actually build up the box and start it.
> vagrant upThis will take FOREVER, so go make yourself a sandwich or something.
To access your server, just run the following command (the password is "vagrant" in case it asks):
> vagrant sshIf you already have a Gemfile, just do the following to install any dependencies.
(This assumes your Gemfile is at /project_name/Gemfile)
> cd /mnt
> bundle install
> rbenv rehashTo run the Rails server, there is one thing you have to do different.
> rails server -b 0.0.0.0You'd still access this from http://localhost:3000 because of the port forwarding.
0.0.0.0 means that Rails is listening on all interfaces, not just the loopback interface. So that pushes to 127.0.0.1(or localhost) on your local machine.
I've made a github repo with all this in it.
The master branch is a boxed up version of this server, so just run vagrant up and after the initial download it shouldn't take too long to up.
We need a way to stop our server when it's not in use, so you can use either of the following:
> vagrant haltThis will shut down your server. If you want to go scorched earth on it and completely destroy everything, do:
> vagrant destroyYou can access this slideshow at