storage.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package service
  2. import (
  3. "io/ioutil"
  4. "github.com/IceWhaleTech/CasaOS-Common/utils/logger"
  5. "github.com/IceWhaleTech/CasaOS/pkg/utils/file"
  6. "github.com/IceWhaleTech/CasaOS/pkg/utils/httper"
  7. "go.uber.org/zap"
  8. )
  9. type StorageService interface {
  10. MountStorage(mountPoint, fs string) error
  11. UnmountStorage(mountPoint string) error
  12. GetStorages() (httper.MountList, error)
  13. CreateConfig(data map[string]string, name string, t string) error
  14. CheckAndMountByName(name string) error
  15. CheckAndMountAll() error
  16. GetConfigByName(name string) (map[string]string, error)
  17. DeleteConfigByName(name string) error
  18. GetConfig() (httper.RemotesResult, error)
  19. }
  20. type storageStruct struct {
  21. }
  22. func (s *storageStruct) MountStorage(mountPoint, fs string) error {
  23. file.IsNotExistMkDir(mountPoint)
  24. return httper.Mount(mountPoint, fs)
  25. }
  26. func (s *storageStruct) UnmountStorage(mountPoint string) error {
  27. err := httper.Unmount(mountPoint)
  28. if err == nil {
  29. dir, _ := ioutil.ReadDir(mountPoint)
  30. if len(dir) == 0 {
  31. file.RMDir(mountPoint)
  32. }
  33. return nil
  34. }
  35. return err
  36. }
  37. func (s *storageStruct) GetStorages() (httper.MountList, error) {
  38. return httper.GetMountList()
  39. }
  40. func (s *storageStruct) CreateConfig(data map[string]string, name string, t string) error {
  41. httper.CreateConfig(data, name, t)
  42. return nil
  43. }
  44. func (s *storageStruct) CheckAndMountByName(name string) error {
  45. storages, _ := MyService.Storage().GetStorages()
  46. currentRemote, _ := httper.GetConfigByName(name)
  47. mountPoint := currentRemote["mount_point"]
  48. isMount := false
  49. for _, v := range storages.MountPoints {
  50. if v.MountPoint == mountPoint {
  51. isMount = true
  52. break
  53. }
  54. }
  55. if !isMount {
  56. return MyService.Storage().MountStorage(mountPoint, name+":")
  57. }
  58. return nil
  59. }
  60. func (s *storageStruct) CheckAndMountAll() error {
  61. storages, err := MyService.Storage().GetStorages()
  62. if err != nil {
  63. return err
  64. }
  65. logger.Info("when CheckAndMountAll storages", zap.Any("storages", storages))
  66. section, err := httper.GetAllConfigName()
  67. if err != nil {
  68. return err
  69. }
  70. logger.Info("when CheckAndMountAll section", zap.Any("section", section))
  71. for _, v := range section.Remotes {
  72. currentRemote, _ := httper.GetConfigByName(v)
  73. mountPoint := currentRemote["mount_point"]
  74. if len(mountPoint) == 0 {
  75. continue
  76. }
  77. isMount := false
  78. for _, v := range storages.MountPoints {
  79. if v.MountPoint == mountPoint {
  80. isMount = true
  81. break
  82. }
  83. }
  84. if !isMount {
  85. logger.Info("when CheckAndMountAll MountStorage", zap.String("mountPoint", mountPoint), zap.String("fs", v))
  86. err := MyService.Storage().MountStorage(mountPoint, v+":")
  87. if err != nil {
  88. logger.Error("when CheckAndMountAll then", zap.String("mountPoint", mountPoint), zap.String("fs", v), zap.Error(err))
  89. }
  90. }
  91. }
  92. return nil
  93. }
  94. func (s *storageStruct) GetConfigByName(name string) (map[string]string, error) {
  95. return httper.GetConfigByName(name)
  96. }
  97. func (s *storageStruct) DeleteConfigByName(name string) error {
  98. return httper.DeleteConfigByName(name)
  99. }
  100. func (s *storageStruct) GetConfig() (httper.RemotesResult, error) {
  101. section, err := httper.GetAllConfigName()
  102. if err != nil {
  103. return httper.RemotesResult{}, err
  104. }
  105. return section, nil
  106. }
  107. func NewStorageService() StorageService {
  108. return &storageStruct{}
  109. }