summaryrefslogtreecommitdiff
path: root/manifests/zone.pp
blob: 828b887f2f33f4433b0b3cb544252272b4eb53a2 (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
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
128
129
130
131
132
133
# @summary Sets up one DNS zone
#
# @example
#   dns::zone { 'example.com':
#     rname => 'ns1.example.com',
#     mname => 'domainmaster.example.com',
#     ns    => [ 'ns1', 'ns2', ]
#   }
#
# @param zone
#   Domain this zone controls.
# @param mname
#   Primary master
# @param rname
#   mail to zone admin
# @param retry
#   Retry value for zone
# @param expire
#   Expire value for zone
# @param negative_ttl
#   Negative ttl for zone
# @param ttl
#   Default ttl for zone
# @param refresh
#   Refresh value for SOA
# @param records
#   Hash of records to create. Instanciates Dns::Record resources.
# @param ns
#   List of nameservers for this zone. Creates Dns::Record resources
#   with NS entries.
# @param type
#   Zonetype. master, slave, ...
# @param update_policy
#   A bind9 update policy, as a string.
# @param dynamic
#   However if this zone should be treated as a dynamic zone. If
#   enabled rndc freezes and thaws the zone around static updates.
#   Otherwise the zone file will be directly changed, and simply
#   reloaded afterwards.
#
#   Defaults to true if an update_policy is set.
# @param soa_ttl
#   TTL of SOA record.
define dns::zone (
  String $rname = undef,
  String $mname = $ns[0],
  Dns::Ttl $refresh = '24h',
  Dns::Ttl $retry = '2h',
  Dns::Ttl $expire = '1000h',
  Dns::Ttl $negative_ttl = '2d',
  Dns::Ttl $ttl = '24h',
  Optional[Dns::Ttl] $soa_ttl = undef,

  String $zone = $name,

  Array[Dns::RecordEntry] $records = [],

  Array[String] $ns = [$mname],

  String $type = 'master',

  Optional[String] $update_policy = undef,
  Boolean $dynamic = $update_policy != undef,
) {
  $zone_ = dns::ensure_ending_period($zone)

  $zone_serial = $facts.get("dns_zone_serial.'${zone_}'", 0)

  concat::fragment { "Dns::Zone - ${zone_}":
    target  => $dns::config_file,
    content => epp("${module_name}/zoneconf.epp", {
        zone          => $zone_,
        type          => $type,
        update_policy => $update_policy,
    }),
  }

  # $ns.each |$ns| {
  #   dns::record { "Dns::Zone - record - ${zone} NS ${ns}":
  #     key   => '@',
  #     type  => 'NS',
  #     zone  => $zone_,
  #     value => $ns,
  #   }
  # }

  # $fixed_records = $records.map |$record| {
  #   ["Dns::Zone - record - ${zone_} - ${record['class']} ${record['type']} ${record['key']} ${record['value']}",
  #   $record + { key => $record['key'] } - ['key']]
  # }.convert_to(Hash)

  # create_resources(dns::record, $fixed_records, {
  #     zone => $zone_,
  # })

  $params = {
    'rname'        => $rname,
    'mname'        => $mname,
    'refresh'      => $refresh,
    'expire'       => $expire,
    'negative_ttl' => $negative_ttl,
    'soa_ttl'      => $soa_ttl,
    'retry'        => $retry,
  }

  if $dynamic {
    dns_zone2 { $zone:
      require => Exec["Dns::zone freeze ${zone_}"],
      *       => $params,
    }

    exec { "Dns::zone freeze ${zone_}":
      command     => [$dns::rndc, 'freeze', $zone_],
      refreshonly => true,
    }

    exec { "Dns::zone thaw ${zone_}":
      command     => [$dns::rndc, 'thaw', $zone_],
      refreshonly => true,
      subscribe   => Dns_zone2[$zone_],
    }
  } else {
    dns_zone2 { $zone:
      notify => Exec["Dns::zone reload ${zone_}"],
      *      => $params,
    }

    exec { "Dns::zone reload ${zone_}":
      command     => [$dns::rndc, 'reload', $zone_],
      refreshonly => true,
    }
  }
}