2019-05-24 22:03:06 +00:00
|
|
|
package storage
|
2015-07-06 20:08:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2019-05-24 22:03:06 +00:00
|
|
|
"net/url"
|
2015-07-06 20:08:27 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2020-02-18 15:06:06 +00:00
|
|
|
"sync"
|
2015-07-06 20:08:27 +00:00
|
|
|
|
2019-05-24 22:03:06 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2015-07-06 20:08:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// FileStorage is a storage adapter storing the data into single local files
|
|
|
|
type FileStorage struct {
|
2019-05-24 22:03:06 +00:00
|
|
|
storagePath string
|
2020-02-18 15:06:06 +00:00
|
|
|
|
|
|
|
dashLock map[string]*sync.RWMutex
|
|
|
|
dashLockLock *sync.Mutex
|
2015-07-06 20:08:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFileStorage instanciates a new FileStorage
|
2019-05-24 22:03:06 +00:00
|
|
|
func NewFileStorage(uri *url.URL) *FileStorage {
|
2015-07-06 20:08:27 +00:00
|
|
|
// Create directory if not exists
|
2019-05-24 22:03:06 +00:00
|
|
|
if _, err := os.Stat(uri.Path); os.IsNotExist(err) {
|
|
|
|
if err := os.MkdirAll(uri.Path, 0700); err != nil {
|
|
|
|
log.WithError(err).Fatal("Could not create storage directory")
|
2015-07-06 20:08:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &FileStorage{
|
2019-05-24 22:03:06 +00:00
|
|
|
storagePath: uri.Path,
|
2020-02-18 15:06:06 +00:00
|
|
|
|
|
|
|
dashLock: map[string]*sync.RWMutex{},
|
|
|
|
dashLockLock: new(sync.Mutex),
|
2015-07-06 20:08:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put writes the given data to FS
|
|
|
|
func (f *FileStorage) Put(dashboardID string, data []byte) error {
|
2020-02-18 15:06:06 +00:00
|
|
|
f.getLock(dashboardID).Lock()
|
|
|
|
defer f.getLock(dashboardID).Unlock()
|
|
|
|
|
2015-07-06 20:08:27 +00:00
|
|
|
err := ioutil.WriteFile(f.getFilePath(dashboardID), data, 0600)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get loads the data for the given dashboard from FS
|
|
|
|
func (f *FileStorage) Get(dashboardID string) ([]byte, error) {
|
2020-02-18 15:06:06 +00:00
|
|
|
f.getLock(dashboardID).RLock()
|
|
|
|
defer f.getLock(dashboardID).RUnlock()
|
|
|
|
|
2015-07-06 20:08:27 +00:00
|
|
|
data, err := ioutil.ReadFile(f.getFilePath(dashboardID))
|
|
|
|
if err != nil {
|
|
|
|
return nil, DashboardNotFoundError{dashboardID}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes the given dashboard from FS
|
|
|
|
func (f *FileStorage) Delete(dashboardID string) error {
|
2020-02-18 15:06:06 +00:00
|
|
|
f.getLock(dashboardID).Lock()
|
|
|
|
defer f.getLock(dashboardID).Unlock()
|
|
|
|
|
2015-07-06 20:08:27 +00:00
|
|
|
if exists, err := f.Exists(dashboardID); err != nil || !exists {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return DashboardNotFoundError{dashboardID}
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.Remove(f.getFilePath(dashboardID))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exists checks for the existence of the given dashboard
|
|
|
|
func (f *FileStorage) Exists(dashboardID string) (bool, error) {
|
2020-02-18 15:06:06 +00:00
|
|
|
f.getLock(dashboardID).RLock()
|
|
|
|
defer f.getLock(dashboardID).RUnlock()
|
|
|
|
|
2015-07-06 20:08:27 +00:00
|
|
|
if _, err := os.Stat(f.getFilePath(dashboardID)); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FileStorage) getFilePath(dashboardID string) string {
|
2019-05-24 22:03:06 +00:00
|
|
|
return path.Join(f.storagePath, dashboardID+".txt")
|
2015-07-06 20:08:27 +00:00
|
|
|
}
|
2020-02-18 15:06:06 +00:00
|
|
|
|
|
|
|
func (f *FileStorage) getLock(dashboardID string) *sync.RWMutex {
|
|
|
|
f.dashLockLock.Lock()
|
|
|
|
defer f.dashLockLock.Unlock()
|
|
|
|
|
|
|
|
l, ok := f.dashLock[dashboardID]
|
|
|
|
if !ok {
|
|
|
|
l = new(sync.RWMutex)
|
|
|
|
f.dashLock[dashboardID] = l
|
|
|
|
}
|
|
|
|
|
|
|
|
return l
|
|
|
|
}
|