host_storage_attach_task.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 HostStorageAttachTask struct {
  26. taskman.STask
  27. }
  28. func init() {
  29. taskman.RegisterTask(HostStorageAttachTask{})
  30. }
  31. func (self *HostStorageAttachTask) taskFail(ctx context.Context, host *models.SHost, reason error) {
  32. if hoststorage := self.getHoststorage(host); hoststorage != nil {
  33. storage := hoststorage.GetStorage()
  34. hoststorage.Detach(ctx, self.GetUserCred())
  35. note := fmt.Sprintf("attach host %s failed: %s", host.Name, reason)
  36. db.OpsLog.LogEvent(storage, db.ACT_ATTACH_FAIL, note, self.GetUserCred())
  37. logclient.AddActionLogWithContext(ctx, storage, logclient.ACT_ATTACH_HOST, note, self.GetUserCred(), false)
  38. }
  39. self.SetStageFailed(ctx, jsonutils.NewString(reason.Error()))
  40. }
  41. func (self *HostStorageAttachTask) getHoststorage(host *models.SHost) *models.SHoststorage {
  42. storageId, _ := self.GetParams().GetString("storage_id")
  43. hoststorages := host.GetHoststorages()
  44. for _, hoststorage := range hoststorages {
  45. if hoststorage.StorageId == storageId {
  46. return &hoststorage
  47. }
  48. }
  49. return nil
  50. }
  51. func (self *HostStorageAttachTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  52. host := obj.(*models.SHost)
  53. hoststorage := self.getHoststorage(host)
  54. if hoststorage == nil {
  55. self.taskFail(ctx, host, errors.Errorf("failed to find hoststorage"))
  56. return
  57. }
  58. storage := hoststorage.GetStorage()
  59. self.SetStage("OnAttachStorageComplete", nil)
  60. driver, err := host.GetHostDriver()
  61. if err != nil {
  62. self.taskFail(ctx, host, errors.Wrapf(err, "GetHostDriver"))
  63. return
  64. }
  65. err = driver.RequestAttachStorage(ctx, hoststorage, host, storage, self)
  66. if err != nil {
  67. self.taskFail(ctx, host, errors.Wrapf(err, "RequestAttachStorage"))
  68. }
  69. }
  70. func (self *HostStorageAttachTask) OnAttachStorageComplete(ctx context.Context, host *models.SHost, data jsonutils.JSONObject) {
  71. storageId, _ := self.GetParams().GetString("storage_id")
  72. storage := models.StorageManager.FetchStorageById(storageId)
  73. db.OpsLog.LogEvent(storage, db.ACT_ATTACH, "", self.GetUserCred())
  74. logclient.AddActionLogWithContext(ctx, storage, logclient.ACT_ATTACH_HOST,
  75. fmt.Sprintf("Attach host %s success", host.Name), self.GetUserCred(), true)
  76. storage.SyncStatusWithHosts(ctx)
  77. host.ClearSchedDescCache()
  78. self.SetStageComplete(ctx, nil)
  79. }
  80. func (self *HostStorageAttachTask) OnAttachStorageCompleteFailed(ctx context.Context, host *models.SHost, reason jsonutils.JSONObject) {
  81. self.taskFail(ctx, host, errors.Errorf("%s", reason.String()))
  82. }