def handler(self, items): is_db2_home_dir = Fn(operator.eq, self.db2_home_path, fptools._) create_path = Fn(file_system.Path, fptools._, self.db2_home_path.path_tool) get_path_name = operator.attrgetter('DB2 Path Name') item = iteratortools.findFirst( comp(is_db2_home_dir, create_path, get_path_name), items) return item and RegistryPath(item.keyPath)
def handler(self, items): get_db_info = operator.attrgetter('DB2_BDINFO') is_not_none = Fn(operator.is_not, fptools._, None) filterd_items = ifilter(comp(is_not_none, get_db_info), items) is_pid_matched = Fn(re.match, '%s\s\d+\s\d+' % self.pid, fptools._) item = fptools.findFirst(comp(is_pid_matched, get_db_info), filterd_items) if item: registry_path = RegistryPath(item.keyPath) if self.isNode(registry_path): node_registry_path = RegistryPath(registry_path.getPath()) registry_path = RegistryPath(node_registry_path.getPath()) return registry_path.name
def decorator(framework): framework = flow.RichFramework(framework) creds_manager = flow.CredsManager(framework) creds = creds_manager.get_creds_for_destination(proto_name) creds = filter(Fn(is_cred_ok_fn, framework, creds_manager, __), creds) return iterate_over_args(main_fn, framework, creds, proto_name, stop_on_first)
def _get_pf_doc(self, pf_path, sys_name, shell): '@types: str, str, Shell -> File, File, InitDocument' base_path = sap_discoverer.findSystemBasePath(pf_path, sys_name) get_def_pf_fn = Fn(discover_default_pf, shell, pf_path, sys_name) get_inst_pf_fn = Fn(sap_discoverer_by_shell.read_pf, shell, pf_path) default_pf_result = self._get_or_discover(base_path, Sfn(get_def_pf_fn)) default_pf_file, default_pf_doc = default_pf_result or (None, None) if not default_pf_file: logger.warn("Failed to get content for the DEFAULT.PFL") pf_file, pf_doc = self._get_or_discover(pf_path, Sfn(get_inst_pf_fn)) if not pf_file: logger.warn("Failed to get content for the instance profile") doc = sap_discoverer.createPfsIniDoc(default_pf_doc, None, pf_doc) return default_pf_file, pf_file, doc
def decorator(framework): vector = ObjectStateHolderVector() framework = RichFramework(framework) creds_manager = CredsManager(framework) creds = creds_manager.get_creds_for_destination(proto_name) creds = filter(Fn(is_cred_ok_fn, framework, creds_manager, __), creds) if not creds: logger.reportErrorObject(_create_missed_creds_error(proto_name)) else: connection_exs = [] discovery_exs = [] warnings = [] at_least_once_discovered = False for cred_id in creds: try: oshs, warnings = main_fn(framework, creds_manager, cred_id) vector.addAll(oshs) at_least_once_discovered = True if stop_on_first: break except ConnectionException, ce: logger.debugException(str(ce)) connection_exs.append(ce) except (DiscoveryException, Exception, JException), de: logger.debugException(str(de)) discovery_exs.append(de)
def parse(self, configParser): r'@types: ConfigParser.ConfigParser -> list[RfcConfiguration]' if not configParser: raise ValueError("ConfigParser is not specified") parseConnectionSection = Sfn(Fn(self.__parseSection, __, configParser)) sessions = configParser.sections() sessions = ifilter(self.__isTrexRfcConnectionConfigSection, sessions) return keep(parseConnectionSection, sessions)
def get_db2_version_by_home_path(reg_provider, db2_home_path): regbased_discoverers = registry.get_discoverers() discover_version = Fn(fptools.safeFunc(__discover_version), reg_provider, fptools._, db2_home_path) return fptools.findFirst(lambda x: x, ifilter(None, imap(discover_version, regbased_discoverers)))
def reportSoftwareCmpsAsCis(cmps, containerOsh): r'@types: list[sap.SoftwareComponent], osh -> oshv' builder = sap.SoftwareComponentBuilder() reporter = sap.SoftwareComponentReporter(builder) report = Fn(reporter.reportSoftwareComponent, fptools._, containerOsh) vector = ObjectStateHolderVector() fptools.each(vector.add, imap(report, cmps)) return vector
def get_instance_name(self, context): pid = self._main_process.getPid() if pid is not None: shell = context.client os_bitcount = shell.is64BitMachine() and 64 or 32 reg_provider = regutils.getProvider(shell) version = self.get_db2_version(context) discoverer = winreg_discoverer.registry.get_discoverer(version, os_bitcount) execute_reg_command = Fn(winreg_base_discoverer.execute_reg_query, reg_provider, fptools._) execute_reg_command = safeFn(execute_reg_command) return fptools.findFirst(bool, map(execute_reg_command, (discoverer.GetInstanceNameByPid(pid), discoverer.GetClusterInstanceNameByPid(pid)) )) else: logger.debug('pid is not available for the main db2 process')
def _report_data_files(mountpoint_by_container, containers_by_tablespace_id, partition_nr_by_container, tablespaces, partition_osh_by_number, node_osh_by_partition_nr, report_datafile, report_file_system): tablespace_by_tablespace_id = {} for tablespace, bufferpool_id, pg_name in tablespaces: tablespace_by_tablespace_id[tablespace.id] = tablespace containers_osh_by_tablespace = defaultdict(list) for tablespace_id, containers in containers_by_tablespace_id.iteritems(): tablespace = tablespace_by_tablespace_id.get(tablespace_id) if tablespace: create_datafile_pdo = Fn(db2_topology.Builder.DataFilePdo, fptools._, tablespace ) for container in containers: if container in partition_nr_by_container: partitiom_nr = partition_nr_by_container[container] if (partitiom_nr in node_osh_by_partition_nr and partitiom_nr in partition_osh_by_number): node_osh = node_osh_by_partition_nr[partitiom_nr] partition_osh = partition_osh_by_number[partitiom_nr] datafile_pdo = create_datafile_pdo(container) file_system_osh = None if node_osh and db2_base_parser.is_win_os_path(container): mountpoint = mountpoint_by_container.get(container) if mountpoint: fs_pdo = db2_topology.FileSystemBuilder.FileSystemPdo(mountpoint, mountpoint) file_system_osh = report_file_system(fs_pdo, node_osh) osh = report_datafile(datafile_pdo, partition_osh, file_system_osh=file_system_osh) containers_osh_by_tablespace[tablespace].append(osh) return containers_osh_by_tablespace
config = (flow.DiscoveryConfigBuilder(framework).dest_data_params_as_list( 'Protocol').dest_data_params_as_list( 'installpath').dest_data_required_params_as_str('sid'). dest_data_required_params_as_str('hanadb_cmdbid')).build() return config._replace(Protocol=config.Protocol[connection_number], installpath=config.installpath[connection_number]) def get_cred_combinations(framework, cred_manager): ip_addresses = framework.getTriggerCIDataAsList('ip_address') cred_ids = framework.getTriggerCIDataAsList('credentialsId') return zip(cred_ids, ip_addresses) _client_factories_provider = Fn(get_clients_provider_fn, __, __, get_cred_combinations, create_default_client) def hana_discoverer_provider(shell, filesystem, pathtool, sid, installpath): try: return hana_tools.hana_discoverer_provider(shell, filesystem, pathtool, sid, installpath) except hana_hdbsql.NoHdbsqlException, e: raise hana_flow.DiscoveryException(e.message) tool_factories = hana_tools.factories.copy() tool_factories.update({ 'sid': lambda config: config.sid, 'config': config_provider, 'installpath': lambda config: config.installpath,
def initialize_env_with_xml_as_output(client, run_timeout=1000): exec_fn = Fn(exec_with_xml_output, client, __, run_timeout) exec_fn(EnableXmlOutputCmd()) return exec_fn
import ip_addr from iteratortools import second from fptools import _ as __, partiallyApply as Fn, comp import re import logger node_text = attrgetter('text') def _opt_fn_call(v, fn): if v is not None: return fn(v) opt_int = Fn(_opt_fn_call, __, int) opt_float = Fn(_opt_fn_call, __, float) class Result(UserDict): def getIndexed(self, name): index = 1 key_ = "%s %s" % (name, index) values = [] while key_ in self: values.append(self.get(key_)) index += 1 key_ = "%s %s" % (name, index) return tuple(values)
def parse_name_value_separated_output(self, output, separator='\s*=\s*'): sep_pattern = re.compile(separator) maxsplit = 1 split_by_sep = comp(Fn(sep_pattern.split, __, maxsplit), string.strip) db_entry_lines = ifilter(bool, output.splitlines()) return dict(imap(split_by_sep, db_entry_lines))
except(Exception, JavaException), e: msg = e.getMessage() if hasattr(e, 'getMessage') else str(e) raise ConnectionException(msg or 'Connection failed') else: try: yield client finally: client.close() def get_clients_provider_fn(framework, cred_manager, get_credentials_fn, create_client_fn): for creds in get_credentials_fn(framework, cred_manager): yield partial(create_client_fn, framework, *creds) default_client_factories_provider = Fn(get_clients_provider_fn, __, __, get_cred_and_ip, create_default_client) def with_clients(*args, **kwargs): ''' Decorator for the DiscoveryMain providing client instances returned by each client factory from client_factories_provider fn @param proto_name: protocol to connect with @param stop_on_first: Stop on first successful discovery @param client_factories_provider: function providing client factories Signature is (Framework, CredsManager -> generator((->Client))) Usage: @webseal_flow.with_clients(db2_flow.default_client_factories_provider)
cred_ids = cred_manager.get_creds_for_destination('httpprotocol') return zip(cred_ids, ip_addresses) def create_secure_data_http_client(http_client): from appilog.common.utils import Protocol capability = http_client.getCapability(SecureDataCapability) capability.replaceWithAttributeValue('${username}', Protocol.PROTOCOL_ATTRIBUTE_USERNAME) capability.replaceWithAttributeValue('${password}', Protocol.PROTOCOL_ATTRIBUTE_PASSWORD) return capability _ipse_only_client_factories_provider = Fn(get_clients_provider_fn, __, __, get_ipse_only_cred_combinations, create_default_client) tool_factories = webseal_tools.factories.copy() tool_factories.update({ 'secure_data_http_client': create_secure_data_http_client, 'pdadmin_api_query': lambda config: config.pdadmin_api_query, 'management_authentication_api_query': lambda config: config.management_authentication_api_query, 'firmware_settings_api_query': lambda config: config.firmware_settings_api_query, 'reverseproxy_api_query': lambda config: config.reverseproxy_api_query, 'protocol':
def process(self, context): r''' @types: applications.ApplicationSignatureContext ''' # ------------------------------------------------------------ DISCOVERY "SAP TREX plug-in DISCOVERY start" | info shell = context.client fs = file_system.createFileSystem(shell) pathtools = file_system.getPath(fs) # x) get process related application hostOsh = context.hostOsh application = context.application destinationIp = application.getConnectionIp() "x) Find TREX Daemon process that has profile path as parameter" | info mainProcess = findFirst(isMainTrexProcess, context.application.getProcesses()) profilePath = sap_discoverer.getProfilePathFromCommandline( mainProcess.commandLine) "x) Read profile content: %s" % profilePath | info getFileContent = Sfn( Fn(self.__getFileWithContent, shell, pathtools, __)) profileFile = profilePath and getFileContent(profilePath) if not profileFile: "Plug-in flow broken. Failed to read instance profile\ content based on path from the TREXDaemon command line" | warn return "x) Instance profile parsing" | info sapIniParser = sap_discoverer.IniParser() instanceProfileParser = sap_discoverer.InstanceProfileParser( sapIniParser) defaultProfileParser = sap_trex_discoverer.DefaultProfileParser( sapIniParser) try: resultAsIni = instanceProfileParser.parseAsIniResult( profileFile.content) instanceProfile = instanceProfileParser.parse(resultAsIni) defaultProfile = defaultProfileParser.parse(resultAsIni) except Exception: logger.warnException("Failed to parse instance profile") return rfcConfigs = [] trexSystem = defaultProfile.getSystem() trexInstance = instanceProfile.getInstance() trexInstanceName = trexInstance.getName() + trexInstance.getNumber() isBiaProduct = 0 versionInfo = None # # master by default, if topology file is not found that means # # that current one is the only instance # isMaster = 1 trexTopology = None "x) Initialize TREX instance layout" | debug systemName = trexSystem.getName() systemBasePath = sap_discoverer.findSystemBasePath( mainProcess.getExecutablePath(), systemName) if systemBasePath: systemLayout = sap_trex_discoverer.SystemLayout( pathtools, systemBasePath, systemName) 'System path: %s' % systemLayout.getRootPath() | info instancePath = systemLayout.composeInstanceDirPath( trexInstanceName) 'Instance path: %s' % instancePath | debug instanceLayout = sap_trex_discoverer.InstanceLayout( pathtools, instancePath, trexInstanceName) "x) Get content of default profile as it contains information about product" "x) Determine whether we deal with BIA based on version information" | debug defaultProfilePath = systemLayout.getDefaultProfileFilePath() defaultProfileFile = getFileContent(defaultProfilePath) try: resultAsIni = instanceProfileParser.parseAsIniResult( defaultProfileFile.content) defaultProfile = defaultProfileParser.parse(resultAsIni) except Exception: logger.warnException("Failed to parse default profile") else: isBiaProduct = defaultProfile.getProductType( ) == sap_trex.Product.BIA (isBiaProduct and "BIA" or "non-BIA", "product detected") | info # get instance host name from profile name instanceHostname = None try: destinationSystem = sap_discoverer.parseSapSystemFromInstanceProfileName( profileFile.getName()) except Exception: msg = "Failed to parse instance hostname from profile file name" logger.debugException(msg) else: instanceHostname = first( destinationSystem.getInstances()).getHostname() "x) Discover whole topology from (topology.ini)" | info # topology.ini file location and format differs depending on the # product: # -a) BIA (has plain-ini file at <SID>/sys/global/trex/data/topology.ini # -b) TREX (has several places where topology.ini can be stored) discoverTopologyIniFilePath = fptools.safeFunc( sap_trex_discoverer.discoverTopologyIniFilePath) topologyFilePath = (isBiaProduct and systemLayout.getTopologyIniPath() or discoverTopologyIniFilePath( fs, instanceLayout, instanceHostname)) topologyFile = topologyFilePath and getFileContent( topologyFilePath) if topologyFile: try: configParser = sap_trex_discoverer.TopologyConfigParser() trexTopology = sap_trex_discoverer.TrexTopologyConfig( configParser.parse(topologyFile.content)) # find instance between master end-points # landscapeSnapshot = topology.getGlobals().getLandscapeSnapshot() # masterEndpoints = landscapeSnapshot.getMasterEndpoints() # activeMasterEndpoints = landscapeSnapshot.getActiveMasterEndpoints() # topologyNodes = topology.getHostNodes() ## # isEndpointWithInstanceHostname = (lambda # ep, hostname = instanceHostname: ep.getAddress() == hostname) # isMaster = len(filter(isEndpointWithInstanceHostname, # landscapeSnapshot.getMasterEndpoints())) # "host role is %s" % (isMaster and "master" or "slave") | info except: logger.warnException( "Failed to parse topology configuration") else: logger.warn( "Failed to get content for the topology configuration") "x) Discover TREX version information from saptrexmanifest.mf" | info # read version info from manifest file manifestFile = getFileContent(instanceLayout.getManifestFilePath()) if manifestFile: manifestParser = sap_trex_discoverer.SapTrexManifestParser( sapIniParser) versionInfo = manifestParser.parseVersion(manifestFile.content) else: 'Failed to discover version from manifest file' | warn 'Second attept to get version from updateConfig.ini file' | info profileSystem = Sfn( sap_discoverer.parseSapSystemFromInstanceProfileName)( profileFile.getName()) if profileSystem: hostname = first( profileSystem.getInstances()).getHostname() updateConfigFile = getFileContent( instanceLayout.composeUpdateConfigIniFilePath( hostname)) versionInfo = updateConfigFile and sap.VersionInfo( updateConfigFile.content.strip()) "x) Discover served systems ( in case if RFC configuration established )" | info rfcServerIniFilePath = ( isBiaProduct and systemLayout.getRfcServerConfigFilePath() or instanceLayout.composeTrexRfcServerIniFilePath( instanceHostname)) rfcServerIniFile = getFileContent(rfcServerIniFilePath) if rfcServerIniFile: rfcConfigs = filter(None, (fptools.safeFunc( sap_trex_discoverer.parseConnectionsInRfcServerIni)( rfcServerIniFile.content))) # -------------------------------------------------------- REPORTING "SAP TREX plug-in REPORTING start" | info trexOsh = application.getOsh() vector = context.resultsVector configFileReporter = file_topology.Reporter(file_topology.Builder()) trexReporter = sap_trex.Reporter(sap_trex.Builder()) linkReporter = sap.LinkReporter() softwareBuilder = sap.SoftwareBuilder() "x) - report profile content as configuration document for the application" | info vector.add(configFileReporter.report(profileFile, trexOsh)) ("x) - report %s" % trexSystem) | info trexSystemOsh = trexReporter.reportSystem(trexSystem) vector.add(trexSystemOsh) vector.add(linkReporter.reportMembership(trexSystemOsh, trexOsh)) "x) - report instance name and version" | info softwareBuilder.updateName(trexOsh, trexInstanceName) "x) report instance number: %s" % trexInstance.getNumber() | info instanceBuilder = sap_trex.Builder() instanceBuilder.updateInstanceNumber(trexOsh, trexInstance.getNumber()) if versionInfo: softwareBuilder.updateVersionInfo(trexOsh, versionInfo) if isBiaProduct: softwareBuilder.updateDiscoveredProductName( trexOsh, sap_trex.Product.BIA.instanceProductName) "x) report RFC connections" | info dnsResolver = netutils.DnsResolverByShell(shell, destinationIp) vector.addAll(reportRfcConfigs(rfcConfigs, dnsResolver, hostOsh)) "x) report all topology nodes" | info if trexTopology: reportHostNode = fptools.partiallyApply(reportTrexHostNode, fptools._, trexTopology, isBiaProduct) vectors = map(reportHostNode, trexTopology.getHostNodes()) fptools.each(vector.addAll, vectors)
''' Created on Apr 9, 2013 @author: vvitvitskiy ''' import operator from itertools import ifilter from iteratortools import second from fptools import _ as __, partiallyApply as Fn, comp is_not_none = Fn(operator.is_not, __, None) def set_non_empty(setter_fn, *pairs): ''' @types: (str, V), seq[tuple[str, V]] -> void ''' for attr_name, value in ifilter(comp(is_not_none, second), pairs): setter_fn(attr_name, value)
def get_db2_version_by_home_path(executor, db2_home_path): discover_version = Fn(fptools.safeFunc(__discover_version), executor, fptools._, db2_home_path) return fptools.findFirst(lambda x: x, ifilter(None, imap(discover_version, registry.get_discoverers())))
def get_db2_version(sql_executor): sqlbased_discoverers = registry.get_discoverers() discover_version = Fn(safeFn(__discover_version), sql_executor, fptools._) return findFirst( lambda x: x, ifilter(None, imap(discover_version, sqlbased_discoverers)))
def _report(ip, config, version_info, partition_groups, partitions, pg_names_by_partition_number, buffer_pools_by_partition_nr, tablespaces, partition_numbers_by_pg_name, mountpoint_by_container, containers_by_tablespace_id, partition_nr_by_container, schemas, tables, sessions, inst_name=None, svcename=None, version=None): oshs = [] add_all_to_result = Fn(_add_vector_and_return, oshs, fptools._) add_to_result = Fn(_add_osh_and_return, oshs, fptools._) link_reporter = db2_topology.LinkReporter() report_usage_link = comp(add_to_result, link_reporter.report_usage) report_client_server_link = comp(add_to_result, link_reporter.report_client_server) report_dbclient_link = comp(add_to_result, link_reporter.report_dbclient) host_reporter = db2_host.Reporter(db2_host.Builder()) report_host = comp(add_to_result, modeling.createHostOSH, str) process_reporter = process.Reporter() report_process = Fn(process_reporter.report, fptools._, fptools._, process.ProcessBuilder()) report_process = comp(add_all_to_result, report_process) host_osh = modeling.createOshByCmdbIdString('node', config.host_id) db_osh = modeling.createOshByCmdbIdString('db2_database', config.db_id) oshs.append(host_osh) oshs.append(db_osh) endpoint_osh, oshs_ = _report_ipse(config, ip, svcename, host_osh, db_osh) oshs.extend(oshs_) db2_rs_osh, oshs_ = _report_instance(config, inst_name, version, db_osh, host_osh, endpoint_osh) oshs.extend(oshs_) if version_info: if db2_rs_osh: db2_topology.SoftwareBuilder.updateVersionDescription(db2_rs_osh, str(version_info)) db2_topology.SoftwareBuilder.updateVersionDescription(db_osh, str(version_info)) db2_reporter = db2_topology.Reporter() report_dbschema = Fn(db2_reporter.reportDbSchema, fptools._, db_osh) report_dbschema = comp(add_to_result, report_dbschema) report_datafile = comp(add_all_to_result, db2_reporter.reportDatafile) report_tablespace = comp(add_all_to_result, db2_reporter.reportTablespace) report_table = comp(add_all_to_result, db2_reporter.reportTable) report_partition_group = Fn(db2_reporter.reportPartitionGroup, fptools._, db_osh) report_partition_group = comp(add_to_result, report_partition_group) report_partition = comp(add_all_to_result, db2_reporter.reportPartition) report_buffer_pool = comp(add_all_to_result, Fn(db2_reporter.reportBufferPool, fptools._, db_osh, fptools._,)) file_system_builder = db2_topology.FileSystemBuilder() file_system_reporter = db2_topology.FileSystemReporter(file_system_builder) report_file_system = comp(add_to_result, file_system_reporter.report_file_system) pg_osh_by_pg_name = _report_pgs(partition_groups, report_partition_group) partition_osh_by_number, node_osh_by_partition_nr = _report_partitions(partitions, db2_rs_osh, oshs, pg_names_by_partition_number, pg_osh_by_pg_name, report_partition, host_reporter) bp_osh_by_partition_nr_and_bp_id = _report_bufferpools(buffer_pools_by_partition_nr, partition_osh_by_number, report_buffer_pool) container_oshes_by_tablespace = _report_data_files(mountpoint_by_container, containers_by_tablespace_id, partition_nr_by_container, tablespaces, partition_osh_by_number, node_osh_by_partition_nr, report_datafile, report_file_system) tablespace_osh_by_tablespace = _report_tablespaces(tablespaces, db_osh, pg_osh_by_pg_name, partition_numbers_by_pg_name, bp_osh_by_partition_nr_and_bp_id, container_oshes_by_tablespace, report_tablespace, report_usage_link) schema_osh_by_name = {} for schema in schemas: schema_osh_by_name[schema.name] = report_dbschema(schema) for table, tablespace, schema_name, owner in tables: table_pdo = db2_topology.Builder.TablePdo(table.name, owner) tablespace_osh = tablespace_osh_by_tablespace.get(tablespace) schemaOsh = schema_osh_by_name.get(schema_name) if schemaOsh: report_table(table_pdo, schemaOsh, tablespaceOsh=tablespace_osh) else: logger.warn("Schema '%s' not found, table '%s' is not reported" % (schema_name, table.name)) port = config.port or resolve_svcename_to_port_nr(svcename) _report_sessions(sessions, endpoint_osh, db2_rs_osh, port, report_host, report_process, report_client_server_link, report_dbclient_link) return oshs