From: Joe Guo Date: Wed, 3 Oct 2018 09:39:04 +0000 (+1300) Subject: uptodateness: migrate more methods from visualize X-Git-Tag: tdb-1.3.17~1292 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6ed82c7c0dae5f6c0feca1d94f85ca88237f3d1d;p=thirdparty%2Fsamba.git uptodateness: migrate more methods from visualize Move methods from cmd_uptodateness to new module. Will reuse in drs cmd later. Signed-off-by: Joe Guo Reviewed-by: Douglas Bagnall Reviewed-by: Andrew Bartlett BUG: https://bugzilla.samba.org/show_bug.cgi?id=13658 --- diff --git a/python/samba/netcmd/visualize.py b/python/samba/netcmd/visualize.py index 568d111830b..2f87dbe433d 100644 --- a/python/samba/netcmd/visualize.py +++ b/python/samba/netcmd/visualize.py @@ -42,6 +42,8 @@ from samba.compat import text_type from samba.uptodateness import ( get_partition_maps, get_partition, + get_own_cursor, + get_utdv, ) COMMON_OPTIONS = [ @@ -658,36 +660,6 @@ class cmd_uptodateness(GraphCommand): help="display this many digits of out-of-date-ness"), ] - def get_utdv(self, samdb, dn): - """This finds the uptodateness vector in the database.""" - cursors = [] - config_dn = samdb.get_config_basedn() - for c in dsdb._dsdb_load_udv_v2(samdb, dn): - inv_id = str(c.source_dsa_invocation_id) - res = samdb.search(base=config_dn, - expression=("(&(invocationId=%s)" - "(objectClass=nTDSDSA))" % inv_id), - attrs=["distinguishedName", "invocationId"]) - settings_dn = str(res[0]["distinguishedName"][0]) - prefix, dsa_dn = settings_dn.split(',', 1) - if prefix != 'CN=NTDS Settings': - raise CommandError("Expected NTDS Settings DN, got %s" % - settings_dn) - - cursors.append((dsa_dn, - inv_id, - int(c.highest_usn), - nttime2unix(c.last_sync_success))) - return cursors - - def get_own_cursor(self, samdb): - res = samdb.search(base="", - scope=SCOPE_BASE, - attrs=["highestCommittedUSN"]) - usn = int(res[0]["highestCommittedUSN"][0]) - now = int(time.time()) - return (usn, now) - def run(self, H=None, output=None, shorten_names=False, key=True, talk_to_remote=False, sambaopts=None, credopts=None, versionopts=None, @@ -716,7 +688,7 @@ class cmd_uptodateness(GraphCommand): if partition not in (part_dn, None): continue # we aren't doing this partition - cursors = self.get_utdv(self.samdb, part_dn) + cursors = get_utdv(self.samdb, part_dn) # we talk to each remote and make a matrix of the vectors # -- for each partition @@ -729,8 +701,8 @@ class cmd_uptodateness(GraphCommand): ldap_url = "ldap://%s" % res[0]["dNSHostName"][0] try: samdb = self.get_db(ldap_url, sambaopts, credopts) - cursors = self.get_utdv(samdb, part_dn) - own_usn, own_time = self.get_own_cursor(samdb) + cursors = get_utdv(samdb, part_dn) + own_usn, own_time = get_own_cursor(samdb) remotes = {dsa_dn: own_usn} for dn, guid, usn, t in cursors: remotes[dn] = usn diff --git a/python/samba/uptodateness.py b/python/samba/uptodateness.py index 06e8f175b5c..90cac550073 100644 --- a/python/samba/uptodateness.py +++ b/python/samba/uptodateness.py @@ -16,7 +16,11 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import time +from ldb import SCOPE_BASE + +from samba import nttime2unix, dsdb from samba.netcmd import CommandError @@ -48,3 +52,35 @@ def get_partition(samdb, part): if part not in long_partitions: raise CommandError("unknown partition %s" % part) return part + + +def get_utdv(samdb, dn): + """This finds the uptodateness vector in the database.""" + cursors = [] + config_dn = samdb.get_config_basedn() + for c in dsdb._dsdb_load_udv_v2(samdb, dn): + inv_id = str(c.source_dsa_invocation_id) + res = samdb.search(base=config_dn, + expression=("(&(invocationId=%s)" + "(objectClass=nTDSDSA))" % inv_id), + attrs=["distinguishedName", "invocationId"]) + settings_dn = str(res[0]["distinguishedName"][0]) + prefix, dsa_dn = settings_dn.split(',', 1) + if prefix != 'CN=NTDS Settings': + raise CommandError("Expected NTDS Settings DN, got %s" % + settings_dn) + + cursors.append((dsa_dn, + inv_id, + int(c.highest_usn), + nttime2unix(c.last_sync_success))) + return cursors + + +def get_own_cursor(samdb): + res = samdb.search(base="", + scope=SCOPE_BASE, + attrs=["highestCommittedUSN"]) + usn = int(res[0]["highestCommittedUSN"][0]) + now = int(time.time()) + return (usn, now)