Rollout Advanced Configuration

[ Up ]

Hooks

file_append and other steps support adding custom hooks - blocks of Perl code that are run just before the modification is done, and just after a modification is done.

The value must be a CODE ref that takes a single argument - the key of the value to modify. The item itself will be localized in $_ inside the closure. You can use this code to modify the item before it is run.

Consider the following example:

file_append => [
  {
    file => "/tmp/rollout_time.txt",
    add => "Current time: ",
    match => qr/^Current time:/,
    pre_hook => sub {
      my($key) = $_;
      l "The value of \$key before modification is $_->{$key}";
      $_->{$key} .= asctime(localtime);
      l "The value of \$key after modification is $_->{$key}";
    },
  }
],

When this file_append entry runs, it will append a line to /tmp/rollout_tmp.txt that contains the current time when the step is run.

There are a set of standard hooks in the HookFactory package that you can use:

  • expand_hostname

    Replace the token %HOSTNAME% in the string with the current hostname.

  • expand_domain

    Replace the token %DOMAIN% in the string with the current domain name (Configured by the domain_name config key).

  • expand_ip

    Replace the token %PRIMARY_IP% in the string with the current primary IP address (Configured by the networks config key).

  • template_replace

    Replace multiple template values. This function takes a second parameter, a hash of keys to replace and their values.

    pre_hook => HookFactory::template_replace("Add", FOOBAR => "foo", BARBAZ => "bar"),

    On an input string of "This %FOOBAR% is really %BARBAZ%" this will output "This foo is really bar".

You can chain multiple hooks together ysing the HookChain() function. Each argument to this function is a closure that will be called in order.

pre_hook => HookChain(HookFactory::expand_hostname("add"), HookFactory::expand_domain("add")),