This repository has been archived by the owner on Jan 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
metadata.go
135 lines (117 loc) · 3.13 KB
/
metadata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
)
var (
recognizedOptions = []string{"share", "filemode", "dirmode", "uid", "gid", "nolock", "remotepath"}
)
type volumeMetadata struct {
CreatedAt time.Time `json:"created_at"`
Account string `json:"account"`
Options VolumeOptions `json:"options"`
}
// VolumeOptions stores the opts passed to the driver by the docker engine.
type VolumeOptions struct {
Share string `json:"share"`
FileMode string `json:"filemode"`
DirMode string `json:"dirmode"`
UID string `json:"uid"`
GID string `json:"gid"`
NoLock bool `json:"nolock"`
RemotePath string `json:"remotepath"`
}
type metadataDriver struct {
metaDir string
}
func newMetadataDriver(metaDir string) (*metadataDriver, error) {
if err := os.MkdirAll(metaDir, 0700); err != nil {
return nil, fmt.Errorf("error creating %s: %v", metaDir, err)
}
return &metadataDriver{metaDir}, nil
}
func (m *metadataDriver) Validate(meta map[string]string) (volumeMetadata, error) {
var v volumeMetadata
var opts VolumeOptions
// Validate keys
for k := range meta {
found := false
for _, opts := range recognizedOptions {
if k == opts {
found = true
break
}
}
if !found {
return v, fmt.Errorf("not a recognized volume driver option: %q", k)
}
}
opts.Share = meta["share"]
opts.DirMode = meta["dirmode"]
opts.FileMode = meta["filemode"]
opts.GID = meta["gid"]
opts.UID = meta["uid"]
opts.RemotePath = meta["remotepath"]
if meta["nolock"] == "true" {
opts.NoLock = true
}
return volumeMetadata{
Options: opts,
}, nil
}
func (m *metadataDriver) Delete(name string) error {
if err := os.RemoveAll(m.path(name)); err != nil {
return fmt.Errorf("cannot delete volume metadata: %v", err)
}
return nil
}
func (m *metadataDriver) Set(name string, meta volumeMetadata) error {
b, err := json.Marshal(meta)
if err != nil {
return fmt.Errorf("cannot serialize metadata: %v", err)
}
if err := ioutil.WriteFile(m.path(name), b, 0600); err != nil {
return fmt.Errorf("cannot write metadata: %v", err)
}
return nil
}
func (m *metadataDriver) Get(name string) (volumeMetadata, error) {
var v volumeMetadata
b, err := ioutil.ReadFile(m.path(name))
if err != nil {
return v, fmt.Errorf("cannot read metadata: %v", err)
}
if err := json.Unmarshal(b, &v); err != nil {
return v, fmt.Errorf("cannot deserialize metadata: %v", err)
}
return v, nil
}
func (m *metadataDriver) List() ([]string, error) {
var volumes []string
// return all the file names under metadata directory
if err := filepath.Walk(m.metaDir, func(path string, info os.FileInfo, inErr error) error {
if inErr != nil {
return inErr
}
if path == m.metaDir {
// directory itself, skip
return nil
}
if info.IsDir() { // a directory
return filepath.SkipDir
}
// base file name indicates the volume name
volumes = append(volumes, filepath.Base(path))
return nil
}); err != nil {
return volumes, fmt.Errorf("cannot list directory: %v", err)
}
return volumes, nil
}
func (m *metadataDriver) path(name string) string {
return filepath.Join(m.metaDir, name)
}