Skip to content

Commit

Permalink
feat: add disable option for storage (close #1476)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Aug 11, 2022
1 parent af88401 commit 74f1154
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/model/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
70 changes: 70 additions & 0 deletions internal/operations/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions server/handles/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 74f1154

Please sign in to comment.