storage_update_task.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 storage
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/util/httputils"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  23. "yunion.io/x/onecloud/pkg/compute/models"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. )
  26. func init() {
  27. taskman.RegisterTask(StorageUpdateTask{})
  28. taskman.RegisterTask(RbdStorageUpdateTask{})
  29. }
  30. type StorageUpdateTask struct {
  31. taskman.STask
  32. }
  33. func (self *StorageUpdateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  34. self.SetStage("OnStorageUpdate", nil)
  35. storage := obj.(*models.SStorage)
  36. driver := models.GetStorageDriver(storage.StorageType)
  37. if driver != nil {
  38. err := driver.DoStorageUpdateTask(ctx, self.UserCred, storage, self)
  39. if err != nil {
  40. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  41. }
  42. }
  43. self.SetStageComplete(ctx, nil)
  44. }
  45. func (self *StorageUpdateTask) OnStorageUpdate(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  46. self.SetStageComplete(ctx, nil)
  47. }
  48. func (self *StorageUpdateTask) OnStorageUpdateFailed(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  49. self.SetStageFailed(ctx, data)
  50. }
  51. type RbdStorageUpdateTask struct {
  52. taskman.STask
  53. }
  54. func (self *RbdStorageUpdateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  55. storage := obj.(*models.SStorage)
  56. hosts := storage.GetAllAttachingHosts()
  57. for _, host := range hosts {
  58. log.Infof("Updata rbd Storage [%s] on host %s ...", storage.Name, host.Name)
  59. url := fmt.Sprintf("%s/storages/update", host.ManagerUri)
  60. headers := mcclient.GetTokenHeaders(self.GetUserCred())
  61. body := jsonutils.Marshal(map[string]interface{}{
  62. "storage_id": storage.Id,
  63. "storage_conf": storage.StorageConf,
  64. })
  65. _, _, err := httputils.JSONRequest(httputils.GetDefaultClient(), ctx, "POST", url, headers, body, false)
  66. //这里尽可能的更新所有在线的hoststorage信息,仅打印warning信息
  67. log.Warningf("update rbd storage info for host %s(%s) error: %v", host.Name, host.Id, err)
  68. }
  69. self.SetStageComplete(ctx, nil)
  70. }