storage_nas.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 storageman
  15. import (
  16. "context"
  17. "fmt"
  18. "path"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/onecloud/pkg/apis"
  23. "yunion.io/x/onecloud/pkg/hostman/hostutils"
  24. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  25. )
  26. type INasStorage interface {
  27. newDisk(diskId string) IDisk
  28. StorageType() string
  29. }
  30. type SNasStorage struct {
  31. SLocalStorage
  32. ins INasStorage
  33. }
  34. func NewNasStorage(manager *SStorageManager, path string, ins INasStorage) *SNasStorage {
  35. ret := &SNasStorage{*NewLocalStorage(manager, path, 0), ins}
  36. return ret
  37. }
  38. func (s *SNasStorage) GetComposedName() string {
  39. return fmt.Sprintf("host_%s_%s_storage_%d", s.Manager.host.GetMasterIp(), s.ins.StorageType(), s.Index)
  40. }
  41. func (s *SNasStorage) GetSnapshotDir() string {
  42. return path.Join(s.Path, _SNAPSHOT_PATH_)
  43. }
  44. func (s *SNasStorage) CreateDisk(diskId string) IDisk {
  45. s.DiskLock.Lock()
  46. defer s.DiskLock.Unlock()
  47. disk := s.ins.newDisk(diskId)
  48. s.Disks = append(s.Disks, disk)
  49. return disk
  50. }
  51. func (s *SNasStorage) GetDiskById(diskId string) (IDisk, error) {
  52. s.DiskLock.Lock()
  53. defer s.DiskLock.Unlock()
  54. for i := 0; i < len(s.Disks); i++ {
  55. if s.Disks[i].GetId() == diskId {
  56. return s.Disks[i], s.Disks[i].Probe()
  57. }
  58. }
  59. var disk = s.ins.newDisk(diskId)
  60. if disk.Probe() == nil {
  61. s.Disks = append(s.Disks, disk)
  62. return disk, nil
  63. }
  64. return nil, errors.ErrNotFound
  65. }
  66. func (s *SNasStorage) SyncStorageInfo() (jsonutils.JSONObject, error) {
  67. if len(s.StorageId) == 0 {
  68. return nil, fmt.Errorf("Sync nfs storage without storage id")
  69. }
  70. content := jsonutils.NewDict()
  71. content.Set("capacity", jsonutils.NewInt(int64(s.GetAvailSizeMb())))
  72. content.Set("storage_type", jsonutils.NewString(s.ins.StorageType()))
  73. content.Set("zone", jsonutils.NewString(s.GetZoneId()))
  74. log.Infof("Sync storage info %s", s.StorageId)
  75. res, err := modules.Storages.Put(
  76. hostutils.GetComputeSession(context.Background()),
  77. s.StorageId, content)
  78. if err != nil {
  79. log.Errorf("SyncStorageInfo Failed: %s: %s", content, err)
  80. }
  81. return res, err
  82. }
  83. func (s *SNasStorage) CreateDiskFromSnapshot(ctx context.Context, disk IDisk, input *SDiskCreateByDiskinfo) (jsonutils.JSONObject, error) {
  84. info := input.DiskInfo
  85. var encryptInfo *apis.SEncryptInfo
  86. if info.Encryption {
  87. encryptInfo = &info.EncryptInfo
  88. }
  89. return disk.CreateFromSnapshotLocation(ctx, input.DiskInfo.SnapshotUrl, int64(input.DiskInfo.DiskSizeMb), encryptInfo)
  90. }