summaryrefslogtreecommitdiffstats
path: root/lib/postmap
blob: 5eba760da1aafb8d89a8435a90614d456418fba8 (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
#!/usr/bin/python

# Create or update postfix's alias and lookup tables
# Copyright © 2013 Guilhem Moulin <guilhem@fripost.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

try:
    import selinux
    HAVE_SELINUX=True
except ImportError:
    HAVE_SELINUX=False


# Look up for the file suffix corresponding to 'db'. If 'db' is unset,
# pick the default_detabase_type of the given instance instead.
def file_suffix(instance, db):
    if not db:
        cmd = [ os.path.join(os.sep, 'usr', 'sbin', 'postconf') ]
        if instance:
            config = os.path.join(os.sep, 'etc', 'postfix-%s' % instance)
            cmd.extend([ '-c', config ])
        cmd.extend([ '-h', 'default_database_type' ])
        null = open (os.devnull, 'wb')
        db = subprocess.check_output(cmd, stderr=null).rstrip()
        null.closed

    # See postmap(1) and postalias(1)
    suffixes = { 'btree': 'db', 'cdb': 'cdb', 'hash': 'db' }
    return suffixes[db]


# Compile the given (alias/lookup) table
def compile(cmd, instance, db, src):
    cmd = [ os.path.join(os.sep, 'usr', 'sbin', cmd) ]
    if instance:
        config = os.path.join(os.sep, 'etc', 'postfix-%s' % instance)
        cmd.extend([ '-c', config ])

    if db:
        src = "%s:%s" % (db,src)

    cmd.append(src)
    subprocess.check_output(cmd, stderr=subprocess.STDOUT)


def main():
    module = AnsibleModule(
        argument_spec = dict(
            src = dict( required=True ),
            db  = dict( choices=['btree','cdb','hash'] ),
            cmd = dict( choices=['postmap','postalias'], default='postmap' ),
            instance = dict( required=False )
        ),
        add_file_common_args=True,
        supports_check_mode=True
    )

    params = module.params
    src = params['src']
    cmd = params['cmd']

    if os.path.isabs(src):
        src = src
    else:
        module.fail_json(msg="absolute paths are required")

    if not os.path.exists(src):
        module.fail_json(src=src, msg="no such file")

    try:
        dst = "%s.%s" % (src, file_suffix(params['instance'], params['db']))
        params['dest'] = dst
        file_args = module.load_file_common_arguments(params)

        changed = False
        msg = None
        if not os.path.exists(dst) or os.path.getmtime(dst) <= os.path.getmtime(src):
            changed = True
            if not module.check_mode:
                msg = compile( params['cmd'], params['instance'], params['db'], src)
    except subprocess.CalledProcessError, e:
        module.fail_json(rv=e.returncode, msg=e.output.rstrip())

    changed = module.set_file_attributes_if_different(file_args, changed)
    module.exit_json(changed=changed, msg=msg)


# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()