-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
270 lines (220 loc) · 5.87 KB
/
storage.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package storage
/*
µdensity can be rebooted without loosing task.Task
*/
import (
"encoding/json"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/factorysh/microdensity/task"
"github.com/factorysh/microdensity/volumes"
)
// DirMode is the default dirmode for volumes
const DirMode fs.FileMode = 0755
const volumesDir = "volumes"
const taskFile = "task.json"
const latestFile = "latest"
// Storage describe all storage primitives
type Storage interface {
Upsert(*task.Task) error
Get(id string) (*task.Task, error)
GetByCommit(service, project, branch, commit string, latest bool) (*task.Task, error)
All() ([]*task.Task, error)
Filter(func(*task.Task) bool) ([]*task.Task, error)
Delete(id string) error
SetLatest(*task.Task) error
GetLatest(service, project, branch string) (*task.Task, error)
GetVolumePath(*task.Task) string
EnsureVolumesDir(*task.Task) error
}
// FSStore contains all storage data and primitives directly on the FS
type FSStore struct {
root string
volumes *volumes.Volumes
}
var _ Storage = (*FSStore)(nil)
// NewFSStore inits a new filesystem store
func NewFSStore(root string) (*FSStore, error) {
err := os.MkdirAll(root, DirMode)
if err != nil {
return nil, err
}
v, err := volumes.New(root)
if err != nil {
return nil, err
}
return &FSStore{
root: root,
volumes: v,
}, nil
}
func (s *FSStore) taskRootPath(t *task.Task) string {
return filepath.Join(s.root, t.Service, t.Project, t.Branch, t.Id.String())
}
func (s *FSStore) taskFilePath(t *task.Task) string {
return filepath.Join(s.taskRootPath(t), taskFile)
}
func (s *FSStore) taskLatestPath(t *task.Task) string {
return filepath.Join(s.root, t.Service, t.Project, t.Branch, latestFile)
}
func taskFromJSON(path string) (*task.Task, error) {
path = filepath.Clean(path)
// read json file
jsonFile, err := os.Open(path) //#nosec I don't know what a correct is
if err != nil {
return nil, err
}
// decode json
var t task.Task
err = json.NewDecoder(jsonFile).Decode(&t)
err2 := jsonFile.Close()
if err != nil {
return nil, err
}
if err2 != nil {
return nil, err2
}
return &t, nil
}
// Upsert takes a task and write it to the underlying fs
func (s *FSStore) Upsert(t *task.Task) error {
// construct the tree on the FS
err := os.MkdirAll(s.GetVolumePath(t), DirMode)
if err != nil {
return err
}
f, err := os.OpenFile(s.taskFilePath(t), os.O_CREATE+os.O_WRONLY, 0600)
if err != nil {
return err
}
err = json.NewEncoder(f).Encode(t)
err2 := f.Close()
if err != nil {
return err
}
return err2
}
// Get takes an id and return a task
func (s *FSStore) Get(id string) (*task.Task, error) {
taskRootPath := ""
err := filepath.WalkDir(s.root, func(path string, d fs.DirEntry, err error) error {
// check for errors
if err != nil {
return err
}
// do not work inside volumes dir
if d.Name() == volumesDir {
return filepath.SkipDir
}
// if task not found
if taskRootPath == "" && d.Name() == id {
taskRootPath = path
}
return nil
})
// if an error occured return
if err != nil {
return nil, err
}
// if not found
if taskRootPath == "" {
return nil, fmt.Errorf("task with id %s not found", id)
}
return taskFromJSON(filepath.Join(taskRootPath, taskFile))
}
// GetByCommit gets the task using the full path from service to commit
func (s *FSStore) GetByCommit(service, project, branch, commit string, latest bool) (*task.Task, error) {
// if latest return early
if latest {
latest, err := s.GetLatest(service, project, branch)
if err != nil {
return nil, err
}
return latest, nil
}
// if not latest, do the heavy stuff
var t *task.Task
basePath := filepath.Join(s.root, service, project, branch)
dirs, err := os.ReadDir(basePath)
if err != nil {
return nil, err
}
for _, dir := range dirs {
t, err = s.Get(dir.Name())
if err != nil {
continue
}
if t.Commit == commit {
return t, nil
}
}
return nil, fmt.Errorf("task with commit `%s` not found", commit)
}
// All returns all the tasks for this storage
func (s *FSStore) All() ([]*task.Task, error) {
var tasks []*task.Task
err := filepath.WalkDir(s.root, func(path string, d fs.DirEntry, err error) error {
// check for errors
if err != nil {
return err
}
// do not work inside volumes dir
if d.Name() == volumesDir {
return filepath.SkipDir
}
if d.Name() == taskFile {
task, err := taskFromJSON(path)
if err != nil {
// FIXME: handle
fmt.Println(err)
return nil
}
tasks = append(tasks, task)
}
return nil
})
if err != nil {
return nil, err
}
return tasks, nil
}
// Filter return all the tasks matching the required predicates from the filter function
func (s *FSStore) Filter(filterFn func(*task.Task) bool) ([]*task.Task, error) {
// TODO: later
return nil, nil
}
// Delete takes an id and delete a task in the fs
func (s *FSStore) Delete(id string) error {
t, err := s.Get(id)
if err != nil {
return err
}
return os.RemoveAll(s.taskRootPath(t))
}
// SetLatest is used save task as latest for this branch
func (s *FSStore) SetLatest(t *task.Task) error {
return os.WriteFile(s.taskLatestPath(t), []byte(t.Id.String()), 0600)
}
// GetLatest is used to get latest task for a service, project, branch
func (s *FSStore) GetLatest(service, project, branch string) (*task.Task, error) {
pth := filepath.Clean(filepath.Join(s.root, service, project, branch, latestFile))
if !strings.HasPrefix(pth, filepath.Join(s.root, service, project, branch)) {
panic("path escape: " + pth)
}
content, err := os.ReadFile(pth)
if err != nil {
return nil, err
}
return s.Get(string(content))
}
// GetVolumePath is used to get the root volume path of a task
func (s *FSStore) GetVolumePath(t *task.Task) string {
return filepath.Join(s.taskRootPath(t), volumesDir)
}
// EnsureVolumesDir is used to create required volume dirs
func (s *FSStore) EnsureVolumesDir(t *task.Task) error {
return s.volumes.Create(t)
}