4

I am setting up a dirvish backup system using puppet. Dirvish has the concept of a vault - a self-contained directory which contains the backup data and the configuration for what and how to backup. These vaults can be anywhere in the filesystem, so you need to explicitly list them in the master dirvish config file.

The way I have done this with puppet duplicates the information about the vaults, by first creating them as resources, and then passing a array of vault names to the master dirvish resource.

  # Dirvish vaults, alphabetically sorted
  dirvish::vault { 'server-a-full':
    client      => 'server-a',
    tree        => '/',
    backup_root => $dirvish_backup_root
  }

  dirvish::vault { 'server-b-example.com':
    client       => 'server-b',
    tree         => '/srv/www/vhosts/example.com/backup',
    rsync_option => '--copy-unsafe-links',
    backup_root  => $dirvish_backup_root
  }

  # TODO - we duplicate the vault definitions here, but I don't 
  # see a better way right now
  class {'dirvish': 
    backup_root => $dirvish_backup_root,
    vaults      => ['server-a-full', 'server-b-example.com']
  }

The dirvish::vault class looks something like this

define dirvish::vault($tree, $client, $exclude = [], $rsync_option = '', $backup_root) {
  file {"$backup_root/$name/dirvish/default.conf":
    ensure => present,
    content => template("dirvish/vault.conf.erb"),
    require => File["$backup_root/$name/dirvish"]
  }
}

The dirvish class uses the vaults variable in a template

Runall:
<% vaults.each do |vault| -%>
        <%= vault %>
<% end -%>

How can I remove this duplication?

Robert Munteanu
  • 1,542
  • 5
  • 22
  • 38

2 Answers2

2

If you move your configuration data to Hiera, the template can use the same data as whatever class declares the actual dirvish::vaults.

E.g., create a structure that lends itself to use with create_resources:

dirvish_vault_defaults:
  backup_root: "<backup-root-value>"

dirvish_vaults:
  'server-a-full':
    client: 'server-a',
    tree: '/',
  'server-b-example.com':
    client: 'server-b',
    tree: '/srv/www/vhosts/example.com/backup',
    rsync_option: '--copy-unsafe-links',

And in the manifest:

create_resources('dirvish::vault', hiera('dirvish_vaults'), hiera('dirvish_vault_defaults'))

The template can load the same hash and iterate its keys etc.

Felix Frank
  • 3,063
  • 1
  • 15
  • 22
1

I think your best option is to use a concat module, like this or some split config, if dirvish supports it (each vault would create its own config file which would then be included in the main config). It's possible to peek into the catalog with the spaceship operator, but that would only allow you to set the order between the resources or set properties of the vaults inside the class. It's not possible to do something like $vaults = Dirvish::Vault <| |>.

As a last resort, you can also hack something in ruby. The type instance seems to be passed the catalog early in the run, in autorequire. You can save the reference at that point an do nasty things afterwards.

Artefacto
  • 1,045
  • 1
  • 8
  • 11