From 74f1154e5e73cc43adbcba6a82815d66e83a103f Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Thu, 11 Aug 2022 21:08:50 +0800 Subject: [PATCH] feat: add disable option for storage (close #1476) --- internal/model/storage.go | 1 + internal/operations/storage.go | 70 ++++++++++++++++++++++++++++++++++ server/handles/storage.go | 28 ++++++++++++++ server/router.go | 2 + 4 files changed, 101 insertions(+) diff --git a/internal/model/storage.go b/internal/model/storage.go index 9a83ebf98ea..f70d62f85bc 100644 --- a/internal/model/storage.go +++ b/internal/model/storage.go @@ -12,6 +12,7 @@ type Storage struct { Addition string `json:"addition" gorm:"type:text"` // Additional information, defined in the corresponding driver Remark string `json:"remark"` Modified time.Time `json:"modified"` + Disabled bool `json:"disabled"` // if disabled Sort Proxy } diff --git a/internal/operations/storage.go b/internal/operations/storage.go index 307bf01c5b3..35092c9c810 100644 --- a/internal/operations/storage.go +++ b/internal/operations/storage.go @@ -56,6 +56,76 @@ func CreateStorage(ctx context.Context, storage model.Storage) error { return nil } +// LoadStorage load exist storage in db to memory +func LoadStorage(ctx context.Context, storage model.Storage) error { + storage.MountPath = utils.StandardizePath(storage.MountPath) + // check driver first + driverName := storage.Driver + driverNew, err := GetDriverNew(driverName) + if err != nil { + return errors.WithMessage(err, "failed get driver new") + } + storageDriver := driverNew() + err = storageDriver.Init(ctx, storage) + if err != nil { + return errors.WithMessage(err, "failed init storage but storage is already created") + } + log.Debugf("storage %+v is created", storageDriver) + storagesMap.Store(storage.MountPath, storageDriver) + return nil +} + +func EnableStorage(ctx context.Context, id uint) error { + storage, err := db.GetStorageById(id) + if err != nil { + return errors.WithMessage(err, "failed get storage") + } + if !storage.Disabled { + return errors.Errorf("this storage have enabled") + } + err = LoadStorage(ctx, *storage) + if err != nil { + return errors.WithMessage(err, "failed load storage") + } + // reget storage from db, because it maybe hava updated + storage, err = db.GetStorageById(id) + if err != nil { + return errors.WithMessage(err, "failed reget storage again") + } + storage.Disabled = false + err = db.UpdateStorage(storage) + if err != nil { + return errors.WithMessage(err, "failed update storage in db, but have load in memory. you can try restart") + } + return nil +} + +func DisableStorage(ctx context.Context, id uint) error { + storage, err := db.GetStorageById(id) + if err != nil { + return errors.WithMessage(err, "failed get storage") + } + if storage.Disabled { + return errors.Errorf("this storage have disabled") + } + storageDriver, err := GetStorageByVirtualPath(storage.MountPath) + if err != nil { + return errors.WithMessage(err, "failed get storage driver") + } + // drop the storage in the driver + if err := storageDriver.Drop(ctx); err != nil { + return errors.WithMessage(err, "failed drop storage") + } + // delete the storage in the memory + storagesMap.Delete(storage.MountPath) + storage.Disabled = true + err = db.UpdateStorage(storage) + if err != nil { + return errors.WithMessage(err, "failed update storage in db, but have drop in memory. you can try restart") + } + return nil +} + // UpdateStorage update storage // get old storage first // drop the storage then reinitialize diff --git a/server/handles/storage.go b/server/handles/storage.go index b9f4b99fb04..68c77a90927 100644 --- a/server/handles/storage.go +++ b/server/handles/storage.go @@ -70,6 +70,34 @@ func DeleteStorage(c *gin.Context) { common.SuccessResp(c) } +func DisableStorage(c *gin.Context) { + idStr := c.Query("id") + id, err := strconv.Atoi(idStr) + if err != nil { + common.ErrorResp(c, err, 400) + return + } + if err := operations.DisableStorage(c, uint(id)); err != nil { + common.ErrorResp(c, err, 500, true) + return + } + common.SuccessResp(c) +} + +func EnableStorage(c *gin.Context) { + idStr := c.Query("id") + id, err := strconv.Atoi(idStr) + if err != nil { + common.ErrorResp(c, err, 400) + return + } + if err := operations.EnableStorage(c, uint(id)); err != nil { + common.ErrorResp(c, err, 500, true) + return + } + common.SuccessResp(c) +} + func GetStorage(c *gin.Context) { idStr := c.Query("id") id, err := strconv.Atoi(idStr) diff --git a/server/router.go b/server/router.go index 65c784eb136..6f4c83019f5 100644 --- a/server/router.go +++ b/server/router.go @@ -61,6 +61,8 @@ func admin(g *gin.RouterGroup) { storage.POST("/create", handles.CreateStorage) storage.POST("/update", handles.UpdateStorage) storage.POST("/delete", handles.DeleteStorage) + storage.POST("/enable", handles.EnableStorage) + storage.POST("/disable", handles.DisableStorage) driver := g.Group("/driver") driver.GET("/list", handles.ListDriverItems)