host_storage_detach_task.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 host
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  22. "yunion.io/x/onecloud/pkg/compute/models"
  23. "yunion.io/x/onecloud/pkg/util/logclient"
  24. )
  25. type HostStorageDetachTask struct {
  26. taskman.STask
  27. }
  28. func init() {
  29. taskman.RegisterTask(HostStorageDetachTask{})
  30. }
  31. func (self *HostStorageDetachTask) taskFail(ctx context.Context, host *models.SHost, reason error) {
  32. var hoststorage = new(models.SHoststorage)
  33. storageId, _ := self.GetParams().GetString("storage_id")
  34. err := models.HoststorageManager.Query().Equals("host_id", host.Id).Equals("storage_id", storageId).First(hoststorage)
  35. if err == nil {
  36. storage := hoststorage.GetStorage()
  37. // note := fmt.Sprintf("detach host %s failed: %s", host.Name, reason)
  38. db.OpsLog.LogEvent(storage, db.ACT_DETACH_FAIL, reason, self.GetUserCred())
  39. logclient.AddActionLogWithContext(ctx, storage, logclient.ACT_DETACH_HOST, reason, self.GetUserCred(), false)
  40. }
  41. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  42. }
  43. func (self *HostStorageDetachTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  44. host := obj.(*models.SHost)
  45. storageId, _ := self.GetParams().GetString("storage_id")
  46. _storage, err := models.StorageManager.FetchById(storageId)
  47. if err != nil {
  48. self.taskFail(ctx, host, errors.Wrapf(err, "FetchById %s", storageId))
  49. return
  50. }
  51. storage := _storage.(*models.SStorage)
  52. self.SetStage("OnDetachStorageComplete", nil)
  53. driver, err := host.GetHostDriver()
  54. if err != nil {
  55. self.taskFail(ctx, host, errors.Wrapf(err, "GetHostDriver"))
  56. return
  57. }
  58. err = driver.RequestDetachStorage(ctx, host, storage, self)
  59. if err != nil {
  60. self.taskFail(ctx, host, errors.Wrapf(err, "RequestDetachStorage"))
  61. }
  62. }
  63. func (self *HostStorageDetachTask) OnDetachStorageComplete(ctx context.Context, host *models.SHost, data jsonutils.JSONObject) {
  64. storageId, _ := self.GetParams().GetString("storage_id")
  65. storage := models.StorageManager.FetchStorageById(storageId)
  66. db.OpsLog.LogEvent(storage, db.ACT_DETACH, "", self.GetUserCred())
  67. logclient.AddActionLogWithContext(ctx, storage, logclient.ACT_DETACH_HOST,
  68. fmt.Sprintf("Detach host %s success", host.Name), self.GetUserCred(), true)
  69. self.SetStageComplete(ctx, nil)
  70. storage.SyncStatusWithHosts(ctx)
  71. host.ClearSchedDescCache()
  72. }
  73. func (self *HostStorageDetachTask) OnDetachStorageCompleteFailed(ctx context.Context, host *models.SHost, reason jsonutils.JSONObject) {
  74. self.taskFail(ctx, host, errors.Errorf("%s", reason.String()))
  75. }