guest_detach_scalinggroup.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. "database/sql"
  18. "fmt"
  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 GuestDetachScalingGroupTask struct {
  29. taskman.STask
  30. }
  31. func init() {
  32. taskman.RegisterTask(GuestDetachScalingGroupTask{})
  33. }
  34. func (self *GuestDetachScalingGroupTask) taskFailed(ctx context.Context, sg *models.SScalingGroup, sgg *models.SScalingGroupGuest, reason jsonutils.JSONObject) {
  35. if sg == nil {
  36. return
  37. }
  38. logclient.AddActionLogWithStartable(self, sg, logclient.ACT_REMOVE_GUEST, reason, self.UserCred, false)
  39. self.SetStageFailed(ctx, reason)
  40. if sgg == nil {
  41. guestId, _ := self.Params.GetString("guest")
  42. sggs, _ := models.ScalingGroupGuestManager.Fetch(sg.GetId(), guestId)
  43. if len(sggs) > 0 {
  44. sgg = &sggs[0]
  45. } else {
  46. return
  47. }
  48. }
  49. sgg.SetGuestStatus(api.SG_GUEST_STATUS_REMOVE_FAILED)
  50. }
  51. func (self *GuestDetachScalingGroupTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  52. sg := obj.(*models.SScalingGroup)
  53. guestId, _ := self.Params.GetString("guest")
  54. guest := models.GuestManager.FetchGuestById(guestId)
  55. if guest == nil {
  56. self.taskFailed(ctx, sg, nil, jsonutils.NewString("unable to FetchGuestById"))
  57. return
  58. }
  59. self.SetStage("OnDetachLoadbalancerComplete", nil)
  60. q := models.LoadbalancerBackendManager.Query().Equals("backend_id", guest.Id)
  61. var lbBackend models.SLoadbalancerBackend
  62. err := q.First(&lbBackend)
  63. if err != nil {
  64. if errors.Cause(err) != sql.ErrNoRows {
  65. self.taskFailed(ctx, sg, nil, jsonutils.NewString(fmt.Sprintf("Fetch loadbalancer backend failed: %s", err.Error())))
  66. return
  67. }
  68. self.OnDetachLoadbalancerComplete(ctx, sg, body)
  69. return
  70. } else {
  71. lbBackend.SetModelManager(models.LoadbalancerBackendManager, &lbBackend)
  72. lbBackend.SetStatus(ctx, self.UserCred, api.LB_STATUS_DELETING, "")
  73. if err = lbBackend.StartLoadBalancerBackendDeleteTask(ctx, self.UserCred, jsonutils.NewDict(), self.Id); err != nil {
  74. self.taskFailed(ctx, sg, nil, jsonutils.NewString(fmt.Sprintf("Detach guest with loadbalancer group failed: %s", err)))
  75. }
  76. }
  77. }
  78. func (self *GuestDetachScalingGroupTask) OnDetachLoadbalancerComplete(ctx context.Context, sg *models.SScalingGroup, data jsonutils.JSONObject) {
  79. guestId, _ := self.Params.GetString("guest")
  80. delete, _ := self.Params.Bool("delete_server")
  81. if !delete {
  82. self.OnDeleteGuestComplete(ctx, sg, data)
  83. return
  84. }
  85. guest := models.GuestManager.FetchGuestById(guestId)
  86. if guest == nil {
  87. self.taskFailed(ctx, sg, nil, jsonutils.NewString("unable to FetchGuestById"))
  88. return
  89. }
  90. self.Params.Set("guest_name", jsonutils.NewString(guest.GetName()))
  91. self.SetStage("OnDeleteGuestComplete", nil)
  92. opts := api.ServerDeleteInput{Purge: false, OverridePendingDelete: true, DeleteSnapshots: true}
  93. err := guest.StartDeleteGuestTask(ctx, self.UserCred, self.Id, opts)
  94. if err != nil {
  95. self.taskFailed(ctx, sg, nil, jsonutils.NewString(err.Error()))
  96. }
  97. }
  98. func (self *GuestDetachScalingGroupTask) OnDetachLoadbalancerCompleteFailed(ctx context.Context, sg *models.SScalingGroup,
  99. data jsonutils.JSONObject) {
  100. self.taskFailed(ctx, sg, nil, data)
  101. }
  102. func (self *GuestDetachScalingGroupTask) OnDeleteGuestComplete(ctx context.Context, sg *models.SScalingGroup, data jsonutils.JSONObject) {
  103. guestId, _ := self.Params.GetString("guest")
  104. guestName, _ := self.Params.GetString("guest_name")
  105. sggs, _ := models.ScalingGroupGuestManager.Fetch(sg.GetId(), guestId)
  106. if len(sggs) > 0 {
  107. sggs[0].Detach(ctx, self.UserCred)
  108. }
  109. logclient.AddActionLogWithStartable(self, sg, logclient.ACT_REMOVE_GUEST, fmt.Sprintf("Instance '%s' was removed", guestId), self.UserCred, true)
  110. if auto, _ := self.Params.Bool("auto"); !auto {
  111. // scale; change the desire number
  112. err := sg.Scale(ctx, SScalingTriggerDesc{guestName}, SScalingActionDesc{}, 0)
  113. if err != nil {
  114. log.Errorf("ScalingGroup '%s' scale after removing instance '%s' failed: %s", sg.GetId(), guestId, err.Error())
  115. }
  116. }
  117. }
  118. type SScalingTriggerDesc struct {
  119. Guest string
  120. }
  121. func (s SScalingTriggerDesc) TriggerDescription() string {
  122. return fmt.Sprintf("A user remove instance '%s' from the scaling group manually", s.Guest)
  123. }
  124. type SScalingActionDesc struct {
  125. }
  126. func (s SScalingActionDesc) Exec(desire int) int {
  127. if desire < 1 {
  128. log.Errorf("desire should not be less than 1")
  129. }
  130. return desire - 1
  131. }
  132. func (s SScalingActionDesc) CheckCoolTime() bool {
  133. return false
  134. }
  135. func (self *GuestDetachScalingGroupTask) OnDeleteGuestCompleteFailed(ctx context.Context, sg *models.SScalingGroup, data jsonutils.JSONObject) {
  136. self.taskFailed(ctx, sg, nil, data)
  137. }