summaryrefslogtreecommitdiffstats
path: root/ipaserver/install/plugins/fix_replica_memberof.py
blob: d4ab75348eea29b61a0442b1e451f81a0471ad67 (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
# Authors:
#   Rob Crittenden <rcritten@redhat.com>
#
# Copyright (C) 2012  Red Hat
# see file 'COPYING' for use and warranty information
#
# 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/>.

import os
import pwd
from ipaserver.install.plugins import PRE_UPDATE, MIDDLE
from ipaserver.install.plugins.baseupdate import PreUpdate
from ipaserver import ipaldap
from ipaserver.install import replication
from ipalib import api

class update_replica_exclude_attribute_list(PreUpdate):
    """
    Run through all replication agreements and ensure that EXCLUDE list
    has all the required attributes so that we don't cause replication
    storms.
    """
    order=MIDDLE

    def execute(self, **options):
        # We need an IPAdmin connection to the backend
        self.log.debug("Start replication agreement exclude list update task")
        conn = ipaldap.IPAdmin(api.env.host, ldapi=True, realm=api.env.realm)
        conn.do_external_bind(pwd.getpwuid(os.geteuid()).pw_name)

        repl = replication.ReplicationManager(api.env.realm, api.env.host,
                                              None, conn=conn)
        entries = repl.find_replication_agreements()
        self.log.debug("Found %d agreement(s)", len(entries))
        for replica in entries:
            self.log.debug(replica.getValue('description'))
            attrlist = replica.getValue('nsDS5ReplicatedAttributeList')
            if attrlist is None:
                self.log.debug("Adding nsDS5ReplicatedAttributeList and nsDS5ReplicatedAttributeListTotal")
                current = replica.toDict()
                # Need to add it altogether
                replica.setValues('nsDS5ReplicatedAttributeList',
                    '(objectclass=*) $ EXCLUDE %s' % " ".join(replication.EXCLUDES))
                replica.setValues('nsDS5ReplicatedAttributeListTotal',
                    '(objectclass=*) $ EXCLUDE %s' % " ".join(replication.TOTAL_EXCLUDES))
                try:
                    repl.conn.updateEntry(replica.dn, current, replica.toDict())
                    self.log.debug("Updated")
                except Exception, e:
                    self.log.error("Error caught updating replica: %s", str(e))
            else:
                attrlist_normalized = attrlist.lower()
                missing = [attr for attr in replication.EXCLUDES
                                if attr not in attrlist_normalized]

                if missing:
                    self.log.debug("Attribute list needs updating")
                    current = replica.toDict()
                    replica.setValue('nsDS5ReplicatedAttributeList',
                        replica.getValue('nsDS5ReplicatedAttributeList') + ' %s' % ' '.join(missing))
                    try:
                        repl.conn.updateEntry(replica.dn, current, replica.toDict())
                        self.log.debug("Updated")
                    except Exception, e:
                        self.log.error("Error caught updating replica: %s", str(e))
                else:
                    self.log.debug("No update necessary")
        self.log.debug("Done updating agreements")

        return (False, False, []) # No restart, no apply now, no updates

api.register(update_replica_exclude_attribute_list)