Automating Barman with Puppet: it2ndq/barman (part one)

March 17, 2015

vagrant-barman-vertical
This is not the first time that 2ndQuadrant has looked at Puppet. Gabriele Bartolini has already written an article in two parts on how to rapidly configure a PostgreSQL server through Puppet and Vagrant, accompanied by the release of the code used in the example on GitHub (http://github.com/2ndquadrant-it/vagrant-puppet-postgresql).

Split into three parts, the aim of this article is to demonstrate automation of the setup and configuration of Barman to backup a PostgreSQL test server.

This article is an update of what was written by Gabriele with the idea of creating two virtual machines instead of one, a PostgreSQL server and a Barman server.

it2ndq/barman is the module released by 2ndQuadrant Italy to manage the installation of Barman through Puppet. The module has a GPLv3 licence and is available on GitHub at the address http://github.com/2ndquadrant-it/puppet-barman. The following procedure was written for an Ubuntu 14.04 Trusty Tahr but can be performed in a similar manner on other distributions.

Requirements

To start the module for Barman on a virtual machine, we need the following software:

Vagrant

Vagrant is a virtual machine manager, capable of supporting many virtualisation softwares with VirtualBox as its default.

We install VirtualBox this way:

$ sudo apt-get install virtualbox virtualbox-dkms

The latest version of Vagrant can be downloaded from the site and installed with the command:

$ sudo dpkg -i /path/to/vagrant_1.7.2_x86_64.deb

Ruby

Regarding Ruby, our advice is to use rbenv, which creates a Ruby development environment in which to specify the version for the current user, thereby avoiding contaminating the system environment. To install rbenv we suggest to use rbenv-installer (http://github.com/fesplugas/rbenv-installer).

Let’s download and execute the script:

$ curl https://raw.githubusercontent.com/fesplugas/rbenv-installer/master/bin/rbenv-installer | bash

At the end, the script will prompt you to append the following lines to the ~/.bash_profile file:

export RBENV_ROOT="${HOME}/.rbenv"

if [ -d "${RBENV_ROOT}" ]; then
  export PATH="${RBENV_ROOT}/bin:${PATH}"
  eval "$(rbenv init -)"
fi

We now need to reload the just changed ~/.bash_profile:

$ exec bash -l

At this point, we locally install a Ruby version (in this case, 2.1.5) and set the user to run this version rather than the system version:

$ rbenv install 2.1.5
$ rbenv global 2.1.5

Puppet

Puppet is required not only on the VMs but also on the machine running them. Therefore we need to install the Puppet gem.

$ gem install puppet

Librarian-puppet

Finally, librarian-puppet is a tool to automate the Puppet modules management. Like Puppet, librarian-puppet can be installed as a gem:

$ gem install librarian-puppet

Vagrant: configuration

Now that we have the dependencies in place, we can start to write the Vagrant and Puppet configurations for our backup system.

We start by creating a working directory:

$ mkdir ~/vagrant_puppet_barman
$ cd ~/vagrant_puppet_barman

Vagrant needs us to write a file called Vagrantfile where it looks for the configuration of the VMs.

The following Vagrantfile starts two Ubuntu Trusty VMs, called pg and backup, with ip addresses 192.168.56.221 and 192.168.56.222. On both machines provisioning will be performed through an inline shell script.

This script launches puppet-bootstrap (http://github.com/hashicorp/puppet-bootstrap), a script that automatically installs and configures Puppet on various types of machines. As it does not need to be run more than once, in the script a test was inserted to prevent further executions.

Vagrant.configure("2") do |config|
  {
    :pg => {
      :ip      => '192.168.56.221',
      :box     => 'ubuntu/trusty64'
    },
    :backup => {
      :ip      => '192.168.56.222',
      :box     => 'ubuntu/trusty64'
    }
  }.each do |name,cfg|
    config.vm.define name do |local|
      local.vm.box = cfg[:box]
      local.vm.hostname = name.to_s + '.local.lan'
      local.vm.network :private_network, ip: cfg[:ip]
      family = 'ubuntu'
      bootstrap_url = 'http://raw.github.com/hashicorp/puppet-bootstrap/master/' + family + '.sh'

      # Run puppet-bootstrap only once
      local.vm.provision :shell, :inline => <<-eos
        if [ ! -e /tmp/.bash.provision.done ]; then
          curl -L #{bootstrap_url} | bash
          touch /tmp/.bash.provision.done
        fi
      eos
    end
  end
end

Bringing up the VMs

We have defined two Ubuntu Trusty VMs containing Puppet. This is not the final Vagrantfile but already allows the creation of the two machines. If you’re curious, it is possible to verify that the two machines have been created with the command:

$ vagrant up

and then connecting using the following commands:

$ vagrant ssh pg
$ vagrant ssh backup

Finally, the machines can be destroyed with:

$ vagrant destroy -f

Conclusions

In this first part of the tutorial we’ve seen how to configure the dependencies and ended up with the two virtual machines on which we’ll install, via Puppet, PostgreSQL and Barman. Writing the Puppet manifest for the actual installation will be the subject of the next article.

Bye for now!

Share this

More Blogs