<s> import os <EOL> import os . path <EOL> import shutil <EOL> import sys <EOL> import time <EOL> import traceback <EOL> from ldif import LDIFParser <EOL> from jsonmerge import merge <EOL> import base64 <EOL> import json <EOL> import uuid <EOL> log = "<STR_LIT>" <EOL> logError = "<STR_LIT>" <EOL> password_file = "<STR_LIT>" <EOL> service = "<STR_LIT>" <EOL> ldapmodify = "<STR_LIT>" <EOL> ldapsearch = "<STR_LIT>" <EOL> ldapdelete = "<STR_LIT>" <EOL> ignore_files = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> ldap_creds = [ '<STR_LIT>' , <EOL> '<STR_LIT:localhost>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> password_file <EOL> ] <EOL> class MyLDIF ( LDIFParser ) : <EOL> def __init__ ( self , input , output ) : <EOL> LDIFParser . __init__ ( self , input ) <EOL> self . targetDN = None <EOL> self . targetAttr = None <EOL> self . targetEntry = None <EOL> self . DNs = [ ] <EOL> self . lastDN = None <EOL> self . lastEntry = None <EOL> def getResults ( self ) : <EOL> return ( self . targetDN , self . targetAttr ) <EOL> def getDNs ( self ) : <EOL> return self . DNs <EOL> def getLastEntry ( self ) : <EOL> return self . lastEntry <EOL> def handle ( self , dn , entry ) : <EOL> if self . targetDN == None : <EOL> self . targetDN = dn <EOL> self . lastDN = dn <EOL> self . DNs . append ( dn ) <EOL> self . lastEntry = entry <EOL> if dn . lower ( ) . strip ( ) == self . targetDN . lower ( ) . strip ( ) : <EOL> self . targetEntry = entry <EOL> if entry . has_key ( self . targetAttr ) : <EOL> self . targetAttr = entry [ self . targetAttr ] <EOL> def addEntry ( dn , entry , ldifModFolder ) : <EOL> newLdif = """<STR_LIT>""" % dn <EOL> for attr in entry . keys ( ) : <EOL> for value in entry [ attr ] : <EOL> newLdif = newLdif + getMod ( attr , value ) <EOL> newLdif = newLdif + "<STR_LIT:\n>" <EOL> f = open ( "<STR_LIT>" % ( ldifModFolder , str ( uuid . uuid4 ( ) ) ) , '<STR_LIT:w>' ) <EOL> f . write ( newLdif ) <EOL> f . close ( ) <EOL> def getNewConfig ( fn ) : <EOL> args = [ ldapsearch ] + ldap_creds + [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> output = getOutput ( args ) <EOL> f = open ( fn , '<STR_LIT:w>' ) <EOL> f . write ( output ) <EOL> f . close ( ) <EOL> logIt ( "<STR_LIT>" % fn ) <EOL> def copyFiles ( backup23_folder ) : <EOL> os . path . walk ( "<STR_LIT>" % backup23_folder , walk_function , None ) <EOL> os . path . walk ( "<STR_LIT>" % backup23_folder , walk_function , None ) <EOL> os . path . walk ( "<STR_LIT>" % backup23_folder , walk_function , None ) <EOL> def deleteEntries ( dn_list ) : <EOL> for dn in dn_list : <EOL> cmd = [ ldapdelete ] + ldap_creds + [ dn ] <EOL> output = getOutput ( cmd ) <EOL> if output : <EOL> logIt ( output ) <EOL> else : <EOL> logIt ( "<STR_LIT>" % dn ) <EOL> def getAttributeValue ( fn , targetAttr ) : <EOL> parser = MyLDIF ( open ( fn , '<STR_LIT:rb>' ) , sys . stdout ) <EOL> parser . targetAttr = targetAttr <EOL> parser . parse ( ) <EOL> value = parser . targetAttr <EOL> return value <EOL> def getOldEntryMap ( folder ) : <EOL> files = os . listdir ( folder ) <EOL> dnMap = { } <EOL> for fn in files : <EOL> if ( fn == "<STR_LIT>" ) or ( fn == "<STR_LIT>" ) : <EOL> continue <EOL> dnList = getDns ( "<STR_LIT>" % ( folder , fn ) ) <EOL> for dn in dnList : <EOL> dnMap [ dn ] = fn <EOL> return dnMap <EOL> def getEntry ( fn , dn ) : <EOL> parser = MyLDIF ( open ( fn , '<STR_LIT:rb>' ) , sys . stdout ) <EOL> parser . targetDN = dn <EOL> parser . parse ( ) <EOL> return parser . targetEntry <EOL> def getDns ( fn ) : <EOL> parser = MyLDIF ( open ( fn , '<STR_LIT:rb>' ) , sys . stdout ) <EOL> parser . parse ( ) <EOL> return parser . DNs <EOL> def getMod ( attr , s ) : <EOL> val = str ( s ) . strip ( ) <EOL> if val . find ( '<STR_LIT:\n>' ) > - <NUM_LIT:1> : <EOL> val = base64 . b64encode ( val ) <EOL> return "<STR_LIT>" % tab_attr ( attr , val , True ) <EOL> elif len ( val ) > ( <NUM_LIT> - len ( attr ) ) : <EOL> return "<STR_LIT>" % tab_attr ( attr , val ) <EOL> else : <EOL> return "<STR_LIT>" % ( attr , val ) <EOL> def getOutput ( args ) : <EOL> try : <EOL> logIt ( "<STR_LIT>" % "<STR_LIT:U+0020>" . join ( args ) ) <EOL> output = os . popen ( "<STR_LIT:U+0020>" . join ( args ) ) . read ( ) . strip ( ) <EOL> return output <EOL> except : <EOL> logIt ( "<STR_LIT>" % "<STR_LIT:U+0020>" . join ( args ) , True ) <EOL> logIt ( traceback . format_exc ( ) , True ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def logIt ( msg , errorLog = False ) : <EOL> if errorLog : <EOL> f = open ( logError , '<STR_LIT:a>' ) <EOL> f . write ( '<STR_LIT>' % ( time . strftime ( '<STR_LIT>' ) , msg ) ) <EOL> f . close ( ) <EOL> f = open ( log , '<STR_LIT:a>' ) <EOL> f . write ( '<STR_LIT>' % ( time . strftime ( '<STR_LIT>' ) , msg ) ) <EOL> f . close ( ) <EOL> def restoreConfig ( ldifFolder , newLdif , ldifModFolder ) : <EOL> ignoreList = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> current_config_dns = getDns ( newLdif ) <EOL> oldDnMap = getOldEntryMap ( ldifFolder ) <EOL> for dn in oldDnMap . keys ( ) : <EOL> old_entry = getEntry ( "<STR_LIT>" % ( ldifFolder , oldDnMap [ dn ] ) , dn ) <EOL> if dn not in current_config_dns : <EOL> addEntry ( dn , old_entry , ldifModFolder ) <EOL> continue <EOL> new_entry = getEntry ( newLdif , dn ) <EOL> for attr in old_entry . keys ( ) : <EOL> if attr in ignoreList : <EOL> continue <EOL> if not new_entry . has_key ( attr ) : <EOL> writeMod ( dn , attr , old_entry [ attr ] , '<STR_LIT>' % ( ldifModFolder , str ( uuid . uuid4 ( ) ) ) , True ) <EOL> logIt ( "<STR_LIT>" % ( attr , dn ) ) <EOL> else : <EOL> mod_list = None <EOL> if old_entry [ attr ] != new_entry [ attr ] : <EOL> if len ( old_entry [ attr ] ) == <NUM_LIT:1> : <EOL> try : <EOL> logIt ( "<STR_LIT>" % attr ) <EOL> old_json = json . loads ( old_entry [ attr ] [ <NUM_LIT:0> ] ) <EOL> new_json = json . loads ( new_entry [ attr ] [ <NUM_LIT:0> ] ) <EOL> new_json = merge ( new_json , old_json ) <EOL> mod_list = [ json . dumps ( new_json ) ] <EOL> except : <EOL> mod_list = old_entry [ attr ] <EOL> else : <EOL> mod_list = old_entry [ attr ] <EOL> logIt ( "<STR_LIT>" % attr ) <EOL> else : <EOL> continue <EOL> writeMod ( dn , attr , mod_list , '<STR_LIT>' % ( ldifModFolder , str ( uuid . uuid4 ( ) ) ) ) <EOL> def startOpenDJ ( ) : <EOL> output = getOutput ( [ service , '<STR_LIT>' , '<STR_LIT:start>' ] ) <EOL> if output . find ( "<STR_LIT>" ) > <NUM_LIT:0> : <EOL> logIt ( "<STR_LIT>" ) <EOL> else : <EOL> logIt ( "<STR_LIT>" , True ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> def stopOpenDJ ( ) : <EOL> output = getOutput ( [ service , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> if output . find ( "<STR_LIT>" ) > <NUM_LIT:0> : <EOL> logIt ( "<STR_LIT>" ) <EOL> else : <EOL> logIt ( "<STR_LIT>" , True ) <EOL> sys . exit ( <NUM_LIT:3> ) <EOL> def tab_attr ( attr , value , encoded = False ) : <EOL> targetLength = <NUM_LIT> <EOL> lines = [ '<STR_LIT>' % attr ] <EOL> if encoded : <EOL> lines = [ '<STR_LIT>' % attr ] <EOL> for char in value : <EOL> current_line = lines [ - <NUM_LIT:1> ] <EOL> if len ( current_line ) < <NUM_LIT> : <EOL> new_line = current_line + char <EOL> del lines [ - <NUM_LIT:1> ] <EOL> lines . append ( new_line ) <EOL> else : <EOL> lines . append ( "<STR_LIT:U+0020>" + char ) <EOL> return "<STR_LIT:\n>" . join ( lines ) <EOL> def uploadLDIF ( ldifFolder , outputLdifFolder ) : <EOL> files = os . listdir ( outputLdifFolder ) <EOL> for fn in files : <EOL> cmd = [ ldapmodify ] + ldap_creds + [ '<STR_LIT>' , '<STR_LIT>' , "<STR_LIT>" % ( outputLdifFolder , fn ) ] <EOL> output = getOutput ( cmd ) <EOL> if output : <EOL> logIt ( output ) <EOL> else : <EOL> logIt ( "<STR_LIT>" % fn , True ) <EOL> dn_list = getDns ( "<STR_LIT>" ) <EOL> deleteEntries ( dn_list ) <EOL> cmd = [ ldapmodify ] + ldap_creds + [ '<STR_LIT>' , '<STR_LIT:-c>' , '<STR_LIT>' , "<STR_LIT>" % ldifFolder ] <EOL> output = getOutput ( cmd ) <EOL> if output : <EOL> logIt ( output ) <EOL> else : <EOL> logIt ( "<STR_LIT>" , True ) <EOL> dn_list = getDns ( "<STR_LIT>" % ldifFolder ) <EOL> if dn_list > <NUM_LIT:2> : <EOL> cmd = [ ldapmodify ] + ldap_creds + [ '<STR_LIT>' , '<STR_LIT:-c>' , '<STR_LIT>' , "<STR_LIT>" % ldifFolder ] <EOL> output = getOutput ( cmd ) <EOL> if output : <EOL> logIt ( output ) <EOL> else : <EOL> logIt ( "<STR_LIT>" , True ) <EOL> def walk_function ( a , dir , files ) : <EOL> for file in files : <EOL> if file in ignore_files : <EOL> continue <EOL> fn = "<STR_LIT>" % ( dir , file ) <EOL> targetFn = fn [ <NUM_LIT:1> : ] <EOL> if os . path . isdir ( fn ) : <EOL> if not os . path . exists ( targetFn ) : <EOL> os . mkdir ( targetFn ) <EOL> else : <EOL> try : <EOL> logIt ( "<STR_LIT>" % targetFn ) <EOL> shutil . copyfile ( fn , targetFn ) <EOL> except : <EOL> logIt ( "<STR_LIT>" % targetFn , True ) <EOL> def writeMod ( dn , attr , value_list , fn , add = False ) : <EOL> operation = "<STR_LIT:replace>" <EOL> if add : <EOL> operation = "<STR_LIT>" <EOL> modLdif = """<STR_LIT>""" % ( dn , operation , attr ) <EOL> if value_list == None : return <EOL> for val in value_list : <EOL> modLdif = modLdif + getMod ( attr , val ) <EOL> modLdif = modLdif + "<STR_LIT:\n>" <EOL> f = open ( fn , '<STR_LIT:w>' ) <EOL> f . write ( modLdif ) <EOL> f . close ( ) <EOL> backup23_folder = None <EOL> error = False <EOL> try : <EOL> backup23_folder = sys . argv [ <NUM_LIT:1> ] <EOL> if ( not os . path . exists ( "<STR_LIT>" % backup23_folder ) ) : <EOL> error = True <EOL> if ( not os . path . exists ( "<STR_LIT>" % backup23_folder ) ) : <EOL> error = True <EOL> if ( not os . path . exists ( "<STR_LIT>" % backup23_folder ) ) : <EOL> error = True <EOL> except : <EOL> error = True <EOL> if error : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> ldif_folder = "<STR_LIT>" % backup23_folder <EOL> outputFolder = "<STR_LIT>" <EOL> outputLdifFolder = "<STR_LIT>" % outputFolder <EOL> if not os . path . exists ( outputFolder ) : <EOL> os . mkdir ( outputFolder ) <EOL> if not os . path . exists ( outputLdifFolder ) : <EOL> os . mkdir ( outputLdifFolder ) <EOL> newLdif = "<STR_LIT>" % outputFolder <EOL> stopOpenDJ ( ) <EOL> copyFiles ( backup23_folder ) <EOL> startOpenDJ ( ) <EOL> getNewConfig ( newLdif ) <EOL> restoreConfig ( ldif_folder , newLdif , outputLdifFolder ) <EOL> uploadLDIF ( ldif_folder , outputLdifFolder ) </s>
