storage.bridge.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package main
  2. import (
  3. "errors"
  4. fs "imuslab.com/arozos/mod/filesystem"
  5. storage "imuslab.com/arozos/mod/storage"
  6. )
  7. /*
  8. Storage functions related to bridged FSH
  9. */
  10. //Initiate bridged storage pool configs
  11. func BridgeStoragePoolInit() {
  12. bridgeRecords, err := bridgeManager.ReadConfig()
  13. if err != nil {
  14. systemWideLogger.PrintAndLog("Storage", "Fail to read File System Handler bridge config", err)
  15. return
  16. }
  17. for _, bridgeConf := range bridgeRecords {
  18. fsh, err := GetFsHandlerByUUID(bridgeConf.FSHUUID)
  19. if err != nil {
  20. //This fsh is not found. Skip this
  21. continue
  22. }
  23. basePool, err := GetStoragePoolByOwner(bridgeConf.SPOwner)
  24. if err != nil {
  25. //This fsh is not found. Skip this
  26. continue
  27. }
  28. err = BridgeFSHandlerToGroup(fsh, basePool)
  29. if err != nil {
  30. systemWideLogger.PrintAndLog("Storage", "Failed to bridge "+fsh.UUID+":/ to "+basePool.Owner+err.Error(), err)
  31. }
  32. systemWideLogger.PrintAndLog("Storage", fsh.UUID+":/ bridged to "+basePool.Owner+" Storage Pool", nil)
  33. }
  34. }
  35. func BridgeStoragePoolForGroup(group string) {
  36. bridgeRecords, err := bridgeManager.ReadConfig()
  37. if err != nil {
  38. systemWideLogger.PrintAndLog("Storage", "Failed to bridge FSH for group "+group, err)
  39. return
  40. }
  41. for _, bridgeConf := range bridgeRecords {
  42. if bridgeConf.SPOwner == group {
  43. fsh, err := GetFsHandlerByUUID(bridgeConf.FSHUUID)
  44. if err != nil {
  45. //This fsh is not found. Skip this
  46. continue
  47. }
  48. basePool, err := GetStoragePoolByOwner(bridgeConf.SPOwner)
  49. if err != nil {
  50. //This fsh is not found. Skip this
  51. continue
  52. }
  53. err = BridgeFSHandlerToGroup(fsh, basePool)
  54. if err != nil {
  55. systemWideLogger.PrintAndLog("Storage", "Failed to bridge "+fsh.UUID+":/ to "+basePool.Owner+err.Error(), err)
  56. }
  57. systemWideLogger.PrintAndLog("Storage", fsh.UUID+":/ bridged to "+basePool.Owner+" Storage Pool", nil)
  58. }
  59. }
  60. }
  61. //Debridge all bridged FSH from this group, origin (i.e. not bridged) fsh will be skipped
  62. func DebridgeAllFSHandlerFromGroup(group string) error {
  63. targetSp, err := GetStoragePoolByOwner(group)
  64. if err != nil {
  65. return err
  66. }
  67. originFsh := []*fs.FileSystemHandler{}
  68. for _, fsh := range targetSp.Storages {
  69. isBridged, err := bridgeManager.IsBridgedFSH(fsh.UUID, group)
  70. if err != nil {
  71. return err
  72. }
  73. if !isBridged {
  74. //Append the fsh that is not bridged into the origin list
  75. originFsh = append(originFsh, fsh)
  76. } else {
  77. systemWideLogger.PrintAndLog("Storage", fsh.UUID+":/ de-bridged from "+group+" Storage Pool", nil)
  78. }
  79. }
  80. targetPg := permissionHandler.GetPermissionGroupByName(group)
  81. if targetPg == nil {
  82. return errors.New("permission group not exists")
  83. }
  84. newSp, err := storage.NewStoragePool(originFsh, group)
  85. if err != nil {
  86. return err
  87. }
  88. targetPg.StoragePool = newSp
  89. return nil
  90. }
  91. //Bridge a FSH to a given Storage Pool
  92. func BridgeFSHandlerToGroup(fsh *fs.FileSystemHandler, sp *storage.StoragePool) error {
  93. //Check if the fsh already exists in the basepool
  94. for _, thisFSH := range sp.Storages {
  95. if thisFSH.UUID == fsh.UUID {
  96. return errors.New("Target File System Handler already bridged to this pool")
  97. }
  98. }
  99. sp.Storages = append(sp.Storages, fsh)
  100. return nil
  101. }
  102. //Debridge a fsh from a given group by fsh ID
  103. func DebridgeFSHandlerFromGroup(fshUUID string, sp *storage.StoragePool) error {
  104. isBridged, err := bridgeManager.IsBridgedFSH(fshUUID, sp.Owner)
  105. if err != nil || !isBridged {
  106. return errors.New("FSH not bridged")
  107. }
  108. newStorageList := []*fs.FileSystemHandler{}
  109. fshExists := false
  110. for _, fsh := range sp.Storages {
  111. if fsh.UUID != fshUUID {
  112. newStorageList = append(newStorageList, fsh)
  113. } else {
  114. fshExists = true
  115. }
  116. }
  117. if fshExists {
  118. sp.Storages = newStorageList
  119. return nil
  120. } else {
  121. return errors.New("Target File System Handler not found")
  122. }
  123. }