forked from vmware/vic
/
store_files.go
390 lines (323 loc) · 11.4 KB
/
store_files.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Copyright 2016 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package management
import (
"bytes"
"fmt"
"path"
"sort"
"strings"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/types"
"github.com/vmware/vic/cmd/vic-machine/common"
"github.com/vmware/vic/lib/config"
"github.com/vmware/vic/lib/constants"
"github.com/vmware/vic/pkg/errors"
"github.com/vmware/vic/pkg/trace"
"github.com/vmware/vic/pkg/vsphere/datastore"
)
const (
volumeRoot = "volumes"
dsScheme = "ds"
)
func (d *Dispatcher) deleteImages(conf *config.VirtualContainerHostConfigSpec) error {
defer trace.End(trace.Begin("", d.op))
var errs []string
d.op.Info("Removing image stores")
for _, imageDir := range conf.ImageStores {
imageDSes, err := d.session.Finder.DatastoreList(d.op, imageDir.Host)
if err != nil {
errs = append(errs, err.Error())
continue
}
if len(imageDSes) != 1 {
errs = append(errs, fmt.Sprintf("Found %d datastores with provided datastore path %s. Provided datastore path must identify exactly one datastore.",
len(imageDSes),
imageDir.String()))
continue
}
// delete images subfolder
imagePath := path.Join(imageDir.Path, constants.StorageParentDir)
if _, err = d.deleteDatastoreFiles(imageDSes[0], imagePath, true); err != nil {
errs = append(errs, err.Error())
}
// delete kvStores subfolder
kvPath := path.Join(imageDir.Path, constants.KVStoreFolder)
if _, err = d.deleteDatastoreFiles(imageDSes[0], kvPath, true); err != nil {
errs = append(errs, err.Error())
}
dsPath, err := datastore.URLtoDatastore(&imageDir)
if err != nil {
errs = append(errs, err.Error())
continue
}
children, err := d.getChildren(imageDSes[0], dsPath)
if err != nil {
if !types.IsFileNotFound(err) {
errs = append(errs, err.Error())
}
continue
}
if len(children) == 0 {
d.op.Debugf("Removing empty image store parent directory [%s] %s", imageDir.Host, imageDir.Path)
if _, err = d.deleteDatastoreFiles(imageDSes[0], imageDir.Path, true); err != nil {
errs = append(errs, err.Error())
}
} else {
d.op.Debug("Image store parent directory not empty, leaving in place. Still contains the following entries: %q", strings.Join(children, ", "))
}
}
if len(errs) > 0 {
return errors.New(strings.Join(errs, "\n"))
}
return nil
}
func (d *Dispatcher) deleteParent(ds *object.Datastore, root string) (bool, error) {
defer trace.End(trace.Begin("", d.op))
// alway forcing delete images
return d.deleteDatastoreFiles(ds, root, true)
}
func (d *Dispatcher) deleteDatastoreFiles(ds *object.Datastore, path string, force bool) (bool, error) {
defer trace.End(trace.Begin(fmt.Sprintf("path %q, force %t", path, force), d.op))
if ds == nil {
err := errors.Errorf("No datastore")
return false, err
}
// refuse to delete everything on the datstore, ignore force
if path == "" {
// #nosec: Errors unhandled.
dsn, _ := ds.ObjectName(d.op)
msg := fmt.Sprintf("refusing to remove datastore files for path \"\" on datastore %q", dsn)
return false, errors.New(msg)
}
var empty bool
dsPath := ds.Path(path)
res, err := d.lsFolder(ds, dsPath)
if err != nil {
if !types.IsFileNotFound(err) {
err = errors.Errorf("Failed to browse folder %q: %s", dsPath, err)
return empty, err
}
d.op.Debugf("Folder %q is not found", dsPath)
empty = true
return empty, nil
}
if len(res.File) > 0 && !force {
d.op.Debugf("Folder %q is not empty, leave it there", dsPath)
return empty, nil
}
m := ds.NewFileManager(d.session.Datacenter, true)
if err = d.deleteFilesIteratively(m, ds, dsPath); err != nil {
return empty, err
}
return true, nil
}
func (d *Dispatcher) isVSAN(ds *object.Datastore) bool {
// #nosec: Errors unhandled.
dsType, _ := ds.Type(d.op)
return dsType == types.HostFileSystemVolumeFileSystemTypeVsan
}
func (d *Dispatcher) deleteFilesIteratively(m *object.DatastoreFileManager, ds *object.Datastore, dsPath string) error {
defer trace.End(trace.Begin(dsPath, d.op))
// If deleting top level folder fails, remove the child files to empty the folder first
err := d.deleteVMFSFiles(m, ds, dsPath)
if err != nil {
d.op.Debugf("Attempt to delete top level folder %s failed. Remove the children files instead.", dsPath)
res, err := d.getSortedChildren(ds, dsPath)
if err != nil {
if !types.IsFileNotFound(err) {
err = errors.Errorf("Failed to browse sub folders %q: %s", dsPath, err)
return err
}
d.op.Debugf("Folder %q is not found", dsPath)
return nil
}
for _, path := range res {
if err = d.deleteVMFSFiles(m, ds, path); err != nil {
return err
}
}
return d.deleteVMFSFiles(m, ds, dsPath)
}
return nil
}
func (d *Dispatcher) deleteVMFSFiles(m *object.DatastoreFileManager, ds *object.Datastore, dsPath string) error {
defer trace.End(trace.Begin(dsPath, d.op))
for _, ext := range []string{"-delta.vmdk", "-flat.vmdk", "-sesparse.vmdk"} {
if strings.HasSuffix(dsPath, ext) {
// Skip backing files, as Delete() call below will remove all related vmdk files via DeleteVirtualDisk
return nil
}
}
if err := m.Delete(d.op, dsPath); err != nil {
d.op.Debugf("Failed to delete %q: %s", dsPath, err)
return err
}
return nil
}
// getChildren returns all children under datastore path in unsorted order. (see also getSortedChildren)
func (d *Dispatcher) getChildren(ds *object.Datastore, dsPath string) ([]string, error) {
res, err := d.lsSubFolder(ds, dsPath)
if err != nil {
return nil, err
}
var result []string
for _, dir := range res.HostDatastoreBrowserSearchResults {
for _, f := range dir.File {
dsf, ok := f.(*types.FileInfo)
if !ok {
continue
}
result = append(result, path.Join(dir.FolderPath, dsf.Path))
}
}
return result, nil
}
// getSortedChildren returns all children under datastore path in reversed order.
func (d *Dispatcher) getSortedChildren(ds *object.Datastore, dsPath string) ([]string, error) {
result, err := d.getChildren(ds, dsPath)
if err != nil {
return nil, err
}
sort.Sort(sort.Reverse(sort.StringSlice(result)))
return result, nil
}
func (d *Dispatcher) lsSubFolder(ds *object.Datastore, dsPath string) (*types.ArrayOfHostDatastoreBrowserSearchResults, error) {
defer trace.End(trace.Begin(dsPath, d.op))
spec := types.HostDatastoreBrowserSearchSpec{
MatchPattern: []string{"*"},
}
b, err := ds.Browser(d.op)
if err != nil {
return nil, err
}
task, err := b.SearchDatastoreSubFolders(d.op, dsPath, &spec)
if err != nil {
return nil, err
}
info, err := task.WaitForResult(d.op, nil)
if err != nil {
return nil, err
}
res := info.Result.(types.ArrayOfHostDatastoreBrowserSearchResults)
return &res, nil
}
func (d *Dispatcher) lsFolder(ds *object.Datastore, dsPath string) (*types.HostDatastoreBrowserSearchResults, error) {
defer trace.End(trace.Begin(dsPath, d.op))
spec := types.HostDatastoreBrowserSearchSpec{
MatchPattern: []string{"*"},
}
b, err := ds.Browser(d.op)
if err != nil {
return nil, err
}
task, err := b.SearchDatastore(d.op, dsPath, &spec)
if err != nil {
return nil, err
}
info, err := task.WaitForResult(d.op, nil)
if err != nil {
return nil, err
}
res := info.Result.(types.HostDatastoreBrowserSearchResults)
return &res, nil
}
func (d *Dispatcher) createVolumeStores(conf *config.VirtualContainerHostConfigSpec) error {
defer trace.End(trace.Begin("", d.op))
for _, url := range conf.VolumeLocations {
// NFS volumestores need only make it into the config of the vch
if url.Scheme != dsScheme {
d.op.Debugf("Skipping nfs volume store for vic-machine creation operation : (%s)", url.String())
continue
}
ds, err := d.session.Finder.Datastore(d.op, url.Host)
if err != nil {
return errors.Errorf("Could not retrieve datastore with host %q due to error %s", url.Host, err)
}
if url.Path == "/" || url.Path == "" {
url.Path = constants.StorageParentDir
}
nds, err := datastore.NewHelper(d.op, d.session, ds, url.Path)
if err != nil {
return errors.Errorf("Could not create volume store due to error: %s", err)
}
// FIXME: (GitHub Issue #1301) this is not valid URL syntax and should be translated appropriately when time allows
url.Path = nds.RootURL.String()
}
return nil
}
// returns # of removed stores
func (d *Dispatcher) deleteVolumeStoreIfForced(conf *config.VirtualContainerHostConfigSpec, volumeStores *DeleteVolumeStores) (removed int) {
defer trace.End(trace.Begin("", d.op))
removed = 0
deleteVolumeStores := d.force || (volumeStores != nil && *volumeStores == AllVolumeStores)
if !deleteVolumeStores {
if len(conf.VolumeLocations) == 0 {
return 0
}
dsVolumeStores := new(bytes.Buffer)
nfsVolumeStores := new(bytes.Buffer)
for label, url := range conf.VolumeLocations {
switch url.Scheme {
case common.DsScheme:
dsVolumeStores.WriteString(fmt.Sprintf("\t%s: %s\n", label, url.Path))
case common.NfsScheme:
nfsVolumeStores.WriteString(fmt.Sprintf("\t%s: %s\n", label, url.Path))
}
}
d.op.Warnf("Since --force was not specified, the following volume stores will not be removed. Use the vSphere UI or supplied nfs targets to delete content you do not wish to keep.\n vsphere volumestores:\n%s\n NFS volumestores:\n%s\n", dsVolumeStores.String(), nfsVolumeStores.String())
return 0
}
d.op.Info("Removing volume stores")
for label, url := range conf.VolumeLocations {
// NOTE: We cannot remove nfs VolumeStores at vic-machine delete time. We are not guaranteed to be on the correct network for any of the nfs stores.
if url.Scheme != dsScheme {
d.op.Warnf("Cannot delete VolumeStore (%s). It may not be reachable by vic-machine and has been skipped by the delete process.", url.String())
continue
}
// FIXME: url is being encoded by the portlayer incorrectly, so we have to convert url.Path to the right url.URL object
dsURL, err := datastore.ToURL(url.Path)
if err != nil {
d.op.Warnf("Didn't receive an expected volume store path format: %q", url.Path)
continue
}
if dsURL.Path == constants.StorageParentDir {
dsURL.Path = path.Join(dsURL.Path, constants.VolumesDir)
}
d.op.Debugf("Provided datastore URL: %q", url.Path)
d.op.Debugf("Parsed volume store path: %q", dsURL.Path)
d.op.Infof("Deleting volume store %q on Datastore %q at path %q",
label, dsURL.Host, dsURL.Path)
datastores, err := d.session.Finder.DatastoreList(d.op, dsURL.Host)
if err != nil {
d.op.Errorf("Error finding datastore %q: %s", dsURL.Host, err)
continue
}
if len(datastores) > 1 {
foundDatastores := new(bytes.Buffer)
for _, d := range datastores {
foundDatastores.WriteString(fmt.Sprintf("\n%s\n", d.InventoryPath))
}
d.op.Errorf("Ambiguous datastore name (%q) provided. Results were: %q", dsURL.Host, foundDatastores)
continue
}
datastore := datastores[0]
if _, err := d.deleteDatastoreFiles(datastore, dsURL.Path, deleteVolumeStores); err != nil {
d.op.Errorf("Failed to delete volume store %q on Datastore %q at path %q", label, dsURL.Host, dsURL.Path)
} else {
removed++
}
}
return removed
}