2018-12-17 05:27:07 -05:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
2018-11-09 09:10:41 -05:00
|
|
|
"sort"
|
|
|
|
|
2022-09-28 16:18:51 -04:00
|
|
|
"github.com/docker/docker/errdefs"
|
2020-08-28 08:35:09 -04:00
|
|
|
"github.com/fvbommel/sortorder"
|
2022-09-28 16:18:51 -04:00
|
|
|
"github.com/pkg/errors"
|
2018-12-17 05:27:07 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
metadataDir = "meta"
|
|
|
|
metaFile = "meta.json"
|
|
|
|
)
|
|
|
|
|
|
|
|
type metadataStore struct {
|
|
|
|
root string
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
2018-11-09 09:10:41 -05:00
|
|
|
func (s *metadataStore) contextDir(id contextdir) string {
|
|
|
|
return filepath.Join(s.root, string(id))
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
2019-04-18 09:12:30 -04:00
|
|
|
func (s *metadataStore) createOrUpdate(meta Metadata) error {
|
2018-11-09 09:10:41 -05:00
|
|
|
contextDir := s.contextDir(contextdirOf(meta.Name))
|
2022-09-30 13:13:22 -04:00
|
|
|
if err := os.MkdirAll(contextDir, 0o755); err != nil {
|
2018-12-17 05:27:07 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
bytes, err := json.Marshal(&meta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-30 13:13:22 -04:00
|
|
|
return os.WriteFile(filepath.Join(contextDir, metaFile), bytes, 0o644)
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseTypedOrMap(payload []byte, getter TypeGetter) (interface{}, error) {
|
|
|
|
if len(payload) == 0 || string(payload) == "null" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if getter == nil {
|
|
|
|
var res map[string]interface{}
|
|
|
|
if err := json.Unmarshal(payload, &res); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
typed := getter()
|
|
|
|
if err := json.Unmarshal(payload, typed); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return reflect.ValueOf(typed).Elem().Interface(), nil
|
|
|
|
}
|
|
|
|
|
2022-09-28 10:33:18 -04:00
|
|
|
func (s *metadataStore) get(name string) (Metadata, error) {
|
2022-09-28 16:18:51 -04:00
|
|
|
m, err := s.getByID(contextdirOf(name))
|
|
|
|
if err != nil {
|
2022-11-04 10:48:30 -04:00
|
|
|
return m, errors.Wrapf(err, "context %q", name)
|
2022-09-28 16:18:51 -04:00
|
|
|
}
|
|
|
|
return m, nil
|
2022-09-28 10:33:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *metadataStore) getByID(id contextdir) (Metadata, error) {
|
|
|
|
bytes, err := os.ReadFile(filepath.Join(s.contextDir(id), metaFile))
|
2018-12-17 05:27:07 -05:00
|
|
|
if err != nil {
|
2022-09-28 16:18:51 -04:00
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
2022-11-04 10:48:30 -04:00
|
|
|
return Metadata{}, errdefs.NotFound(errors.Wrap(err, "context not found"))
|
2022-09-28 16:18:51 -04:00
|
|
|
}
|
|
|
|
return Metadata{}, err
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
var untyped untypedContextMetadata
|
2019-04-18 09:12:30 -04:00
|
|
|
r := Metadata{
|
2018-12-17 05:27:07 -05:00
|
|
|
Endpoints: make(map[string]interface{}),
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(bytes, &untyped); err != nil {
|
2019-04-18 09:12:30 -04:00
|
|
|
return Metadata{}, err
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
2018-11-09 09:10:41 -05:00
|
|
|
r.Name = untyped.Name
|
2018-12-17 05:27:07 -05:00
|
|
|
if r.Metadata, err = parseTypedOrMap(untyped.Metadata, s.config.contextType); err != nil {
|
2019-04-18 09:12:30 -04:00
|
|
|
return Metadata{}, err
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
for k, v := range untyped.Endpoints {
|
|
|
|
if r.Endpoints[k], err = parseTypedOrMap(v, s.config.endpointTypes[k]); err != nil {
|
2019-04-18 09:12:30 -04:00
|
|
|
return Metadata{}, err
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return r, err
|
|
|
|
}
|
|
|
|
|
2022-09-28 10:33:18 -04:00
|
|
|
func (s *metadataStore) remove(name string) error {
|
2022-09-28 16:18:51 -04:00
|
|
|
if err := os.RemoveAll(s.contextDir(contextdirOf(name))); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to remove metadata")
|
|
|
|
}
|
|
|
|
return nil
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
2019-04-18 09:12:30 -04:00
|
|
|
func (s *metadataStore) list() ([]Metadata, error) {
|
2018-11-09 09:10:41 -05:00
|
|
|
ctxDirs, err := listRecursivelyMetadataDirs(s.root)
|
2018-12-17 05:27:07 -05:00
|
|
|
if err != nil {
|
2022-11-04 09:01:42 -04:00
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
2018-11-09 09:10:41 -05:00
|
|
|
return nil, nil
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-18 09:12:30 -04:00
|
|
|
var res []Metadata
|
2018-11-09 09:10:41 -05:00
|
|
|
for _, dir := range ctxDirs {
|
2022-09-28 10:33:18 -04:00
|
|
|
c, err := s.getByID(contextdir(dir))
|
2018-12-17 05:27:07 -05:00
|
|
|
if err != nil {
|
2022-11-04 09:01:42 -04:00
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
2022-09-28 16:21:31 -04:00
|
|
|
continue
|
|
|
|
}
|
2022-09-28 16:18:51 -04:00
|
|
|
return nil, errors.Wrap(err, "failed to read metadata")
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
2018-11-09 09:10:41 -05:00
|
|
|
res = append(res, c)
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
2018-11-09 09:10:41 -05:00
|
|
|
sort.Slice(res, func(i, j int) bool {
|
|
|
|
return sortorder.NaturalLess(res[i].Name, res[j].Name)
|
|
|
|
})
|
2018-12-17 05:27:07 -05:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isContextDir(path string) bool {
|
|
|
|
s, err := os.Stat(filepath.Join(path, metaFile))
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return !s.IsDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
func listRecursivelyMetadataDirs(root string) ([]string, error) {
|
2022-02-25 08:37:07 -05:00
|
|
|
fis, err := os.ReadDir(root)
|
2018-12-17 05:27:07 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var result []string
|
|
|
|
for _, fi := range fis {
|
|
|
|
if fi.IsDir() {
|
|
|
|
if isContextDir(filepath.Join(root, fi.Name())) {
|
|
|
|
result = append(result, fi.Name())
|
|
|
|
}
|
|
|
|
subs, err := listRecursivelyMetadataDirs(filepath.Join(root, fi.Name()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, s := range subs {
|
2022-09-28 11:09:59 -04:00
|
|
|
result = append(result, filepath.Join(fi.Name(), s))
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type untypedContextMetadata struct {
|
|
|
|
Metadata json.RawMessage `json:"metadata,omitempty"`
|
|
|
|
Endpoints map[string]json.RawMessage `json:"endpoints,omitempty"`
|
2018-11-09 09:10:41 -05:00
|
|
|
Name string `json:"name,omitempty"`
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|