diff options
| author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-07-10 23:24:34 +0000 |
|---|---|---|
| committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-07-10 23:24:34 +0000 |
| commit | 2d3c920e0fdef4ca38e14677a4a670f0890ff994 (patch) | |
| tree | 01dfd2f3106dd6605cd9055c306493e29fd7029c | |
| parent | fdfe0a387d95045d442b945d54a6b757b9581e2e (diff) | |
Adding support for a "mailalias" type, with /etc/aliases support initially. I have not yet figured out how to best rebuild the aliases file when necessary.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2676 980ebf18-57e1-0310-9a29-db15c13687c0
| -rw-r--r-- | CHANGELOG | 2 | ||||
| -rw-r--r-- | lib/puppet/metatype/attributes.rb | 20 | ||||
| -rwxr-xr-x | lib/puppet/provider/mailalias/aliases.rb | 31 | ||||
| -rwxr-xr-x | lib/puppet/type/mailalias.rb | 50 | ||||
| -rw-r--r-- | test/data/types/mailalias/file1 | 183 | ||||
| -rwxr-xr-x | test/ral/providers/mailalias/aliases.rb | 57 | ||||
| -rwxr-xr-x | test/ral/providers/parsedfile.rb | 25 | ||||
| -rwxr-xr-x | test/ral/types/mailalias.rb | 50 |
8 files changed, 399 insertions, 19 deletions
@@ -1,3 +1,5 @@ + Added a 'mailalias' type for managing mail aliases. + Added patch by Valentin Vidic that adds the '+>' syntax to resources, so parameter values can be added to. diff --git a/lib/puppet/metatype/attributes.rb b/lib/puppet/metatype/attributes.rb index 4ba3588a1..189768090 100644 --- a/lib/puppet/metatype/attributes.rb +++ b/lib/puppet/metatype/attributes.rb @@ -295,13 +295,16 @@ class Puppet::Type [self.name, name] end + if parent = options[:parent] + options.delete(:parent) + else + parent = Puppet::Property + end + # We have to create our own, new block here because we want to define # an initial :retrieve method, if told to, and then eval the passed # block if available. - prop = genclass(name, - :parent => options[:parent] || Puppet::Property, - :hash => @validproperties - ) do + prop = genclass(name, :parent => parent, :hash => @validproperties, :attributes => options) do # If they've passed a retrieve method, then override the retrieve # method on the class. if options[:retrieve] @@ -322,15 +325,6 @@ class Puppet::Type @properties << prop end - if options[:event] - prop.event = options[:event] - end - - # Grr. - if options[:required_features] - prop.required_features = options[:required_features] - end - # define_method(name) do # @parameters[name].should # end diff --git a/lib/puppet/provider/mailalias/aliases.rb b/lib/puppet/provider/mailalias/aliases.rb new file mode 100755 index 000000000..e3bf2c59f --- /dev/null +++ b/lib/puppet/provider/mailalias/aliases.rb @@ -0,0 +1,31 @@ +require 'puppet/provider/parsedfile' + +Puppet::Type.type(:mailalias).provide(:aliases, + :parent => Puppet::Provider::ParsedFile, + :default_target => "/etc/aliases", + :filetype => :flat +) do + text_line :comment, :match => /^#/ + text_line :blank, :match => /^\s*$/ + + record_line :aliases, :fields => %w{name recipient}, :separator => /\s*:\s*/, :block_eval => :instance do + def post_parse(record) + record[:recipient] = record[:recipient].split(/\s*,\s*/).collect { |d| d.gsub(/^['"]|['"]$/, '') } + record + end + + def to_line(record) + dest = record[:recipient].collect do |d| + # Quote aliases that have non-alpha chars + if d =~ /[^-\w@.]/ + '"%s"' % d + else + d + end + end.join(",") + return "%s: %s" % [record[:name], dest] + end + end +end + +# $Id$ diff --git a/lib/puppet/type/mailalias.rb b/lib/puppet/type/mailalias.rb new file mode 100755 index 000000000..79dfe42cc --- /dev/null +++ b/lib/puppet/type/mailalias.rb @@ -0,0 +1,50 @@ +module Puppet + newtype(:mailalias) do + @doc = "Creates an email alias in the local alias database." + + ensurable + + newparam(:name, :namevar => true) do + desc "The alias name." + end + + newproperty(:recipient, :array_matching => :all) do + desc "Where email should should be sent. Multiple values + should be specified as an array." + + def is_to_s(value) + if value.include?(:absent) + super + else + value.join(",") + end + end + + def should + @should + end + + def should_to_s(value) + if value.include?(:absent) + super + else + value.join(",") + end + end + end + + newproperty(:target) do + desc "The file in which to store the aliases. Only used by + those providers that write to disk (i.e., not NetInfo)." + + defaultto { if @resource.class.defaultprovider.ancestors.include?(Puppet::Provider::ParsedFile) + @resource.class.defaultprovider.default_target + else + nil + end + } + end + end +end + +# $Id$ diff --git a/test/data/types/mailalias/file1 b/test/data/types/mailalias/file1 new file mode 100644 index 000000000..a7cbd4ebf --- /dev/null +++ b/test/data/types/mailalias/file1 @@ -0,0 +1,183 @@ +# This is the aliases file - it says who gets mail for whom. +# It was originally generated by `eximconfig', part of the exim package +# distributed with Debian, but it may edited by the mail system administrator. +# This file originally generated by eximconfig at Tue Jul 12345 12345:12345:12345 CDT 12345 +# See exim info section for details of the things that can be configured here. + +# Local aliases +eight-phone: 12345@domain.com +eight-pager: eight-phone +eight-sms: eight-phone +cindy-phone: 12345@domain.com +cindy-pager: cindy-phone +cindy-sms: cindy-phone + +ben-phone: 12345@txt.att.net +ben: eight +ajax: eight + +teyo: teyo@brainfinger.org + +#----------------# +# Family aliases # +#----------------# + +smithes: eight,six,two,seven,isaiah,kristen,cindy,three +smith: smithes +#morris: morrisbpaddlin@hotmail.com +#six: grendelwench@hotmail.com +two: twosmith@thirddomain.com +seven: sevensmith@thirddomain.com +isaiah: ismith@thirddomain.com +kristen: kristenwells@thirddomain.com +one-phone: 12345@otherdomain.com +one-pager: one-phone +seven-phone: 12345@otherdomain.com +seven-pager: seven-phone +seven-sms: seven-phone +ike-phone: 12345@otherdomain.com +ike-page: ike-phone +ike-sms: ike-phone +six-phone: 12345@otherdomain.com +six-page: six-phone +six-sms: six-phone +three: tferguson@mmc.edu +three-phone: 12345@otherdomain.com +three-page: three-phone +three-sms: three-phone +four-phone: 12345@otherdomain.com +four-page: four-phone +four-sms: four-phone +two-phone: 12345@otherdomain.com +two-page: two-phone +two-sms: two-phone +five-phone: 12345@otherdomain.com +five-page: five-phone +five-sms: five-phone + +#----------------# +# work aliases # +#----------------# +consulting: eight +svn: eight +info: eight +blog: eight +paypal: eight +trac: puppet-dev +dev: eight +sales: eight +training: eight +support: eight +faq: eight +puppet-docs: eight,nine@domain.com,ten@domain.com,eleven@other.com +docs: puppet-docs +jobs: eight + +# System stuff +postmaster: eight +root: eight + +daemon: root +bin: root +sys: root +sync: root +games: root +man: root +lp: root +mail: root +news: root +uucp: root +proxy: root +postgres: root +www-data: root +backup: root +operator: root +list: root +irc: root +gnats: root +nobody: root + +filemaker: eight + +hostmaster: root +usenet: root +webmaster: root +www: root +ftp: root +abuse: root +noc: root +security: root + +mailer-daemon: postmaster + +tap: one@two.com,two@three.com,three@four.com,four@five.com,five@six.com + +mailman: "|/var/lib/mailman/mail/mailman post mailman" +mailman-admin: "|/var/lib/mailman/mail/mailman admin mailman" +mailman-bounces: "|/var/lib/mailman/mail/mailman bounces mailman" +mailman-confirm: "|/var/lib/mailman/mail/mailman confirm mailman" +mailman-join: "|/var/lib/mailman/mail/mailman join mailman" +mailman-leave: "|/var/lib/mailman/mail/mailman leave mailman" +mailman-owner: "|/var/lib/mailman/mail/mailman owner mailman" +mailman-request: "|/var/lib/mailman/mail/mailman request mailman" +mailman-subscribe: "|/var/lib/mailman/mail/mailman subscribe mailman" +mailman-unsubscribe: "|/var/lib/mailman/mail/mailman unsubscribe mailman" + +puppet-dev: "|/var/lib/mailman/mail/mailman post puppet-dev" +puppet-dev-admin: "|/var/lib/mailman/mail/mailman admin puppet-dev" +puppet-dev-bounces: "|/var/lib/mailman/mail/mailman bounces puppet-dev" +puppet-dev-confirm: "|/var/lib/mailman/mail/mailman confirm puppet-dev" +puppet-dev-join: "|/var/lib/mailman/mail/mailman join puppet-dev" +puppet-dev-leave: "|/var/lib/mailman/mail/mailman leave puppet-dev" +puppet-dev-owner: "|/var/lib/mailman/mail/mailman owner puppet-dev" +puppet-dev-request: "|/var/lib/mailman/mail/mailman request puppet-dev" +puppet-dev-subscribe: "|/var/lib/mailman/mail/mailman subscribe puppet-dev" +puppet-dev-unsubscribe: "|/var/lib/mailman/mail/mailman unsubscribe puppet-dev" + +## puppet-commit mailing list +puppet-commit: "|/var/lib/mailman/mail/mailman post puppet-commit" +puppet-commit-admin: "|/var/lib/mailman/mail/mailman admin puppet-commit" +puppet-commit-bounces: "|/var/lib/mailman/mail/mailman bounces puppet-commit" +puppet-commit-confirm: "|/var/lib/mailman/mail/mailman confirm puppet-commit" +puppet-commit-join: "|/var/lib/mailman/mail/mailman join puppet-commit" +puppet-commit-leave: "|/var/lib/mailman/mail/mailman leave puppet-commit" +puppet-commit-owner: "|/var/lib/mailman/mail/mailman owner puppet-commit" +puppet-commit-request: "|/var/lib/mailman/mail/mailman request puppet-commit" +puppet-commit-subscribe: "|/var/lib/mailman/mail/mailman subscribe puppet-commit" +puppet-commit-unsubscribe: "|/var/lib/mailman/mail/mailman unsubscribe puppet-commit" + +## bnageek mailing list +bnageek: "|/var/lib/mailman/mail/mailman post bnageek" +bnageek-admin: "|/var/lib/mailman/mail/mailman admin bnageek" +bnageek-bounces: "|/var/lib/mailman/mail/mailman bounces bnageek" +bnageek-confirm: "|/var/lib/mailman/mail/mailman confirm bnageek" +bnageek-join: "|/var/lib/mailman/mail/mailman join bnageek" +bnageek-leave: "|/var/lib/mailman/mail/mailman leave bnageek" +bnageek-owner: "|/var/lib/mailman/mail/mailman owner bnageek" +bnageek-request: "|/var/lib/mailman/mail/mailman request bnageek" +bnageek-subscribe: "|/var/lib/mailman/mail/mailman subscribe bnageek" +bnageek-unsubscribe: "|/var/lib/mailman/mail/mailman unsubscribe bnageek" + +## puppet-users mailing list +puppet-users: "|/var/lib/mailman/mail/mailman post puppet-users" +puppet-users-admin: "|/var/lib/mailman/mail/mailman admin puppet-users" +puppet-users-bounces: "|/var/lib/mailman/mail/mailman bounces puppet-users" +puppet-users-confirm: "|/var/lib/mailman/mail/mailman confirm puppet-users" +puppet-users-join: "|/var/lib/mailman/mail/mailman join puppet-users" +puppet-users-leave: "|/var/lib/mailman/mail/mailman leave puppet-users" +puppet-users-owner: "|/var/lib/mailman/mail/mailman owner puppet-users" +puppet-users-request: "|/var/lib/mailman/mail/mailman request puppet-users" +puppet-users-subscribe: "|/var/lib/mailman/mail/mailman subscribe puppet-users" +puppet-users-unsubscribe: "|/var/lib/mailman/mail/mailman unsubscribe puppet-users" + +## puppet-bugs mailing list +puppet-bugs: "|/var/lib/mailman/mail/mailman post puppet-bugs" +puppet-bugs-admin: "|/var/lib/mailman/mail/mailman admin puppet-bugs" +puppet-bugs-bounces: "|/var/lib/mailman/mail/mailman bounces puppet-bugs" +puppet-bugs-confirm: "|/var/lib/mailman/mail/mailman confirm puppet-bugs" +puppet-bugs-join: "|/var/lib/mailman/mail/mailman join puppet-bugs" +puppet-bugs-leave: "|/var/lib/mailman/mail/mailman leave puppet-bugs" +puppet-bugs-owner: "|/var/lib/mailman/mail/mailman owner puppet-bugs" +puppet-bugs-request: "|/var/lib/mailman/mail/mailman request puppet-bugs" +puppet-bugs-subscribe: "|/var/lib/mailman/mail/mailman subscribe puppet-bugs" +puppet-bugs-unsubscribe: "|/var/lib/mailman/mail/mailman unsubscribe puppet-bugs" diff --git a/test/ral/providers/mailalias/aliases.rb b/test/ral/providers/mailalias/aliases.rb new file mode 100755 index 000000000..d0a95d877 --- /dev/null +++ b/test/ral/providers/mailalias/aliases.rb @@ -0,0 +1,57 @@ +#!/usr/bin/env ruby + +$:.unshift("../../../lib") if __FILE__ =~ /\.rb$/ + +require 'puppettest' +require 'puppettest/fileparsing' +require 'puppet/type/mailalias' + +class TestMailaliasAliasesProvider < Test::Unit::TestCase + include PuppetTest + include PuppetTest::FileParsing + + def setup + super + @provider = Puppet.type(:mailalias).provider(:aliases) + + @oldfiletype = @provider.filetype + + @alias = mkalias + end + + def teardown + Puppet::Util::FileType.filetype(:ram).clear + @provider.filetype = @oldfiletype + @provider.clear + super + end + + def mkalias(name = "me") + if defined? @pcount + @pcount += 1 + else + @pcount = 1 + end + args = { + :name => name, + :recipient => %w{here there} + } + + fakeresource = fakeresource(:mailalias, args[:name]) + + key = @provider.new(fakeresource) + args.each do |p,v| + key.send(p.to_s + "=", v) + end + + return key + end + + def test_data_parsing_and_generating + fakedata("data/types/mailalias").each { |file| + fakedataparse(file) + } + end +end + +# $Id$ diff --git a/test/ral/providers/parsedfile.rb b/test/ral/providers/parsedfile.rb index 2d9f7ae9d..e31bba75d 100755 --- a/test/ral/providers/parsedfile.rb +++ b/test/ral/providers/parsedfile.rb @@ -197,15 +197,13 @@ class TestParsedFile < Test::Unit::TestCase prov.default_target = :default # Create a couple of demo files - prov.target_object(:file1).write "bill b c" + prov.target_object(:file1).write "bill b c\njill b d" - prov.target_object(:file2).write "jill b d" - - prov.target_object(:default).write "will b d" + prov.target_object(:default).write "will b d\n" # Create some resources for some of those demo files - resource = mkresource "bill", :target => :file1 - default = mkresource "will", :target => :default + resource = mkresource "bill", :target => :file1, :one => "b", :two => "c" + default = mkresource "will", :target => :default, :one => "b", :two => "d" resources = {"bill" => resource, "will" => default} @@ -215,6 +213,7 @@ class TestParsedFile < Test::Unit::TestCase # Make sure we prefetched our resources. assert_equal("b", resource.provider.one, "did not prefetch resource from file1") + assert_equal("c", resource.provider.two, "did not prefetch resource from file1") assert_equal("b", default.provider.one, "did not prefetch resource from default") assert_equal("d", default.provider.two, "did not prefetch resource from default") @@ -232,6 +231,20 @@ class TestParsedFile < Test::Unit::TestCase assert(providers.find { |provider| provider.name == name}, "Did not return %s in list" % name) end + + # Now modify our resources and write them out, making sure that prefetching + # hasn't somehow destroyed this ability + resource[:one] = "a" + default[:one] = "a" + + + assert_apply(resource) + assert_apply(default) + + assert_equal("bill a c\njill b d\n", prov.target_object(:file1).read, + "Did not write changed resource correctly") + assert_equal("will a d\n", prov.target_object(:default).read, + "Did not write changed default resource correctly") end # Make sure we can correctly prefetch on a target. diff --git a/test/ral/types/mailalias.rb b/test/ral/types/mailalias.rb new file mode 100755 index 000000000..6dce531e9 --- /dev/null +++ b/test/ral/types/mailalias.rb @@ -0,0 +1,50 @@ +#!/usr/bin/env ruby + +$:.unshift("../../lib") if __FILE__ =~ /\.rb$/ + +require 'puppettest' +require 'mocha' + +class TestMailAlias < Test::Unit::TestCase + include PuppetTest + + def setup + super + @type = Puppet::Type.type(:mailalias) + + @provider = @type.defaultprovider + + # Make sure they aren't using something funky like netinfo + unless @provider.name == :aliases + @type.defaultprovider = @type.provider(:aliases) + end + + cleanup do @type.defaultprovider = nil end + + if @provider.respond_to?(:default_target=) + @default_file = @provider.default_target + cleanup do + @provider.default_target = @default_file + end + @target = tempfile() + @provider.default_target = @target + end + end + + # This isn't much of a test, but then, it's not much of a type. + def test_recipient_arrays + resource = @type.create(:name => "luke", :recipient => "yay", :target => tempfile) + resource.provider.expects(:recipient).returns([:absent]) + values = nil + assert_nothing_raised("Could not retrieve mailalias") do + values = resource.retrieve.inject({}) { |hash, a| hash[a[0].name] = a[1]; hash } + end + assert_equal([:absent], values[:recipient]) + resource.property(:recipient).expects(:set).with(%w{yay}) + assert_nothing_raised("Could not sync mailalias") do + resource.property(:recipient).sync + end + end +end + +# $Id$ |
