def deployLdif(cls, host, ldif): target_ldif = os.path.join(KNOX_CONF, "users.ldif") backup_ldif = target_ldif + ".bk" # Copy test LDIF file to remote machine. tempName = "users-%d.ldif" % time.time() tempFile = Machine.copyFileFromLocalToRemoteTmpDir( host, ldif, remoteFileName=tempName, madeExecutable=False, appendTimeToRemotePath=False, logoutput=True) assert Machine.pathExists( user=Machine.getAdminUser(), host=host, filepath=tempFile, passwd=Machine.getAdminPasswd( )), "Failed to copy %s to %s on %s" % (ldif, tempFile, host) # Backup original LDIF file. exists = Machine.pathExists(user=Machine.getAdminUser(), host=host, filepath=target_ldif, passwd=Machine.getAdminPasswd()) if exists: if Machine.isWindows(): if Machine.pathExists(filepath=backup_ldif, host=host, user=Machine.getAdminUser(), passwd=Machine.getAdminPasswd()): Machine.rm(filepath=backup_ldif, host=host, user=Machine.getAdminUser(), passwd=Machine.getAdminPasswd()) Machine.rename(user=Machine.getAdminUser(), host=host, src=target_ldif, dest=backup_ldif, passwd=Machine.getAdminPasswd()) assert Machine.pathExists(user=Machine.getAdminUser(),host=host, filepath=backup_ldif, passwd=Machine.getAdminPasswd() ), "Failed to backup %s to %s on %s" % \ (target_ldif, backup_ldif, host) # Rename temp LDIF file to target name. if Machine.isWindows(): if Machine.pathExists(filepath=target_ldif, host=host, user=Machine.getAdminUser(), passwd=Machine.getAdminPasswd()): Machine.rm(filepath=target_ldif, host=host, user=Machine.getAdminUser(), passwd=Machine.getAdminPasswd()) Machine.rename(user=Machine.getAdminUser(), host=host, src=tempFile, dest=target_ldif, passwd=Machine.getAdminPasswd()) assert Machine.pathExists(user=Machine.getAdminUser(),host=host, filepath=target_ldif, passwd=Machine.getAdminPasswd() ), "Failed to rename %s to %s on %s" % \ (tempFile, target_ldif, host) # Set LDIF permissions. Machine.runas(Machine.getAdminUser(), "chown -R %s %s" % ("root", target_ldif)) logger.info("KnoxLdp LDIF file %s copied to %s" % (ldif, target_ldif)) # Restart Knox test LDAP server. Knox.restartLdap()
def deployTopology(cls, host, topology, template, params, topo_url="", username="******", password="******", poll_status_code=200, header={ 'Accept': 'application/json', 'Accept-Encoding': 'identity' }, skipAuth=False, remoteTopology=""): logger.info("Start deploying topology") if len(remoteTopology) == 0: remoteTopology = topology logger.info("LOCAL TOPOLOGY_Name=%s" % topology) logger.info("REMOTE TOPOLOGY_Name=%s" % remoteTopology) logger.info("TOPOLOGY_FILE=%s" % template) logger.info("TOPOLOGY_DATA=%s" % params) # Load the template topo_tmpl = Template(file=template, searchList=[params]) topo_xml = str(topo_tmpl) # Write template to a local temp file local_temp_name = os.path.join(Machine.getTempDir(), "%s-%d" % (topology, time.time())) with open(local_temp_name, "w") as local_temp_file: local_temp_file.write(topo_xml) assert os.path.isfile( local_temp_name), "Failed create local temp topology file %s" % ( local_temp_name) # Copy the local temp file to a remote temp file if Machine.isSameHost(host): remote_temp_name = local_temp_name else: remote_temp_name = Machine.copyFileFromLocalToRemoteTmpDir( localPath=local_temp_name, host=host, madeExecutable=False, appendTimeToRemotePath=False) exists = Machine.pathExists(filepath=remote_temp_name, host=host, user=Machine.getAdminUser(), passwd=Machine.getAdminPasswd()) assert exists, "Failed to copy local temp topology file %s to %s on host %s" % ( local_temp_name, remote_temp_name, host) # Move the remote temp file to the topologies dir remote_topo_name = os.path.join(Config.get('knox', 'KNOX_CONF'), 'topologies', remoteTopology + ".xml") if Machine.isWindows(): if Machine.pathExists(filepath=remote_topo_name, host=host, user=Machine.getAdminUser(), passwd=Machine.getAdminPasswd()): Machine.rm(filepath=remote_topo_name, host=host, user=Machine.getAdminUser(), passwd=Machine.getAdminPasswd()) Machine.rename(user=Machine.getAdminUser(), host=host, src=remote_temp_name, dest=remote_topo_name, passwd=Machine.getAdminPasswd()) exists = Machine.pathExists(user=Machine.getAdminUser(), host=host, filepath=remote_topo_name, passwd=Machine.getAdminPasswd()) assert exists, "Failed to move remote temp topology file %s to %s on host %s" % ( remote_temp_name, remote_topo_name, host) # Poll until the topology url is found or timeout. found = False delay = 2 count = 15 status = 0 logger.info( "Continue to poll the topology status using admin version url %s" % topo_url) authentication = HTTPBasicAuth(username, password) for n in xrange(0, count): time.sleep(delay) if len(topo_url) == 0: topo_url = "%s/%s/%s" % (Knox.getAdminUrl(host=host), 'topologies', remoteTopology) response = requests.get(url=topo_url, verify=False, auth=authentication) else: if skipAuth: response = requests.get(topo_url, headers=header, verify=False) else: response = requests.get(topo_url, headers=header, verify=False, auth=authentication) status = response.status_code logger.info("Polling status : %d" % status) #print status if status == poll_status_code: found = True # An extra sleep because we were still getting a 404 sometimes. time.sleep(delay) break time.sleep(60) assert found, "Failed to deploy topology %s after %d seconds, status %s" % ( remoteTopology, delay * count, status)
def updateGatewaySiteConfig(cls, config, restartKnox=True): timestamp = str(time.time()) # Write the config to a local config xml file localTempName = "gateway-site_source-%s.xml" % timestamp localTempConfFile = os.path.join(Machine.getTempDir(), localTempName) util.writeDictToConfigXmlFile(config, localTempConfFile) assert os.stat( localTempConfFile ).st_size > 0, "Failed to write Knox config to local host, file empty %s" % localTempConfFile # Transfer the local config xml file to a remote temp file remoteTempName = "gateway-site_target-%s.xml" % timestamp remoteTempFile = Machine.copyFileFromLocalToRemoteTmpDir( KNOX_HOST, localTempConfFile, remoteFileName=remoteTempName, madeExecutable=False, appendTimeToRemotePath=False, logoutput=True) # Delete the local temp file Machine.rm(filepath=localTempConfFile, user=None, host=None) assert not os.path.isfile( localTempConfFile ), "Failed to delete local temp Knox config file %s" & localTempConfFile # Backup the remote config file. remoteConfFile = os.path.join(Config.get('knox', 'KNOX_CONF'), 'gateway-site.xml') remoteConfFileBackup = "%s_%s" % (remoteConfFile, timestamp) Machine.rename(src=remoteConfFile, dest=remoteConfFileBackup, host=KNOX_HOST, user=Machine.getAdminUser(), passwd=Machine.getAdminPasswd()) exists = Machine.pathExists(filepath=remoteConfFileBackup, host=KNOX_HOST, user=Machine.getAdminUser(), passwd=Machine.getAdminPasswd()) assert exists, "Failed to backup %s to %s on host %s" % ( remoteTempFile, remoteConfFile, KNOX_HOST) try: # Move the remote temp file over the target file Machine.rename(src=remoteTempFile, dest=remoteConfFile, host=KNOX_HOST, user=Machine.getAdminUser(), passwd=Machine.getAdminPasswd()) exists = Machine.pathExists(filepath=remoteConfFile, host=KNOX_HOST, user=Machine.getAdminUser(), passwd=Machine.getAdminPasswd()) assert exists, "Failed to move remote temp conf file %s to %s on host %s" % ( remoteTempFile, remoteConfFile, KNOX_HOST) # Restart Knox. if restartKnox: cls.restartKnox() except: # Restore the remote backup config file. Machine.rename(src=remoteConfFileBackup, dest=remoteConfFile, host=KNOX_HOST, user=Machine.getAdminUser(), passwd=Machine.getAdminPasswd()) raise