Ejemplo n.º 1
0
    def delete_async(self):
        LOG.debug("prepare delete instance %s" % self.id)
        deleted_at = utils.utcnow()
        # Delete guest queue.
        _item = InstanceGroupItem.get_by_instance_id(self.context,self.id)
        group_id = _item.group_id

        if _item.type == DBInstanceType.MASTER:
            standby = None
            try:
                standby = InstanceGroupItem.get_by_gid_type(self.context, group_id,DBInstanceType.STANDBY)
            except exception.ModelNotFoundError:
                pass
            if standby is not None:

                standby.delete()
                standby_inst_id = standby.instance_id
                standby_tasks = self.load(self.context,standby_inst_id)
                standby_tasks.update_db(deleted=True, deleted_at=deleted_at,
                                        task_status=InstanceTasks.NONE)
                standby_tasks.set_servicestatus_deleted()
                standby_tasks._delete_instance_config()

        item_list = InstanceGroupItem.list_by_gid(self.context,group_id)
        if len(item_list)==1:
            # Delete associated security group
            if CONF.trove_security_groups_support:
                try:
                    SecurityGroup.delete_for_group(self.group_id,
                                                   self.context)
                except Exception as e:
                    LOG.error(utils.get_traceback_stack())

            LOG.info("send notify to monitor when delete instance %s" % self.id)
            group_rpcapi.API(self.context).group_update(group_id,notify_when_delete=True)

            LOG.info("Delete group %s" % group_id)
            InstanceGroup.delete(self.context,group_id)

        if _item.type in [DBInstanceType.MASTER,DBInstanceType.SINGLE]:
            try:
                LOG.info("Delete autobackup_setting of group_id %s" % group_id)
                AutoBackup.delete(self.context,group_id)
            except:
                LOG.error(utils.get_traceback_stack())


        _item.delete()
        self.update_db(deleted=True, deleted_at=deleted_at,
                       task_status=InstanceTasks.NONE)
        self.set_servicestatus_deleted()
Ejemplo n.º 2
0
    def upgrade_instance_toha(self, flavor, image_id, databases, users,
                              service_type, volume_size, security_groups, master_id,
                              backup_id):
        #
        # Fore. 2015/07/01. Ugly Code. create_instance is very hard to understand. 
        # 
        self.update_db(task_status=InstanceTasks.UPGRADING)
        master_phyhost = inst_models.Instance.get_pyhhostname(self.context, master_id)
        ignore_hosts = [master_phyhost]

        self.create_instance(flavor, image_id, databases, users, service_type, volume_size,
                             security_groups, backup_id, DBInstanceType.STANDBY,
                             ignore_hosts=ignore_hosts, master_id=master_id)
        
        expired_at = AutoBackup.get_autobackup_expiretime(self.context, self.group_id)
        task_api.KSC_API(self.context).create_autobackup(self.group_id, expired_at)
        self.update_db(task_status=InstanceTasks.NONE)
