Commit 6232194a authored by Raphaël Pinson's avatar Raphaël Pinson
Browse files

Merge pull request #33 from raphink/dev/refactor

Refactor postfix module
parents 364c2bff 6f078e57
fixtures:
repositories:
"augeas": "git://github.com/camptocamp/puppet-augeas.git"
"stdlib": "git://github.com/puppetlabs/puppetlabs-stdlib.git"
symlinks:
"postfix": "#{source_dir}"
......@@ -32,7 +32,7 @@ let space_or_eol (sep:regexp) (default:string) =
del (space_or_eol_re? . sep . space_or_eol_re?) default
(* View: word *)
let word = store /[A-Za-z0-9@\*.-]+/
let word = store /[A-Za-z0-9@\*.+-]+/
(* View: comma *)
let comma = space_or_eol "," ", "
......
#class postfix::augeas
# This class provides the augeas lenses used by the postfix class
#
class postfix::augeas {
augeas::lens {'postfix_transport':
ensure => present,
lens_source => 'puppet:///modules/postfix/lenses/postfix_transport.aug',
test_source => 'puppet:///modules/postfix/lenses/test_postfix_transport.aug',
stock_since => '1.0.0',
}
augeas::lens {'postfix_virtual':
ensure => present,
lens_source => 'puppet:///modules/postfix/lenses/postfix_virtual.aug',
test_source => 'puppet:///modules/postfix/lenses/test_postfix_virtual.aug',
stock_since => '1.0.0',
}
}
#
#== Definition: postfix::config
# == Definition: postfix::config
#
#Uses the "postconf" command to add/alter/remove options in postfix main
#configuation file (/etc/postfix/main.cf).
# Uses the "postconf" command to add/alter/remove options in postfix main
# configuation file (/etc/postfix/main.cf).
#
#Parameters:
#- *name*: name of the parameter.
#- *ensure*: present/absent. defaults to present.
#- *value*: value of the parameter.
# === Parameters
#
#Requires:
#- Class["postfix"]
# [*name*] - name of the parameter.
# [*ensure*] - present/absent. defaults to present.
# [*value*] - value of the parameter.
#
#Example usage:
# === Requires
#
# node "toto.example.com" {
# - Class["postfix"]
#
# include postfix
#
# postfix::config {
# "smtp_use_tls" => "yes";
# "smtp_sasl_auth_enable" => "yes";
# "smtp_sasl_password_maps" => "hash:/etc/postfix/my_sasl_passwords";
# "relayhost" => "[mail.example.com]:587";
# }
# }
# === Examples
#
# postfix::config { 'smtp_use_tls':
# ensure => 'present',
# value => 'yes',
# }
#
define postfix::config ($value, $ensure = present) {
validate_string($value)
validate_string($ensure)
validate_re($ensure, ['present', 'absent'],
"\$ensure must be either 'present' or 'absent', got '${ensure}'")
Augeas {
incl => '/etc/postfix/main.cf',
lens => 'Postfix_Main.lns',
......@@ -39,12 +38,12 @@ define postfix::config ($value, $ensure = present) {
case $ensure {
present: {
augeas { "set postfix '${name}' to '${value}'":
changes => "set $name '$value'",
changes => "set ${name} '${value}'",
}
}
absent: {
augeas { "rm postfix '${name}'":
changes => "rm $name",
changes => "rm ${name}",
}
}
default: {}
......
class postfix::files {
include postfix::params
$alias_maps = $postfix::all_alias_maps
$inet_interfaces = $postfix::inet_interfaces
$mail_user = $postfix::mail_user
$master_smtp = $postfix::master_smtp
$master_smtps = $postfix::master_smtps
$master_submission = $postfix::master_submission
$myorigin = $postfix::myorigin
$root_mail_recipient = $postfix::root_mail_recipient
$smtp_listen = $postfix::_smtp_listen
$use_amavisd = $postfix::use_amavisd
$use_dovecot_lda = $postfix::use_dovecot_lda
$use_schleuder = $postfix::use_schleuder
$use_sympa = $postfix::use_sympa
file { '/etc/mailname':
ensure => 'file',
content => "${::fqdn}\n",
seltype => $postfix::params::seltype,
}
# Aliases
file { '/etc/aliases':
ensure => 'file',
content => "# file managed by puppet\n",
notify => Exec['newaliases'],
replace => false,
seltype => $postfix::params::seltype,
}
# Aliases
exec { 'newaliases':
command => '/usr/bin/newaliases',
refreshonly => true,
subscribe => File['/etc/aliases'],
}
# Config files
if $postfix::mastercf_source {
$mastercf_content = undef
} else {
$mastercf_content = template(
$postfix::params::master_os_template,
'postfix/master.cf.common.erb'
)
}
file { '/etc/postfix/master.cf':
ensure => 'file',
content => $mastercf_content,
group => 'root',
mode => '0644',
owner => 'root',
seltype => $postfix::params::seltype,
source => $postfix::mastercf_source,
}
# Config files
file { '/etc/postfix/main.cf':
ensure => 'file',
group => 'root',
mode => '0644',
owner => 'root',
replace => false,
seltype => $postfix::params::seltype,
source => $postfix::maincf_source,
}
::postfix::config {
'alias_maps': value => $alias_maps;
'inet_interfaces': value => $inet_interfaces;
'myorigin': value => $myorigin;
}
case $::osfamily {
'RedHat': {
::postfix::config {
'mailq_path': value => '/usr/bin/mailq.postfix';
'newaliases_path': value => '/usr/bin/newaliases.postfix';
'sendmail_path': value => '/usr/sbin/sendmail.postfix';
}
}
default: {}
}
mailalias {'root':
recipient => $root_mail_recipient,
notify => Exec['newaliases'],
}
}
#== Definition: postfix::hash
# == Definition: postfix::hash
#
#Creates postfix hashed "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.
# Creates postfix hashed "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.
# === Parameters
#
#Requires:
#- Class["postfix"]
# [*name*] - the name of the map file.
# [*ensure*] - present/absent, defaults to present.
# [*source*] - file source.
#
#Example usage:
# === Requires
#
# node "toto.example.com" {
# - Class["postfix"]
#
# include postfix
# === Examples
#
# postfix::hash { "/etc/postfix/virtual":
# ensure => present,
# }
# postfix::config { "virtual_alias_maps":
# value => "hash:/etc/postfix/virtual"
# }
# }
# postfix::hash { '/etc/postfix/virtual':
# ensure => present,
# }
# postfix::config { 'virtual_alias_maps':
# value => 'hash:/etc/postfix/virtual',
# }
#
define postfix::hash ($ensure='present', $source=false, $content=false) {
define postfix::hash (
$ensure='present',
$source=undef,
$content=undef,
) {
include ::postfix::params
# selinux labels differ from one distribution to another
case $::operatingsystem {
validate_absolute_path($name)
validate_string($source)
validate_string($content)
validate_string($ensure)
validate_re($ensure, ['present', 'absent'],
"\$ensure must be either 'present' or 'absent', got '${ensure}'")
RedHat, CentOS: {
case $::lsbmajdistrelease {
'4': { $postfix_seltype = 'etc_t' }
'5','6': { $postfix_seltype = 'postfix_etc_t' }
default: { $postfix_seltype = undef }
}
}
default: {
$postfix_seltype = undef
}
if $source and $content {
fail 'You must provide either \'source\' or \'content\', not both'
}
File {
mode => '0600',
owner => root,
group => root,
seltype => $postfix_seltype,
seltype => $postfix::params::seltype,
}
if $source != false {
file {$name:
ensure => $ensure,
source => $source,
require => Package['postfix'],
}
} else {
if $content != false {
file {$name:
ensure => $ensure,
content => $content,
require => Package['postfix'],
}
} else {
file {$name:
ensure => $ensure,
require => Package['postfix'],
}
}
file { $name:
ensure => $ensure,
source => $source,
content => $content,
require => Package['postfix'],
}
file {"${name}.db":
......
......@@ -4,174 +4,156 @@
# This class provides a basic setup of postfix with local and remote
# delivery and an SMTP server listening on the loopback interface.
#
# Parameters:
# - *$postfix_smtp_listen*: address on which the smtp service will listen to.
# defaults to 127.0.0.1
# - *$root_mail_recipient*: who will recieve root's emails. defaults to 'nobody'
# === Parameters
#
# Example usage:
# [*alias_maps*] - (string)
#
# node 'toto.example.com' {
# $postfix_smtp_listen = '192.168.1.10'
# include postfix
# [*inet_interfaces*] - (string)
#
# [*ldap*] - (boolean) Whether to use LDAP
#
# [*ldap_base*] - (string)
#
# [*ldap_host*] - (string)
#
# [*ldap_options*] - (string)
#
# [*mail_user*] - (string) The mail user
#
# [*mailman*] - (boolean)
#
# [*maincf_source*] - (string)
#
# [*mastercf_source*] - (string)
#
# [*master_smtp*] - (string)
#
# [*master_smtps*] - (string)
#
# [*master_submission*] - (string)
#
# [*mta*] - (boolean) Configure postfix minimally, as a simple MTA
#
# [*mydestination*] - (string)
#
# [*mynetworks*] - (string)
#
# [*myorigin*] - (string)
#
# [*relayhost*] - (string)
#
# [*root_mail_recipient*] - (string)
#
# [*satellite*] - (boolean) Whether to use as a satellite
# (implies MTA)
#
# [*smtp_listen*] - (string) The SMTP listen interface
#
# [*use_amavisd*] - (boolean) Whether to setup for Amavis
#
# [*use_dovecot_lda*] - (boolean) Whether to setup for Dovecot LDA
#
# [*use_schleuder*] - (boolean) Whether to setup for Schleuder
#
# [*use_sympa*] - (boolean) Whether to setup for Sympa
#
# === Examples
#
# class { 'postfix':
# smtp_listen => '192.168.1.10',
# }
#
class postfix {
# selinux labels differ from one distribution to another
case $::operatingsystem {
RedHat, CentOS: {
case $::lsbmajdistrelease {
'4': { $postfix_seltype = 'etc_t' }
'5','6': { $postfix_seltype = 'postfix_etc_t' }
default: { $postfix_seltype = undef }
}
}
default: {
$postfix_seltype = undef
class postfix (
$alias_maps = 'hash:/etc/aliases',
$inet_interfaces = 'all',
$ldap = false,
$ldap_base = undef,
$ldap_host = undef,
$ldap_options = undef,
$mail_user = 'vmail', # postfix_mail_user
$mailman = false,
$maincf_source = "puppet:///modules/${module_name}/main.cf",
$mastercf_source = undef,
$master_smtp = undef, # postfix_master_smtp
$master_smtps = undef, # postfix_master_smtps
$master_submission = undef, # postfix_master_submission
$mta = false,
$mydestination = '$myorigin', # postfix_mydestination
$mynetworks = '127.0.0.0/8', # postfix_mynetworks
$myorigin = $::fqdn,
$relayhost = undef, # postfix_relayhost
$root_mail_recipient = 'nobody', # root_mail_recipient
$satellite = false,
$smtp_listen = '127.0.0.1', # postfix_smtp_listen
$use_amavisd = false, # postfix_use_amavisd
$use_dovecot_lda = false, # postfix_use_dovecot_lda
$use_schleuder = false, # postfix_use_schleuder
$use_sympa = false, # postfix_use_sympa
) inherits postfix::params {
validate_bool($ldap)
validate_bool($mailman)
validate_bool($mta)
validate_bool($satellite)
validate_bool($use_amavisd)
validate_bool($use_dovecot_lda)
validate_bool($use_schleuder)
validate_bool($use_sympa)
validate_string($alias_maps)
validate_string($inet_interfaces)
validate_string($ldap_base)
validate_string($ldap_host)
validate_string($ldap_options)
validate_string($mail_user)
validate_string($maincf_source)
validate_string($mastercf_source)
validate_string($master_smtp)
validate_string($master_smtps)
validate_string($mydestination)
validate_string($mynetworks)
validate_string($myorigin)
validate_string($relayhost)
validate_string($root_mail_recipient)
validate_string($smtp_listen)
$_smtp_listen = $mailman ? {
true => '0.0.0.0',
default => $smtp_listen,
}
$all_alias_maps = $ldap ? {
false => $alias_maps,
true => "\"${alias_maps}, ldap:/etc/postfix/ldap-aliases.cf\"",
}
class { 'postfix::packages': } ->
class { 'postfix::files': } ~>
class { 'postfix::service': } ->
Class['postfix']
if $ldap {
include ::postfix::ldap
}
if $mta {
if $satellite {
fail('enabling both the $mta and $satellite parameters is not supported. Please disable one.')
}
include ::postfix::mta
}
# Default value for various options
if $postfix_smtp_listen == '' {
$postfix_smtp_listen = '127.0.0.1'
}
if $root_mail_recipient == '' {
$root_mail_recipient = 'nobody'
}
if $postfix_use_amavisd == '' {
$postfix_use_amavisd = 'no'
}
if $postfix_use_dovecot_lda == '' {
$postfix_use_dovecot_lda = 'no'
}
if $postfix_use_schleuder == '' {
$postfix_use_schleuder = 'no'
}
if $postfix_use_sympa == '' {
$postfix_use_sympa = 'no'
}
if $postfix_mail_user == '' {
$postfix_mail_user = 'vmail'
}
case $::operatingsystem {
/RedHat|CentOS|Fedora/: {
$mailx_package = 'mailx'
}
/Debian|kFreeBSD/: {
$mailx_package = $::lsbdistcodename ? {
/lenny|etch|sarge/ => 'mailx',
default => 'bsd-mailx',
}
}
'Ubuntu': {
if (versioncmp('10', $::lsbmajdistrelease) > 0) {
$mailx_package = 'mailx'
} else {
$mailx_package = 'bsd-mailx'
}
}
}
$master_os_template = $::operatingsystem ? {
/RedHat|CentOS/ => template('postfix/master.cf.redhat.erb', 'postfix/master.cf.common.erb'),
/Debian|Ubuntu|kFreeBSD/ => template('postfix/master.cf.debian.erb', 'postfix/master.cf.common.erb'),
}
package { 'postfix':
ensure => installed,
}
package { 'mailx':
ensure => installed,
name => $mailx_package,
}
service { 'postfix':
ensure => running,
enable => true,
hasstatus => true,
restart => '/etc/init.d/postfix reload',
require => Package['postfix'],
}
file { '/etc/mailname':
ensure => present,
content => "$::fqdn\n",
seltype => $postfix_seltype,
}
# Aliases
file { '/etc/aliases':
ensure => present,
content => '# file managed by puppet\n',
replace => false,
seltype => $postfix_seltype,
notify => Exec['newaliases'],
}
# Aliases
exec { 'newaliases':
command => '/usr/bin/newaliases',
refreshonly => true,
require => Package['postfix'],
subscribe => File['/etc/aliases'],
}
# Config files
file { '/etc/postfix/master.cf':
ensure => present,
owner => 'root',
group => 'root',
mode => '0644',
content => $master_os_template,
seltype => $postfix_seltype,
notify => Service['postfix'],
require => Package['postfix'],
}
# Config files
file { '/etc/postfix/main.cf':
ensure => present,
owner => 'root',
group => 'root',
mode => '0644',
source => 'puppet:///modules/postfix/main.cf',
replace => false,
seltype => $postfix_seltype,
notify => Service['postfix'],
require => Package['postfix'],
}
# Default configuration parameters
$myorigin = $valid_fqdn ? {
'' => $::fqdn,
default => $valid_fqdn,
}
postfix::config {
'myorigin': value => $myorigin;
'alias_maps': value => 'hash:/etc/aliases';
'inet_interfaces': value => 'all';
}
case $::operatingsystem {
RedHat, CentOS: {
postfix::config {
'sendmail_path': value => '/usr/sbin/sendmail.postfix';
'newaliases_path': value => '/usr/bin/newaliases.postfix';
'mailq_path': value => '/usr/bin/mailq.postfix';
}
if $satellite {
if $mta {
fail('enabling both the $mta and $satellite parameters is not supported. Please disable one.')
}
default: {}
include ::postfix::satellite
}
mailalias {'root':
recipient => $root_mail_recipient,
notify => Exec['newaliases'],
if $mailman {
include ::postfix::mailman
}
}
class postfix::ldap inherits postfix {
Postfix::Config['alias_maps'] {