def __test_create_configuration(self): c = Configuration.create(self.conf_name, self.conf_desc, self.tanant_id, self.datastore, self.ds_version.id) self.configuration = c self.assertTrue(c != None and isinstance(c, DBConfiguration)) defaultConfig = Configuration.create(self.conf_name, self.conf_desc, self.default_tanant_id, self.datastore, self.ds_version.id) self.defaultConfiguration = defaultConfig self.assertTrue(defaultConfig != None and isinstance(defaultConfig, DBConfiguration)) def _error_no_version(): Configuration.create(self.conf_name, self.conf_desc, self.tanant_id, self.datastore, None) def _error_no_conf_name(): Configuration.create(None, self.conf_desc, self.tanant_id, self.datastore, self.ds_version.id) def _error_no_tanant_id(): Configuration.create(self.conf_name, self.conf_desc, None, self.datastore, self.ds_version.id) def _error_provide_error_config_type(): Configuration.create(self.conf_name, self.conf_desc, self.tanant_id, self.datastore, self.ds_version.id, None) self.assertRaises(Exception, _error_no_version) self.assertRaises(Exception, _error_no_conf_name) self.assertRaises(Exception, _error_no_tanant_id) self.assertRaises(Exception, _error_provide_error_config_type)
def unassign_configuration(self): LOG.debug("Unassigning the configuration from the instance %s.", self.id) if self.configuration and self.configuration.id: LOG.debug("Unassigning the configuration id %s.", self.configuration.id) self.guest.update_overrides({}, remove=True) # Dynamically reset the configuration values back to their default # values from the configuration template. # Reset the values only if the default is available for all of # them and restart is not required by any. # Mark the instance with a 'RESTART_REQUIRED' status otherwise. flavor = self.get_flavor() default_config = self._render_config_dict(flavor) current_config = Configuration(self.context, self.configuration.id) current_overrides = current_config.get_configuration_overrides() # Check the configuration template has defaults for all modified # values. has_defaults_for_all = all(key in default_config.keys() for key in current_overrides.keys()) if not current_config.does_configuration_need_restart() and has_defaults_for_all: self.guest.apply_overrides({k: v for k, v in default_config.items() if k in current_overrides}) else: LOG.debug("Could not revert all configuration changes dynamically. " "A restart will be required.") self.update_db(task_status=InstanceTasks.RESTART_REQUIRED) else: LOG.debug("No configuration found on instance. Skipping.")
def test_create_instance_config_from_tempalte(self): self.__test_create_configuration() old_config_items = Configuration.load_items(self.context, self.configuration.id) valid_values = {"character_set_connection" : 'utf-8', "innodb_buffer_pool_size" : 1000} items = Configuration.create_items(self.configuration.id, valid_values) new_config_items = Configuration.load_items(self.context, self.configuration.id) instance_config = Configuration.ksc_create_instance_config_from_template(self.context,self.configuration.id) print instance_config
def __test_admin_update_default_config(self): self.defaultConfiguration.name = 'admin_update_default_config_name' items = Configuration.load_items(self.context, self.defaultConfiguration.id) items.append(ConfigurationParameter(configuration_id=self.defaultConfiguration.id, configuration_key='some_key', configuration_value='some_value', deleted=False)) self.context.is_admin = True Configuration.save(self.context, self.defaultConfiguration, items)
def _create_resources(): if cluster_config: cluster_id = cluster_config.get("id", None) shard_id = cluster_config.get("shard_id", None) instance_type = cluster_config.get("instance_type", None) else: cluster_id = shard_id = instance_type = None db_info = DBInstance.create(name=name, flavor_id=flavor_id, tenant_id=context.tenant, volume_size=volume_size, datastore_version_id= datastore_version.id, task_status=InstanceTasks.BUILDING, configuration_id=configuration_id, slave_of_id=slave_of_id, cluster_id=cluster_id, shard_id=shard_id, type=instance_type) LOG.debug("Tenant %(tenant)s created new Trove instance %(db)s." % {'tenant': context.tenant, 'db': db_info.id}) # if a configuration group is associated with an instance, # generate an overrides dict to pass into the instance creation # method config = Configuration(context, configuration_id) overrides = config.get_configuration_overrides() service_status = InstanceServiceStatus.create( instance_id=db_info.id, status=tr_instance.ServiceStatuses.NEW) if CONF.trove_dns_support: dns_client = create_dns_client(context) hostname = dns_client.determine_hostname(db_info.id) db_info.hostname = hostname db_info.save() root_password = None if cls.get_root_on_create( datastore_version.manager) and not backup_id: root_password = utils.generate_random_password() task_api.API(context).create_instance(db_info.id, name, flavor, image_id, databases, users, datastore_version.manager, datastore_version.packages, volume_size, backup_id, availability_zone, root_password, nics, overrides, slave_of_id, cluster_config) return SimpleInstance(context, db_info, service_status, root_password)
def tearDown(self): self.context.is_admin = False Configuration.delete(self.context, self.configuration) self.assertTrue(self.configuration.deleted == True) def _error_not_admin_delete_default_template(): Configuration.delete(self.context, self.defaultConfiguration) self.assertRaises(Exception, _error_not_admin_delete_default_template) self.context.is_admin = True Configuration.delete(self.context, self.defaultConfiguration) self.assertTrue(self.defaultConfiguration.deleted == True)
def test_load(self): self.__test_create_configuration() conf = Configuration.load(self.context, self.configuration.id) self.assertTrue(self.configuration.name == conf.name) defaultConf = Configuration.load(self.context, self.defaultConfiguration.id) self.assertTrue(self.defaultConfiguration.name == defaultConf.name) def _error_not_found(): Configuration.load(self.context, "sldkfjasdlkfjsdlkf") self.assertRaises(ModelNotFoundError, _error_not_found)
def test_load_default_templates(self): self.tanant_id = CONF.default_template_tenant_id self.context = TroveContext(tenant = self.tanant_id) self.__test_create_configuration() #old_config_items = Configuration.load_items(self.conext, self.configuration.id) valid_values = {"character_set_connection" : 'utf-8', "innodb_buffer_pool_size" : 1000} items = Configuration.create_items(self.configuration.id, valid_values) #new_config_items = Configuration.load_items(self.conext, self.configuration.id) configs, marker = Configurations.load_default_templates(self.context) print configs, marker
def assign_configuration(self, configuration_id): self._validate_can_perform_assign() try: configuration = Configuration.load(self.context, configuration_id) except exception.ModelNotFoundError: raise exception.NotFound( message='Configuration group id: %s could not be found.' % configuration_id) config_ds_v = configuration.datastore_version_id inst_ds_v = self.db_info.datastore_version_id if (config_ds_v != inst_ds_v): raise exception.ConfigurationDatastoreNotMatchInstance( config_datastore_version=config_ds_v, instance_datastore_version=inst_ds_v) config = Configuration(self.context, configuration.id) LOG.debug("Config config is %s.", config) self.update_db(configuration_id=configuration.id) self.update_overrides(config)
def test_configuration_parameters(self): self.__test_create_configuration() self.assertRaises(Exception, self.__test_not_admin_update_default_config) self.__test_admin_update_default_config() old_config_items = Configuration.load_items(self.context, self.configuration.id) self.assertTrue(old_config_items != None and len(old_config_items) == 0) print old_config_items valid_values = {"character_set_connection" : 'utf-8', "innodb_buffer_pool_size" : 1000} items = Configuration.create_items(self.configuration.id, valid_values) self.assertTrue(items != None and len(items) == len(valid_values), items) new_config_items = Configuration.load_items(self.context, self.configuration.id) self.assertTrue(new_config_items != None and len(new_config_items) == 2) print new_config_items overrides = Configuration.get_configuration_overrides(self.context, self.configuration.id) self.assertTrue(valid_values == overrides, overrides) print valid_values, overrides
def assign_configuration(self, configuration_id): try: configuration = Configuration.load(self.context, configuration_id) except exception.ModelNotFoundError: raise exception.NotFound( message='Configuration group id: %s could not be found' % configuration_id) config_ds_v = configuration.datastore_version_id inst_ds_v = self.db_info.datastore_version_id if (config_ds_v != inst_ds_v): raise exception.ConfigurationDatastoreNotMatchInstance( config_datastore_version=config_ds_v, instance_datastore_version=inst_ds_v) overrides = Configuration.get_configuration_overrides( self.context, configuration.id) LOG.info(overrides) self.update_overrides(overrides) self.update_db(configuration_id=configuration.id)
def assign_configuration(self, configuration_id): self._validate_can_perform_assign() try: configuration = Configuration.load(self.context, configuration_id) except exception.ModelNotFoundError: raise exception.NotFound(message="Configuration group id: %s could not be found." % configuration_id) config_ds_v = configuration.datastore_version_id inst_ds_v = self.db_info.datastore_version_id if config_ds_v != inst_ds_v: raise exception.ConfigurationDatastoreNotMatchInstance( config_datastore_version=config_ds_v, instance_datastore_version=inst_ds_v ) config = Configuration(self.context, configuration.id) LOG.debug("Config config is %s." % config) self.update_db(configuration_id=configuration.id) self.update_overrides(config)
def unassign_configuration(self, flavor, configuration_id): LOG.debug("Unassigning the configuration from the instance %s" % self.id) LOG.debug("Unassigning the configuration id %s" % self.configuration.id) def _find_item(items, item_name): LOG.debug("items: %s" % items) LOG.debug("item_name: %s" % item_name) # find the item in the list for i in items: if i[0] == item_name: return i def _convert_value(value): # split the value and the size e.g. 512M=['512','M'] pattern = re.compile("(\d+)(\w+)") split = pattern.findall(value) if len(split) < 2: return value digits, size = split conversions = {"K": 1024, "M": 1024 ** 2, "G": 1024 ** 3} return str(int(digits) * conversions[size]) default_config = self._render_config_dict(flavor) args = {"ds_manager": self.ds_version.manager, "config": default_config} LOG.debug("default %(ds_manager)s section: %(config)s" % args) LOG.debug("self.configuration: %s" % self.configuration.__dict__) overrides = {} config_items = Configuration.load_items(self.context, configuration_id) for item in config_items: LOG.debug("finding item(%s)" % item.__dict__) try: key, val = _find_item(default_config, item.configuration_key) except TypeError: val = None restart_required = inst_models.InstanceTasks.RESTART_REQUIRED self.update_db(task_status=restart_required) if val: overrides[item.configuration_key] = _convert_value(val) LOG.debug("setting the default variables in dict: %s" % overrides) self.update_overrides(overrides, remove=True) self.update_db(configuration_id=None)
def _create_resources(): if cluster_config: cluster_id = cluster_config.get("id", None) shard_id = cluster_config.get("shard_id", None) instance_type = cluster_config.get("instance_type", None) else: cluster_id = shard_id = instance_type = None ids = [] names = [] root_passwords = [] root_password = None for instance_index in range(0, instance_count): db_info = DBInstance.create( name=name, flavor_id=flavor_id, tenant_id=context.tenant, volume_size=volume_size, datastore_version_id=datastore_version.id, task_status=InstanceTasks.BUILDING, configuration_id=configuration_id, slave_of_id=slave_of_id, cluster_id=cluster_id, shard_id=shard_id, type=instance_type) LOG.debug("Tenant %(tenant)s created new Trove instance " "%(db)s.", {'tenant': context.tenant, 'db': db_info.id}) instance_id = db_info.id instance_name = name ids.append(instance_id) names.append(instance_name) root_passwords.append(None) # change the name to be name + replica_number if more than one if multi_replica: replica_number = instance_index + 1 names[instance_index] += '-' + str(replica_number) setattr(db_info, 'name', names[instance_index]) db_info.save() # if a configuration group is associated with an instance, # generate an overrides dict to pass into the instance creation # method config = Configuration(context, configuration_id) overrides = config.get_configuration_overrides() service_status = InstanceServiceStatus.create( instance_id=instance_id, status=tr_instance.ServiceStatuses.NEW) if CONF.trove_dns_support: dns_client = create_dns_client(context) hostname = dns_client.determine_hostname(instance_id) db_info.hostname = hostname db_info.save() if cls.get_root_on_create( datastore_version.manager) and not backup_id: root_password = utils.generate_random_password() root_passwords[instance_index] = root_password if instance_count > 1: instance_id = ids instance_name = names root_password = root_passwords task_api.API(context).create_instance( instance_id, instance_name, flavor, image_id, databases, users, datastore_version.manager, datastore_version.packages, volume_size, backup_id, availability_zone, root_password, nics, overrides, slave_of_id, cluster_config) return SimpleInstance(context, db_info, service_status, root_password)
def configuration(self): if self.db_info.configuration_id is not None: return Configuration.load(self.context, self.db_info.configuration_id)
def _error_not_admin_delete_default_template(): Configuration.delete(self.context, self.defaultConfiguration)
def _error_no_version(): Configuration.create(self.conf_name, self.conf_desc, self.tanant_id, self.datastore, None)
def _error_no_tanant_id(): Configuration.create(self.conf_name, self.conf_desc, None, self.datastore, self.ds_version.id)
def _error_provide_error_config_type(): Configuration.create(self.conf_name, self.conf_desc, self.tanant_id, self.datastore, self.ds_version.id, None)
def _error_not_found(): Configuration.load(self.context, "sldkfjasdlkfjsdlkf")
def __test_not_admin_update_default_config(self): self.defaultConfiguration.name = 'update_default_config_name' old_config_items = Configuration.load_items(self.context, self.defaultConfiguration.id) Configuration.save(self.context, self.defaultConfiguration, old_config_items)