-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
340 lines (293 loc) · 8.03 KB
/
store.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package imagestore
// TODO:
// - Add a job to cleanup any failed download attempts
// - Add a job to compare disk to metadata, remove any metadata that has no dataset - the opposite is harder
import (
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"syscall"
"github.com/mistifyio/kvite"
"github.com/mistifyio/mistify-agent/client"
"github.com/mistifyio/mistify-agent/rpc"
logx "github.com/mistifyio/mistify-logrus-ext"
"gopkg.in/mistifyio/go-zfs.v1"
)
const (
// EAGAIN is a shortcut to syscall.EAGAIN
EAGAIN = syscall.EAGAIN
// EEXIST is a shortcut to syscall.EEXIST
EEXIST = syscall.EEXIST
// ENOSPC is a shortcut to syscall.ENOSPC
ENOSPC = syscall.ENOSPC
// EINVAL is a shortcut to syscall.EINVAL
EINVAL = syscall.EINVAL
// DBTABLE is the tablename for images
DBTABLE = "mistify-agent-image"
)
var (
// ErrNotFound is an error when resource not being found
ErrNotFound = errors.New("not found")
// ErrNotVolume is an error when the resouce is expected to be a volume and isn't
ErrNotVolume = errors.New("not a volume")
// ErrNotSnapshot is an error when the resouce is expected to be a snapshot and isn't
ErrNotSnapshot = errors.New("not a snapshot")
// ErrNotValid is an error when the resouce is expected to be a dataset and isn't
ErrNotValid = errors.New("not a valid dataset")
)
type (
// ImageStore manages disk images
ImageStore struct {
// Config passed in
config Config
// clone worker - we only use one for now
cloneWorker *cloneWorker
// clone requests
usersCloneChan chan *cloneRequest
fetcher *fetcher
// exit signal
timeToDie chan struct{}
// root of the image store
dataset string
DB *kvite.DB
tempDir string
}
// Config contains configuration for the ImageStore
Config struct {
ImageServer string // if we get a relative url, we prepend this
NumFetchers uint // workers to use for fetching images
MaxPending uint // maximum number of queued fetch image
Zpool string
}
)
// Create creates an image store with the given config
func Create(config Config) (*ImageStore, error) {
if config.NumFetchers == 0 {
config.NumFetchers = uint(runtime.NumCPU())
}
store := &ImageStore{
config: config,
usersCloneChan: make(chan *cloneRequest),
timeToDie: make(chan struct{}),
tempDir: filepath.Join("/", config.Zpool, "images", "temp"),
dataset: filepath.Join(config.Zpool, "images"),
}
_, err := zfs.GetDataset(store.dataset)
if err != nil {
if strings.Contains(err.Error(), "dataset does not exist") {
_, err := zfs.CreateFilesystem(store.dataset, nil)
if err != nil {
return nil, err
}
} else {
return nil, err
}
}
guestPath := filepath.Join(config.Zpool, "guests")
if _, err := zfs.GetDataset(guestPath); err != nil {
if strings.Contains(err.Error(), "dataset does not exist") {
_, err := zfs.CreateFilesystem(guestPath, nil)
if err != nil {
return nil, err
}
} else {
return nil, err
}
}
fi, err := os.Stat(store.tempDir)
if err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(store.tempDir, 0755); err != nil {
return nil, err
}
} else {
return nil, err
}
}
if fi != nil {
if !fi.Mode().IsDir() {
return nil, fmt.Errorf("%s is not a directory", store.tempDir)
}
}
db, err := kvite.Open(filepath.Join("/", config.Zpool, "images", ".images.db"), DBTABLE)
if err != nil {
return nil, err
}
store.DB = db
err = store.DB.Transaction(func(tx *kvite.Tx) error {
_, err := tx.CreateBucketIfNotExists("images")
return err
})
if err != nil {
return nil, err
}
// start our clone worker
store.cloneWorker = newCloneWorker(store)
// start the fetcher
store.fetcher = newFetcher(store, config.MaxPending, config.NumFetchers)
return store, nil
}
// Destroy destroys a store
func (store *ImageStore) Destroy() error {
var q struct{}
// singal shutdown
store.timeToDie <- q
// wait for shutdown
<-store.timeToDie
return nil
}
// Run starts processing for jobs
func (store *ImageStore) Run() {
store.cloneWorker.Run()
store.fetcher.run()
q := <-store.timeToDie
store.cloneWorker.Exit()
store.fetcher.exit()
logx.LogReturnedErr(store.DB.Close, nil, "failed to close store")
store.timeToDie <- q
}
// TODO: have a background thread to update from datasets? no images should come through
// unless they are in the database
// SpaceAvailible returns the available disk space
// ensure we are not "over-committing" on disk
func (store *ImageStore) SpaceAvailible() (uint64, error) {
var total uint64
ds, err := zfs.GetDataset(store.config.Zpool)
if err != nil {
return 0, err
}
total = ds.Avail
if ds.Quota != 0 && ds.Quota < total {
total = ds.Quota
}
ds, err = zfs.GetDataset(filepath.Join(store.config.Zpool, "guests"))
if err != nil {
return 0, err
}
if ds.Quota != 0 && ds.Quota < total {
total = ds.Quota
}
datasets, err := zfs.Datasets(store.config.Zpool)
if err != nil {
return 0, err
}
for _, ds := range datasets {
switch ds.Type {
//filesystems roll up into top-level usage (I think)
case "filesystem":
case "snapshot":
// not sure this is correct
total = total - ds.Written
case "volume":
total = total - ds.Volsize
}
}
return total / 1024, nil
}
// VerifyDisks verifys a guests's disk configuration before vm creation
// used for pre-flight check for vm creation
// we should also check to see if we have enough disk space for it. perhaps in a seperate call?
func (store *ImageStore) VerifyDisks(r *http.Request, request *rpc.GuestRequest, response *rpc.GuestResponse) error {
if request.Guest == nil || request.Guest.ID == "" || len(request.Guest.Disks) == 0 {
return EINVAL
}
availible, err := store.SpaceAvailible()
if err != nil {
return err
}
var total uint64
for i := range request.Guest.Disks {
disk := &request.Guest.Disks[i]
if disk.Image == "" && disk.Size == 0 {
return EINVAL
}
if disk.Image != "" {
image, err := store.getImage(disk.Image)
if err != nil {
return err
}
disk.Size = image.Size
}
total = total + disk.Size
}
if total > availible {
return ENOSPC
}
*response = rpc.GuestResponse{
Guest: request.Guest,
}
return nil
}
// CreateGuestDisks creates guest disks
func (store *ImageStore) CreateGuestDisks(r *http.Request, request *rpc.GuestRequest, response *rpc.GuestResponse) error {
err := store.VerifyDisks(r, request, response)
if err != nil {
return err
}
// VerifyDisks filled in response
guest := response.Guest
for i := range guest.Disks {
disk := &guest.Disks[i]
disk.Volume = fmt.Sprintf("%s/guests/%s/disk-%d", store.config.Zpool, guest.ID, i)
ds, err := zfs.GetDataset(disk.Volume)
if err == nil {
//already exists
disk.Source = deviceForDataset(ds)
continue
} else {
if !strings.Contains(err.Error(), "does not exist") {
return err
}
}
if disk.Image != "" {
image, err := store.getImage(disk.Image)
if err != nil {
return err
}
s, err := zfs.GetDataset(image.Snapshot)
if err != nil {
return err
}
ds, err := s.Clone(disk.Volume, defaultZFSOptions)
if err != nil {
return err
}
disk.Source = deviceForDataset(ds)
} else {
ds, err := zfs.CreateVolume(disk.Volume, disk.Size*1024*1024, defaultZFSOptions)
if err != nil {
return err
}
disk.Source = deviceForDataset(ds)
}
}
return nil
}
// DeleteGuestsDisks removes guests disks. It actually removes the entire guest filesystem.
func (store *ImageStore) DeleteGuestsDisks(r *http.Request, request *rpc.GuestRequest, response *rpc.GuestResponse) error {
if request.Guest == nil || request.Guest.ID == "" {
return EINVAL
}
name := fmt.Sprintf("%s/guests/%s", store.config.Zpool, request.Guest.ID)
ds, err := zfs.GetDataset(name)
*response = rpc.GuestResponse{
Guest: request.Guest,
}
response.Guest.Disks = []client.Disk{}
if err != nil {
if strings.Contains(err.Error(), "not found") {
// not there
return nil
}
return err
}
// we assume guest disk were created by this service, or at least in the same structure
if err := ds.Destroy(true); err != nil {
return err
}
return nil
}