n00b-alert-2: Provisioning the Development Servers with Chef Solo

Following my previous post about setting up the ability to spawn multiple *nix (Ubuntu) development VMs with Cygwin, VirtualBox and Vagrant, this tutorial is for Chef Solo, to provision the VMs with their databases, servers and programming environments we need. Although this is essentially a duplicate post of Dan’s, I find it best to put everything down myself, to help re-inforce the steps.

Setting up the Chef library

According to Dan, Chef will try to load all the cookbooks, regardless of whether it uses them or not. Therefore to keep things lean, it is best to only use the relevant cookbooks at a project level. To facilitate this, I will create a Chef library which I can then copy and paste the relevant cookbooks from into each project.

Now, there isn’t much in here, so I am going to set up my first project and use this to configure Chef Solo. Thereafter, I will copy these files across into the Chef Library, so that when I create further projects, I can take the Chef files I want from the library.

Creating the first Chef project

The Vagrantfile has now been made and we are ready to copy over the Chef files that we need from the Library. Although these files are currently empty, we will propogate them and then copy them back into the Library. When making future instances, we only need to copy them across from the Library to the Project and off we go. By copying across the files, we preserve the directory structure, to avoid us getting lost.

At this point is is probably best to import the project folder into your IDE, in my case Aptana. I can then edit the config files from the comfort of the IDE and not the command line.

Configuring Vagrant

Configuring Chef

Firstly the recipe we will be using. In this case, we are using it to install Zend Server.

Next we need to configure the Role that our server will be using, in this case we are calling it WebServer. Although the name is generic, we can here specify which webserver we wish to set up, currently Zend Server, which installs with Apache2. Should we wish to completely change the web server completely, we only need to add a new recipe for the new server and then change the configuration of the Role.

Now everything is configured for a basic PHP webserver using Zend Server.

Booting the VM

Cd to the directory with the Vagrant file

We hit a problem

Oh dear, something went wrong. Upon investigation it is the nightmare to understand first two execute codes in the recipe. We need to use not_if instead of only_if:

The logic is as follows:

If zend cannot be found in list of apt-keys (i.e. grep returns 0) then run the command in the line above to wget and add the key. Grep returns nothing, i.e. 0 / false and the not_if only runs if it evaluates to false / true, whereas the only_if runs on true. Our error was saying if the Zend Key can be found, try to download it – this is obviously the opposite of what we want.

Ditto for the source of Zend server, but this is by grepping the apt sources list.

The confusion was the double negatives, but all now makes sense.

Re-provisioning the VM

As the VM is already running, we do not need to restart it, instead we can re-provision it, following the changes to our recipe.

If this gives you any trouble, then you may need to destroy the VM completely and then boot it again:

Now we can log in via SSH via the port 2222 forwarded to localhost.

Default username and password is vagrant.

To test the webserver, open http://10.0.0.2/ or if port forwarding is set up http://127.0.0.1:port (in our case 8081).

Configuring Apache2

Now edit the recipe to include:

finally to get it to work:

Now via my IDE, I can change my index.htm under the app folder, and it will change when I refresh the website http://10.0.0.2/ we can see the changes.

Filling the library

Finally, we can copy back the Recipes and Roles into the Chef Library we made earlier. If we wish to expand on these recipes, without affecting other instances, we can then take copies of these when we make new projects. Alternatively if we wish to just use them straight out of the box, we can symlink to these directories from the new projects. By symlinking we can easily re-provision multiple instances without the need to edit each of the recipes.

Leave a Reply