scaling_group_delete_task.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 scaling_group
  15. import (
  16. "context"
  17. "fmt"
  18. "time"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. api "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  25. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  26. "yunion.io/x/onecloud/pkg/compute/models"
  27. "yunion.io/x/onecloud/pkg/util/logclient"
  28. )
  29. type ScalingGroupDeleteTask struct {
  30. taskman.STask
  31. }
  32. func init() {
  33. taskman.RegisterTask(ScalingGroupDeleteTask{})
  34. }
  35. func (self *ScalingGroupDeleteTask) taskFailed(ctx context.Context, sg *models.SScalingGroup, reason jsonutils.JSONObject) {
  36. log.Errorf("scaling group delete task fail: %s", reason)
  37. sg.SetStatus(ctx, self.UserCred, api.SG_STATUS_DELETE_FAILED, reason.String())
  38. db.OpsLog.LogEvent(sg, db.ACT_DELETE_FAIL, reason, self.UserCred)
  39. logclient.AddActionLogWithStartable(self, sg, logclient.ACT_DELETE, reason, self.UserCred, false)
  40. self.SetStageFailed(ctx, reason)
  41. }
  42. func (self *ScalingGroupDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  43. sg := obj.(*models.SScalingGroup)
  44. sg.SetStatus(ctx, self.UserCred, api.SG_STATUS_DELETING, "")
  45. // Set all scaling policy's status as deleting
  46. sps, err := sg.ScalingPolicies()
  47. if err != nil {
  48. self.taskFailed(ctx, sg, jsonutils.NewString(err.Error()))
  49. return
  50. }
  51. spids := make([]string, len(sps))
  52. for i := range sps {
  53. spids[i] = sps[i].GetId()
  54. err := func() error {
  55. lockman.LockObject(ctx, &sps[i])
  56. defer lockman.ReleaseObject(ctx, &sps[i])
  57. return sps[i].SetStatus(ctx, self.UserCred, api.SP_STATUS_DELETING, "delete scaling group")
  58. }()
  59. if err != nil {
  60. self.taskFailed(ctx, sg, jsonutils.NewString(fmt.Sprintf("set scaling policy %s as deleting status failed: %s",
  61. sps[i].GetId(), err)))
  62. return
  63. }
  64. }
  65. log.Debugf("finish to mark all scaling policies deleted")
  66. // wait for activites finished
  67. sg.SetStatus(ctx, self.UserCred, api.SG_STATUS_WAIT_ACTIVITY_OVER, "wait all activities over")
  68. waitSeconds, interval, seconds := 180, 5, 0
  69. checkids := spids
  70. allReady := false
  71. for seconds < waitSeconds {
  72. // onlyread, lock no need
  73. tmpIds, err := models.ScalingActivityManager.FetchByStatus(ctx, checkids, []string{api.SA_STATUS_SUCCEED,
  74. api.SA_STATUS_FAILED}, "not")
  75. log.Debugf("scalingactivities not in 'succeed' or 'failed': %v", tmpIds)
  76. if err != nil {
  77. log.Errorf("ScalingActivityManager.FetchByStatus: %s", err.Error())
  78. continue
  79. }
  80. if len(tmpIds) == 0 {
  81. allReady = true
  82. break
  83. }
  84. checkids = tmpIds
  85. seconds += interval
  86. time.Sleep(time.Duration(interval) * time.Second)
  87. }
  88. if err != nil {
  89. self.taskFailed(ctx, sg, jsonutils.NewString(fmt.Sprintf("wait for all scaling activities finished: %s", err)))
  90. return
  91. }
  92. if !allReady {
  93. self.taskFailed(ctx, sg, jsonutils.NewString("some scaling activities are still in progress"))
  94. return
  95. }
  96. count, err := sg.GuestNumber()
  97. if err != nil {
  98. self.taskFailed(ctx, sg, jsonutils.NewString(fmt.Sprintf("SScalingGroup.GuestNumber: %s", err)))
  99. return
  100. }
  101. if count != 0 {
  102. self.taskFailed(ctx, sg, jsonutils.NewString("There are some guests in ScalingGroup, please delete them firstly"))
  103. return
  104. }
  105. // delete SScalingPolicies
  106. policies, err := sg.ScalingPolicies()
  107. if err != nil {
  108. self.taskFailed(ctx, sg, jsonutils.NewString(fmt.Sprintf("SScalingGroup.ScalingPolicies: %s", err.Error())))
  109. return
  110. }
  111. for i := range policies {
  112. err := policies[i].RealDelete(ctx, self.UserCred)
  113. if err != nil {
  114. self.taskFailed(ctx, sg, jsonutils.NewString(fmt.Sprintf("delete scaling group '%s' failed: %s", policies[i].GetId(), err.Error())))
  115. return
  116. }
  117. }
  118. // delete SScalingAvtivities
  119. activities, err := sg.Activities()
  120. if err != nil {
  121. self.taskFailed(ctx, sg, jsonutils.NewString(fmt.Sprintf("ScalingGroup.Activities: %s", err.Error())))
  122. }
  123. for i := range activities {
  124. err := activities[i].Delete(ctx, self.UserCred)
  125. if err != nil {
  126. self.taskFailed(ctx, sg, jsonutils.NewString(fmt.Sprintf("delete scaling activity '%s' failed: %s", activities[i].Id, err.Error())))
  127. return
  128. }
  129. }
  130. err = sg.RealDelete(ctx, self.UserCred)
  131. if err != nil {
  132. self.taskFailed(ctx, sg, jsonutils.NewString(fmt.Sprintf("ScalingGroup.RealDelete: %s", err.Error())))
  133. }
  134. db.OpsLog.LogEvent(sg, db.ACT_DELETE, "", self.UserCred)
  135. logclient.AddActionLogWithStartable(self, sg, logclient.ACT_DELETE, "", self.UserCred, true)
  136. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  137. Obj: sg,
  138. Action: notifyclient.ActionDelete,
  139. })
  140. }