def _getClientConfig(clusterName, configName = None): """ Gets an Arakoon client object for an existing cluster @type clusterName: string @param clusterName: the name of the cluster for which you want to get a client @return arakoon client object """ clientConfig = X.getConfig('/'.join ([X.cfgDir,"arakoonclients"])) if not clientConfig.has_section(clusterName): X.raiseError("No such client configured for cluster [%s]" % clusterName) else: node_dict = {} clientCfg = ArakoonClient._getConfig(clusterName, configName) cfgFile = X.getConfig( clientCfg ) if not cfgFile.has_section("global") : if configName is not None: msg = "Named client '%s' for cluster '%s' does not exist" % (configName, clusterName) else : msg = "No client available for cluster '%s'" % clusterName X.raiseError(msg ) clusterParam = cfgFile.get("global", "cluster") for node in clusterParam.split(",") : node = node.strip() ips = cfgFile.get(node, "ip") ip_list = map(lambda x: x.strip(), ips.split(',')) port = cfgFile.get(node, "client_port") ip_port = (ip_list, port) node_dict.update({node: ip_port}) clusterId = cfgFile.get('global', 'cluster_id') config = X.arakoon_client.ArakoonClientConfig(clusterId, node_dict) return config
def testAddLocalNode(self): cid = self._clusterId cluster = self._getCluster() for i in range(0, 3): cluster.addNode("%s_%s" % (cid, i)) n0 = '%s_0' % cid n1 = '%s_1' % cid cluster.addLocalNode(n1) sn = self.__servernodes() cfgPath = '/'.join( [X.cfgDir, "qconfig", "arakoon", cid, "%s_local_nodes" % cid]) config = X.getConfig(cfgPath) assert_equals(Compat.sectionAsDict(config, "global"), {'cluster': n1}) cluster.addLocalNode(n0) config = X.getConfig(cfgPath) assert_equals(Compat.sectionAsDict(config, "global"), {'cluster': '%s,%s' % (n1, n0)})
def generateFromServerConfig(self): """ Generate the client config file from the servers """ clusterId = self._clusterId fn = '/'.join([X.cfgDir, 'arakoonclusters']) p = X.getConfig(fn) clusterExists = p.has_section(clusterId) if not clusterExists: X.raiseError("No server cluster '%s' is defined." % clusterId) serverConfigDir = p.get(clusterId, "path") serverConfigPath = '/'.join([serverConfigDir, clusterId]) serverConfig = X.getConfig(serverConfigPath) if serverConfig.has_section("global"): nodes = self.__getNodes(serverConfig) for name in nodes: if name in self.getNodes(): self.removeNode(name) ips = serverConfig.get(name, 'ip') ip_list = map(lambda x: x.strip(), ips.split(',')) self.addNode(name, ip_list, serverConfig.get(name, "client_port"))
def _getClientConfig(clusterName, configName=None): """ Gets an Arakoon client object for an existing cluster @type clusterName: string @param clusterName: the name of the cluster for which you want to get a client @return arakoon client object """ clientConfig = X.getConfig('/'.join([X.cfgDir, "arakoonclients"])) if not clientConfig.has_section(clusterName): X.raiseError("No such client configured for cluster [%s]" % clusterName) else: node_dict = {} clientCfg = ArakoonClient._getConfig(clusterName, configName) cfgFile = X.getConfig(clientCfg) if not cfgFile.has_section("global"): if configName is not None: msg = "Named client '%s' for cluster '%s' does not exist" % ( configName, clusterName) else: msg = "No client available for cluster '%s'" % clusterName X.raiseError(msg) clusterParam = cfgFile.get("global", "cluster") for node in clusterParam.split(","): node = node.strip() ips = cfgFile.get(node, "ip") ip_list = map(lambda x: x.strip(), ips.split(',')) port = cfgFile.get(node, "client_port") ip_port = (ip_list, port) node_dict.update({node: ip_port}) clusterId = cfgFile.get('global', 'cluster_id') config = X.arakoon_client.ArakoonClientConfig(clusterId, node_dict) return config
def testNameDiffersFromId(self): name = self._clusterId id_inside = "id_inside" cluster = C._getCluster(name) cfg_name = cluster._getConfigFileName() logging.debug("cfg_name = %s", cfg_name) #/opt/qbase3/cfg//qconfig/arakoon/cluster_name ok = cfg_name.endswith(name) assert_true(ok) cluster.addNode('node_0') cfg = X.getConfig(cfg_name) logging.debug("cfg = %s", X.cfg2str(cfg)) id0 = cfg.get('global', 'cluster_id') assert_equals(id0, name) # now set it to id cfg.set('global', 'cluster_id', id_inside) X.writeConfig(cfg, cfg_name) logging.debug('cfg_after = %s', X.cfg2str(cfg)) cluster = C._getCluster(name) #ccfg = cluster.getClientConfig() #print ccfg client = cluster.getClient() ccfg2 = client._config logging.debug("ccfg2=%s", ccfg2) ccfg_id = ccfg2.getClusterId() assert_equals(ccfg_id, id_inside)
def _getConfig(clusterName, configName): fn = '/'.join([X.cfgDir, 'arakoonclients']) p = X.getConfig(fn) clusterDir = p.get(clusterName, "path") last = None if configName is None: last = "%s_client" % clusterName else: last = "%s_client_%s" % (clusterName, configName) return '/'.join([clusterDir, last])
def regenerateClientConfig( cluster_id ): h = '/'.join([X.cfgDir,'arakoonclients']) p = X.getConfig(h) if cluster_id in p.sections(): clusterDir = p.get(cluster_id, "path") clientCfgFile = '/'.join([clusterDir, "%s_client.cfg" % cluster_id]) if X.fileExists(clientCfgFile): X.removeFile(clientCfgFile) client = ArakoonClient.ArakoonClient() cliCfg = client.getClientConfig( cluster_id ) cliCfg.generateFromServerConfig()
def getClientConfig (self, clusterName, configName = None): """ Adds an Arakoon client to the configuration. @type clusterName: string @param clusterName: the name of the cluster for which you want to add a client @type configName: optional string @param configName: the name of the client configuration for this cluster """ fn = '/'.join([X.cfgDir, 'arakoonclients']) p = X.getConfig(fn) if not p.has_section(clusterName): p.add_section(clusterName) cfgDir = '/'.join([X.cfgDir, "qconfig", "arakoon", clusterName]) p.set(clusterName, "path", cfgDir) X.writeConfig(p, fn) cfgFile = self._getConfig(clusterName, configName) return ArakoonClientExtConfig(clusterName, cfgFile)
def getClientConfig(self, clusterName, configName=None): """ Adds an Arakoon client to the configuration. @type clusterName: string @param clusterName: the name of the cluster for which you want to add a client @type configName: optional string @param configName: the name of the client configuration for this cluster """ fn = '/'.join([X.cfgDir, 'arakoonclients']) p = X.getConfig(fn) if not p.has_section(clusterName): p.add_section(clusterName) cfgDir = '/'.join([X.cfgDir, "qconfig", "arakoon", clusterName]) p.set(clusterName, "path", cfgDir) X.writeConfig(p, fn) cfgFile = self._getConfig(clusterName, configName) return ArakoonClientExtConfig(clusterName, cfgFile)
def getNodes(self): """ Get an object that contains all node information @return dict the dict can be used as param for the ArakoonConfig object """ config = X.getConfig(self._configPath) clientconfig = {} if config.has_section("global"): nodes = self.__getNodes(config) for name in nodes: ips = config.get(name, 'ip') ip_list = map(lambda x: x.strip(), ips.split(',')) clientconfig[name] = (ip_list, config.get(name, "client_port")) return clientconfig
def assertSetting(self, section, option, value): #pylint: disable-msg=C0103 '''Assert some config option has the expected value If `value` is `None`, the option should not be set. :param section: Option section :type section: `str` :param option: Option name :type option: `str` :param value: Expected value, or `None` :type value: `str` ''' fn = self.cluster._getConfigFileName() cfg = X.getConfig(fn) if value is None: self.assertFalse(cfg.has_option(section, option)) else: self.assertEqual(cfg.get(section, option, raw=True), value)
def addNode(self, name, ip, clientPort): """ Add a node to the client configuration @param name: the name of the node @param ip: the ip the node @param clientPort: the port of the node """ self.__validateName(name) if isinstance(ip, basestring): ip = [ip] clusterId = self._clusterId inifile_path = self._configPath config = X.getConfig(inifile_path) if not config.has_section("global"): config.add_section("global") config.set("global", "cluster_id", clusterId) config.set("global", "cluster", "") nodes = self.__getNodes(config) if name in nodes: raise Exception("There is already a node with name %s configured" % name) nodes.append(name) config.add_section(name) config.set(name, "ip", ', '.join(ip)) config.set(name, "client_port", clientPort) config.set("global", "cluster", ",".join(nodes)) X.writeConfig(config, inifile_path)
def addNode(self, name, ip, clientPort): """ Add a node to the client configuration @param name: the name of the node @param ip: the ip the node @param clientPort: the port of the node """ self.__validateName(name) if isinstance(ip, basestring): ip = [ip] clusterId = self._clusterId inifile_path = self._configPath config = X.getConfig(inifile_path) if not config.has_section("global"): config.add_section("global") config.set("global", "cluster_id", clusterId) config.set("global","cluster", "") nodes = self.__getNodes(config) if name in nodes: raise Exception("There is already a node with name %s configured" % name) nodes.append(name) config.add_section(name) config.set(name, "ip", ', '.join(ip)) config.set(name, "client_port", clientPort) config.set("global","cluster", ",".join(nodes)) X.writeConfig(config,inifile_path)
def listClients(self): """ Returns a list with the existing clients. """ config = X.getConfig("arakoonclients") return config.sections()
def getConfigLocation(clusterId): cfg = X.getConfig("arakoonclusters") if cfg.has_key(clusterId): return "%s/%s.cfg" % (cfg[clusterId]["path"], clusterId) else: raise RuntimeError("Unknown nursery cluster %s" % clusterId)