5. Appendix - Vagrant
Vagrant is a tool to easily create and manage headless virtual machines for development. It can be used for servers as well but the performance will be poor. One of Vagrant’s best features is in testing Chef recipes. All the commands in this book ‘could’ be used on a Vagrant virtual machine with a few changes, but the purpose of this book is to get confortable with deploying to a real server.
First, you need to install the Oracle build of VirtualBox to your local machine since the version in the Ubuntu repos is out of date.
Echo to your sources list. Precise is Ubuntu 12.04. Please check virtualbox.org for the latest instructions for your installation (If you use the more recent releases you can skip this step and just install use sudo apt-get install virtualbox).
Get the public key:
wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add -
Add the apt link:
sudo bash -c "echo 'deb http://download.virtualbox.org/virtualbox/debian precise contrib' >> /etc/apt/sources.list"
Install VirtualBox on your local machine.
sudo apt-get update
sudo apt-get install virtualbox-4.3
Make sure to reboot to avoid any kernel module errors.
ssh-keygen -t rsa
and
ssh add
Download the Vagrant package from vagrantup.com and install it.
You need to download the server image. Goto Vagrantbox.es and pick out an image. I recommend this Ubuntu precise 64 VirtualBox image but it can become out of date. Either download it manually or through Vagrant.
vagrant box add ubuntu1204 http://files.vagrantup.com/precise64.box
You can get help on any vagrant command with.
vagrant box help
Vagrant relies on a configuration file to setup the box.
Create a new directory and enter it.
mkdir VM && cd VM
Intialize the directory with Vagrant
vagrant init
Now check the Vagrantfile.
nano Vagrantfile
There are a wild variety of options to investigate in the future. For now, I recommend deleting all the text and pasting in this.
Vagrant::Config.run do |config|
config.vm.define :chefbox do |mconfig|
mconfig.vm.box = "ubuntu1204"
mconfig.vm.network :hostonly, "33.33.13.39"
mconfig.vm.customize ["modifyvm", :id, "--name",
"chefbox", "--memory", "512"]
end
end
Let’s go through each of the options in the inner block. The first sets the name of the image to use (Vagrant will try to download if it hasn’t already). You should change this to whatever image you downloaded. The second sets the machine to be accessible only by the host and at the ip address 33.33.13.39. The third option sets the name of the vm and the memory in megabytes.
Once the download is finished create and start the server.
vagrant up
You can ssh into a server quickly with:
vagrant ssh
Or you can ssh in by ip like a regular server. ‘vagrant’ is the user and password.
Stop the server with:
vagrant halt
To avoid Chef repeatly asking for a password you can use an SSH key. Now where do you get the key for a box downloaded from the internet? Thankfully (in this case), most boxes use the same SSH key which you can fetch from here while in your Chef directory:
curl https://raw.github.com/mitchellh/vagrant/master/keys/vagrant > vagrant.key
chmod 600 vagrant.key
knife solo prepare --identity-file vagrant.key vagrant@33.33.13.39
knife solo cook --identity-file vagrant.key vagrant@33.33.13.39
There are plenty more options with Vagrant to explore that are outside the scope of this small guide (including having Vagrant automatically use Chef to build the VM).
This part of the book is in the sample edition. You can test the entire recipe with:
git clone https://github.com/jbwyatt4/railsbluebook2014.git
Run bundle update in the root railsbluebook folder, then knife solo prepare first and then the cook command. I have left the 33.33.13.39.json file for you to use.
If you wish to use this in a production setting make sure to change secret_token in railsbluebook2014/cookbooks/deployserver/templates/nginx.erb and the password defined for the user.