def _checkServiceURL(self, serviceName, options): """Ensure service URL is properly configured in the CS.""" url = self._getURL(serviceName, options) system = options["System"] module = options["Module"] self.log.info("Checking URLs for %s/%s" % (system, module)) urlsConfigPath = Path.cfgPath( PathFinder.getSystemURLSection(system=system, setup=self.setup), module) urls = gConfig.getValue(urlsConfigPath, []) self.log.debug("Found configured URLs for %s: %s" % (module, urls)) self.log.debug("This URL is %s" % url) runitStatus = options["RunitStatus"] wouldHave = "Would have " if not self.commitURLs else "" if runitStatus == "Run" and url not in urls: urls.append(url) message = "%sAdded URL %s to URLs for %s/%s" % (wouldHave, url, system, module) self.log.info(message) self.accounting[serviceName + "/URL"]["Treatment"] = message self.csAPI.modifyValue(urlsConfigPath, ",".join(urls)) if runitStatus == "Down" and url in urls: urls.remove(url) message = "%sRemoved URL %s from URLs for %s/%s" % (wouldHave, url, system, module) self.log.info(message) self.accounting[serviceName + "/URL"]["Treatment"] = message self.csAPI.modifyValue(urlsConfigPath, ",".join(urls))
def getComponentSection(system, component=False, setup=False, componentCategory="Services"): """Function returns the path to the component. :param str system: system name or component name prefixed by the system in which it is placed. e.g. 'WorkloadManagement/SandboxStoreHandler' :param str component: component name, e.g. 'SandboxStoreHandler' :param str setup: Name of the setup. :param str componentCategory: Category of the component, it can be: 'Agents', 'Services', 'Executors' or 'Databases'. :return: Complete path to the component :rtype: str :raise RuntimeException: If in the system - the system part does not correspond to any known system in DIRAC. Examples: getComponentSection('WorkloadManagement/SandboxStoreHandler', setup='Production', componentCategory='Services') getComponentSection('WorkloadManagement', 'SandboxStoreHandler', 'Production') """ system, component = divideFullName(system, component) return Path.cfgPath(getSystemSection(system, setup=setup), componentCategory, component)
def getSystemURLSection(system, setup=False): """Get URLs section in a system :param str system: system name :param str setup: setup name :return: str """ return Path.cfgPath(getSystemSection(system, setup=setup), "URLs")
def _getComponentOption(self, instanceType, system, componentName, option, default): """Get component option from DIRAC CS, using components' base classes methods.""" componentPath = PathFinder.getComponentSection( system=system, component=componentName, setup=self.setup, componentCategory=instanceType, ) if instanceType != "Agents": return gConfig.getValue(Path.cfgPath(componentPath, option), default) # deal with agent configuration componentLoadModule = gConfig.getValue( Path.cfgPath(componentPath, "Module"), componentName) fullComponentName = Path.cfgPath(system, componentName) fullComponentLoadName = Path.cfgPath(system, componentLoadModule) return AgentModule(fullComponentName, fullComponentLoadName).am_getOption( option, default)
def getSystemInstance(system, setup=False): """Find system instance name :param str system: system name :param str setup: setup name :return: str """ optionPath = Path.cfgPath("/DIRAC/Setups", setup or getDIRACSetup(), system) instance = gConfigurationData.extractOptionFromCFG(optionPath) if not instance: raise RuntimeError("Option %s is not defined" % optionPath) return instance
def _getDefaultComponentStatus(self): """Get the configured status of the components.""" host = socket.getfqdn() defaultStatus = {"Down": set(), "Run": set(), "All": set()} resRunning = gConfig.getOptionsDict( Path.cfgPath("/Registry/Hosts/", host, "Running")) resStopped = gConfig.getOptionsDict( Path.cfgPath("/Registry/Hosts/", host, "Stopped")) if not resRunning["OK"]: return resRunning if not resStopped["OK"]: return resStopped defaultStatus["Run"] = set(resRunning["Value"]) defaultStatus["Down"] = set(resStopped["Value"]) defaultStatus["All"] = defaultStatus["Run"] | defaultStatus["Down"] if defaultStatus["Run"].intersection(defaultStatus["Down"]): self.logError( "Overlap in configuration", str(defaultStatus["Run"].intersection(defaultStatus["Down"]))) return S_ERROR("Bad host configuration") return S_OK(defaultStatus)
def getSystemSection(system, instance=False, setup=False): """Get system section :param str system: system name :param str instance: instance name :param str setup: setup name :return: str -- system section path """ system, _ = divideFullName(system, "_") # for backward compatibility return Path.cfgPath( "/Systems", system, instance or getSystemInstance(system, setup=setup), )
def checkURLs(self): """Ensure that the running services have their URL in the Config.""" self.log.info("Checking URLs") # get services again, in case they were started/stop in controlComponents gConfig.forceRefresh(fromMaster=True) # get port used for https based services try: tornadoSystemInstance = PathFinder.getSystemInstance( system="Tornado", setup=self.setup, ) self._tornadoPort = gConfig.getValue( Path.cfgPath("/System/Tornado/", tornadoSystemInstance, "Port"), self._tornadoPort, ) except RuntimeError: pass self.log.debug("Using Tornado Port:", self._tornadoPort) res = self.getRunningInstances(instanceType="Services", runitStatus="All") if not res["OK"]: return S_ERROR("Failure to get running services") self.services = res["Value"] for service, options in sorted(self.services.items()): self.log.debug("Checking URL for %s with options %s" % (service, options)) # ignore SystemAdministrator, does not have URLs if "SystemAdministrator" in service: continue self._checkServiceURL(service, options) if self.csAPI.csModified and self.commitURLs: self.log.info("Commiting changes to the CS") result = self.csAPI.commit() if not result["OK"]: self.logError("Commit to CS failed", result["Message"]) return S_ERROR("Failed to commit to CS") return S_OK()