map.pp 2.62 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
# == Definition: postfix::map
#
# Creates postfix "map" files. It will create "${name}", and then build
# "${name}.db" using the "postmap" command. The map file can then be referred to
# using postfix::config.
#
# === Parameters
#
# [*name*]   - the name of the map file.
# [*ensure*] - present/absent, defaults to present.
# [*source*] - file source.
# [*type*]   - type of the postfix map (valid values are cidr, pcre, hash...)
# [*path*]   - path of the created file. By default it is placed in the
14
15
#              postfix directory.
# [*mode*]   - mode of the created file. By default it is '0640'.
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#
# === Requires
#
# - Class["postfix"]
#
# === Examples
#
#   postfix::map { '/etc/postfix/virtual':
#     ensure => present,
#   }
#   postfix::config { 'virtual_alias_maps':
#     value => 'hash:/etc/postfix/virtual',
#   }
#
define postfix::map (
Raphaël Pinson's avatar
Raphaël Pinson committed
31
32
  Enum['present', 'absent']             $ensure = 'present',
  Variant[Array[String], String, Undef] $source = undef,
33
  Optional[Variant[Sensitive[String],String]] $content = undef,
Raphaël Pinson's avatar
Raphaël Pinson committed
34
  String                                $type = 'hash',
Romain Tartière's avatar
Romain Tartière committed
35
  Optional[Stdlib::Absolutepath]        $path = undef,
36
  String[4,4]                           $mode = '0640'
37
) {
Romain Tartière's avatar
Romain Tartière committed
38
  include postfix
Christos Papageorgiou's avatar
Christos Papageorgiou committed
39
  include postfix::params
40

Romain Tartière's avatar
Romain Tartière committed
41
42
  $_path = pick($path, "${postfix::confdir}/${name}")

43
44
45
46
47
48
49
50
51
52
53
54
  if (!defined(Class['postfix'])) {
    fail 'You must define class postfix before using postfix::config!'
  }

  if $source and $content {
    fail 'You must provide either \'source\' or \'content\', not both'
  }

  # CIDR and PCRE maps need a postfix reload, but not a postmap
  if $type =~ /^(cidr|pcre)$/ {
    $manage_notify = Service['postfix']
  } else {
55
56
57
58
59
    if $ensure == 'present' {
      $manage_notify = Exec["generate ${name}.db"]
    } else {
      $manage_notify = undef
    }
60
61
62
63
  }

  file { "postfix map ${name}":
    ensure  => $ensure,
Romain Tartière's avatar
Romain Tartière committed
64
    path    => $_path,
65
66
67
68
    source  => $source,
    content => $content,
    owner   => 'root',
    group   => 'postfix',
69
    mode    => $mode,
70
71
72
73
74
    require => Package['postfix'],
    notify  => $manage_notify,
  }

  if $type !~ /^(cidr|pcre)$/ {
Christos Papageorgiou's avatar
Christos Papageorgiou committed
75
    file { "postfix map ${name}.db":
76
      ensure  => $ensure,
Romain Tartière's avatar
Romain Tartière committed
77
      path    => "${_path}.db",
78
79
      owner   => 'root',
      group   => 'postfix',
80
      mode    => $mode,
81
82
      require => File["postfix map ${name}"],
      notify  => $manage_notify,
83
84
85
    }
  }

86
  $generate_cmd = $ensure ? {
Romain Tartière's avatar
Romain Tartière committed
87
88
    'absent'  => "rm ${_path}.db",
    'present' => "postmap ${_path}",
89
90
  }

Christos Papageorgiou's avatar
Christos Papageorgiou committed
91
  exec { "generate ${name}.db":
92
    command     => $generate_cmd,
93
    path        => $facts['path'],
94
95
96
97
    #creates    => "${name}.db", # this prevents postmap from being run !
    refreshonly => true,
  }
}