You can find the files for this step of the workshop in the following github repository:
You can find a lot of common cookbooks on the web and in the chef supermaket (https://supermarket.chef.io/) to provision a lot of services in a flexible way. You should use them because you need to implement less by your self.
In this step we will use the apache2 cookbook and have a look how we can integrate it with berkshelf to download and resolve dependencies.
The first step is, that you need to defined the dependencies in your "metadata.rb" file:
In the previous Example we defined that we define the "apache2" and "hostsfile" cookbook in a fixed version.
Including a dependency in a fixed version has the advantage that your cookbook will stell work, when there are breaking changes in the depending cookbook and you can race your dependecy, when you have tested your cookbook with the newer version.
To be able to use Berkshelf, the first step would be to install the Vagrant Berkshelf plugin:
When the vagrant plugin is ready we can add add file with the name "Berksfile" on the top level beside the "Vagrantfile".
We fill our Berksfile with the following content:
During the proviosioning berkshelf will resolve the dependencies on the cookbooks "apache" and "hostsfile" and download them, that they are available in the provisioning process.
Now we need to implement the recipe. The recipe will create the folder structure, a dummy site and the apache configuration with the domain "shop.local.dev".
As usual you can start the box with "vagrant up" and it will be available after a few minutes.
Now you need to create a hosts entry in your local host system to resolve the name of your apache virtual host.
You can find it:
And need to add the following line:
When you open "shop.local.dev" in your browser now, your will see a dummy site, that was created with the recipe:
Maybe you have recognized that a file Berksfile.lock exists beside the normal Berksfile? It has the following content:
This file contains the finger print of the cookbook dependencies at the time, the box was provisioned. As you might remember, the dependencies in "metadata.rb" could be defined with wildcards to e.g.: point to the latest relase.
The drawback is, your cookbook would break when there is a breaking change in the depending cookbbok. But if you now commit the Berksfile.lock, the dependecies from this file will be taken instead from Berksfile.
This can be used to implement a "continous integration process". The process would be "Checkout cookbook, delete Berksfile.lock => Vagrant up => Test if box still works with latest dependencies => commit Berksfile.lock". To use it like this, the depedencies need to point to the latest version for sure.
Now you know how to write one cookbooks and how to include external cookbooks. When you use more and more resources with vagrant and chef, the provisioning is getting slower. To prevent this, i've summarized some experiences to improve the performance of a vagrant setup.