fix: GetIImages correctly without 'image_cache' dir

After creating vms, there is no dir named 'image_cache' exists in
storage of esxi. So handle this case to not affecte the synchronization of the
VMware Template.
This commit is contained in:
rainzm
2020-08-14 20:36:58 +08:00
parent 3bc1381c36
commit c5a731daa9
+19 -16
View File
@@ -21,6 +21,7 @@ import (
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/onecloud/pkg/cloudprovider"
"yunion.io/x/onecloud/pkg/mcclient"
@@ -77,15 +78,31 @@ func (self *SDatastoreImageCache) GetPath() string {
func (self *SDatastoreImageCache) GetIImages() ([]cloudprovider.ICloudImage, error) {
ctx := context.Background()
ret := make([]cloudprovider.ICloudImage, 0, 2)
// get vm template with only one disk
tems, err := self.host.GetTemplateVMs()
if err != nil {
log.Errorf("fail to get templateVMs of host '%s' in SDatastoreImageCache.GetIImages", self.host.GetName())
return ret, nil
}
for _, tem := range tems {
// for now, add vm template with only one disk as cachedimage
if len(tem.vdisks) != 1 {
continue
}
ret = append(ret, NewVMTemplate(tem, self))
}
files, err := self.datastore.ListDir(ctx, IMAGE_CACHE_DIR_NAME)
if errors.Cause(err) == errors.ErrNotFound {
return ret, nil
}
if err != nil {
log.Errorf("GetIImages ListDir fail %s", err)
return nil, err
}
ret := make([]cloudprovider.ICloudImage, 0)
validFilenames := make(map[string]bool)
for i := 0; i < len(files); i += 1 {
@@ -119,20 +136,6 @@ func (self *SDatastoreImageCache) GetIImages() ([]cloudprovider.ICloudImage, err
}
}
// get vm template with only one disk
tems, err := self.host.GetTemplateVMs()
if err != nil {
log.Errorf("fail to get templateVMs of host '%s' in SDatastoreImageCache.GetIImages", self.host.GetName())
return ret, nil
}
for _, tem := range tems {
// for now, add vm template with only one disk as cachedimage
if len(tem.vdisks) != 1 {
continue
}
ret = append(ret, NewVMTemplate(tem, self))
}
return ret, nil
}