image_ceph_cache_storages.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 models
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/errors"
  20. computeapi "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/image/options"
  22. "yunion.io/x/onecloud/pkg/mcclient/auth"
  23. "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  24. "yunion.io/x/onecloud/pkg/util/cephutils"
  25. )
  26. func GetRegionCephStorages() (map[string]*computeapi.RbdStorageConf, error) {
  27. q := struct {
  28. Scope string `json:"scope"`
  29. StorageType string `json:"storage_type"`
  30. Brand string `json:"brand"`
  31. }{
  32. "system", computeapi.STORAGE_RBD, computeapi.ONECLOUD_BRAND_ONECLOUD,
  33. }
  34. res, err := compute.Storages.List(auth.GetAdminSession(context.Background(), options.Options.Region), jsonutils.Marshal(q))
  35. if err != nil {
  36. return nil, errors.Wrap(err, "compute.Storages.List")
  37. }
  38. if res.Total <= 0 {
  39. return nil, nil
  40. }
  41. storages := []computeapi.StorageDetails{}
  42. err = jsonutils.Update(&storages, res.Data)
  43. if err != nil {
  44. return nil, errors.Wrap(err, "json parse storage details")
  45. }
  46. cephStorages := map[string]*computeapi.RbdStorageConf{}
  47. for i := range storages {
  48. if storages[i].StorageType != computeapi.STORAGE_RBD {
  49. continue
  50. }
  51. if jsonutils.QueryBoolean(storages[i].StorageConf, "auto_cache_images", false) {
  52. conf := new(computeapi.RbdStorageConf)
  53. if err := storages[i].StorageConf.Unmarshal(conf); err != nil {
  54. log.Errorf("failed unmarshal storage %s: %s", storages[i].StorageConf, err)
  55. continue
  56. }
  57. cephStorages[storages[i].Id] = conf
  58. }
  59. }
  60. return cephStorages, nil
  61. }
  62. type SCephStorageConf struct {
  63. StorageIdConf map[string]*computeapi.RbdStorageConf
  64. CephFsidStorageId map[string][]string
  65. }
  66. var (
  67. cephStorages *SCephStorageConf
  68. requestedCephStorages bool
  69. )
  70. func GetCephStorages() *SCephStorageConf {
  71. cephutils.SetCephConfTempDir(options.Options.DefaultImageServiceHomeDir)
  72. if requestedCephStorages {
  73. return cephStorages
  74. }
  75. cephStorages = getCephStorages()
  76. requestedCephStorages = true
  77. return cephStorages
  78. }
  79. func getCephStorages() *SCephStorageConf {
  80. storagesConf, err := GetRegionCephStorages()
  81. if err != nil {
  82. log.Errorf("failed GetCephStorages %s", err)
  83. return nil
  84. }
  85. if len(storagesConf) == 0 {
  86. log.Infof("No enable auto_cache_image ceph storage found...")
  87. return nil
  88. }
  89. fsidStorageMap := map[string][]string{}
  90. for id := range storagesConf {
  91. fsid := getStorageFsid(storagesConf[id])
  92. if fsid == "" {
  93. continue
  94. }
  95. storages, ok := fsidStorageMap[fsid]
  96. if !ok {
  97. storages = make([]string, 0)
  98. }
  99. fsidStorageMap[fsid] = append(storages, id)
  100. }
  101. return &SCephStorageConf{
  102. StorageIdConf: storagesConf,
  103. CephFsidStorageId: fsidStorageMap,
  104. }
  105. }
  106. func getStorageFsid(conf *computeapi.RbdStorageConf) string {
  107. cli, err := cephutils.NewClient(conf.MonHost, conf.Key, conf.Pool, conf.EnableMessengerV2, 0, 0, 0)
  108. if err != nil {
  109. log.Errorf("failed new client of ceph storage %s:%s", conf.MonHost, conf.Pool)
  110. return ""
  111. }
  112. defer cli.Close()
  113. fsid, err := cli.Fsid()
  114. if err != nil {
  115. log.Errorf("failed get fsid of ceph storage %s:%s: %s", conf.MonHost, conf.Pool, err)
  116. return ""
  117. }
  118. return fsid
  119. }