File: transport.pp

package info (click to toggle)
puppet-module-camptocamp-postfix 5.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 576 kB
  • sloc: ruby: 1,544; sh: 10; makefile: 4
file content (127 lines) | stat: -rw-r--r-- 4,148 bytes parent folder | download
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# @summary Manage the transport map of postfix
#
# Manages content of the /etc/postfix/transport map.
#
# @example Simple transport map config
#   include postfix
#   postfix::hash { '/etc/postfix/transport':
#     ensure => present,
#   }
#   postfix::config { 'transport_maps':
#     value => 'hash:/etc/postfix/transport, regexp:/etc/postfix/transport_regexp',
#   }
#   postfix::transport {
#     'mailman.example.com':
#        ensure      => present,
#        destination => 'mailman';
#     'slow_transport':
#        ensure      => present,
#        nexthop     => '/^user-.*@mydomain\.com/'
#        file        => '/etc/postfix/transport_regexp',
#        destination => 'slow'
#   }
#
# @param ensure
#   Defines whether the transport entry is present or not. Value can either be present or absent.
#
# @param destination
#   The destination to be delivered to (transport(5)).
#   Example: `mailman`.
#
# @param nexthop
#   A string to define where and how to deliver the mail (transport(5)).
#   Example: `[smtp.google.com]:25`.
#
# @param file
#   Where to create the file. If not defined "${postfix::confdir}/transport"
#   will be used as path.
#
# @see https://www.postfix.org/transport.5.html
#
define postfix::transport (
  Enum['present', 'absent']      $ensure      = 'present',
  Optional[String]               $destination = undef,
  Optional[String]               $nexthop     = undef,
  Optional[Stdlib::Absolutepath] $file        = undef,
) {
  include postfix

  $_file = pick($file, "${postfix::confdir}/transport")

  $smtp_nexthop = (String($nexthop) =~ /\[.*\]/)

  case $ensure {
    'present': {
      if ($smtp_nexthop) {
        $change_destination = "rm pattern[. = '${name}']/transport"
      } else {
        if ($destination) {
          $change_destination = "set pattern[. = '${name}']/transport '${destination}'"
        } else {
          $change_destination = "clear pattern[. = '${name}']/transport"
        }
      }

      if ($nexthop) {
        # This is some paranoia over splitting.  We can't do a simple split here, as there
        # could be ipv6 addrs in a nexthop's host.  So we're definitely into neeeding a regexp.
        # The regexp needs to be 'the same base' as used in smtp_nexthop above, while also
        # capturing the port.
        # From https://github.com/voxpupuli/puppet-postfix/issues/241, this exists
        # to handle [host]:port nexthop.  Other cases are handled by simply passing nexthop
        # straight through.
        $nexthop_match = $nexthop.match(/(:?\[.*\]):(\d+)/)
        # If this matched, we'll have a length of 3: [whole string, host, port].
        if ($nexthop_match =~ Array and $nexthop_match.length == 3) {
          $change_nexthop = [
            "rm pattern[. = '${name}']/nexthop",
            "set pattern[. = '${name}']/host '${nexthop_match[1]}'",
            "set pattern[. = '${name}']/port '${nexthop_match[2]}'",
          ]
        } else {
          # If it didn't match, we just report the nexthop unmodified; remove any breakout of
          # host/port that may have existed.
          $change_nexthop = [
            "rm pattern[. = '${name}']/host",
            "rm pattern[. = '${name}']/port",
            "set pattern[. = '${name}']/nexthop '${nexthop}'",
          ]
        }
      } else {
        $change_nexthop = [
          "clear pattern[. = '${name}']/nexthop",
          "rm pattern[. = '${name}']/host",
          "rm pattern[. = '${name}']/port",
        ]
      }

      $changes = flatten([
          "set pattern[. = '${name}'] '${name}'",
          $change_destination,
          $change_nexthop,
      ])
    }

    'absent': {
      $changes = "rm pattern[. = '${name}']"
    }

    default: {
      fail "\$ensure must be either 'present' or 'absent', got '${ensure}'"
    }
  }

  augeas { "Postfix transport - ${name}":
    lens    => 'Postfix_Transport.lns',
    incl    => $_file,
    changes => $changes,
  }

  if defined(Package['postfix']) {
    Package['postfix'] -> Postfix::Transport[$title]
  }

  if defined(Postfix::Hash[$_file]) {
    Postfix::Transport[$title] ~> Postfix::Hash[$_file]
  }
}