guest_renew_task.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 guest
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/pkg/util/billing"
  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 GuestRenewTask struct {
  29. SGuestBaseTask
  30. }
  31. func init() {
  32. taskman.RegisterTask(GuestRenewTask{})
  33. taskman.RegisterTask(PrepaidRecycleHostRenewTask{})
  34. }
  35. func (self *GuestRenewTask) taskFailed(ctx context.Context, guest *models.SGuest, err error) {
  36. db.OpsLog.LogEvent(guest, db.ACT_REW_FAIL, err, self.UserCred)
  37. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_RENEW, err, self.UserCred, false)
  38. guest.SetStatus(ctx, self.GetUserCred(), api.VM_RENEW_FAILED, err.Error())
  39. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  40. return
  41. }
  42. func (self *GuestRenewTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  43. guest := obj.(*models.SGuest)
  44. durationStr, _ := self.GetParams().GetString("duration")
  45. bc, _ := billing.ParseBillingCycle(durationStr)
  46. drv, err := guest.GetDriver()
  47. if err != nil {
  48. self.taskFailed(ctx, guest, errors.Wrapf(err, "GetDriver"))
  49. return
  50. }
  51. exp, err := drv.RequestRenewInstance(ctx, guest, bc)
  52. if err != nil {
  53. self.taskFailed(ctx, guest, errors.Wrapf(err, "RequestRenewInstance"))
  54. return
  55. }
  56. err = models.SaveRenewInfo(ctx, self.UserCred, guest, &bc, &exp, "")
  57. if err != nil {
  58. msg := fmt.Sprintf("SaveRenewInfo fail %s", err)
  59. log.Errorf("%v", msg)
  60. self.SetStageFailed(ctx, jsonutils.NewString(msg))
  61. return
  62. }
  63. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_RENEW, nil, self.UserCred, true)
  64. guest.StartSyncstatus(ctx, self.UserCred, "")
  65. self.SetStageComplete(ctx, nil)
  66. }
  67. type PrepaidRecycleHostRenewTask struct {
  68. taskman.STask
  69. }
  70. func (self *PrepaidRecycleHostRenewTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  71. host := obj.(*models.SHost)
  72. durationStr, _ := self.GetParams().GetString("duration")
  73. bc, _ := billing.ParseBillingCycle(durationStr)
  74. ihost, err := host.GetIHost(ctx)
  75. if err != nil {
  76. msg := fmt.Sprintf("host.GetIHost fail %s", err)
  77. log.Errorf("%v", msg)
  78. self.SetStageFailed(ctx, jsonutils.NewString(msg))
  79. return
  80. }
  81. iVM, err := ihost.GetIVMById(host.RealExternalId)
  82. if err != nil {
  83. msg := fmt.Sprintf("ihost.GetIVMById fail %s", err)
  84. log.Errorf("%v", msg)
  85. self.SetStageFailed(ctx, jsonutils.NewString(msg))
  86. return
  87. }
  88. log.Debugf("expire before %s", iVM.GetExpiredAt())
  89. err = iVM.Renew(bc)
  90. if err != nil {
  91. msg := fmt.Sprintf("iVM.Renew fail %s", err)
  92. log.Errorf("%v", msg)
  93. self.SetStageFailed(ctx, jsonutils.NewString(msg))
  94. return
  95. }
  96. err = iVM.Refresh()
  97. if err != nil {
  98. msg := fmt.Sprintf("refresh after renew fail %s", err)
  99. log.Errorf("%v", msg)
  100. self.SetStageFailed(ctx, jsonutils.NewString(msg))
  101. return
  102. }
  103. log.Debugf("expire after %s", iVM.GetExpiredAt())
  104. exp := iVM.GetExpiredAt()
  105. err = host.DoSaveRenewInfo(ctx, self.UserCred, &bc, &exp)
  106. if err != nil {
  107. msg := fmt.Sprintf("SaveRenewInfo fail %s", err)
  108. log.Errorf("%v", msg)
  109. self.SetStageFailed(ctx, jsonutils.NewString(msg))
  110. return
  111. }
  112. self.SetStageComplete(ctx, nil)
  113. }