instance_backup_pack_task.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 backup
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/log"
  19. "yunion.io/x/onecloud/pkg/apis/compute"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  23. "yunion.io/x/onecloud/pkg/compute/models"
  24. "yunion.io/x/onecloud/pkg/util/logclient"
  25. )
  26. type InstanceBackupPackTask struct {
  27. taskman.STask
  28. }
  29. func init() {
  30. taskman.RegisterTask(InstanceBackupPackTask{})
  31. }
  32. func (self *InstanceBackupPackTask) taskFailed(ctx context.Context, ib *models.SInstanceBackup, reason jsonutils.JSONObject) {
  33. reasonStr, _ := reason.GetString()
  34. ib.SetStatus(ctx, self.UserCred, compute.INSTANCE_BACKUP_STATUS_PACK_FAILED, reasonStr)
  35. logclient.AddActionLogWithStartable(self, ib, logclient.ACT_PACK, reason, self.UserCred, false)
  36. db.OpsLog.LogEvent(ib, db.ACT_PACK_FAIL, ib.GetShortDesc(ctx), self.GetUserCred())
  37. self.SetStageFailed(ctx, reason)
  38. }
  39. func (self *InstanceBackupPackTask) taskSuccess(ctx context.Context, ib *models.SInstanceBackup) {
  40. ib.SetStatus(ctx, self.UserCred, compute.INSTANCE_BACKUP_STATUS_READY, "")
  41. logclient.AddActionLogWithStartable(self, ib, logclient.ACT_PACK, nil, self.UserCred, true)
  42. db.OpsLog.LogEvent(ib, db.ACT_PACK, ib.GetShortDesc(ctx), self.GetUserCred())
  43. self.SetStageComplete(ctx, nil)
  44. }
  45. func (self *InstanceBackupPackTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  46. ib := obj.(*models.SInstanceBackup)
  47. packageName, _ := self.GetParams().GetString("package_name")
  48. self.SetStage("OnPackComplete", nil)
  49. err := ib.GetRegionDriver().RequestPackInstanceBackup(ctx, ib, self, packageName)
  50. if err != nil {
  51. self.taskFailed(ctx, ib, jsonutils.NewString(err.Error()))
  52. return
  53. }
  54. }
  55. func (self *InstanceBackupPackTask) OnPackComplete(ctx context.Context, ib *models.SInstanceBackup, data jsonutils.JSONObject) {
  56. log.Infof("OnPackComplete %s", data)
  57. notifyclient.NotifyImportantWithCtx(
  58. ctx,
  59. []string{self.UserCred.GetUserId()},
  60. false,
  61. "BACKUP_PACK_COMPLETE",
  62. data,
  63. )
  64. self.taskSuccess(ctx, ib)
  65. }
  66. func (self *InstanceBackupPackTask) OnPackCompleteFailed(ctx context.Context, ib *models.SInstanceBackup, data jsonutils.JSONObject) {
  67. self.taskFailed(ctx, ib, data)
  68. }