Remove box count check when trying to recover boxes
The get box + stat operations are not that slow. No need to also go through listing out all the boxes in the vagrant box dir.
This commit is contained in:
parent
c23e617fce
commit
211da5be23
@ -228,58 +228,25 @@ func (b *BoxCollection) RecoverBoxes() (err error) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dirBoxes, err := b.listBoxDir()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// If the number of boxes in the box directory and in the database match then
|
||||
// assume that the box collection is in order (eg. all the boxes in the box
|
||||
// directory and db match)
|
||||
if len(dirBoxes) == len(resp.Boxes) {
|
||||
return
|
||||
}
|
||||
|
||||
// If the number of boxes in the boxes dir is less than in the db then there are
|
||||
// extra boxes in the db. The extra boxes in the db should be removed from the db
|
||||
// since they are not actually available.
|
||||
// If the number of boxes in the boxes dir is more than in the db, then that's ok.
|
||||
if len(dirBoxes) < len(resp.Boxes) {
|
||||
for _, boxRef := range resp.Boxes {
|
||||
box, erro := b.basis.client.GetBox(b.basis.ctx, &vagrant_server.GetBoxRequest{Box: boxRef})
|
||||
// If the box directory does not exist, then the box doesn't exist.
|
||||
if _, err := os.Stat(box.Box.Directory); err != nil {
|
||||
// Remove the box
|
||||
_, erro := b.basis.client.DeleteBox(b.basis.ctx, &vagrant_server.DeleteBoxRequest{Box: boxRef})
|
||||
if erro != nil {
|
||||
return erro
|
||||
}
|
||||
}
|
||||
// Ensure that each box exists
|
||||
for _, boxRef := range resp.Boxes {
|
||||
box, erro := b.basis.client.GetBox(b.basis.ctx, &vagrant_server.GetBoxRequest{Box: boxRef})
|
||||
// If the box directory does not exist, then the box doesn't exist.
|
||||
if _, err := os.Stat(box.Box.Directory); err != nil {
|
||||
// Remove the box
|
||||
_, erro := b.basis.client.DeleteBox(b.basis.ctx, &vagrant_server.DeleteBoxRequest{Box: boxRef})
|
||||
if erro != nil {
|
||||
return erro
|
||||
}
|
||||
}
|
||||
if erro != nil {
|
||||
return erro
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (b *BoxCollection) listBoxDir() (boxPaths []string, err error) {
|
||||
boxPaths = []string{}
|
||||
err = filepath.Walk(b.directory,
|
||||
func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Each box has a metadata.json file. If that file is found, then
|
||||
// the path belonds to a box.
|
||||
if filepath.Base(path) == "metadata.json" {
|
||||
boxPaths = append(boxPaths, strings.ReplaceAll(path, "metadata.json", ""))
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (b *BoxCollection) generateDirectoryName(path string) (out string) {
|
||||
out = strings.ReplaceAll(path, ":", VagrantColon)
|
||||
return strings.ReplaceAll(out, "/", VagrantSlash)
|
||||
|
||||
@ -154,7 +154,7 @@ func TestFind(t *testing.T) {
|
||||
require.NotNil(t, boxes)
|
||||
}
|
||||
|
||||
func TestRemoveBox(t *testing.T) {
|
||||
func TestRemoveMissingBox(t *testing.T) {
|
||||
// Create initial box collection
|
||||
bc := newBoxCollection(t)
|
||||
td, err := ioutil.TempDir("/tmp", "box")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user