summaryrefslogtreecommitdiffstats
path: root/lib/postmulti
blob: a6e971cf03677907cc06c0a8888890a2ef8e431c (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
#!/usr/bin/python
#
# Create and manage postfix instances.
#
# Copyright 2013 Guilhem Moulin <guilhem@fripost.org>
#
# Licensed under the GNU GPL version 3 or higher.
#


# Look up postfix configuration variable
def postconf(k, instance=None):
    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', k ])
    return subprocess.check_output(cmd, stderr=subprocess.STDOUT).rstrip()


def main():
    module = AnsibleModule(
        argument_spec = dict(
            instance = dict( required=True ),
            group    = dict( required=False )
        ),
        supports_check_mode=True
    )

    params = module.params
    instance = params['instance']
    group = params['group']

    changed=False
    try:
        enable  = postconf('multi_instance_enable')
        wrapper = postconf('multi_instance_wrapper')

        if enable != "yes" or not wrapper:
            # Initiate postmulti
            changed = True
            if module.check_mode:
                module.exit_json(changed=changed, msg="init postmulti")
            cmd = [ os.path.join(os.sep, 'usr', 'sbin', 'postmulti') ]
            cmd.extend([ '-e', 'init' ])
            subprocess.check_output(cmd, stderr=subprocess.STDOUT).rstrip()

        instances = postconf('multi_instance_directories').split()
        if os.path.join(os.sep, 'etc', 'postfix-%s' % instance) not in instances:
            changed = True
            # Create the instance

            if module.check_mode:
                module.exit_json(changed=changed, msg="create postmulti")
            cmd = [ os.path.join(os.sep, 'usr', 'sbin', 'postmulti') ]
            cmd.extend([ '-e', 'create' ])
            if group:
                cmd.extend([ '-G', group ])
            cmd.extend([ '-I', 'postfix-%s' % instance ])
            subprocess.check_output(cmd, stderr=subprocess.STDOUT).rstrip()

        elif group != postconf('multi_instance_group', instance):
           changed = True

           # Assign a new group, or remove the existing group
           if module.check_mode:
               module.exit_json(changed=changed, msg="assign group")
           cmd = [ os.path.join(os.sep, 'usr', 'sbin', 'postmulti') ]
           cmd.extend([ '-e', 'assign', '-i', 'postfix-%s' % instance ])
           if group:
               cmd.extend([ '-G', group ])
           else:
               cmd.extend([ '-G', '-' ])
           subprocess.check_output(cmd, stderr=subprocess.STDOUT).rstrip()

        module.exit_json(changed=changed)

    except subprocess.CalledProcessError, e:
        module.fail_json(rv=e.returncode, msg=e.output.rstrip())


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