A Modern Perl Configuration Management Framework

August 21, 2011

I’ve uploaded Thorium, a configuration framework for Perl to CPAN.

This is the source code to what’s described in a previous post. Note, the name space was refactored as an existing module Amethyst was already on CPAN.

Here’s the Quick Start excerpt from the documentation

QUICK START

The full source code is available in this distributions examples directory.

1. Choose A Namespace

We will be extending Thorium::BuildConf and Thorium::Conf. I suggest something unique that won’t show up on CPAN. For this example we are going to use Pizza. And our app will be creating a pizza.

2. Extend Thorium::BuildConf

This example will use a fictional Thorium::BuildConf::Knob for demonstration purposes. You are free and encouraged to create your own specific knobs.

package Pizza::BuildConf;

use Moose;

use Pizza::BuildConf::Knob::CrustType;

extends 'Thorium::BuildConf';

has '+type' => (default => 'Pizza Maker');

has '+files' => ('default' => 'awesome-pizza.tt2');

has '+knobs' => (
    'default' => sub {
        [
            Pizza::BuildConf::Knob::CrustType->new(
                'conf_key_name' => 'pizza.crust_type',
                'name'          => 'Crust type',
                'question'      => 'What kind of crust do you want?'
            )
        ];
    }
);

__PACKAGE__->meta->make_immutable;
no Moose;

We now have configurable item for the user.

3. Create conf/presets/defaults.yaml

This file will be the base for all configurable data that we will be accessing through a derived Thorium::Conf object. Use YAML::XS compatible syntax.

---
pizza:
    crust_type: thin

Now in your Template file, awesome-pizza.tt2, you have access to that data via a . separated syntax. For example, to get the crust type you’d use:

[% pizza.crust_type %]

You may also alter this data in your own defaults.yaml derived “preset”. See Thorium::BuildConf for more.

4. Extend Thorium::Conf

This will be our class to access to the YAML data we created in Step 3:

package Pizza::Conf;

use Moose;

extends 'Thorium::Conf';

# core
use File::Spec;

# CPAN
use Dir::Self;

has '+component_name' => ('default' => 'pizza-maker');

has '+component_root' => ('default' => File::Spec->catdir(__DIR__, '..', '..'));

__PACKAGE__->meta->make_immutable;
no Moose;

local.yaml is the resulting saved file of all configuration data. More about overriding defaults in Thorium::Conf.

5. Create the configure Script

#!/usr/bin/env perl

use strict;

use Find::Lib '../lib';
use Find::Lib 'lib';

use Pizza::BuildConf;

Pizza::BuildConf->new(
    'conf_type' => 'Pizza::Conf',
)->run;

6. Run configure

./configure

At this point you should see console GUI.

If you go to the Configure option you should see your crust type.