summaryrefslogtreecommitdiff
path: root/manifests/hook.pp
blob: a1fda6eb85571e25c5bc9af7c27110830394a150 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# @summary A Pacman hook

# @param ensure
# @param priority
# @param description
# @param when
# @param exec
# @param depends
# @param abort_on_fail
# @param needs_targets
# @param trigger
define pacman::hook (
  Enum['PreTransation', 'PostTransaction'] $when,
  String $exec,
  Variant[Pacman::Trigger, Array[Pacman::Trigger, 1]] $trigger,
  Enum['present', 'absent'] $ensure = 'present',
  Integer $priority = 50,
  Optional[String] $description = undef,
  Optional[Variant[String, Array[String, 1]]] $depends = undef,
  Boolean $abort_on_fail = false, # only for PreTransation
  Boolean $needs_targets = false,
) {
  require pacman

  if ($abort_on_fail and $when != 'PreTransation') {
    fail('abort_on_fail only valid when "when" => "PreTransation"')
  }

  # Normalize triggers to list
  $triggers = ($trigger ? {
      Array => $trigger,
      default  => [$trigger],
  }).map |$trigger| {
    # Normalize contents of each trigger, making
    {
      type      => $trigger['type'],
      operation => $trigger['operation'] ? {
        Array   => $trigger['operation'],
        default => [$trigger['operation']],
      },
      target => $trigger['target'] ? {
        Array   => $trigger['target'],
        default => [$trigger['target']],
      }
    }
  }

  $triggers.each |$trigger| {
    if $trigger['type'] == 'Path' {
      $trigger['target'].each |$target| {
        if $target[0] == '/' {
          fail("Target paths shouldn't start with '/' ${target} in trigger ${name}")
        }
      }
    }
  }

  $str = epp('pacman/hook.epp', {
      description   => $description,
      depends       => $depends ? {
        Optional => [],
        Array    => $depends,
        default  => [$depends],
      },
      triggers      => $triggers,
      exec          => $exec,
      when          => $when,
      abort_on_fail => $abort_on_fail,
      needs_targets => $needs_targets,
  })

  $chksum = $str.md5()

  file { $chksum:
    ensure         => $ensure,
    content        => $str,
    path           => "${pacman::hooks_path}/${priority}-${name}.hook",
    checksum       => 'md5',
    checksum_value => $chksum,
  }
}