guest_rebuild_root_task.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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/util/osprofile"
  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/taskman"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  25. "yunion.io/x/onecloud/pkg/compute/models"
  26. "yunion.io/x/onecloud/pkg/util/logclient"
  27. )
  28. func init() {
  29. taskman.RegisterTask(GuestRebuildRootTask{})
  30. taskman.RegisterTask(KVMGuestRebuildRootTask{})
  31. taskman.RegisterTask(ManagedGuestRebuildRootTask{})
  32. }
  33. type GuestRebuildRootTask struct {
  34. SGuestBaseTask
  35. }
  36. func (self *GuestRebuildRootTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  37. guest := obj.(*models.SGuest)
  38. if jsonutils.QueryBoolean(self.Params, "need_stop", false) {
  39. self.SetStage("OnStopServerComplete", nil)
  40. guest.StartGuestStopTask(ctx, self.UserCred, 60, false, false, self.GetTaskId())
  41. } else {
  42. self.StartRebuildRootDisk(ctx, guest)
  43. }
  44. }
  45. func (self *GuestRebuildRootTask) OnStopServerComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  46. self.StartRebuildRootDisk(ctx, guest)
  47. }
  48. func (self *GuestRebuildRootTask) markFailed(ctx context.Context, guest *models.SGuest, reason jsonutils.JSONObject) {
  49. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_VM_REBUILD, reason, self.UserCred, false)
  50. guest.SetStatus(ctx, self.GetUserCred(), api.VM_REBUILD_ROOT_FAIL, reason.String())
  51. self.SGuestBaseTask.SetStageFailed(ctx, reason)
  52. notifyclient.EventNotify(ctx, self.GetUserCred(), notifyclient.SEventNotifyParam{
  53. Obj: guest,
  54. Action: notifyclient.ActionRebuildRoot,
  55. IsFail: true,
  56. })
  57. gds := guest.CategorizeDisks()
  58. imageId, _ := self.Params.GetString("origin_image_id")
  59. _, err := db.Update(gds.Root, func() error {
  60. gds.Root.TemplateId = imageId
  61. return nil
  62. })
  63. if err != nil {
  64. log.Errorf("recover root disk image id %s failed %v", imageId, err)
  65. }
  66. }
  67. func (self *GuestRebuildRootTask) StartRebuildRootDisk(ctx context.Context, guest *models.SGuest) {
  68. db.OpsLog.LogEvent(guest, db.ACT_REBUILDING_ROOT, nil, self.UserCred)
  69. gds := guest.CategorizeDisks()
  70. imageId, _ := self.Params.GetString("image_id")
  71. oldStatus := gds.Root.Status
  72. self.GetParams().Set("origin_image_id", jsonutils.NewString(gds.Root.TemplateId))
  73. _, err := db.Update(gds.Root, func() error {
  74. gds.Root.TemplateId = imageId
  75. gds.Root.Status = api.DISK_REBUILD
  76. return nil
  77. })
  78. if err != nil {
  79. self.markFailed(ctx, guest, jsonutils.NewString(err.Error()))
  80. return
  81. } else {
  82. db.OpsLog.LogEvent(gds.Root, db.ACT_UPDATE_STATUS,
  83. fmt.Sprintf("%s=>%s", oldStatus, api.DISK_REBUILD), self.UserCred)
  84. }
  85. self.SetStage("OnRebuildRootDiskComplete", nil)
  86. guest.SetStatus(ctx, self.UserCred, api.VM_REBUILD_ROOT, "")
  87. // clear logininfo
  88. loginParams := make(map[string]interface{})
  89. loginParams["login_account"] = "none"
  90. loginParams["login_key"] = "none"
  91. loginParams["login_key_timestamp"] = "none"
  92. guest.SetAllMetadata(ctx, loginParams, self.UserCred)
  93. drv, err := guest.GetDriver()
  94. if err != nil {
  95. self.OnRebuildRootDiskCompleteFailed(ctx, guest, jsonutils.NewString(err.Error()))
  96. return
  97. }
  98. drv.RequestRebuildRootDisk(ctx, guest, self)
  99. }
  100. func (self *GuestRebuildRootTask) OnRebuildRootDiskComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  101. allDisks := jsonutils.QueryBoolean(self.Params, "all_disks", false)
  102. if allDisks {
  103. disks, _ := guest.GetDisks()
  104. for i := 1; i < len(disks); i += 1 {
  105. disks[i].SetStatus(ctx, self.UserCred, api.DISK_INIT, "rebuild data disks")
  106. }
  107. self.SetStage("OnRebuildingDataDisksComplete", nil)
  108. self.OnRebuildingDataDisksComplete(ctx, guest, data)
  109. } else {
  110. self.OnRebuildAllDisksComplete(ctx, guest, data)
  111. }
  112. }
  113. func (self *GuestRebuildRootTask) OnRebuildingDataDisksComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  114. diskReady := true
  115. disks, _ := guest.GetDisks()
  116. if len(disks) > 0 {
  117. guest.SetStatus(ctx, self.UserCred, api.VM_REBUILD_ROOT, "rebuild data disks")
  118. }
  119. for i := 1; i < len(disks); i += 1 {
  120. if disks[i].Status == api.DISK_INIT {
  121. diskReady = false
  122. disks[i].StartDiskCreateTask(ctx, self.UserCred, true, "", self.GetTaskId())
  123. }
  124. }
  125. if diskReady {
  126. self.OnRebuildAllDisksComplete(ctx, guest, data)
  127. }
  128. }
  129. func (self *GuestRebuildRootTask) OnRebuildingDataDisksCompleteFailed(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  130. /*
  131. XXX ignore rebuild data disk errors
  132. db.OpsLog.LogEvent(guest, db.ACT_REBUILD_ROOT_FAIL, data, self.UserCred)
  133. guest.SetStatus(ctx,self.UserCred, models.VM_REBUILD_ROOT_FAIL, "OnRebuildingDataDisksCompleteFailed")
  134. logclient.AddActionLog(guest, logclient.ACT_VM_REBUILD, data, self.UserCred, false)
  135. self.SetStageFailed(ctx, data.String())
  136. */
  137. self.OnRebuildAllDisksComplete(ctx, guest, data)
  138. }
  139. func (self *GuestRebuildRootTask) OnRebuildAllDisksComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  140. imgId, _ := self.Params.GetString("image_id")
  141. imginfo, err := models.CachedimageManager.GetImageById(ctx, self.UserCred, imgId, false)
  142. if err != nil {
  143. self.markFailed(ctx, guest, jsonutils.NewString(err.Error()))
  144. return
  145. }
  146. osprof, err := osprofile.GetOSProfileFromImageProperties(imginfo.Properties, guest.Hypervisor)
  147. if err != nil {
  148. self.markFailed(ctx, guest, jsonutils.NewString(err.Error()))
  149. return
  150. }
  151. var bios = "BIOS"
  152. isUefi, _ := imginfo.Properties["uefi_support"]
  153. if isUefi == "true" {
  154. bios = "UEFI"
  155. }
  156. log.Infof("guest rebuild root new bios %s", bios)
  157. err = guest.SetMetadata(ctx, "__os_profile__", osprof, self.UserCred)
  158. if err != nil {
  159. self.markFailed(ctx, guest, jsonutils.NewString(err.Error()))
  160. return
  161. }
  162. if guest.OsType != osprof.OSType || guest.Bios != bios {
  163. _, err := db.Update(guest, func() error {
  164. guest.OsType = osprof.OSType
  165. guest.Bios = bios
  166. return nil
  167. })
  168. if err != nil {
  169. self.markFailed(ctx, guest, jsonutils.NewString(err.Error()))
  170. return
  171. }
  172. }
  173. db.OpsLog.LogEvent(guest, db.ACT_REBUILD_ROOT, "", self.UserCred)
  174. guest.EventNotify(ctx, self.UserCred, notifyclient.ActionRebuildRoot)
  175. self.SetStage("OnSyncStatusComplete", nil)
  176. guest.StartSyncstatus(ctx, self.UserCred, self.GetTaskId())
  177. }
  178. func (self *GuestRebuildRootTask) OnRebuildRootDiskCompleteFailed(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  179. db.OpsLog.LogEvent(guest, db.ACT_REBUILD_ROOT_FAIL, data, self.UserCred)
  180. guest.SetStatus(ctx, self.UserCred, api.VM_REBUILD_ROOT_FAIL, "OnRebuildRootDiskCompleteFailed")
  181. self.markFailed(ctx, guest, data)
  182. }
  183. func (self *GuestRebuildRootTask) OnSyncStatusComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  184. if guest.Status == api.VM_READY && jsonutils.QueryBoolean(self.Params, "auto_start", false) {
  185. self.SetStage("OnGuestStartComplete", nil)
  186. guest.StartGueststartTask(ctx, self.UserCred, nil, self.GetTaskId())
  187. } else {
  188. self.SetStageComplete(ctx, nil)
  189. }
  190. oldImageId, _ := self.GetParams().GetString("origin_image_id")
  191. imgId, _ := self.Params.GetString("image_id")
  192. notes := map[string]interface{}{}
  193. if oldImageId != imgId && len(oldImageId) > 0 {
  194. notes["old image id"] = oldImageId
  195. }
  196. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_VM_REBUILD, notes, self.UserCred, true)
  197. }
  198. func (self *GuestRebuildRootTask) OnGuestStartComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  199. self.SetStageComplete(ctx, nil)
  200. }
  201. /* -------------------------------------------------- */
  202. /* ------------ KVMGuestRebuildRootTask ------------- */
  203. /* -------------------------------------------------- */
  204. type KVMGuestRebuildRootTask struct {
  205. SGuestBaseTask
  206. }
  207. func (self *KVMGuestRebuildRootTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  208. guest := obj.(*models.SGuest)
  209. gds := guest.CategorizeDisks()
  210. self.SetStage("OnRebuildRootDiskComplete", nil)
  211. gds.Root.StartDiskCreateTask(ctx, self.UserCred, true, "", self.GetTaskId())
  212. }
  213. func (self *KVMGuestRebuildRootTask) OnRebuildRootDiskComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  214. guest := obj.(*models.SGuest)
  215. self.SetStage("OnGuestDeployComplete", nil)
  216. guest.SetStatus(ctx, self.UserCred, api.VM_DEPLOYING, "")
  217. deployParams, _ := self.Params.Get("deploy_params")
  218. var params *jsonutils.JSONDict
  219. if deployParams != nil {
  220. params = deployParams.(*jsonutils.JSONDict)
  221. } else {
  222. params = jsonutils.NewDict()
  223. }
  224. // params := jsonutils.NewDict()
  225. // params.Set("reset_password", jsonutils.JSONTrue)
  226. guest.StartGuestDeployTask(ctx, self.UserCred, params, "rebuild", self.GetTaskId())
  227. }
  228. func (self *KVMGuestRebuildRootTask) OnRebuildRootDiskCompleteFailed(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  229. self.SetStageFailed(ctx, data)
  230. }
  231. func (self *KVMGuestRebuildRootTask) OnGuestDeployComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  232. // guest := obj.(*models.SGuest)
  233. self.SetStageComplete(ctx, nil)
  234. // logclient.AddActionLogWithStartable(self, guest, logclient.ACT_VM_REBUILD, nil, self.UserCred, true)
  235. }
  236. func (self *KVMGuestRebuildRootTask) OnGuestDeployCompleteFailed(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  237. self.SetStageFailed(ctx, data)
  238. }
  239. type ManagedGuestRebuildRootTask struct {
  240. SGuestBaseTask
  241. }
  242. func (self *ManagedGuestRebuildRootTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  243. guest := obj.(*models.SGuest)
  244. self.SetStage("OnHostCacheImageComplete", nil)
  245. drv, err := guest.GetDriver()
  246. if err != nil {
  247. self.OnHostCacheImageCompleteFailed(ctx, guest, jsonutils.NewString(err.Error()))
  248. return
  249. }
  250. drv.RequestGuestCreateAllDisks(ctx, guest, self)
  251. }
  252. func (self *ManagedGuestRebuildRootTask) OnHostCacheImageComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  253. guest := obj.(*models.SGuest)
  254. self.SetStage("OnGuestDeployComplete", nil)
  255. guest.SetStatus(ctx, self.UserCred, api.VM_DEPLOYING, "rebuild deploy")
  256. deployParams, _ := self.Params.Get("deploy_params")
  257. var params *jsonutils.JSONDict
  258. if deployParams != nil {
  259. params = deployParams.(*jsonutils.JSONDict)
  260. } else {
  261. params = jsonutils.NewDict()
  262. }
  263. guest.StartGuestDeployTask(ctx, self.UserCred, params, "rebuild", self.GetTaskId())
  264. }
  265. func (self *ManagedGuestRebuildRootTask) OnHostCacheImageCompleteFailed(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  266. // guest := obj.(*models.SGuest)
  267. self.SetStageFailed(ctx, data)
  268. // logclient.AddActionLogWithStartable(self, guest, logclient.ACT_VM_REBUILD, data, self.UserCred, false)
  269. }
  270. func (self *ManagedGuestRebuildRootTask) OnGuestDeployComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  271. self.SetStageComplete(ctx, nil)
  272. }
  273. func (self *ManagedGuestRebuildRootTask) OnGuestDeployCompleteFailed(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  274. self.SetStageFailed(ctx, data)
  275. }