def downloadRemoteScanFile(Framework): remoteScanFileLocation = Framework.getProperty(InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_FILE_LOCATION) # download scanner log file before downloading scan file downloadScanLogFile(Framework) if remoteScanFileLocation is None: logger.debug("No scan file to downloaded from current execution") return None logger.debug("About to download scan file from current execution:", remoteScanFileLocation) extension = InventoryUtils.getFileExtension(remoteScanFileLocation) localScanFileName = InventoryUtils.generateScanFileName(Framework, extension) # folder for scan files localScanFileFolderPath = ( CollectorsParameters.PROBE_MGR_INVENTORY_XMLENRICHER_FILES_FOLDER + XmlEnricherConstants.INCOMING_FOLDER_NAME ) downloadedScanFilesDir = File(localScanFileFolderPath) downloadedScanFilesDir.mkdirs() # this scan file will be created after downloading from remote machine targetScanFile = File(downloadedScanFilesDir, localScanFileName) # get file to the local machine logger.debug("Scan file to be downloaded to location:", targetScanFile.getCanonicalPath()) if not InventoryUtils.copyRemoteFileToLocal(Framework, remoteScanFileLocation, targetScanFile.getCanonicalPath()): return None return targetScanFile
def downloadScanFile(Framework): targetScanFile = downloadRemoteScanFile(Framework) if not targetScanFile: logger.debug("Remote scan file was not downloaded, will try previously downloaded scan file(if exists)") targetScanFile = useTempScanFile(Framework) if not targetScanFile: logger.debug("No scan file downloaded from previous execution, download file step failed") Framework.setStepExecutionStatus(WorkflowStepStatus.FATAL_FAILURE) Framework.reportError(inventoryerrorcodes.INVENTORY_DISCOVERY_REMOTE_SCANFILE_NOT_FOUND, None) return logger.debug("Scan file was successfully downloaded to ", targetScanFile.getCanonicalPath()) # set download time to current time Framework.setProperty(InventoryUtils.AGENT_OPTION_DISCOVERY_SCANFILE_DOWNLOAD_TIME, Date()) # Check the drity files in sending folder sendingFolder = ( CollectorsParameters.PROBE_MGR_INVENTORY_XMLENRICHER_FILES_FOLDER + XmlEnricherConstants.SENDING_FOLDER_NAME ) deleteDirtyFile(File(sendingFolder, targetScanFile.getName())) # delete temporary scan file from previous execution (if any) tempScanFileFolder = ( CollectorsParameters.PROBE_MGR_TEMPDOWNLOAD + Framework.getDiscoveryJobId() + CollectorsParameters.FILE_SEPARATOR ) tempScanFileName = InventoryUtils.generateScanFileName(Framework, InventoryUtils.SCANFILE_EXTENTION) deleteDirtyFile(File(tempScanFileFolder, tempScanFileName)) tempScanFileName = InventoryUtils.generateScanFileName(Framework, InventoryUtils.SCANFILE_DELTA_EXTENTION) deleteDirtyFile(File(tempScanFileFolder, tempScanFileName)) Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def getScanFilelocation(Framework): try: client = Framework.getConnectedClient() except: logger.warn( "Connect failed during [Check Scanner Finished], parking for next try " ) return None #on previous Download Scan File step execution we can already obtain remote scan file path #but failed to copy it to base dir. That is why we can try to get it from properties remoteScanFileLocation = Framework.getProperty( InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_FILE_LOCATION) if not InventoryUtils.isPathValid(remoteScanFileLocation): options = LockUtils.getClientOptionsMap(client) remoteScanFileLocation = options.get( InventoryUtils.AGENT_OPTION_DISCOVERY_SCANFILENAME) if InventoryUtils.isPathValid(remoteScanFileLocation): Framework.setProperty( InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_FILE_LOCATION, remoteScanFileLocation) logger.debug( 'Got agent option ' + InventoryUtils.AGENT_OPTION_DISCOVERY_SCANFILENAME + ' ', remoteScanFileLocation) else: logger.debug('Remote scan file location from agent options:', remoteScanFileLocation) else: logger.debug('Got scan file location from properties ', remoteScanFileLocation) return remoteScanFileLocation
def StepMain(Framework): # if we have shell credentials and we are able to connect with them then connect otherwise we should connect with agent ip = Framework.getDestinationAttribute('ip_address') domain = Framework.getDestinationAttribute('ip_domain') codepage = Framework.getCodePage() allShellProtocols = [] allShellCredentials = [] allShellIps = [] allShellCodePages = [] protocols = netutils.getAvailableProtocols(Framework, ClientsConsts.DDM_AGENT_PROTOCOL_NAME, ip, domain) for protocol in protocols: allShellProtocols.append(ClientsConsts.DDM_AGENT_PROTOCOL_NAME) allShellCredentials.append(protocol) allShellIps.append(ip) allShellCodePages.append(codepage) logger.debug('Will going to attempt to connect in this order: ', allShellCredentials) Framework.setProperty(InventoryUtils.STATE_PROPERTY_CONNECTION_PROTOCOLS, allShellProtocols) Framework.setProperty(InventoryUtils.STATE_PROPERTY_CONNECTION_CREDENIALS, allShellCredentials) Framework.setProperty(InventoryUtils.STATE_PROPERTY_CONNECTION_IPS, allShellIps) Framework.setProperty(InventoryUtils.STATE_PROPERTY_CONNECTION_CODEPAGES, allShellCodePages) InventoryUtils.executeStep(Framework, connectToRemoteNode, InventoryUtils.STEP_REQUIRES_CONNECTION, InventoryUtils.STEP_DOESNOT_REQUIRES_LOCK)
def connectToRemoteNode(Framework): if AgentUtils.isMigrateNeeded(Framework): #setting connected client identifier #using host name since uduid is stored in agent options and on old and new ddmi agent their location is different logger.debug('Connected using uda.') client = Framework.getConnectedClient() sysInfo = client.getSysInfo() hostName = sysInfo.getProperty('computerName') Framework.setProperty(InventoryUtils.UD_HOSTNAME, hostName) AgentUtils.setUdAgentProtocolForMigration(Framework, client.getCredentialId()) logger.debug('Migrate is going to be performed') if client.hasShell(): logger.debug('The connected Agent already supports shell, assume it is a non-native agent.') reason = 'The connected Agent already supports shell,it may be a non-native agent.' Framework.setProperty(InventoryUtils.generateSkipStep('Install Non-Native UD Agent'), reason) #Framework.setProperty(InventoryUtils.generateSkipStep('Check Non-Native Agent Installed'), reason) platform = Framework.getProperty(InventoryUtils.STATE_PROPERTY_PLATFORM) if platform == 'windows': # In windows, it is native already if it has shell. logger.debug('This is windows, it must be native agent.') Framework.setProperty(AgentUtils.DOWNLOAD_MIGRATE_LOG_FILE, '') reason = 'Native installation is used for Windows platform.' Framework.setProperty(InventoryUtils.generateSkipStep('Init Update from Non-Native to Native'), reason) Framework.setProperty(InventoryUtils.generateSkipStep('Install Native UD Agent'), reason) else: logger.debug('The connected client does NOT support the shell capability. This is DDMi agent!') Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def getRemoteScanLogFilelocation(Framework): client = Framework.getConnectedClient() remoteScanLogFileLocation = Framework.getProperty(InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_LOG_FILE_LOCATION) if not InventoryUtils.isPathValid(remoteScanLogFileLocation): options = LockUtils.getClientOptionsMap(client) remoteScanLogFileLocation = options.get(InventoryUtils.AGENT_OPTION_DISCOVERY_SCANLOGFILENAME) if InventoryUtils.isPathValid(remoteScanLogFileLocation): Framework.setProperty( InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_LOG_FILE_LOCATION, remoteScanLogFileLocation ) logger.debug( "Got agent option " + InventoryUtils.AGENT_OPTION_DISCOVERY_SCANLOGFILENAME + " ", remoteScanLogFileLocation, ) else: logger.debug("Remote scan log file location from agent options:", remoteScanLogFileLocation) else: logger.debug("Got scan log file location from properties ", remoteScanLogFileLocation) if remoteScanLogFileLocation is None: remoteScanFileLocation = Framework.getProperty(InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_FILE_LOCATION) if remoteScanFileLocation is not None: remoteScanLogFileLocation = os.path.splitext(remoteScanFileLocation)[0] + ".log" return remoteScanLogFileLocation
def useTempScanFile(Framework): tempScanFilePath = Framework.getProperty(InventoryUtils.STATE_PROPERTY_TEMP_SCAN_FILE) if tempScanFilePath is None: logger.debug("No scan file found from previous scanner execution") return None logger.debug("Using scan file from previous execution:", tempScanFilePath) extension = InventoryUtils.getFileExtension(tempScanFilePath) localScanFileName = InventoryUtils.generateScanFileName(Framework, extension) # folder for scan files localScanFileFolderPath = ( CollectorsParameters.PROBE_MGR_INVENTORY_XMLENRICHER_FILES_FOLDER + XmlEnricherConstants.INCOMING_FOLDER_NAME ) downloadedScanFilesDir = File(localScanFileFolderPath) downloadedScanFilesDir.mkdirs() # this scan file will be created after downloading from remote machine targetScanFile = File(downloadedScanFilesDir, localScanFileName) logger.debug("Scan file from previous execution will be moved to ", targetScanFile.getCanonicalPath()) tempScanFile = File(tempScanFilePath) if not tempScanFile.renameTo(targetScanFile): return None return targetScanFile
def downloadRemoteScanFile(Framework): remoteScanFileLocation = Framework.getProperty(InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_FILE_LOCATION) # download scanner log file before downloading scan file downloadScanLogFile(Framework) if remoteScanFileLocation is None: logger.debug('No scan file to downloaded from current execution') return None logger.debug('About to download scan file from current execution:', remoteScanFileLocation) extension = InventoryUtils.getFileExtension(remoteScanFileLocation) localScanFileName = InventoryUtils.generateScanFileName(Framework, extension) #folder for scan files localScanFileFolderPath = CollectorsParameters.PROBE_MGR_INVENTORY_XMLENRICHER_FILES_FOLDER + XmlEnricherConstants.INCOMING_FOLDER_NAME downloadedScanFilesDir = File(localScanFileFolderPath) downloadedScanFilesDir.mkdirs() #this scan file will be created after downloading from remote machine targetScanFile = File(downloadedScanFilesDir, localScanFileName) #get file to the local machine logger.debug('Scan file to be downloaded to location:', targetScanFile.getCanonicalPath()) if not InventoryUtils.copyRemoteFileToLocal(Framework, remoteScanFileLocation, targetScanFile.getCanonicalPath()): return None return targetScanFile
def unInstallAgent(Framework): protocolName = Framework.getProperty( InventoryUtils.STATE_PROPERTY_CONNECTED_SHELL_PROTOCOL_NAME) logger.debug('Protocal name: ', protocolName) client = Framework.getConnectedClient() uduid = InventoryUtils.getUduid(client) logger.debug('UD_UNIQUE_ID: ', uduid) Framework.setProperty(InventoryUtils.ATTR_UD_UNIQUE_ID, uduid) if protocolName == ClientsConsts.DDM_AGENT_PROTOCOL_NAME: # Should release lock first if there will be no connected credential after agent uninstallation. logger.debug( 'The connected credential is UDA. Try to release lock first.') LockUtils.releaseScannerLock(Framework) if AgentUtils.isAgentInstalled(Framework): logger.debug('There is an agent in remote machine.') # Run uninstall command. shouldStop = AgentUtils.agentUnInstallRoutine(Framework) if shouldStop != 0: Framework.setStepExecutionStatus(WorkflowStepStatus.FATAL_FAILURE) logger.debug('Failed to uninstall agent.') else: logger.debug( 'There is no agent in remote machine. The job will be done.') reason = 'There is no agent in remote machine' Framework.setProperty( InventoryUtils.generateSkipStep('Check Agent UnInstalled'), reason) Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def StepMain(Framework): skipStepReason = Framework.getProperty( InventoryUtils.STEP_SKIP_ALL_STEPS_PROPERTY) if skipStepReason is not None: logger.debug('Skipped by request to skip all steps, reason:', skipStepReason) Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS) return skipStepReason = Framework.getProperty( InventoryUtils.generateSkipStep( Framework.getState().getCurrentStepName())) if skipStepReason is not None: logger.debug('Step skipped, reason:', skipStepReason) Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS) return if not Framework.getProperty(FIRST_TRY_CHECK_ERROR_CODE): # we don't want immediately check whether agent installation successful or not, # since for sure it is not. go to parking to let others to install logger.debug( 'UD agent install command just run, will check result code after parking' ) Framework.setProperty(FIRST_TRY_CHECK_ERROR_CODE, FIRST_TRY_CHECK_ERROR_CODE) Framework.setStepExecutionStatus(WorkflowStepStatus.FAILURE) return else: logger.debug( 'Going to check whether agent installation successful or not') InventoryUtils.executeStep(Framework, CheckInstallErrorCode, InventoryUtils.STEP_REQUIRES_CONNECTION, InventoryUtils.STEP_DOESNOT_REQUIRES_LOCK)
def unInstallAgent(Framework): protocolName = Framework.getProperty(InventoryUtils.STATE_PROPERTY_CONNECTED_SHELL_PROTOCOL_NAME) logger.debug('Protocal name: ', protocolName) client = Framework.getConnectedClient() uduid = InventoryUtils.getUduid(client) logger.debug('UD_UNIQUE_ID: ', uduid) Framework.setProperty(InventoryUtils.ATTR_UD_UNIQUE_ID, uduid) if protocolName == ClientsConsts.DDM_AGENT_PROTOCOL_NAME: # Should release lock first if there will be no connected credential after agent uninstallation. logger.debug('The connected credential is UDA. Try to release lock first.') LockUtils.releaseScannerLock(Framework) if AgentUtils.isAgentInstalled(Framework): logger.debug('There is an agent in remote machine.') # Run uninstall command. shouldStop = AgentUtils.agentUnInstallRoutine(Framework) if shouldStop != 0: Framework.setStepExecutionStatus(WorkflowStepStatus.FATAL_FAILURE) logger.debug('Failed to uninstall agent.') else: logger.debug('There is no agent in remote machine. The job will be done.') reason = 'There is no agent in remote machine' Framework.setProperty(InventoryUtils.generateSkipStep('Check Agent UnInstalled'), reason) Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def StepMain(Framework): InventoryUtils.executeStep( Framework, checkUpgradeRequired, InventoryUtils.STEP_DOESNOT_REQUIRES_CONNECTION, InventoryUtils.STEP_DOESNOT_REQUIRES_LOCK, )
def backupSWUtilizationConfig(Framework): # keep remote ini files BASEDIR = AgentPlatformParameters.getAgentConfigurationPath(Framework) localDiscusgeFile = File.createTempFile( "discusge" + str(System.currentTimeMillis()) + Framework.getTriggerCIData('id'), ".ini") remoteDiscusgeFile = BASEDIR + "discusge.ini" if InventoryUtils.copyRemoteFileToLocal( Framework, remoteDiscusgeFile, localDiscusgeFile.getCanonicalPath(), 0, 1): Framework.setProperty("local_discusge_temp_file", localDiscusgeFile.getCanonicalPath()) else: Framework.reportWarning( "backup discusge.ini file in remote server failed, upgrade agent will use default configuration." ) localPluginFile = File.createTempFile( "plugin" + str(System.currentTimeMillis()) + Framework.getTriggerCIData('id'), ".ini") remotePluginFile = BASEDIR + "plugin.ini" if InventoryUtils.copyRemoteFileToLocal(Framework, remotePluginFile, localPluginFile.getCanonicalPath(), 0, 1): Framework.setProperty("local_plugin_temp_file", localPluginFile.getCanonicalPath()) else: Framework.reportWarning( "backup discusge.ini file in remote server failed, upgrade agent will use default configuration." ) Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def connectToRemoteNode(Framework): Framework.setProperty(InventoryUtils.STATE_PROPERTY_RESOLVED_BASEDIR, Framework.getProperty(InventoryUtils.STATE_PROPERTY_RESOLVED_CONFIGURED_BASEDIR)) Framework.getConnectedClient().setOptionsDirectory(Framework.getProperty(InventoryUtils.STATE_PROPERTY_RESOLVED_BASEDIR)) if AgentUtils.isUpgradeProcess(Framework): # we here means that destination data shows version different from OOTB installer version. # need to recheck with real agent logger.debug('Checking if real version of agent differs from OOTB installer version') agentVersion = None connectedUDACredentialId = None installCredentialId = Framework.getParameter(AgentUtils.UDAGENT_CONNECT_CREDENTIAL_ID_PARAM) logger.debug('Credential id will be used:', installCredentialId) client = Framework.getConnectedClient() if AgentUtils.isUpgradeByUDAgent(Framework): agentVersion = client.getVersion() connectedUDACredentialId = client.getCredentialId() logger.debug('Credential id on remote:', connectedUDACredentialId) AgentUtils.updateCallHomeParams(Framework) AgentUtils.updateSWUtilization(Framework) InventoryUtils.setConnectedClientIdentifier(Framework, client) #Same version and same credential, skip upgrade if AgentUtils.versionsEqual(Framework, agentVersion) and (not installCredentialId or installCredentialId == connectedUDACredentialId): logger.debug('Installed agent version equals to local installer version, skipping upgrade') Framework.setProperty(InventoryUtils.STEP_SKIP_ALL_STEPS_PROPERTY, 'Upgrade not required, real installed agent version equals to the local installer version') client.close() elif Framework.getParameter("UpgradeAgent") == 'false': logger.debug("Upgrade is not required because the job parameter 'UpgradeAgent' is false") Framework.setProperty(InventoryUtils.STEP_SKIP_ALL_STEPS_PROPERTY, "Upgrade is not required because the job parameter 'UpgradeAgent' is false") client.close() else: logger.debug('Installed agent version does not equal to local installer version, Going to execute agent upgrade') Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def downloadScanFile(Framework): targetScanFile = downloadRemoteScanFile(Framework) if not targetScanFile: logger.debug('Remote scan file was not downloaded, will try previously downloaded scan file(if exists)') targetScanFile = useTempScanFile(Framework) if not targetScanFile: logger.debug('No scan file downloaded from previous execution, download file step failed') Framework.setStepExecutionStatus(WorkflowStepStatus.FATAL_FAILURE) Framework.reportError(inventoryerrorcodes.INVENTORY_DISCOVERY_REMOTE_SCANFILE_NOT_FOUND, None) return logger.debug('Scan file was successfully downloaded to ', targetScanFile.getCanonicalPath()) #set download time to current time Framework.setProperty(InventoryUtils.AGENT_OPTION_DISCOVERY_SCANFILE_DOWNLOAD_TIME, Date()) #Check the drity files in sending folder sendingFolder = CollectorsParameters.PROBE_MGR_INVENTORY_XMLENRICHER_FILES_FOLDER + XmlEnricherConstants.SENDING_FOLDER_NAME deleteDirtyFile(File(sendingFolder, targetScanFile.getName())) #delete temporary scan file from previous execution (if any) tempScanFileFolder = CollectorsParameters.PROBE_MGR_TEMPDOWNLOAD + Framework.getDiscoveryJobId() + CollectorsParameters.FILE_SEPARATOR tempScanFileName = InventoryUtils.generateScanFileName(Framework, InventoryUtils.SCANFILE_EXTENTION) deleteDirtyFile(File(tempScanFileFolder, tempScanFileName)) tempScanFileName = InventoryUtils.generateScanFileName(Framework, InventoryUtils.SCANFILE_DELTA_EXTENTION) deleteDirtyFile(File(tempScanFileFolder, tempScanFileName)) Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def StepMain(Framework): Framework.setProperty(InventoryUtils.STATE_PROPERTY_PLATFORM_CONFIGFILE, CollectorsConstants.AGENTSSBYPLATFORM_FILE_NAME) Framework.setProperty(InventoryUtils.STATE_PROPERTY_IS_MIGRATE, String('true')) Framework.setProperty(InventoryUtils.STATE_PROPERTY_IS_MIGRATE_JOB, String('true')) InventoryUtils.executeStep(Framework, initMigrate, InventoryUtils.STEP_DOESNOT_REQUIRES_CONNECTION, InventoryUtils.STEP_DOESNOT_REQUIRES_LOCK)
def connectToRemoteNode(Framework): client = Framework.getConnectedClient() Framework.setProperty( InventoryUtils.STATE_PROPERTY_RESOLVED_BASEDIR, Framework.getProperty( InventoryUtils.STATE_PROPERTY_RESOLVED_CONFIGURED_BASEDIR)) client.setOptionsDirectory( Framework.getProperty(InventoryUtils.STATE_PROPERTY_RESOLVED_BASEDIR)) InventoryUtils.setConnectedClientIdentifier(Framework, client) Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def StepMain(Framework): Framework.setProperty( InventoryUtils.STATE_PROPERTY_PLATFORM_CONFIGFILE, CollectorsConstants.SCANNERSBYPLATFORM_FILE_NAME ) InventoryUtils.executeStep( Framework, connectToRemoteNode, InventoryUtils.STEP_REQUIRES_CONNECTION, InventoryUtils.STEP_DOESNOT_REQUIRES_LOCK, )
def releaseScannerLock(Framework): logger.debug('Finally, Starting Unlock Scanner Node') if not LockUtils.releaseScannerLock(Framework): Framework.setStepExecutionStatus(WorkflowStepStatus.FAILURE) else: logger.debug('Unlock Scanner Node finished') Framework.setProperty(LockUtils.ScannerNodeUnSetLock, LockUtils.ScannerNodeUnSetLock) Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS) logger.debug('Releasing connection after unlock scan node') InventoryUtils.releaseConnection(Framework)
def InitUninstallAgent(Framework): removeData = Framework.getParameter('RemoveAgentData') if removeData.lower() == 'true': logger.debug('Skip step Unlock Scanner Node because the lock will be removed by step Remove Agent Data.') reason = 'The lock will be removed by step Remove Agent Data' Framework.setProperty(InventoryUtils.generateSkipStep('Unlock Scanner Node'), reason) else: logger.debug('Skip step Remove Agent Data because the parameter RemoveAgentData is not true') reason = 'Do not need remove agent data' Framework.setProperty(InventoryUtils.generateSkipStep('Remove Agent Data'), reason) Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def removeScanData(Framework): RemoveScanData = Boolean.parseBoolean( Framework.getParameter('RemoveScanData')) remoteScanFileLocation = Framework.getProperty( InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_FILE_LOCATION) remoteScanFileLogLocation = os.path.splitext( remoteScanFileLocation)[0] + '.log' scannerExecutableRemotePath = Framework.getProperty( InventoryUtils.SCANNER_EXECUTABLE_REMOTE_PATH) scannerConfigRemotePath = Framework.getProperty( InventoryUtils.SCANNER_CONFIG_REMOTE_PATH) protocolName = Framework.getProperty( InventoryUtils.STATE_PROPERTY_CONNECTED_SHELL_PROTOCOL_NAME) isUDA = protocolName == ClientsConsts.DDM_AGENT_PROTOCOL_NAME if RemoveScanData and not isUDA and (remoteScanFileLocation is not None): if not InventoryUtils.deleteFile(Framework, remoteScanFileLocation): errorMessage = 'Failed to delete scan file ' + remoteScanFileLocation logger.debug(errorMessage) Framework.reportError(errorMessage) Framework.setStepExecutionStatus(WorkflowStepStatus.FAILURE) return if RemoveScanData and not isUDA and (remoteScanFileLogLocation is not None): if not InventoryUtils.deleteFile(Framework, remoteScanFileLogLocation): errorMessage = 'Failed to delete scan log ' + remoteScanFileLogLocation logger.debug(errorMessage) Framework.reportError(errorMessage) Framework.setStepExecutionStatus(WorkflowStepStatus.FAILURE) return if RemoveScanData and not isUDA and (scannerExecutableRemotePath is not None): logger.debug("Remove scan executable file:", scannerExecutableRemotePath) if not InventoryUtils.deleteFile(Framework, scannerExecutableRemotePath): errorMessage = 'Failed to delete scan executable file ' + scannerExecutableRemotePath logger.debug(errorMessage) Framework.reportError(errorMessage) Framework.setStepExecutionStatus(WorkflowStepStatus.FAILURE) return if RemoveScanData and not isUDA and (scannerConfigRemotePath is not None): logger.debug("Remove scan config file:", scannerConfigRemotePath) if not InventoryUtils.deleteFile(Framework, scannerConfigRemotePath): errorMessage = 'Failed to delete scan config file ' + scannerConfigRemotePath logger.debug(errorMessage) Framework.reportError(errorMessage) Framework.setStepExecutionStatus(WorkflowStepStatus.FAILURE) return Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def connectToRemoteNode(Framework): Framework.setProperty( InventoryUtils.STATE_PROPERTY_RESOLVED_BASEDIR, Framework.getProperty( InventoryUtils.STATE_PROPERTY_RESOLVED_CONFIGURED_BASEDIR)) Framework.getConnectedClient().setOptionsDirectory( Framework.getProperty(InventoryUtils.STATE_PROPERTY_RESOLVED_BASEDIR)) if AgentUtils.isUpgradeProcess(Framework): # we here means that destination data shows version different from OOTB installer version. # need to recheck with real agent logger.debug( 'Checking if real version of agent differs from OOTB installer version' ) agentVersion = None connectedUDACredentialId = None installCredentialId = Framework.getParameter( AgentUtils.UDAGENT_CONNECT_CREDENTIAL_ID_PARAM) logger.debug('Credential id will be used:', installCredentialId) client = Framework.getConnectedClient() if AgentUtils.isUpgradeByUDAgent(Framework): agentVersion = client.getVersion() connectedUDACredentialId = client.getCredentialId() logger.debug('Credential id on remote:', connectedUDACredentialId) AgentUtils.updateCallHomeParams(Framework) AgentUtils.updateSWUtilization(Framework) InventoryUtils.setConnectedClientIdentifier(Framework, client) #Same version and same credential, skip upgrade if AgentUtils.versionsEqual(Framework, agentVersion) and ( not installCredentialId or installCredentialId == connectedUDACredentialId): logger.debug( 'Installed agent version equals to local installer version, skipping upgrade' ) Framework.setProperty( InventoryUtils.STEP_SKIP_ALL_STEPS_PROPERTY, 'Upgrade not required, real installed agent version equals to the local installer version' ) client.close() elif Framework.getParameter("UpgradeAgent") == 'false': logger.debug( "Upgrade is not required because the job parameter 'UpgradeAgent' is false" ) Framework.setProperty( InventoryUtils.STEP_SKIP_ALL_STEPS_PROPERTY, "Upgrade is not required because the job parameter 'UpgradeAgent' is false" ) client.close() else: logger.debug( 'Installed agent version does not equal to local installer version, Going to execute agent upgrade' ) Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def checkScanFileExistance(Framework): # this step is always finished with success since we DON'T require scan file from previous execution, just nice to have Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS) DownloadScanFileBeforeExecution = Boolean.parseBoolean(Framework.getParameter('DownloadScanFileBeforeExecution')) if DownloadScanFileBeforeExecution: try: client = Framework.getConnectedClient() options = LockUtils.getClientOptionsMap(client) previousExecutionStarted = options.get(InventoryUtils.STATE_PROPERTY_EXECUTION_STARTED) if (previousExecutionStarted is None) or (len(previousExecutionStarted.strip()) == 0): logger.debug('Previous execution timestamp no found, continuing with workflow') return remoteScanFileLocation = options.get(InventoryUtils.AGENT_OPTION_DISCOVERY_SCANFILENAME) if not InventoryUtils.isPathValid(remoteScanFileLocation): logger.debug('No scan file path found on remote machine, continuing with workflow') return lastSuccessExecuton = Framework.getState().getJobLastSuccessfulRun() logger.debug('Last success execution ' + str(lastSuccessExecuton)) logger.debug('Remote scan file execution ' + str(previousExecutionStarted)) if long(lastSuccessExecuton) > long(previousExecutionStarted): logger.debug('Scan file on probe side is newer than on remote machine, skipping downloading') return logger.debug('Last success execution ' + str(lastSuccessExecuton) + ' older than scan file on remote machine ' + str(remoteScanFileLocation) + '. Going to download scan file:' + str(remoteScanFileLocation)) tempScanFileFolder = CollectorsParameters.PROBE_MGR_TEMPDOWNLOAD + Framework.getDiscoveryJobId() + CollectorsParameters.FILE_SEPARATOR File(tempScanFileFolder).mkdirs() extension = InventoryUtils.getFileExtension(remoteScanFileLocation) tempScanFileName = InventoryUtils.generateScanFileName(Framework, extension) tempScanFile = File(tempScanFileFolder, tempScanFileName) tempScanFilePath = tempScanFile.getCanonicalPath() logger.debug('Try to download scan file to the:', tempScanFilePath) if not InventoryUtils.copyRemoteFileToLocal(Framework, remoteScanFileLocation, tempScanFilePath, 0): logger.debug('Failed to download scan file before current execution') Framework.setProperty(InventoryUtils.STATE_PROPERTY_TEMP_SCAN_FILE, tempScanFilePath) except: reason = str(sys.exc_info()[1]) logger.debug('Failed to check/download scan file from previous execution. Reason:', reason) else: logger.debug('Even not checking whether scan file exists on remote machine or not.')
def updateCmdForDeltaScanning(commandLine, Framework): originalScanFileFolderPath = CollectorsParameters.PROBE_MGR_INVENTORY_XMLENRICHER_FILES_FOLDER + XmlEnricherConstants.ORIGINAL_FOLDER_NAME originalScanFile = File(originalScanFileFolderPath, InventoryUtils.generateScanFileName(Framework)) if originalScanFile.exists(): scan = None try: try: buffer = jarray.zeros(0x24, 'b') fileSize = originalScanFile.length() if fileSize > 0x24: scan = RandomAccessFile(originalScanFile, "r") scan.readFully(buffer) if (buffer[0] == 0x1F) and ((buffer[1] & 0xFF) == 0x8B) and (buffer[2] == 0x08): scan.seek(fileSize - 8) scan.readFully(buffer, 0, 8) crc32 = getInt(buffer, 0) size = getInt(buffer, 4) deltaParams = ' -oldscanid:' + str(crc32) + ' -oldscansize:' + str(size) + ' ' index = String(commandLine).indexOf(ENTERPRISE_MODE) + String(ENTERPRISE_MODE).length() commandLine = commandLine[0:index] + deltaParams + commandLine[index + 1:] logger.debug('Scanner execution command updated to ', commandLine) except: logger.debugException("Failed to calculate CRC32 and size of zipped scan file " + originalScanFile.getAbsolutePath()) finally: if scan is not None: try: scan.close() except: pass return commandLine
def upgradeScannerExecutable(scannerPlatformConfig, Framework, shell): logger.debug('Installing scanner') #copying local scanner executable and config file to the remove machine BASEDIR = Framework.getProperty(InventoryUtils.STATE_PROPERTY_RESOLVED_BASEDIR) #scanner local executable file scannerExecutable = scannerPlatformConfig.getScannerExecutable() logger.debug('Scanner executable to be used:', scannerExecutable) #scanner remote executable file scannerRemoteExecutable = scannerPlatformConfig.getScannerRemoteExecutableName() #local location of scanner and config file scannerExecutableLocalPath = CollectorsParameters.PROBE_MGR_RESOURCES_DIR + 'ud_scanners' + str(File.separator) + scannerExecutable logger.debug('Scanner executable local path:', scannerExecutableLocalPath) if not checkResourceExists(Framework, scannerExecutableLocalPath): return 0 scannerExecutableRemotePath = BASEDIR + scannerRemoteExecutable if not InventoryUtils.copyLocalFileToRemote(Framework, scannerExecutableLocalPath, scannerExecutableRemotePath): Framework.setStepExecutionStatus(WorkflowStepStatus.FAILURE) # Try to terminate scanner, if scanner process is stopping the upload logger.debug('Upload cannot proceed due to scanner process, terminate it.') terminateScanner(Framework, shell, scannerRemoteExecutable) return 0 # OK, now scanner file has already upgrade successful Framework.setProperty(InventoryUtils.SCANNER_UPGRADE_DATE, Date()) Framework.setProperty(InventoryUtils.SCANNER_UPGRADE_STATE, '1') Framework.setProperty(InventoryUtils.SCANNER_EXECUTABLE_REMOTE_PATH, scannerExecutableRemotePath) return 1
def getLogFileContent(Framework, localFile, remotePath): localPath = localFile.getCanonicalPath() logger.debug('Going to download remote agent log file ', remotePath, ' to local path:', localPath) if not InventoryUtils.copyRemoteFileToLocal(Framework, remotePath, localPath): return logger.debug('Start reading content from ' + localPath) localClient = None try: try: localClient = Framework.createClient( ClientsConsts.LOCAL_SHELL_PROTOCOL_NAME) localClient.executeCmd('type "' + localPath + '"') except: errorMessage = str(sys.exc_info()[1]) logger.debugException('Failed to load content of file:' + localPath + ';' + errorMessage) finally: if localClient is not None: try: localClient.close() except: pass
def upgradeConfigFile(scannerPlatformConfig, Framework): logger.debug('Installing configuration file') #copying local scanner executable and config file to the remove machine BASEDIR = Framework.getProperty(InventoryUtils.STATE_PROPERTY_RESOLVED_BASEDIR) #scanner config local file scannerConfigPerPlatformSettings = Framework.getParameter('ScannerConfigurationFile') scannerConfig = ScannerConfigurationUtil.getInstance().loadScannerConfigurationPerPlatformWrapper(scannerConfigPerPlatformSettings) platform = Framework.getProperty(InventoryUtils.STATE_PROPERTY_PLATFORM) architecture = Framework.getProperty(InventoryUtils.STATE_PROPERTY_ARCHITECTURE) scannerConfigFile = scannerConfig.getScannerNameForPlatform(platform, architecture) logger.debug('Config file to be used:', scannerConfigFile) #scanner config remote file scannerRemoteConfigFile = scannerPlatformConfig.getScannerRemoteConfigFileName() scannerConfigLocalpath = CollectorsParameters.PROBE_MGR_SCANNER_CONFIG_DIR + scannerConfigFile logger.debug('Scanner config file local path:', scannerConfigLocalpath) if not checkResourceExists(Framework, scannerConfigLocalpath): return 0 scannerConfigRemotePath = BASEDIR + scannerRemoteConfigFile if not InventoryUtils.copyLocalFileToRemote(Framework, scannerConfigLocalpath, scannerConfigRemotePath): Framework.setStepExecutionStatus(WorkflowStepStatus.FAILURE) return 0 Framework.setProperty(InventoryUtils.SCANNER_CONFIG_REMOTE_PATH, scannerConfigRemotePath) return 1
def InitUninstallAgent(Framework): removeData = Framework.getParameter('RemoveAgentData') if removeData.lower() == 'true': logger.debug( 'Skip step Unlock Scanner Node because the lock will be removed by step Remove Agent Data.' ) reason = 'The lock will be removed by step Remove Agent Data' Framework.setProperty( InventoryUtils.generateSkipStep('Unlock Scanner Node'), reason) else: logger.debug( 'Skip step Remove Agent Data because the parameter RemoveAgentData is not true' ) reason = 'Do not need remove agent data' Framework.setProperty( InventoryUtils.generateSkipStep('Remove Agent Data'), reason) Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def releaseScannerLock(Framework): logger.debug('Starting Unlock Scanner Node step') logger.debug("Start checking ud_unique_id...") client = Framework.getConnectedClient() if client: uduid = InventoryUtils.getUduid( client, InventoryUtils.isStampEnabled(Framework, client.getIpAddress())) Framework.setProperty(InventoryUtils.ATTR_UD_UNIQUE_ID, uduid) if not LockUtils.releaseScannerLock(Framework): Framework.setStepExecutionStatus(WorkflowStepStatus.FAILURE) else: logger.debug('Unlock Scanner Node step finished') Framework.setProperty(LockUtils.ScannerNodeUnSetLock, LockUtils.ScannerNodeUnSetLock) Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS) logger.debug('Releasing connection after unlock scan node') InventoryUtils.releaseConnection(Framework)
def removeScanData(Framework): RemoveScanData = Boolean.parseBoolean(Framework.getParameter('RemoveScanData')) remoteScanFileLocation = Framework.getProperty(InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_FILE_LOCATION) remoteScanFileLogLocation = os.path.splitext(remoteScanFileLocation)[0] + '.log' scannerExecutableRemotePath = Framework.getProperty(InventoryUtils.SCANNER_EXECUTABLE_REMOTE_PATH) scannerConfigRemotePath = Framework.getProperty(InventoryUtils.SCANNER_CONFIG_REMOTE_PATH) protocolName = Framework.getProperty(InventoryUtils.STATE_PROPERTY_CONNECTED_SHELL_PROTOCOL_NAME) isUDA = protocolName == ClientsConsts.DDM_AGENT_PROTOCOL_NAME if RemoveScanData and not isUDA and (remoteScanFileLocation is not None): if not InventoryUtils.deleteFile(Framework, remoteScanFileLocation): errorMessage = 'Failed to delete scan file ' + remoteScanFileLocation logger.debug(errorMessage) Framework.reportError(errorMessage) Framework.setStepExecutionStatus(WorkflowStepStatus.FAILURE) return if RemoveScanData and not isUDA and (remoteScanFileLogLocation is not None): if not InventoryUtils.deleteFile(Framework, remoteScanFileLogLocation): errorMessage = 'Failed to delete scan log ' + remoteScanFileLogLocation logger.debug(errorMessage) Framework.reportError(errorMessage) Framework.setStepExecutionStatus(WorkflowStepStatus.FAILURE) return if RemoveScanData and not isUDA and (scannerExecutableRemotePath is not None): logger.debug("Remove scan executable file:", scannerExecutableRemotePath) if not InventoryUtils.deleteFile(Framework, scannerExecutableRemotePath): errorMessage = 'Failed to delete scan executable file ' + scannerExecutableRemotePath logger.debug(errorMessage) Framework.reportError(errorMessage) Framework.setStepExecutionStatus(WorkflowStepStatus.FAILURE) return if RemoveScanData and not isUDA and (scannerConfigRemotePath is not None): logger.debug("Remove scan config file:", scannerConfigRemotePath) if not InventoryUtils.deleteFile(Framework, scannerConfigRemotePath): errorMessage = 'Failed to delete scan config file ' + scannerConfigRemotePath logger.debug(errorMessage) Framework.reportError(errorMessage) Framework.setStepExecutionStatus(WorkflowStepStatus.FAILURE) return Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def releaseResources(Framework): logger.debug('Releasing resources') # we try to remove lock if lock was acquired and not unlocked yet logger.debug('Releasing scanner node lock if not unlocked yet') logger.debug('LockUtils.ScannerNodeSetLock in Framework: ', Framework.getProperty(LockUtils.ScannerNodeSetLock)) logger.debug('LockUtils.ScannerNodeUnSetLock in Framework: ', Framework.getProperty(LockUtils.ScannerNodeUnSetLock)) if Framework.getProperty(LockUtils.ScannerNodeSetLock) and not Framework.getProperty(LockUtils.ScannerNodeUnSetLock): if InventoryUtils.ensureConnection(Framework): LockUtils.releaseScannerLock(Framework) else: logger.debug('Failed to remove lock as failed to connect to host') else: logger.debug('Not removing lock as lock was not acquired or already unlocked') if Framework.getProperty(InventoryUtils.STATE_PROPERTY_CLEAN_UP_STATE_FINALLY): logger.debug('Clear up state saved in DB') Framework.clearState() Framework.setProperty(InventoryUtils.STATE_PROPERTY_CLEAN_UP_STATE_FINALLY, '') logger.debug('Releasing shell connection to the remote node if not released yet') InventoryUtils.releaseConnection(Framework)
def runScanner(Framework): client = Framework.getConnectedClient() scannersConfigFile = Framework.getConfigFile( CollectorsConstants.SCANNERSBYPLATFORM_FILE_NAME) platform = Framework.getProperty(InventoryUtils.STATE_PROPERTY_PLATFORM) architecture = Framework.getProperty( InventoryUtils.STATE_PROPERTY_ARCHITECTURE) logger.debug('Platform:', platform, ' architecture ', architecture) scannerPlatformConfig = scannersConfigFile.getPlatformConfiguration( platform, architecture) BASEDIR = Framework.getProperty( InventoryUtils.STATE_PROPERTY_RESOLVED_BASEDIR) lockValue = Framework.getProperty(LockUtils.ScannerNodeLock) client_options = LockUtils.getClientOptionsMap(client) logger.debug('Settings agent options') options = HashMap() options.put(InventoryUtils.AGENT_OPTION_DISCOVERY_SCANFILENAME, '') options.put(LockUtils.ScannerNodeLock, lockValue) options.put(InventoryUtils.STATE_PROPERTY_EXECUTION_STARTED, str(System.currentTimeMillis())) logger.debug('Agent option ', InventoryUtils.AGENT_OPTION_DISCOVERY_SCANFILENAME, ':', '') logger.debug('Agent option ', LockUtils.ScannerNodeLock, ':', lockValue) logger.debug('Agent option ', InventoryUtils.STATE_PROPERTY_EXECUTION_STARTED, ':', str(System.currentTimeMillis())) filterOptionsByBlackList( client_options) #filter the options of client by black list client_options.putAll(options) client.setOptionsMap(client_options, 1) shell = shellutils.ShellUtils(client, skip_set_session_locale=True) shell.execCmd('cd ' + BASEDIR) runScannerCommand = scannerPlatformConfig.getRunScannerCommand() logger.debug('Launching scanner on remote machine') commandLine = runScannerCommand.getCommand() commandLine = updateCmdWithLogLevel(commandLine, Framework) commandLine = updateCmdForDeltaScanning(commandLine, Framework) commandLine = InventoryUtils.handleBaseDirPath(Framework, commandLine) commandLine = updateWithPrePostScriptCmd(Framework, commandLine) logger.debug('Scanner execution command:', commandLine) shell.execCmd(commandLine) logger.debug('Scanner Launched on remote machine') Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def getRemoteScanLogFilelocation(Framework): client = Framework.getConnectedClient() remoteScanLogFileLocation = Framework.getProperty(InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_LOG_FILE_LOCATION) if not InventoryUtils.isPathValid(remoteScanLogFileLocation): options = LockUtils.getClientOptionsMap(client) remoteScanLogFileLocation = options.get(InventoryUtils.AGENT_OPTION_DISCOVERY_SCANLOGFILENAME) if InventoryUtils.isPathValid(remoteScanLogFileLocation): Framework.setProperty(InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_LOG_FILE_LOCATION, remoteScanLogFileLocation) logger.debug('Got agent option ' + InventoryUtils.AGENT_OPTION_DISCOVERY_SCANLOGFILENAME + ' ', remoteScanLogFileLocation) else: logger.debug('Remote scan log file location from agent options:', remoteScanLogFileLocation) else: logger.debug('Got scan log file location from properties ', remoteScanLogFileLocation) if remoteScanLogFileLocation is None: remoteScanFileLocation = Framework.getProperty(InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_FILE_LOCATION) if remoteScanFileLocation is not None: remoteScanLogFileLocation = os.path.splitext(remoteScanFileLocation)[0] + '.log' return remoteScanLogFileLocation
def downloadScanFile(Framework): remoteScanFileLocation = getScanFilelocation(Framework) if not InventoryUtils.isPathValid(remoteScanFileLocation): logger.debug('No scan file yet. Scanner is still running.') Framework.reportError(inventoryerrorcodes.INVENTORY_DISCOVERY_SCANFILE_NOTREADY, ['Empty']) Framework.setStepExecutionStatus(WorkflowStepStatus.FAILURE) # get scanner status information retrieveScannerStatus(Framework, 0) return if retrieveScannerStatus(Framework, 1): Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def backupSWUtilizationConfig(Framework): # keep remote ini files BASEDIR = AgentPlatformParameters.getAgentConfigurationPath(Framework) localDiscusgeFile = File.createTempFile("discusge" + str(System.currentTimeMillis()) + Framework.getTriggerCIData('id'), ".ini") remoteDiscusgeFile = BASEDIR + "discusge.ini" if InventoryUtils.copyRemoteFileToLocal(Framework, remoteDiscusgeFile, localDiscusgeFile.getCanonicalPath(), 0, 1): Framework.setProperty("local_discusge_temp_file", localDiscusgeFile.getCanonicalPath()) else : Framework.reportWarning("backup discusge.ini file in remote server failed, upgrade agent will use default configuration.") localPluginFile = File.createTempFile("plugin" + str(System.currentTimeMillis()) + Framework.getTriggerCIData('id'), ".ini") remotePluginFile = BASEDIR + "plugin.ini" if InventoryUtils.copyRemoteFileToLocal(Framework, remotePluginFile, localPluginFile.getCanonicalPath(), 0, 1): Framework.setProperty("local_plugin_temp_file", localPluginFile.getCanonicalPath()) else : Framework.reportWarning("backup discusge.ini file in remote server failed, upgrade agent will use default configuration.") Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def getScanFilelocation(Framework): try: client = Framework.getConnectedClient() except: logger.warn("Connect failed during [Check Scanner Finished], parking for next try ") return None #on previous Download Scan File step execution we can already obtain remote scan file path #but failed to copy it to base dir. That is why we can try to get it from properties remoteScanFileLocation = Framework.getProperty(InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_FILE_LOCATION) if not InventoryUtils.isPathValid(remoteScanFileLocation): options = LockUtils.getClientOptionsMap(client) remoteScanFileLocation = options.get(InventoryUtils.AGENT_OPTION_DISCOVERY_SCANFILENAME) if InventoryUtils.isPathValid(remoteScanFileLocation): Framework.setProperty(InventoryUtils.STATE_PROPERTY_REMOTE_SCAN_FILE_LOCATION, remoteScanFileLocation) logger.debug('Got agent option ' + InventoryUtils.AGENT_OPTION_DISCOVERY_SCANFILENAME + ' ', remoteScanFileLocation) else: logger.debug('Remote scan file location from agent options:', remoteScanFileLocation) else: logger.debug('Got scan file location from properties ', remoteScanFileLocation) return remoteScanFileLocation
def getLocalScanLogFileLocation(Framework): localScanLogName = InventoryUtils.generateScanLogName(Framework) #folder for scan files localScanLogFolder = CollectorsParameters.PROBE_MGR_INVENTORY_XMLENRICHER_FILES_FOLDER + XmlEnricherConstants.LOGS_FOLDER_NAME downloadedScanLogDir = File(localScanLogFolder) downloadedScanLogDir.mkdirs() #this scan log file will be created after downloading from remote machine targetScanFile = File(downloadedScanLogDir, localScanLogName) localScanLogLocation = targetScanFile.getCanonicalPath() return localScanLogLocation
def checkEnrichedFileExisted(Framework): localScanFileName = InventoryUtils.generateScanFileName(Framework) localScanFileSendingFolderPath = CollectorsParameters.PROBE_MGR_INVENTORY_XMLENRICHER_FILES_FOLDER + XmlEnricherConstants.SENDING_FOLDER_NAME targetScanFile = File(localScanFileSendingFolderPath, localScanFileName) if not targetScanFile.exists(): logger.debug('No processed scan file yet. XML-Enricher is still running.') Framework.reportError(inventoryerrorcodes.INVENTORY_DISCOVERY_ENRICHED_SCANFILE_NOTREADY, [localScanFileName]) Framework.setStepExecutionStatus(WorkflowStepStatus.FAILURE) else: logger.debug('find processed scan file, goto next step') Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def CheckAgentInstalledBeforeInstallation(Framework): try: logger.debug('Check if the UDA has already been installed...') agentInstalled = AgentUtils.isAgentInstalled(Framework) logger.debug('Agent installed result:%s' % agentInstalled) # Framework.setProperty('isAgentInstalled', agentInstalled) if agentInstalled: logger.debug('Check if the UDA can be connected...') agentConnected = isAgentConnected(Framework) logger.debug('Agent connecting result:%s' % agentConnected) # Framework.setProperty('isAgentConnected', agentConnected) if agentConnected: reason = 'The UDA has been already installed and can be connected successfully.' Framework.setProperty(InventoryUtils.generateSkipStep('Install Agent'), reason) Framework.setProperty(InventoryUtils.generateSkipStep('Check Agent Install Error Code'), reason) Framework.setProperty(InventoryUtils.generateSkipStep('Check Agent Installed'), reason) else: logger.debug('Agent installed but can not connect, reinstall it.') Framework.setProperty(InventoryUtils.STATE_PROPERTY_IS_UPGRADE, 'true') except: Framework.setStepExecutionStatus(WorkflowStepStatus.FATAL_FAILURE) Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def downloadScanFile(Framework): remoteScanFileLocation = getScanFilelocation(Framework) if not InventoryUtils.isPathValid(remoteScanFileLocation): logger.debug('No scan file yet. Scanner is still running.') Framework.reportError( inventoryerrorcodes.INVENTORY_DISCOVERY_SCANFILE_NOTREADY, ['Empty']) Framework.setStepExecutionStatus(WorkflowStepStatus.FAILURE) # get scanner status information retrieveScannerStatus(Framework, 0) return if retrieveScannerStatus(Framework, 1): Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def useTempScanFile(Framework): tempScanFilePath = Framework.getProperty(InventoryUtils.STATE_PROPERTY_TEMP_SCAN_FILE) if tempScanFilePath is None: logger.debug('No scan file found from previous scanner execution') return None logger.debug('Using scan file from previous execution:', tempScanFilePath) extension = InventoryUtils.getFileExtension(tempScanFilePath) localScanFileName = InventoryUtils.generateScanFileName(Framework, extension) #folder for scan files localScanFileFolderPath = CollectorsParameters.PROBE_MGR_INVENTORY_XMLENRICHER_FILES_FOLDER + XmlEnricherConstants.INCOMING_FOLDER_NAME downloadedScanFilesDir = File(localScanFileFolderPath) downloadedScanFilesDir.mkdirs() #this scan file will be created after downloading from remote machine targetScanFile = File(downloadedScanFilesDir, localScanFileName) logger.debug('Scan file from previous execution will be moved to ', targetScanFile.getCanonicalPath()) tempScanFile = File(tempScanFilePath) if not tempScanFile.renameTo(targetScanFile): return None return targetScanFile
def removeLocalScanLog(Framework): localScanLogName = InventoryUtils.generateScanLogName(Framework) localScanLogFolderPath = CollectorsParameters.PROBE_MGR_INVENTORY_XMLENRICHER_FILES_FOLDER + XmlEnricherConstants.LOGS_FOLDER_NAME + CollectorsParameters.FILE_SEPARATOR localScanLogFile = File(localScanLogFolderPath, localScanLogName) try: # if the local scan log exists, delete it before next steps if localScanLogFile.exists(): logger.debug("local scan log file found, just delete it: ", localScanLogFile.getCanonicalPath()) if not localScanLogFile.delete(): logger.warn("delete scan log file failed, ensure the there's permission and it's not locked:", localScanLogFile.getCanonicalPath()) except: logger.warn("delete scan log file failed: ", localScanLogFile.getCanonicalPath()) Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def upgradePrePostScriptResource(isAllUnix, Framework): logger.debug('Installing resource files for pre/post script') BASEDIR = Framework.getProperty(InventoryUtils.STATE_PROPERTY_RESOLVED_BASEDIR) platform = Framework.getProperty(InventoryUtils.STATE_PROPERTY_PLATFORM) architecture = Framework.getProperty(InventoryUtils.STATE_PROPERTY_ARCHITECTURE) if isAllUnix: logger.debug("No specific platform for the device, use all-unix.") platform = 'unix' architecture = '' elif platform in ['aix', 'windows']: # since in PrePostScriptEditor, the aix and windows platforms have no architecture definitions, # so just ignore the architecture value for aix and windows architecture = '' #get all local file path from resource folder and generate remote file path prepostScanScriptResourcePathArray = getPrePostScriptResourcePathArray(BASEDIR, platform, architecture) if prepostScanScriptResourcePathArray is not None: # copy local resource files to remote for (resourceLocalPath, resourceRemotePath) in prepostScanScriptResourcePathArray: if os.path.exists(resourceLocalPath): InventoryUtils.copyLocalFileToRemote(Framework, resourceLocalPath, resourceRemotePath)
def StepMain(Framework): skipStepReason = Framework.getProperty(InventoryUtils.STEP_SKIP_ALL_STEPS_PROPERTY) if skipStepReason is not None: logger.debug('Skipped by request to skip all steps, reason:', skipStepReason) Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS) return skipStepReason = Framework.getProperty(InventoryUtils.generateSkipStep(Framework.getState().getCurrentStepName())) if skipStepReason is not None: logger.debug('Step skipped, reason:', skipStepReason) Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS) return if not Framework.getProperty(FIRST_TRY_CHECK_ERROR_CODE): # we don't want immediately check whether agent installation successful or not, # since for sure it is not. go to parking to let others to install logger.debug('UD agent install command just run, will check result code after parking') Framework.setProperty(FIRST_TRY_CHECK_ERROR_CODE, FIRST_TRY_CHECK_ERROR_CODE) Framework.setStepExecutionStatus(WorkflowStepStatus.FAILURE) return else: logger.debug('Going to check whether agent installation successful or not') InventoryUtils.executeStep(Framework, CheckInstallErrorCode, InventoryUtils.STEP_REQUIRES_CONNECTION, InventoryUtils.STEP_DOESNOT_REQUIRES_LOCK)
def getLocalScanLogFileLocation(Framework): localScanLogName = InventoryUtils.generateScanLogName(Framework) # folder for scan files localScanLogFolder = ( CollectorsParameters.PROBE_MGR_INVENTORY_XMLENRICHER_FILES_FOLDER + XmlEnricherConstants.LOGS_FOLDER_NAME ) downloadedScanLogDir = File(localScanLogFolder) downloadedScanLogDir.mkdirs() # this scan log file will be created after downloading from remote machine targetScanFile = File(downloadedScanLogDir, localScanLogName) localScanLogLocation = targetScanFile.getCanonicalPath() return localScanLogLocation
def terminateScanner(Framework, shell, scannerRemoteExecutable): platform = Framework.getProperty(InventoryUtils.STATE_PROPERTY_PLATFORM) if InventoryUtils.isUnix(platform): psArgs = ' -e ' if platform == 'macosx': psArgs = ' -A ' cmdOutput = shell.execCmd('ps' + psArgs + '| grep ' + scannerRemoteExecutable) if cmdOutput and len(cmdOutput) > 0: pid = cmdOutput.split()[0] if pid.isdigit(): shell.execCmd('kill ' + pid) else: cmdOutput = shell.execCmd('tasklist /FO csv | findstr ' + scannerRemoteExecutable) if cmdOutput and len(cmdOutput) > 0: pid = cmdOutput.split('","')[1] if pid.isdigit(): shell.execCmd('taskkill /PID ' + pid)
def runScanner(Framework): client = Framework.getConnectedClient() scannersConfigFile = Framework.getConfigFile(CollectorsConstants.SCANNERSBYPLATFORM_FILE_NAME) platform = Framework.getProperty(InventoryUtils.STATE_PROPERTY_PLATFORM) architecture = Framework.getProperty(InventoryUtils.STATE_PROPERTY_ARCHITECTURE) logger.debug('Platform:', platform, ' architecture ', architecture) scannerPlatformConfig = scannersConfigFile.getPlatformConfiguration(platform, architecture) BASEDIR = Framework.getProperty(InventoryUtils.STATE_PROPERTY_RESOLVED_BASEDIR) lockValue = Framework.getProperty(LockUtils.ScannerNodeLock) client_options = LockUtils.getClientOptionsMap(client) logger.debug('Settings agent options') options = HashMap() options.put(InventoryUtils.AGENT_OPTION_DISCOVERY_SCANFILENAME, '') options.put(LockUtils.ScannerNodeLock, lockValue) options.put(InventoryUtils.STATE_PROPERTY_EXECUTION_STARTED, str(System.currentTimeMillis())) logger.debug('Agent option ', InventoryUtils.AGENT_OPTION_DISCOVERY_SCANFILENAME, ':', '') logger.debug('Agent option ', LockUtils.ScannerNodeLock, ':', lockValue) logger.debug('Agent option ', InventoryUtils.STATE_PROPERTY_EXECUTION_STARTED, ':', str(System.currentTimeMillis())) filterOptionsByBlackList(client_options) #filter the options of client by black list client_options.putAll(options) client.setOptionsMap(client_options, 1) shell = shellutils.ShellUtils(client, skip_set_session_locale=True) shell.execCmd('cd ' + BASEDIR) runScannerCommand = scannerPlatformConfig.getRunScannerCommand() logger.debug('Launching scanner on remote machine') commandLine = runScannerCommand.getCommand() commandLine = updateCmdWithLogLevel(commandLine, Framework) commandLine = updateCmdForDeltaScanning(commandLine, Framework) commandLine = InventoryUtils.handleBaseDirPath(Framework, commandLine) commandLine = updateWithPrePostScriptCmd(Framework, commandLine) logger.debug('Scanner execution command:', commandLine) shell.execCmd(commandLine) logger.debug('Scanner Launched on remote machine') Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def removeLocalScanLog(Framework): localScanLogName = InventoryUtils.generateScanLogName(Framework) localScanLogFolderPath = CollectorsParameters.PROBE_MGR_INVENTORY_XMLENRICHER_FILES_FOLDER + XmlEnricherConstants.LOGS_FOLDER_NAME + CollectorsParameters.FILE_SEPARATOR localScanLogFile = File(localScanLogFolderPath, localScanLogName) try: # if the local scan log exists, delete it before next steps if localScanLogFile.exists(): logger.debug("local scan log file found, just delete it: ", localScanLogFile.getCanonicalPath()) if not localScanLogFile.delete(): logger.warn( "delete scan log file failed, ensure the there's permission and it's not locked:", localScanLogFile.getCanonicalPath()) except: logger.warn("delete scan log file failed: ", localScanLogFile.getCanonicalPath()) Framework.setStepExecutionStatus(WorkflowStepStatus.SUCCESS)
def downloadScanLogFile(Framework): needDownloadLog = Framework.getProperty(InventoryUtils.DOWNLOAD_SCANNER_LOG) if not needDownloadLog: return logger.debug('Downloading remote scanner log file to local location') try: remoteScanLogLocation = getRemoteScanLogFilelocation(Framework) localScanLogLocation = getLocalScanLogFileLocation(Framework) if (remoteScanLogLocation is None) or (localScanLogLocation is None): logger.debug('Download scanner log file failed: remoteScanLogLocation=', remoteScanLogLocation, ', localScanLogLocation=', localScanLogLocation) return #get file to the local machine if not InventoryUtils.copyRemoteFileToLocal(Framework, remoteScanLogLocation, localScanLogLocation): return except: errorMessage = str(sys.exc_info()[1]) logger.warn('Download scanner log file failed: ', errorMessage)