2018-03-22 17:11:03 -04:00
|
|
|
package service // import "github.com/docker/docker/volume/service"
|
2016-12-01 17:17:07 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
2018-03-13 15:39:23 -04:00
|
|
|
"github.com/docker/docker/errdefs"
|
2016-12-01 17:17:07 -05:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-09-11 18:12:55 -04:00
|
|
|
bolt "go.etcd.io/bbolt"
|
2016-12-01 17:17:07 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var volumeBucketName = []byte("volumes")
|
|
|
|
|
|
|
|
type volumeMetadata struct {
|
|
|
|
Name string
|
|
|
|
Driver string
|
|
|
|
Labels map[string]string
|
|
|
|
Options map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *VolumeStore) setMeta(name string, meta volumeMetadata) error {
|
|
|
|
return s.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
return setMeta(tx, name, meta)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func setMeta(tx *bolt.Tx, name string, meta volumeMetadata) error {
|
|
|
|
metaJSON, err := json.Marshal(meta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-13 15:39:23 -04:00
|
|
|
b, err := tx.CreateBucketIfNotExists(volumeBucketName)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error creating volume bucket")
|
|
|
|
}
|
2016-12-01 17:17:07 -05:00
|
|
|
return errors.Wrap(b.Put([]byte(name), metaJSON), "error setting volume metadata")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *VolumeStore) getMeta(name string) (volumeMetadata, error) {
|
|
|
|
var meta volumeMetadata
|
|
|
|
err := s.db.View(func(tx *bolt.Tx) error {
|
|
|
|
return getMeta(tx, name, &meta)
|
|
|
|
})
|
|
|
|
return meta, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMeta(tx *bolt.Tx, name string, meta *volumeMetadata) error {
|
|
|
|
b := tx.Bucket(volumeBucketName)
|
2018-03-13 15:39:23 -04:00
|
|
|
if b == nil {
|
|
|
|
return errdefs.NotFound(errors.New("volume bucket does not exist"))
|
|
|
|
}
|
2016-12-01 17:17:07 -05:00
|
|
|
val := b.Get([]byte(name))
|
2018-03-13 15:39:23 -04:00
|
|
|
if len(val) == 0 {
|
2016-12-01 17:17:07 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(val, meta); err != nil {
|
|
|
|
return errors.Wrap(err, "error unmarshaling volume metadata")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *VolumeStore) removeMeta(name string) error {
|
|
|
|
return s.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
return removeMeta(tx, name)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeMeta(tx *bolt.Tx, name string) error {
|
|
|
|
b := tx.Bucket(volumeBucketName)
|
|
|
|
return errors.Wrap(b.Delete([]byte(name)), "error removing volume metadata")
|
|
|
|
}
|
|
|
|
|
2016-12-22 11:49:47 -05:00
|
|
|
// listMeta is used during restore to get the list of volume metadata
|
|
|
|
// from the on-disk database.
|
|
|
|
// Any errors that occur are only logged.
|
|
|
|
func listMeta(tx *bolt.Tx) []volumeMetadata {
|
|
|
|
var ls []volumeMetadata
|
2016-12-01 17:17:07 -05:00
|
|
|
b := tx.Bucket(volumeBucketName)
|
|
|
|
b.ForEach(func(k, v []byte) error {
|
2016-12-22 11:49:47 -05:00
|
|
|
if len(v) == 0 {
|
|
|
|
// don't try to unmarshal an empty value
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var m volumeMetadata
|
|
|
|
if err := json.Unmarshal(v, &m); err != nil {
|
|
|
|
// Just log the error
|
|
|
|
logrus.Errorf("Error while reading volume metadata for volume %q: %v", string(k), err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ls = append(ls, m)
|
2016-12-01 17:17:07 -05:00
|
|
|
return nil
|
|
|
|
})
|
2016-12-22 11:49:47 -05:00
|
|
|
return ls
|
2016-12-01 17:17:07 -05:00
|
|
|
}
|