From 525d67394ac37fc188d2de8965622f66a5b5de5a Mon Sep 17 00:00:00 2001 From: Stanislav Voroniy Date: Wed, 30 Sep 2015 09:54:46 +0200 Subject: [PATCH] Support for any kind of postfix lookup tables --- README.md | 52 ++++++++++++------- recipes/default.rb | 2 +- recipes/hash_maps.rb | 30 ----------- recipes/maps.rb | 50 ++++++++++++++++++ templates/default/{hash_maps.erb => maps.erb} | 2 +- 5 files changed, 84 insertions(+), 52 deletions(-) delete mode 100644 recipes/hash_maps.rb create mode 100644 recipes/maps.rb rename templates/default/{hash_maps.erb => maps.erb} (80%) diff --git a/README.md b/README.md index 9d49063..d89999a 100644 --- a/README.md +++ b/README.md @@ -174,31 +174,43 @@ To use Chef Server search to automatically detect a node that is the relayhost, **Note** This recipe will set the `node['postfix']['mail_type']` to "master" with an override attribute. -### hash_maps -General recipe to manage any number of hash: tables. You can replace with it recipes like `transport` or `virtual_aliases`, but what is more important - you can create any kinds of hash maps, which has no own recipe. +### maps +General recipe to manage any number of any type postfix lookup tables. You can replace with it recipes like `transport` or `virtual_aliases`, but what is more important - you can create any kinds of maps, which has no own recipe, including database lookup maps configuration. +`maps` is a hash keys of which is a lookup table type and value is another hash with filenames as the keys and hash with file content as the value. File content is an any number of key/value pairs which meaning depends on lookup table type. Examlle: ```json "override_attributes": { "postfix": { - "hash_maps": { - "/etc/postfix/vmailbox": { - "john@example.com": "ok", - "john@example.net": "ok", - }, - "/etc/postfix/virtual": { - "postmaster@example.com": "john@example.com", - "postmaster@example.net": "john@example.net", - "root@mail.example.net": "john@example.net" - }, - "/etc/postfix/envelope_senders": { - "@example.com": "john@example.com", - "@example.net": "john@example.net" - }, - "/etc/postfix/relay_recipients": { - "john@example.net": "ok", - "john@example.com": "ok", - "admin@example.com": "ok", + "maps": { + "hash": { + "/etc/postfix/vmailbox": { + "john@example.com": "ok", + "john@example.net": "ok", + }, + "/etc/postfix/virtual": { + "postmaster@example.com": "john@example.com", + "postmaster@example.net": "john@example.net", + "root@mail.example.net": "john@example.net" + }, + "/etc/postfix/envelope_senders": { + "@example.com": "john@example.com", + "@example.net": "john@example.net" + }, + "/etc/postfix/relay_recipients": { + "john@example.net": "ok", + "john@example.com": "ok", + "admin@example.com": "ok", + } + }, + "pgsql": { + "/etc/postfix/pgtest": { + "hosts": "db.local:2345", + "user": "postfix", + "password": "test", + "dbname": "postdb", + "query": "SELECT replacement FROM aliases WHERE mailbox = '%s'" + } } } } diff --git a/recipes/default.rb b/recipes/default.rb index ae442fa..82dc22a 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -34,4 +34,4 @@ include_recipe 'postfix::virtual_aliases_domains' if node['postfix']['use_virtua include_recipe 'postfix::relay_restrictions' if node['postfix']['use_relay_restrictions_maps'] -include_recipe 'postfix::hash_maps' if node['postfix']['hash_maps'] +include_recipe 'postfix::maps' if node['postfix']['maps'] diff --git a/recipes/hash_maps.rb b/recipes/hash_maps.rb deleted file mode 100644 index 3c382b0..0000000 --- a/recipes/hash_maps.rb +++ /dev/null @@ -1,30 +0,0 @@ -# encoding: utf-8 -# Copyright:: Copyright (c) 2012, Chef Software, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -node['postfix']['hash_maps'].each do |file, content| - execute "update-postmap-#{file}" do - command "postmap #{file}" - environment PATH: "#{ENV['PATH']}:/opt/omni/bin:/opt/omni/sbin" if platform_family?('omnios') - action :nothing - end - - template file do - source 'hash_maps.erb' - variables(map: content) - notifies :run, "execute[update-postmap-#{file}]" - notifies :restart, 'service[postfix]' - end -end diff --git a/recipes/maps.rb b/recipes/maps.rb new file mode 100644 index 0000000..4078fc4 --- /dev/null +++ b/recipes/maps.rb @@ -0,0 +1,50 @@ +# encoding: utf-8 +# Copyright:: Copyright (c) 2012, Chef Software, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +node['postfix']['maps'].each do |type, maps| + if node['platform_family'] == 'debian' + if ['pgsql', 'mysql', 'ldap', 'cdb'].include?(type) + package "postfix-#{type}" + end + end + + maps.each do |file, content| + if ['btree', 'cdb', 'dbm', 'hash', 'sdbm'].include?(type) + execute "update-postmap-#{file}" do + command "postmap #{file}" + environment PATH: "#{ENV['PATH']}:/opt/omni/bin:/opt/omni/sbin" if platform_family?('omnios') + action :nothing + end + end + if ['pgsql', 'mysql', 'ldap', 'memcache', 'sqlite'].include?(type) + separator = " = " + else + separator = " " + end + template file do + source 'maps.erb' + only_if "postconf -m | grep -q #{type}" + variables( + map: content, + separator: separator + ) + if ['btree', 'cdb', 'dbm', 'hash', 'sdbm'].include?(type) + notifies :run, "execute[update-postmap-#{file}]" + end + notifies :restart, 'service[postfix]' + end + end +end diff --git a/templates/default/hash_maps.erb b/templates/default/maps.erb similarity index 80% rename from templates/default/hash_maps.erb rename to templates/default/maps.erb index 0cde725..f3bfc72 100644 --- a/templates/default/hash_maps.erb +++ b/templates/default/maps.erb @@ -5,5 +5,5 @@ # <% @map.each do |key, value| -%> -<%= key %> <%= value %> +<%= key %><%= @separator %><%= value %> <% end unless @map.nil? -%>