import os import sys import commands import getpass import time import re debug = 1 xaddir = "/var/opt/novell/xad/" class helper : def __init__ (self) : log_file = xaddir+"/log/dsfw_install.log" if (not os.path.exists(xaddir+"/log")): os.makedirs(xaddir+"/log", 0755) self.file = open(log_file, "a+") self.debug = debug def __del__ (self) : self.file.close() def ini_get(self, key, section=None): cmd = "/opt/novell/xad/share/dcinit/printConfigKey.pl %s"%key (exit_code, value) = commands.getstatusoutput(cmd) if (exit_code != 0 or value == ""): self.put_to_file("The key [%s] is not defined\n"%(key)) #sys.exit (1) value = "none" return value def put_to_file (self, message) : self.file.write(message+"\n") self.file.flush() def run_cmd_with_output (self, cmd, timeout=None) : if self.debug >= 1: #print "Executing .. ",cmd if (self.debug >= 2) : return ("none nothing",0) if not timeout : timeout = 1000 self.put_to_file("Executing : %s\n"%cmd) (exit_code, output) = commands.getstatusoutput(cmd) if (self.debug == 2): print "The status of command [%s]: %d"%(cmd,exit_code) str = "Return Status .. [%d]\n"%exit_code self.put_to_file(str) self.put_to_file(output) return (output,exit_code) class InstallDSfW : def __init__ (self): self.help = helper() self.instdir = xaddir+"/ds/install/" if (not os.path.exists(self.instdir)): os.makedirs(self.instdir, 0755) def get_all_passwords (self) : install_type = self.help.ini_get("INSTALL_TYPE") mapped = self.help.ini_get("MAPPED") is_replica = self.help.ini_get("IS_REPLICA") self.read_and_validate_password("NDSDOMAINADMIN") if (is_replica == "FALSE") : if (install_type == "child") : self.read_and_validate_password("NDSPARENTDOMAINADMIN") if (mapped == "1" or install_type == "child") : self.read_and_validate_password("NDSEXISTINGADMINNAME") else : self.read_and_validate_password("NDSEXISTINGADMINNAME") def read_and_validate_password (self, admin_type): admin_name = self.help.ini_get(admin_type); regex = re.compile("\.") admin_name = regex.sub(",", admin_name) prompt = "Enter password [%s] : "%admin_name count = 0 while (count < 5) : password = getpass.getpass(prompt) cmd = "/usr/bin/ldapsearch -s base -b \" \" -D %s -w %s -Z"%(admin_name, password) (error, output) = commands.getstatusoutput (cmd); if (error) : print "Failed to authenticate [error %s]:[%s].. retry\n"%(error,output) else : break count = count + 1 if (count == 5 and error) : print "Invalid password, exiting ..\n" sys.exit(1) self.set_password (admin_type, password) return password def set_password (self, admin_type, password) : if (admin_type == "NDSDOMAINADMIN") : self.domain_admin_passwd = password elif (admin_type == "NDSPARENTDOMAINADMIN") : self.parent_domain_admin_passwd = password elif (admin_type == "NDSEXISTINGADMINNAME") : self.tree_admin_passwd = password else : print "Invalid admin type [%s]"%admin_type def is_mapped (self): return self.help.ini_get("MAPPED") def is_replica (self): return self.help.ini_get("IS_REPLICA") def exit_on_failure(self,status) : self.help.get_log_file(tofile="%s/ndsdcinit.log_%s"%(self.instdir,self.dc_name)) sys.exit(status) def run_script (self, task_name, command, pwd, retry_count_if_fails=None) : if (not retry_count_if_fails): retry_count = 0 else : retry_count = retry_count_if_fails file_path = self.instdir+command if os.path.isfile(file_path) : self.help.put_to_file("%s already done"%command.split('.')[0]) return count = 0 print "Executing : %s\n"%task_name self.help.put_to_file(">>>> Task : %s\n"%task_name) path = "/opt/novell/xad/share/dcinit/provision" command = "%s %s/%s"%(pwd, path,command) if (debug >= 3) : self.help.put_to_file(">> Command : %s"%command) while (count <= retry_count) : (output, status) = self.help.run_cmd_with_output(command) count = count + 1 if (status != 0 and count <= retry_count) : self.help.put_to_file("Command failed, retrying : %s"%command) time.sleep(10) else : break if (status != 0) : self.help.put_to_file ("The execution of command [%s] failed: %s"%(command,status)) self.exit_on_failure(status) open(file_path, 'w').close() def execute_provisioning_scripts(self): self.help.put_to_file(">>>> Executing Provisioning Scripts") install_type = self.help.ini_get("INSTALL_TYPE") if (self.help.ini_get("MAPPED") == "1") : mapped = "TRUE" else : mapped = "FALSE" is_replica = self.help.ini_get("IS_REPLICA") if (install_type != "frd" or mapped == "TRUE" or is_replica == "TRUE") : treeAdminPasswd = self.tree_admin_passwd domainAdminPasswd = self.domain_admin_passwd if (install_type == "child" and is_replica == "FALSE") : parent_domain_admin_passwd = self.parent_domain_admin_passwd path = "/opt/novell/xad/share/dcinit/provision" if (is_replica == "TRUE") : cmd_type1 = "ADM_PASSWD=%s NDSEXISTINGADMINPASSWD=%s "%(domainAdminPasswd,treeAdminPasswd) cmd_type2 = "ADM_PASSWD=%s "%domainAdminPasswd elif (install_type == "frd"): if (mapped == "TRUE") : cmd_type1 = "ADM_PASSWD=%s NDSEXISTINGADMINPASSWD=%s "%(domainAdminPasswd,treeAdminPasswd) cmd_type2 = "ADM_PASSWD=%s NDSEXISTINGADMINPASSWD=%s "%(treeAdminPasswd,treeAdminPasswd) else : cmd_type1 = cmd_type2 = "ADM_PASSWD=%s "%domainAdminPasswd elif (install_type == "child"): cmd_type1 = cmd_type2 = "ADM_PASSWD_DOMAIN=%s ADM_PASSWD_PARENT=%s NDSEXISTINGADMINPASSWD=%s "%(domainAdminPasswd,parent_domain_admin_passwd,treeAdminPasswd) self.run_script("PRE_CHECK", "provision_precheck.pl", cmd_type1,2) if (is_replica == "FALSE") : self.run_script("DNS_CONFIGURATION", "provision_dns.pl", cmd_type2,2) if ((mapped == "FALSE" and (install_type == "frd" or install_type == "child")) and (is_replica == "FALSE")) : self.run_script("DOMAIN_PARTITION_CREATION", "provision_partition_domain.pl", cmd_type1, 2) if (not(mapped == "FALSE" and install_type == "frd") or (is_replica == "TRUE")) : self.run_script("ADD_DOMAIN_REPLICA", "provision_add_domain_replica.pl", cmd_type2, 2) self.run_script("SLAPI_CONFIGURATION", "provision_config_slapi.pl", cmd_type2) if (is_replica == "FALSE") : self.run_script("DOMAIN_OBJECT_ADDITION", "provision_add_domainobj.pl", cmd_type2) if (not(is_replica == "TRUE" and self.help.ini_get("CONFIG_SCHEMA_REPLICAS") == "FALSE")) : self.run_script("CONFIG_PARTITION_CREATION", "provision_partition_configuration.pl", cmd_type2, 2) self.run_script("SCHEMA_PARTITION_CREATION","provision_partition_schema.pl", cmd_type2, 2) if (is_replica == "FALSE"): self.run_script("CONFIG_OBJECT_ADDITION", "provision_add_configobj.pl", cmd_type2) if (is_replica == "TRUE"): self.run_script("DOMAIN_JOIN", "provision_domain_join.pl", cmd_type1,2) self.run_script("ACL_CONFIGURATION", "provision_config_acl.pl", cmd_type2, 2) if (is_replica == "FALSE") : self.run_script("SAMIFY_OBJECTS", "provision_samify.pl", cmd_type1) self.run_script("RESTART_DSFW", "provision_restart_dsfw.pl", cmd_type2) self.run_script("SET_CREDENTIALS", "provision_set_cred_foraccounts.pl", cmd_type1) self.run_script("ENABLE_LOCAL_KRB", "provision_enable_local_krb.pl", cmd_type1) if (mapped == "TRUE" and is_replica == "FALSE") : nkdc_realm = self.help.ini_get("NKDC_REALM") if (nkdc_realm != "none"): self.run_script("MIGRATE_NKDC", "provision_migrate_nkdc.pl", cmd_type1) if (install_type == "child" and is_replica == "FALSE") : self.run_script("TRUST_CREATION", "provision_trusts_crossref.pl", cmd_type1,2) self.run_script("CRONTAB_ENTRIES", "provision_crontab_entry_add.pl", cmd_type2) if (is_replica == "TRUE"): self.run_script("DNS_CONFIGURATION", "provision_dns.pl", cmd_type1) self.run_script("CLEANUP", "provision_cleanup.pl", cmd_type1) def main (): install = InstallDSfW() install.get_all_passwords() install.execute_provisioning_scripts() if __name__ == "__main__": main()