bugfix(esxiagent): PrefetchImageCacheByCopy & SHost.FileUrlPathToDsPath

1. Fix the bug when preparing remote image by copy
2. SHost.FileUrlPathToDsPath will not work when shost has multiple storage.
This commit is contained in:
Rain
2020-01-16 22:00:07 +08:00
parent 3e9915523f
commit bc83e2940a
6 changed files with 33 additions and 28 deletions
@@ -18,12 +18,14 @@ import (
"context"
"fmt"
"path/filepath"
"reflect"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
"yunion.io/x/onecloud/pkg/cloudprovider"
"yunion.io/x/onecloud/pkg/compute/models"
"yunion.io/x/onecloud/pkg/hostman/hostutils"
"yunion.io/x/onecloud/pkg/multicloud/esxi"
@@ -96,20 +98,20 @@ func (c *SAgentImageCacheManager) prefetchImageCacheByCopy(ctx context.Context,
exists := false
log.Infof("check file: src=%s, dst=%s", srcPath, dstPath)
dstVmdkInfo, err := dstDs.GetVmdkInfo(ctx, dstPath)
if err != nil {
if err != nil && errors.Cause(err) != cloudprovider.ErrNotFound {
return nil, err
}
srcVmdkInfo, err := srcDs.GetVmdkInfo(ctx, srcPath)
if err != nil {
return nil, err
}
if dstVmdkInfo == srcVmdkInfo {
if dstVmdkInfo != nil && reflect.DeepEqual(dstVmdkInfo, srcVmdkInfo) {
exists = true
}
dstUrl := dstDs.GetPathUrl(dstPath)
if !exists || data.IsForce {
_, err = dstDs.MakeDir(ctx, dstPath)
err = dstDs.CheckDirC(filepath.Dir(dstPath))
if err != nil {
return nil, errors.Wrap(err, "dstDs.MakeDir")
}
@@ -124,11 +126,12 @@ func (c *SAgentImageCacheManager) prefetchImageCacheByCopy(ctx context.Context,
return nil, errors.Wrap(err, "dstDs.GetVmdkInfo")
}
}
dstPath = dstDs.GetFullPath(dstPath)
ret := jsonutils.NewDict()
ret.Add(jsonutils.NewInt(dstVmdkInfo.Size()), "size")
ret.Add(jsonutils.NewString(dstUrl), "path")
ret.Add(jsonutils.NewString(dstPath), "path")
ret.Add(jsonutils.NewString(data.ImageId), "image_id")
_, err = hostutils.RemoteStoragecacheCacheImage(ctx, data.StoragecacheId, data.ImageId, "ready", dstUrl)
_, err = hostutils.RemoteStoragecacheCacheImage(ctx, data.StoragecacheId, data.ImageId, "ready", dstPath)
if err != nil {
return nil, err
}
@@ -189,7 +192,7 @@ func (c *SAgentImageCacheManager) prefetchImageCacheByUpload(ctx context.Context
return nil, errors.Wrap(err, "SDatastore.ImportTemplate")
}
}
remotePath = filepath.Join(ds.GetUrl(), remotePath)
remotePath = ds.GetFullPath(remotePath)
remoteImg := localImage.(*jsonutils.JSONDict)
remoteImg.Add(jsonutils.NewString(remotePath), "path")
+7 -3
View File
@@ -680,7 +680,7 @@ func (self *SHost) DoCreateVM(ctx context.Context, ds *SDatastore, data *jsonuti
if len(cdromPath) != 0 && !strings.HasPrefix(cdromPath, "[") {
cdromPath, err = self.FileUrlPathToDsPath(cdromPath)
if err != nil {
return nil, errors.Wrapf(err, "SHost.FileUrlPathToDsPath for cdrom path '%s'", cdromPath)
return nil, errors.Wrapf(err, "SHost.FileUrlPathToDsPath", cdromPath)
}
}
deviceChange = append(deviceChange, addDevSpec(NewCDROMDev(cdromPath, 16000, 201)))
@@ -702,7 +702,7 @@ func (self *SHost) DoCreateVM(ctx context.Context, ds *SDatastore, data *jsonuti
} else {
imagePath, err = self.FileUrlPathToDsPath(imagePath)
if err != nil {
return nil, errors.Wrapf(err, "SHost.FileUrlPathToDsPath for image path '%s'", imagePath)
return nil, errors.Wrapf(err, "SHost.FileUrlPathToDsPath", imagePath)
}
}
uuid, _ := disk.GetString("disk_id")
@@ -928,10 +928,14 @@ func (host *SHost) FileUrlPathToDsPath(path string) (string, error) {
}
for _, ds := range dss {
rds := ds.(*SDatastore)
log.Debugf("rds: %s", rds.GetUrl())
if strings.HasPrefix(path, rds.GetUrl()) {
newPath = fmt.Sprintf("[%s] %s", rds.GetRelName(), path[len(rds.GetUrl()):])
break
}
break
}
if len(newPath) == 0 {
return newPath, fmt.Errorf("path '%s' don't belong any datastore of host '%s'", path, host.GetName())
}
return newPath, nil
}
+2 -2
View File
@@ -504,7 +504,7 @@ func (cli *SESXiClient) CopyDisk(ctx context.Context, src, dst string, isForce b
dm := object.NewVirtualDiskManager(cli.client.Client)
task, err := dm.CopyVirtualDisk(ctx, src, nil, dst, nil, nil, isForce)
if err != nil {
return err
return errors.Wrap(err, "CopyVirtualDisk")
}
return task.Wait(ctx)
}
@@ -513,7 +513,7 @@ func (cli *SESXiClient) MoveDisk(ctx context.Context, src, dst string, isForce b
dm := object.NewVirtualDiskManager(cli.client.Client)
task, err := dm.MoveVirtualDisk(ctx, src, nil, dst, nil, isForce)
if err != nil {
return err
return errors.Wrap(err, "MoveVirtualDisk")
}
return task.Wait(ctx)
}
+2 -3
View File
@@ -170,12 +170,11 @@ func init() {
if err != nil {
return err
}
ctx := context.Background()
path, err := dsObj.MakeDir(ctx, args.DIR)
err = dsObj.MakeDir(args.DIR)
if err != nil {
return err
}
fmt.Println("Make dir success", path)
fmt.Println("Make dir success")
return nil
})
+12 -13
View File
@@ -418,7 +418,7 @@ func (self *SDatastore) getPathString(path string) string {
return fmt.Sprintf("[%s] %s", self.SManagedObject.GetName(), path)
}
func (self *SDatastore) getFullPath(remotePath string) string {
func (self *SDatastore) GetFullPath(remotePath string) string {
remotePath = self.cleanPath(remotePath)
return path.Join(self.GetUrl(), remotePath)
}
@@ -438,6 +438,9 @@ func (self *SDatastore) FileGetContent(ctx context.Context, remotePath string) (
var bytes []byte
err = self.manager.client.Do(ctx, req, func(resp *http.Response) error {
if resp.StatusCode == 404 {
return cloudprovider.ErrNotFound
}
if resp.StatusCode >= 400 {
return fmt.Errorf("%s", resp.Status)
}
@@ -690,20 +693,18 @@ func (self *SDatastore) getDatastoreObj() *object.Datastore {
return object.NewDatastore(self.manager.client.Client, self.getDatastore().Self)
}
func (self *SDatastore) MakeDir(ctx context.Context, remotePath string) (string, error) {
dnm := object.NewDatastoreNamespaceManager(self.manager.client.Client)
func (self *SDatastore) MakeDir(remotePath string) error {
remotePath = self.cleanPath(remotePath)
objDS := self.getDatastoreObj()
return dnm.CreateDirectory(ctx, objDS, remotePath, "")
m := object.NewFileManager(self.manager.client.Client)
path := fmt.Sprintf("[%s] %s", self.GetRelName(), remotePath)
return m.MakeDirectory(self.manager.context, path, self.datacenter.getObjectDatacenter(), true)
}
func (self *SDatastore) RemoveDir(ctx context.Context, remotePath string) error {
dnm := object.NewDatastoreNamespaceManager(self.manager.client.Client)
remotePath = self.getFullPath(remotePath)
remotePath = self.GetFullPath(remotePath)
dc, err := self.GetDatacenter()
if err != nil {
@@ -717,17 +718,15 @@ func (self *SDatastore) RemoveDir(ctx context.Context, remotePath string) error
// CheckDirC will check that Dir 'remotePath' is exist, if not, create one.
func (self *SDatastore) CheckDirC(remotePath string) error {
_, err := self.CheckFile(context.Background(), remotePath)
_, err := self.CheckFile(self.manager.context, remotePath)
if err == nil {
return nil
}
if errors.Cause(err) != cloudprovider.ErrNotFound {
return err
}
m := object.NewFileManager(self.manager.client.Client)
path := fmt.Sprintf("[%s] %s", self.GetRelName(), remotePath)
return m.MakeDirectory(self.manager.context, path, self.datacenter.getObjectDatacenter(),
true)
return self.MakeDir(remotePath)
}
func (self *SDatastore) IsSysDiskStore() bool {
+1 -1
View File
@@ -203,7 +203,7 @@ func (disk *SVirtualDisk) GetAccessPath() string {
return ""
}
ds := istore.(*SDatastore)
return ds.getFullPath(disk.getBackingInfo().GetFileName())
return ds.GetFullPath(disk.getBackingInfo().GetFileName())
}
func (disk *SVirtualDisk) GetDiskFormat() string {