guest_deploy_task.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  24. "yunion.io/x/onecloud/pkg/compute/models"
  25. "yunion.io/x/onecloud/pkg/util/logclient"
  26. )
  27. type GuestDeployTask struct {
  28. SGuestBaseTask
  29. }
  30. func (self *GuestDeployTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  31. guest := obj.(*models.SGuest)
  32. if !guest.IsNetworkAllocated() {
  33. self.SetStageFailed(ctx, jsonutils.NewString(fmt.Sprintf("Guest %s network not ready!!", guest.Name)))
  34. } else {
  35. self.OnGuestNetworkReady(ctx, guest)
  36. }
  37. }
  38. func (self *GuestDeployTask) OnGuestNetworkReady(ctx context.Context, guest *models.SGuest) {
  39. self.SetStage("OnDeployWaitServerStop", nil)
  40. if jsonutils.QueryBoolean(self.Params, "restart", false) {
  41. guest.StartGuestStopTask(ctx, self.UserCred, 60, false, false, self.GetTaskId())
  42. } else {
  43. // Note: have to use LocalTaskRun, run to another place implement OnDeployWaitServerStop
  44. taskman.LocalTaskRun(self, func() (jsonutils.JSONObject, error) {
  45. return nil, nil
  46. })
  47. }
  48. }
  49. func (self *GuestDeployTask) OnDeployWaitServerStop(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  50. self.SetStage("OnDeployGuestComplete", nil)
  51. targetHostId, _ := self.Params.GetString("target_host_id")
  52. if len(targetHostId) == 0 {
  53. targetHostId = guest.HostId
  54. }
  55. host := models.HostManager.FetchHostById(targetHostId)
  56. self.DeployOnHost(ctx, guest, host)
  57. }
  58. func (self *GuestDeployTask) DeployOnHost(ctx context.Context, guest *models.SGuest, host *models.SHost) {
  59. drv, err := guest.GetDriver()
  60. if err != nil {
  61. self.OnDeployGuestFail(ctx, guest, err)
  62. return
  63. }
  64. err = drv.RequestDeployGuestOnHost(ctx, guest, host, self)
  65. if err != nil {
  66. log.Errorf("request_deploy_guest_on_host %s", err)
  67. self.OnDeployGuestFail(ctx, guest, err)
  68. } else {
  69. guest.SetStatus(ctx, self.UserCred, api.VM_DEPLOYING, "")
  70. }
  71. }
  72. func (self *GuestDeployTask) OnDeployGuestFail(ctx context.Context, guest *models.SGuest, err error) {
  73. guest.SetStatus(ctx, self.UserCred, api.VM_DEPLOY_FAILED, err.Error())
  74. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  75. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_VM_DEPLOY, err, self.UserCred, false)
  76. db.OpsLog.LogEvent(guest, db.ACT_VM_DEPLOY_FAIL, err.Error(), self.UserCred)
  77. }
  78. func (self *GuestDeployTask) OnDeployGuestComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  79. log.Infof("guest %s(%s) on_guest_deploy_task_data_received %s", obj.GetName(), obj.GetId(), data)
  80. guest := obj.(*models.SGuest)
  81. drv, err := guest.GetDriver()
  82. if err != nil {
  83. self.OnDeployGuestCompleteFailed(ctx, guest, jsonutils.NewString(err.Error()))
  84. return
  85. }
  86. drv.OnGuestDeployTaskDataReceived(ctx, guest, self, data)
  87. action, _ := self.Params.GetString("deploy_action")
  88. keypair, _ := self.Params.GetString("keypair")
  89. reset_password := jsonutils.QueryBoolean(self.Params, "reset_password", false)
  90. unbind_kp := jsonutils.QueryBoolean(self.Params, "__delete_keypair__", false)
  91. _log := false
  92. if action == "deploy" {
  93. if len(keypair) >= 32 {
  94. if unbind_kp {
  95. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_VM_UNBIND_KEYPAIR, nil, self.UserCred, true)
  96. _log = true
  97. } else {
  98. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_VM_BIND_KEYPAIR, nil, self.UserCred, true)
  99. _log = true
  100. }
  101. } else if reset_password {
  102. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_VM_RESET_PSWD, "", self.UserCred, true)
  103. guest.EventNotify(ctx, self.UserCred, notifyclient.ActionResetPassword)
  104. _log = true
  105. }
  106. }
  107. if !_log {
  108. // 如果 deploy 有其他事件,统一记在这里。
  109. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_VM_DEPLOY, action, self.UserCred, true)
  110. }
  111. if self.HasParentTask() {
  112. self.SetStageComplete(ctx, nil)
  113. return
  114. }
  115. if jsonutils.QueryBoolean(self.GetParams(), "restart", false) {
  116. self.SetStage("OnDeployStartGuestComplete", nil)
  117. guest.StartGueststartTask(ctx, self.GetUserCred(), nil, self.GetTaskId())
  118. } else {
  119. self.SetStage("OnDeployGuestSyncstatusComplete", nil)
  120. guest.StartSyncstatus(ctx, self.GetUserCred(), self.GetTaskId())
  121. }
  122. }
  123. func (self *GuestDeployTask) OnDeployGuestCompleteFailed(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  124. guest := obj.(*models.SGuest)
  125. action, _ := self.Params.GetString("deploy_action")
  126. keypair, _ := self.Params.GetString("keypair")
  127. if action == "deploy" && len(keypair) >= 32 {
  128. _, err := db.Update(guest, func() error {
  129. guest.KeypairId = ""
  130. return nil
  131. })
  132. if err != nil {
  133. log.Errorf("unset guest %s keypair failed %v", guest.Name, err)
  134. }
  135. }
  136. guest.SetStatus(ctx, self.UserCred, api.VM_DEPLOY_FAILED, data.String())
  137. self.SetStageFailed(ctx, data)
  138. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_VM_DEPLOY, data, self.UserCred, false)
  139. db.OpsLog.LogEvent(guest, db.ACT_VM_DEPLOY_FAIL, data, self.UserCred)
  140. }
  141. func (self *GuestDeployTask) OnDeployStartGuestComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  142. self.SetStageComplete(ctx, nil)
  143. }
  144. func (self *GuestDeployTask) OnDeployStartGuestCompleteFailed(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  145. self.SetStageFailed(ctx, data)
  146. }
  147. func (self *GuestDeployTask) OnDeployGuestSyncstatusComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  148. self.SetStageComplete(ctx, nil)
  149. }
  150. func (self *GuestDeployTask) OnDeployGuestSyncstatusCompleteFailed(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  151. self.SetStageFailed(ctx, data)
  152. }
  153. func init() {
  154. taskman.RegisterTask(GuestDeployTask{})
  155. }