Ejemplo n.º 1
0
    def __check_config_is_right(self):
        config = KSC_Configuration.load(utils.get_context(), self.config.id)
        if not config:
            raise Exception("create config, not write in db")
        
        if self.name != config.name or self.description != config.description:
            raise Exception("create config, name or description bad value")
        
        if self.config.config_type != '1':
            raise Exception("create config, config_type bad value")
        
        overrides = KSC_Configuration.get_configuration_overrides(utils.get_context(), self.config.id)
        
        diffs = {}
        for k, v in self.values.iteritems():
            if overrides[k] != v and float(overrides[k]) != float(v):                
                diffs[k] = (v, overrides[k])
        if len(diffs):
            raise Exception("create config, values error")
            
#         
#         if overrides != self.values:
#             raise Exception("create config, values error")
        
        datastore_version = DatastoreVersion.load_by_uuid(self.config.datastore_version_id)
        if not datastore_version:
            raise Exception("create config, bad value of datastore_version_id")
        
        if datastore_version.name != self.datastore_version:
            raise Exception("create config, bad value of datastore_version_id")
Ejemplo n.º 2
0
    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 = KSC_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)
        config_group = KSC_Configuration.load(self.context , configuration_id)
        KSC_Configuration.delete(self.context, config_group)
Ejemplo n.º 3
0
    def update(self, req, body, tenant_id, id):
        LOG.info(_("Updating configuration for tenant id %s") % tenant_id)
        context = req.environ[wsgi.CONTEXT_KEY]
        group = models.Configuration.load(context, id)
        datastore_args = body["configuration"].get("datastore", {})
        datastore, datastore_version = ds_models.get_datastore_version(**datastore_args)
        instances = instances_models.DBInstance.find_all(
            tenant_id=context.tenant, configuration_id=id, deleted=False
        ).all()

        # if name/description are provided in the request body, update the
        # model with these values as well.
        if "name" in body["configuration"]:
            group.name = body["configuration"]["name"]

        if "description" in body["configuration"]:
            group.description = body["configuration"]["description"]

        """validate if params are valid"""
        is_configuration_valid = False

        validation_result = KSC_ConfigurationsController.validate_configuration(
            context, body["configuration"]["values"], datastore_manager=datastore_version.manager
        )

        if instances:
            current_config = KSC_Configuration.load(context, id)
            validation_dynamic_result = KSC_Configuration.validate_dynamic_params(
                context, current_config, instances, body["configuration"]["values"], dynamic_param=True
            )
            if validation_result or validation_dynamic_result:
                if validation_dynamic_result:
                    validation_result.update(validation_dynamic_result)
            elif not validation_result and not validation_dynamic_result:
                is_configuration_valid = True
        else:
            if not validation_result:
                is_configuration_valid = True

        result = {"is_validate_pass": is_configuration_valid, "validation_result": validation_result}

        if validation_result:
            return wsgi.Result(result, 400)
        else:
            deleted_at = datetime.utcnow()
            models.Configuration.remove_all_items(context, group.id, deleted_at)
            LOG.info(_("loaded configuration instances: %s") % instances)
            items = self.configuration_items_list(group, body["configuration"])
            models.Configuration.save(context, group, items, instances)

            result["is_validate_pass"] = is_configuration_valid
            return wsgi.Result(result, 202)
Ejemplo n.º 4
0
 def update(self):
     self.create()
     configuration_id = self.config.id
     values = {"myisam_sort_buffer_size":115343360,"join_buffer_size":307200,'sort_buffer_size':327680}
     valuesJson = json.dumps(values)
     name = 'update_%s' % self.config.name
     description = 'update_%s' % self.config.description
     utils.configuration_update_byclient(configuration_id, valuesJson, name, description)
     config = KSC_Configuration.load(utils.get_context(), configuration_id)
     if config == self.config:
         raise Exception("update config, something is not update, config = %s" % config)
     
     override = KSC_Configuration.get_configuration_overrides(utils.get_context(), configuration_id)
     if values == override:
         raise Exception("update config, something is not update, config override = %s" % override)
Ejemplo n.º 5
0
 def patch(self):
     if not self.config:
         self.create()
     configuration_id = self.config.id
     values = json.dumps({"myisam_sort_buffer_size":116391936,"join_buffer_size":409600})
     utils.configuration_patch_byclient(configuration_id, values)
     override = KSC_Configuration.get_configuration_overrides(utils.get_context(), configuration_id)
     if values == override:
         raise Exception("patch config, something is not update,  override = %s" % override)
Ejemplo n.º 6
0
    def edit(self, req, body, tenant_id, id):
        context = req.environ[wsgi.CONTEXT_KEY]
        group = models.Configuration.load(context, id)
        datastore_args = body["configuration"].get("datastore", {})
        datastore, datastore_version = ds_models.get_datastore_version(**datastore_args)
        instances = instances_models.DBInstance.find_all(
            tenant_id=context.tenant, configuration_id=id, deleted=False
        ).all()
        LOG.info(_("loaded configuration instances: %s") % instances)

        """validate if params are valid"""
        is_configuration_valid = False

        validation_result = KSC_ConfigurationsController.validate_configuration(
            context, body["configuration"]["values"], datastore_manager=datastore_version.manager
        )

        if instances:
            current_config = KSC_Configuration.load(context, id)
            validation_dynamic_result = (
                KSC_Configuration.validate_dynamic_params(
                    context, current_config, instances, body["configuration"]["values"], dynamic_param=True
                )
                or {}
            )
            if validation_result or validation_dynamic_result:
                validation_result.update(validation_dynamic_result)
            elif not validation_result and not validation_dynamic_result:
                is_configuration_valid = True
        else:
            if not validation_result:
                is_configuration_valid = True

        result = {"is_validate_pass": is_configuration_valid, "validation_result": validation_result}

        if validation_result:
            return wsgi.Result(result, 400)
        else:
            items = self.configuration_items_list(group, body["configuration"])
            models.Configuration.save(context, group, items, instances)

            result["is_validate_pass"] = is_configuration_valid

            return wsgi.Result(result, 202)
Ejemplo n.º 7
0
 def delete(self):
     self.create()
     #config_type = "1"
     #config = utils.get_config_bytenant(config_type)
     config = self.config
     utils.configuration_delete_byclient(config.id)
     is_OK = 0
     #check ...
     try:
         config = KSC_Configuration.load(utils.get_context(), config.id)
     except Exception as e:
         is_OK = 1
             
     try:
         override = KSC_Configuration.get_configuration_overrides(utils.get_context(), config.id)
     except Exception as e1:
         is_OK = is_OK + 1
     
     if is_OK != 2:
         raise Exception("delete config, no delete success..")
Ejemplo n.º 8
0
    def patch_new(self):
        if not self.config:
            self.create_new()
        configuration_id = self.config.id
        values = {"myisam_sort_buffer_size":'xxx',"join_buffer_size":'xxx'}
#         values = json.dumps({"myisam_sort_buffer_size":116391936,"join_buffer_size":409600})
        utils.configuration_patch_byclient(configuration_id, json.dumps(values))
        override = KSC_Configuration.get_configuration_overrides(utils.get_context(), configuration_id)
        for k, v in values.iteritems():
            if override.get(k) != v:
        
#         if values == override:
                raise Exception("patch config, something is not update,  override = %s" % override)        
Ejemplo n.º 9
0
    def create_instance(self, flavor, image_id, databases, users,
                        service_type, volume_size, security_groups,
                        backup_id, instance_type, ignore_hosts=None,
                        master_id=None, extend=None):

        if instance_type==DBInstanceType.STANDBY or instance_type==DBInstanceType.READ_REPLI:
            if master_id is None:
                raise Exception("when instance_type is STANDBY or RR, The master_id can't none")

        availability_zone=None
        overrides = {}
        if extend != None:
            availability_zone=extend.get('availability_zone', None)
            ds_version_id = extend.get('datastore_version_id', None)
            overrides = extend.get('overrides', {})

        self.update_db(task_status=InstanceTasks.BUILDING_SERVER)
        try:
            server, volume_info = self._create_server_volume_individually(
                flavor,
                image_id,
                security_groups,
                service_type,
                ignore_hosts, availability_zone)
        except Exception as e:
            self.set_servicestatus(ServiceStatuses.UNKNOWN)
            raise e

        try:
            configuration_id =self.db_info.configuration_id
            LOG.debug("Prepare task  instance id = %s,  configuration id =%s" % (self.id, configuration_id))
            overrides = KSC_Configuration.get_configuration_overrides(self.context, configuration_id)
        except Exception:
            pass

        self.update_db(task_status=InstanceTasks.GUEST_PREPARE)
        LOG.info("======= > groupid %s" % self.group_id)
        group = InstanceGroup.get_by_groupid(self.context, self.group_id)
        sys_variables = {"port" : group.db_port, "read_only" : "OFF" if instance_type in (DBInstanceType.MASTER, DBInstanceType.SINGLE) else "ON"}
        overrides.update(sys_variables)
        config = self._render_config(flavor)
        overrides_config = self._render_override_config(flavor, overrides)

        self._guest_prepare(server, flavor['ram'], volume_info,
                            databases, users, backup_id,
                            config.config_contents,
                            overrides_contents = overrides_config.config_contents)

        try:
            utils.poll_until(self._service_is_active,
                             sleep_time=USAGE_SLEEP_TIME,
                             time_out=USAGE_TIMEOUT)
        except Exception as e:
            self.set_servicestatus(ServiceStatuses.UNKNOWN)
            raise e

        group_item = InstanceGroupItem.get_by_instance_id(self.context, self.id)
        group_id = group_item.group_id

        self.update_db(task_status=InstanceTasks.CONFIG_MYSQL)
        if instance_type==DBInstanceType.STANDBY or instance_type==DBInstanceType.READ_REPLI:
            if instance_type==DBInstanceType.STANDBY:
                self._update_instance_type(master_id, DBInstanceType.MASTER)

            self._update_instance_type(self.id, instance_type)
            self._relocate_master(master_id,self.id,backup_id)

            #when upgrade single to ha
            if instance_type == DBInstanceType.STANDBY:
                master_group_item = InstanceGroupItem.get_by_instance_id(self.context,master_id)
                InstanceGroupItem.update_type(self.context, item_id=master_group_item.id, type=DBInstanceType.MASTER)

        if instance_type==DBInstanceType.SINGLE or instance_type==DBInstanceType.MASTER:
            self._create_master_user(instance_id=self.id, user=extend.get('admin_user'), \
                                     password=extend.get('admin_password'))

            self._update_instance_type(self.id, instance_type)


        if instance_type==DBInstanceType.SINGLE or instance_type==DBInstanceType.MASTER:
            try:
                self.update_db(task_status=InstanceTasks.BACKUPING)
                expiretime = AutoBackup.get_autobackup_expiretime(self.context, group_id)
                backup_name = self._backup_name(instance_id=self.id)


                # Fore. 2014/07/02 Get Service Image ID from datastore version 
                #                 _type = self.service_type
                #                 _image = ServiceImage.find_by(self.context,service_name=_type)
                #ds, ds_version = ds_models.get_datastore_version(type = None, version = ds_version_id)
                #service_image_id = image_id

                desc = 'Init backup for new instance'
                Backup.create(context=self.context, instance=self.id,
                              name=backup_name, description=desc, group_id=group_id,
                              backup_type=BackupType.AUTOBACKUP, expire_at=expiretime,init=True,service_image_id=image_id)
            except Exception as e:
                msg = "Error creating backup for instance %s %s" % (self.id,utils.get_traceback_stack())
                LOG.error(msg)

        self.update_db(task_status=InstanceTasks.SETIOTUNE)
        LOG.debug("Set block iotune for instance %s ." % self.id)

        self._set_blkiotune(flavor)

        if instance_type!=DBInstanceType.STANDBY:
            if CONF.trove_vip_support:
                self.update_db(task_status=InstanceTasks.ALLOCATE_VIP)
                try:
                    rip = self._get_instance_ip(self.id)
                    vip = InstanceVip.allocate(self.context,instance_id=self.id,rip = rip)
                    LOG.debug("Allocated vip %s for instance %s"%(vip, self.id))
                except Exception as e:
                    self.set_servicestatus(ServiceStatuses.UNKNOWN)
                    raise e

        self.update_db(deleted=False,task_status=InstanceTasks.NONE)

        LOG.info("create instance_id:%s,notify monitor,autobackup" % self.id)
        group_rpcapi.API(self.context).group_update(group_id)
        self.send_usage_event('create', instance_size=flavor['ram'])