Ejemplo n.º 3
0
    def _delete_resources(self, fake):
        group_item = InstanceGroupItem.get_by_instance_id(self.context, self.id)
        group_id = group_item.group_id
        inst_type = group_item.type
        instance_id = self.db_info.id

        if self.server and self.db_info.server_status == "ACTIVE":
            # set instance to read only model
            LOG.info("Set readonly for instance %s" % self.id)
            self._set_instance_readonly(instance_id=self.id)
        else:
            LOG.info("vm_status is not ACTIVE for %s" % self.id)

        if inst_type == DBInstanceType.MASTER:
            rrinsts = []
            try:
                standby = InstanceGroupItem.get_by_gid_type(self.context, group_id, DBInstanceType.STANDBY)
                rrinsts = InstanceGroupItem.get_by_gid_type(self.context, group_id, DBInstanceType.READ_REPLI)
                standby_inst_id = standby.instance_id
                LOG.info("MASTER %s,it hava STANDBY %s,RRS %s",
                         (self.id, standby_inst_id, [_inst.instance_id for _inst in rrinsts]))
                InstanceGroupItem.delete(self.context, standby_inst_id)
            except Exception as e:
                LOG.error(utils.get_traceback_stack())

            # waite replication group db sysnc
            if len(rrinsts) > 0:
                self.guest.ksc_set_read_only(True)

            for _inst in rrinsts:
                try:
                    rr_instance = self.load(self.context, _inst.instance_id)
                    rr_instance.waite_rpl_synchronize(time_out=CONF.delete_waite_rplg_sync)
                except Exception as e:
                    LOG.error(utils.get_traceback_stack())

            # delete standby instance
            try:
                try:
                    standby_instance = self.load(self.context, standby_inst_id)
                except exception.UnprocessableEntity:
                    standby_instance = FreshInstanceTasks.load(self.context, standby_inst_id)
                standby_instance.update_db(deleted=True, deleted_at=utils.utcnow(), task_status=InstanceTasks.NONE)
                standby_instance.set_servicestatus_deleted()
                standby_instance._delete_instance_config()

                if standby_instance.server:
                    LOG.info("Delete STANDBY compute server %s" % standby_instance.server.id)
                    standby_instance.get_guest().delete_queue()
                    standby_instance.server.delete()

                    poll_until(standby_instance.server_is_finished, sleep_time=1, time_out=CONF.server_delete_time_out)
                else:
                    LOG.info("standby instance vm_status is not ACTIVE for %s" % standby_inst_id)
            except Exception as e:
                LOG.error(utils.get_traceback_stack())

            if fake is True and self.type == DBInstanceType.MASTER:
                try:
                    LOG.debug("fake is True, %s is MASTER, stop mysqld", self.id)
                    self.guest.ksc_stop_db(do_not_start_on_reboot=True)
                except Exception as e:
                    msg = "fake_delete, instance: %s, stop mysqld error, exception: %s " % (self.id, str(e))
                    LOG.error("%s, %s", msg, utils.get_traceback_stack())
                    AlarmRpc(self.context).alarm(self.tenant_id, level=AlarmRpc.LEVEL_ERROR,
                                                 _type=AlarmRpc.TYPE_TASKMANAGER, message=msg)

        if self.server:
            if fake is True and self.type == DBInstanceType.MASTER:
                LOG.debug("fake is True, %s is MASTER, skip delete server", self.id)
            else:
                try:
                    LOG.info("Delete compute server %s" % self.server.id)
                    guest = self.get_guest()
                    guest.delete_queue()
                    self.server.delete()
                    poll_until(self.server_is_finished, sleep_time=1, time_out=CONF.server_delete_time_out)
                except Exception as e:
                    LOG.error(utils.get_traceback_stack())

        # delete group_item/autobackup_setting/group
        if self.type in [DBInstanceType.MASTER, DBInstanceType.SINGLE]:
            try:
                LOG.info("Delete autobackup_setting of group_id %s" % group_id)
                AutoBackup.delete(self.context, group_id)
            except:
                LOG.error(utils.get_traceback_stack())

        # remove vip.
        if CONF.trove_vip_support and \
                        self.type in [DBInstanceType.MASTER, DBInstanceType.SINGLE, DBInstanceType.READ_REPLI]:
            if fake is True and self.type == DBInstanceType.MASTER:
                LOG.debug("fake is True, %s is MASTER, skip release vip", self.id)
            else:
                try:
                    self.update_db(task_status=InstanceTasks.RELEASE_VIP)
                    LOG.info("release vip for instance %s" % instance_id)
                    if inst_type in [DBInstanceType.MASTER, DBInstanceType.SINGLE]:
                        cur_vip = vipService.InstanceVip.get_by_instance_id(self.context, instance_id)
                        vipService.InstanceVip.release_vip(self.context, cur_vip)
                    elif inst_type in [DBInstanceType.READ_REPLI]:
                        vipService.InstanceVip.deallocate(self.context, instance_id, deleted=False, purge=True)
                except Exception as e:
                    LOG.error(utils.get_traceback_stack())
Ejemplo n.º 4
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'])
Ejemplo n.º 5
0
    def create(self, req, body, tenant_id):
        LOG.debug("Creating a Backup for tenant '%s'" % tenant_id)
        context = req.environ[wsgi.CONTEXT_KEY]
        data = body['backup']
        instance = data.get('instance',None)
        group = data.get('group',None)
        name = data['name']
        type = data.get("type", "snapshot")
        #expire_at = data.get("expire_after", 7)
        desc = data.get('description')
        parent_id = data.get('parent_id')
        LOG.info("parent_id:%s", parent_id)

        if group is None and instance is None:
            raise exception.BadRequest("you must specify group or instance")

        instance_id = None

        if group is not None:
            try:
                instance_id = InstanceGroupItem.get_by_gid_type(context, group, DBInstanceType.STANDBY).instance_id
            except:
                instance_id = InstanceGroupItem.get_by_gid_type(context, group, DBInstanceType.SINGLE).instance_id

        if instance_id is None and instance is not None:
            instance_id = inst_utils.virtual_instid_2_origin_instid(instance)

        _instance = DBInstance.find_by(context,id=instance_id)
        _type = _instance.service_type
        #_image = ServiceImage.find_by(context,service_name=_type)
        #service_image_id = _image.id
        ds,ds_version = ds_models.get_datastore_version(_type)
        service_image_id = ds_version.image_id

        grp_item = InstanceGroupItem.get_by_instance_id(context, _instance.id)
        group_id = grp_item.group_id

        # get this group's autobackup config and set the expire_after default
        _autobackup = AutoBackup.get_by_gid(context, group_id)
        expire_after = data.get("expire_after", _autobackup.expire_after)
        duration = _autobackup.duration
        expire_at = AutoBackup.calculate_expire_at(expire_after, duration)
        LOG.info("group_id %s, expire_at :%s", group_id, time.ctime(expire_at))

        if grp_item.type == DBInstanceType.MASTER:
            try:
                instance_id = InstanceGroupItem.get_by_gid_type(context, group_id, DBInstanceType.STANDBY).instance_id
            except Exception as e:
                LOG.error(e)

        backup = Backup.create(context, instance_id, name, description=desc,group_id=group_id,backup_type=type,expire_at=expire_at,service_image_id=service_image_id,parent_id=parent_id)
        try:
            #service = inst_models.ServiceImage.find_by(id=backup.service_image_id)
            #backup.db_type = service['service_name']
            ds,ds_version = ds_patch_models.find_datastore_by_image_id(backup.service_image_id)
            backup.db_type = ds.name
        except Exception as ex:
            backup.db_type = ""
            LOG.warn("Failed get db type information of backup %s, %s", backup.id, ex)
        chain = self._get_chain_ids(context, id)
        LOG.info(_("chain : '%s'") % chain)
        return wsgi.Result(views.BackupView(backup).data(), 202)