dbinstance_recovery_task.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package dbinstance
  15. import (
  16. "context"
  17. "time"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. api "yunion.io/x/onecloud/pkg/apis/compute"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  25. "yunion.io/x/onecloud/pkg/compute/models"
  26. "yunion.io/x/onecloud/pkg/util/logclient"
  27. )
  28. type DBInstanceRecoveryTask struct {
  29. taskman.STask
  30. }
  31. func init() {
  32. taskman.RegisterTask(DBInstanceRecoveryTask{})
  33. }
  34. func (self *DBInstanceRecoveryTask) taskFailed(ctx context.Context, instance *models.SDBInstance, err error) {
  35. instance.SetStatus(ctx, self.UserCred, api.DBINSTANCE_RESTORE_FAILED, err.Error())
  36. db.OpsLog.LogEvent(instance, db.ACT_RESTORE, err, self.GetUserCred())
  37. logclient.AddActionLogWithStartable(self, instance, logclient.ACT_RESTORE, err, self.UserCred, false)
  38. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  39. }
  40. func (self *DBInstanceRecoveryTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  41. instance := obj.(*models.SDBInstance)
  42. iRds, err := instance.GetIDBInstance(ctx)
  43. if err != nil {
  44. self.taskFailed(ctx, instance, errors.Wrap(err, "instance.GetIDBInstance"))
  45. return
  46. }
  47. input := &api.SDBInstanceRecoveryConfigInput{}
  48. err = self.GetParams().Unmarshal(input)
  49. if err != nil {
  50. self.taskFailed(ctx, instance, errors.Wrap(err, "self.GetParams().Unmarshal(input)"))
  51. return
  52. }
  53. _backup, err := models.DBInstanceBackupManager.FetchById(input.DBInstancebackupId)
  54. if err != nil {
  55. self.taskFailed(ctx, instance, errors.Wrapf(err, "instance.GetDBInstanceBackup(%s)", input.DBInstancebackupId))
  56. return
  57. }
  58. backup := _backup.(*models.SDBInstanceBackup)
  59. origin, _ := backup.GetDBInstance()
  60. conf := &cloudprovider.SDBInstanceRecoveryConfig{
  61. BackupId: backup.ExternalId,
  62. Databases: input.Databases,
  63. }
  64. if origin != nil {
  65. conf.OriginDBInstanceExternalId = origin.ExternalId
  66. }
  67. err = iRds.RecoveryFromBackup(conf)
  68. if err != nil {
  69. self.taskFailed(ctx, instance, errors.Wrap(err, "iRds.RecoveryFromBackup"))
  70. return
  71. }
  72. err = cloudprovider.WaitStatus(iRds, api.DBINSTANCE_RUNNING, time.Second*10, time.Minute*40)
  73. if err != nil {
  74. self.taskFailed(ctx, instance, errors.Wrap(err, "cloudprovider.WaitStatus(running)"))
  75. return
  76. }
  77. databases, err := iRds.GetIDBInstanceDatabases()
  78. if err != nil {
  79. log.Errorf("failed to get dbinstance %s database error: %v", instance.Name, err)
  80. } else {
  81. models.DBInstanceDatabaseManager.SyncDBInstanceDatabases(ctx, self.UserCred, instance, databases)
  82. }
  83. db.OpsLog.LogEvent(instance, db.ACT_RESTORE, nil, self.GetUserCred())
  84. logclient.AddActionLogWithStartable(self, instance, logclient.ACT_RESTORE, backup, self.UserCred, true)
  85. instance.SetStatus(ctx, self.UserCred, api.DBINSTANCE_RUNNING, "")
  86. self.SetStageComplete(ctx, nil)
  87. }