lunedì 18 maggio 2015

Installing ang configuring Jenkins using Chef - Part 2 - Introducing Vagrant

In the first part of this series we created a Chef cookbook that installs Jenkins, configure it and creates a new Job.
Now we have to execute it.
The simplest thing we can do is directly launching Chef on the machine we are working with:

sudo chef-client --local-mode --runlist 'recipe[jenkins_server]'

This installs Jenkins on the same machine. If we need to install Jenkins on another machine we could move our cookbook on a Chef server and then our target machine can grab it from the server and execute it locally.

But today I want to show you a third way that I find very effective for development environments: installing on a virtual machine managed by Vagrant.

Vagrant is a tool that allows you to easily create and configure virtual machines in a reproducible way. Vagrant does not run the virtual machine itself but instead stands on the shoulders of giants like VirtualBox, VMware, AWS, etc.

If we install Vagrant (and a virtualization tool, for example VirtualBox) we only have to create a little script like:

Vagrant.configure(2) do |config|
  config.vm.box = "hashicorp/precise32"
end

and call it "Vagrantfile". When you launch the command

vagrant up

Vagrant will:
  • download from its repository the VM you requested, in our case an Ubuntu 12.04 (Precise Pangolin) 32 bit VM
  • identify your virtualization tool (VirtualBox?)
  • import the VM in your virtualization tool
  • launch the VM
The VM (or 'box, as Vagrant calls it) is downloaded only the first time. Successive uses of the same box does not require a new download.

To stop the VM the command is:

vagrant halt

The VM will remain available for successive restart with vagrant up. If we want to completely remove the VM use

vagrant destroy

We can put in our Vagrantfile some network configuration, for example with

Vagrant.configure(2) do |config|
  config.vm.box = "hashicorp/precise32"
  config.vm.network "public_network", ip: "192.168.0.50"
end

we are saying Vagrant that we want our VM published on our local network with the given IP address. In this way we can access our VM with ssh (user: vagrant pwd: vagrant).

To launch Jenkins on our VM, first install the vagrant berkshelf plugin.

vagrant plugin install vagrant-berkshelf

In this way Vagrant will be able to automatically download the cookbooks from the Chef supermarket.

Then modify our Vagrantfile in this way:

Vagrant.configure(2) do |config|
  config.vm.box = "hashicorp/precise32"
  config.vm.network "public_network", ip: "192.168.0.52"

  config.berkshelf.enabled = true

  config.vm.provision "chef_solo" do |chef|
    chef.add_recipe "tomcat_server"
  end
end

and in the same directory put a file called Berksfile containing

source 'https://supermarket.chef.io'

cookbook 'jenkins_server', path: './cookbooks/jenkins_server'

Now copy your cookbook in the directory specified in the Berksfile and you are able to launch a VM with Jenkins installed and configured.

Notice that all the files we have created until now are text files contained in a single directory. You can copy this directory wherever you want and, supposed you installed Vagrant and a Virtualization tool, you can launch a full configured Jenkins machine in minutes.

You can even put it on a versioning server (Git, Subversion, etc.) so you can maintain and branch it whenever you want.

The directory contains only a few kbytes of text files. Imagine you want to do the same thing using an exported VM. The resulting file will weight a lot of Gbytes. And if you need different versions you have to maitain a lot of this heavyweight files!

It's all for now. In the next part we will talk about how the solve the problems we left behind in the first part.

Nessun commento: