removed support for the file:// storage backend (#90)

pull/99/head
James Batt 4 years ago committed by GitHub
parent 482e01cce2
commit 41173e2a98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -52,11 +52,15 @@ Example connection string:
- `mysql://user:password@localhost:3306/database?ssl-mode=disabled`
### File (deprecated)
### File (removed)
The `file://` backend has been deprecated in 0.3.0 and will be removed in 0.4.0
The `file://` backend was deprecated in 0.3.0 and has been removed in 0.4.0
You can use the migration guide below to migrate to a different storage backend.
If you'd like to migrate your `file://` storage to a supported backend you must use
version 0.3.0 and then follow the migration guide below to migrate to a different storage backend.
_Note that the migration tool itself doesn't support the `file://` backend on versions
released after 0.3.0_.
## Migration Between Backends

@ -23,7 +23,7 @@ type AppConfig struct {
ExternalHost string `yaml:"externalHost"`
// The storage backend where device configuration will
// be persisted.
// Supports memory:// file:// postgres:// mysql:// sqlite3://
// Supports memory:// postgres:// mysql:// sqlite3://
// Defaults to memory://
Storage string `yaml:"storage"`
// DisableMetadata allows you to turn off collection of device

@ -60,10 +60,6 @@ func NewStorage(uri string) (Storage, error) {
case "memory":
logrus.Warn("storing data in memory - devices will not persist between restarts")
return NewMemoryStorage(), nil
case "file":
logrus.Warn("[DEPRECATION NOTICE] using file:// storage is deprecated and will be removed in an upcoming minor release. Please use sqlite3:// for filesystem based storage.")
logrus.Infof("storing data in %s", u.Path)
return NewFileStorage(u.Path), nil
case "postgres":
fallthrough
case "mysql":

@ -15,15 +15,6 @@ func TestMemoryStorage(t *testing.T) {
require.IsType(&InMemoryStorage{}, s)
}
func TestFileStorage(t *testing.T) {
require := require.New(t)
s, err := NewStorage("file:///some/path")
require.NoError(err)
require.IsType(&FileStorage{}, s)
}
func TestPostgresqlStorage(t *testing.T) {
require := require.New(t)

@ -1,129 +0,0 @@
package storage
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// implements Storage interface
type FileStorage struct {
*InProcessWatcher
directory string
}
func NewFileStorage(directory string) *FileStorage {
return &FileStorage{
InProcessWatcher: NewInProcessWatcher(),
directory: directory,
}
}
func (s *FileStorage) Open() error {
if _, err := os.Stat(s.directory); os.IsNotExist(err) {
if err := os.MkdirAll(s.directory, 0600); err != nil {
return errors.Wrap(err, "failed to create storage directory")
}
}
return nil
}
func (s *FileStorage) Close() error {
return nil
}
func (s *FileStorage) Save(device *Device) error {
key := key(device)
path := s.deviceFilePath(key)
logrus.Debugf("saving device %s", path)
bytes, err := json.Marshal(device)
if err != nil {
return errors.Wrap(err, "failed to marshal device")
}
os.MkdirAll(filepath.Dir(path), 0600)
if err := ioutil.WriteFile(path, bytes, 0600); err != nil {
return errors.Wrapf(err, "failed to write device to file %s", path)
}
s.emitAdd(device)
return nil
}
func (s *FileStorage) List(username string) ([]*Device, error) {
prefix := func() string {
if username != "" {
return keyStr(username, "")
}
return ""
}()
files := []string{}
err := filepath.Walk(s.directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
p := strings.TrimPrefix(path, s.directory)
p = strings.TrimPrefix(p, string(os.PathSeparator))
if strings.HasPrefix(p, prefix) && filepath.Ext(path) == ".json" {
files = append(files, path)
}
return nil
})
if err != nil {
return nil, errors.Wrap(err, "failed to list storage directory")
}
logrus.Debugf("Found files: %+v", files)
devices := []*Device{}
for _, file := range files {
bytes, err := ioutil.ReadFile(file)
if err != nil {
return nil, errors.Wrapf(err, "failed to read device file %s", file)
}
device := &Device{}
if err := json.Unmarshal(bytes, device); err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal device file %s", file)
}
if err != nil {
return nil, errors.Wrap(err, "failed to read device file")
}
devices = append(devices, device)
}
logrus.Debugf("Found devices: %+v", devices)
return devices, nil
}
func (s *FileStorage) Get(owner string, name string) (*Device, error) {
key := keyStr(owner, name)
path := s.deviceFilePath(key)
bytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, errors.Wrapf(err, "failed to read device file %s", path)
}
device := &Device{}
if err := json.Unmarshal(bytes, device); err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal device file %s", path)
}
return device, nil
}
func (s *FileStorage) Delete(device *Device) error {
if err := os.Remove(s.deviceFilePath(key(device))); err != nil {
return errors.Wrap(err, "failed to delete device file")
}
s.emitDelete(device)
return nil
}
func (s *FileStorage) deviceFilePath(key string) string {
// TODO: protect against path traversal
// and make sure names are reasonably sane
return filepath.Join(s.directory, fmt.Sprintf("%s.json", key))
}
Loading…
Cancel
Save