fix(glance): image save (#13673)

This commit is contained in:
屈轩
2022-03-11 16:16:10 +08:00
committed by GitHub
parent 50e93fd336
commit 844377b7f3
3 changed files with 59 additions and 37 deletions
+50 -32
View File
@@ -394,6 +394,14 @@ func (self *SImage) CustomizeCreate(ctx context.Context, userCred mcclient.Token
return nil
}
func (self *SImage) GetLocalPath(format string) string {
path := filepath.Join(options.Options.FilesystemStoreDatadir, self.Id)
if len(format) > 0 {
path = fmt.Sprintf("%s.%s", path, format)
}
return path
}
func (self *SImage) GetPath(format string) string {
path := filepath.Join(options.Options.FilesystemStoreDatadir, self.Id)
if options.Options.StorageDriver == api.IMAGE_STORAGE_DRIVER_S3 {
@@ -483,51 +491,61 @@ func (self *SImage) saveSize(newSize, totalSize int64) error {
//Image always do probe and customize after save from stream
func (self *SImage) SaveImageFromStream(reader io.Reader, totalSize int64, calChecksum bool) error {
localPath := self.GetPath("")
localPath := self.GetLocalPath("")
sp, err := self.saveImageFromStream(localPath, reader, totalSize, calChecksum)
if err != nil {
log.Errorf("saveImageFromStream fail %s", err)
return err
}
err := func() error {
sp, err := self.saveImageFromStream(localPath, reader, totalSize, calChecksum)
if err != nil {
return errors.Wrapf(err, "saveImageFromStream")
}
virtualSizeBytes := int64(0)
format := ""
img, err := qemuimg.NewQemuImage(localPath)
if err != nil {
return err
}
format = string(img.Format)
virtualSizeBytes = img.SizeBytes
var fastChksum string
if calChecksum {
fastChksum, err = fileutils2.FastCheckSum(localPath)
virtualSizeBytes := int64(0)
format := ""
img, err := qemuimg.NewQemuImage(localPath)
if err != nil {
return err
}
}
format = string(img.Format)
virtualSizeBytes = img.SizeBytes
_, err = db.Update(self, func() error {
self.Size = sp.Size
var fastChksum string
if calChecksum {
self.Checksum = sp.CheckSum
self.FastHash = fastChksum
fastChksum, err = fileutils2.FastCheckSum(localPath)
if err != nil {
return errors.Wrapf(err, "FastCheckSum %s", localPath)
}
}
self.Location = fmt.Sprintf("%s%s", LocalFilePrefix, localPath)
if len(format) > 0 {
self.DiskFormat = format
}
if virtualSizeBytes > 0 {
self.MinDiskMB = int32(math.Ceil(float64(virtualSizeBytes) / 1024 / 1024))
_, err = db.Update(self, func() error {
self.Size = sp.Size
if calChecksum {
self.Checksum = sp.CheckSum
self.FastHash = fastChksum
}
self.Location = fmt.Sprintf("%s%s", LocalFilePrefix, localPath)
if len(format) > 0 {
self.DiskFormat = format
}
if virtualSizeBytes > 0 {
self.MinDiskMB = int32(math.Ceil(float64(virtualSizeBytes) / 1024 / 1024))
}
return nil
})
if err != nil {
return errors.Wrapf(err, "db.Update")
}
return nil
})
}()
if err != nil {
return err
if fileutils2.IsFile(localPath) {
if e := os.Remove(localPath); e != nil {
log.Errorf("remove failed file %s error: %v", err)
}
}
}
return nil
return err
}
func (self *SImage) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {
+3
View File
@@ -156,6 +156,9 @@ func (s *S3Storage) Type() string {
}
func (s *S3Storage) SaveImage(ctx context.Context, imagePath string) (string, error) {
if !fileutils2.IsFile(imagePath) {
return "", fmt.Errorf("%s not valid file", imagePath)
}
return s3.Put(ctx, imagePath, imagePathToName(imagePath))
}
+6 -5
View File
@@ -82,10 +82,11 @@ func (self *ImageProbeTask) StartImageProbe(ctx context.Context, image *models.S
}
func imageGetPath(image *models.SImage) string {
diskPath := image.GetPath("")
diskPath := image.GetLocalPath("")
if !fileutils2.Exists(diskPath) {
diskPath = image.GetPath(image.DiskFormat)
diskPath = image.GetLocalPath(image.DiskFormat)
if !fileutils2.Exists(diskPath) {
log.Errorf("file %s not exist", image.Location)
return ""
}
}
@@ -113,7 +114,7 @@ func (self *ImageProbeTask) updateImageMetadata(
) error {
imagePath := imageGetPath(image)
if len(imagePath) == 0 {
return errors.Wrap(httperrors.ErrNotFound, "image file not found")
return errors.Wrapf(httperrors.ErrNotFound, "image file %s not found", image.Location)
}
fp, err := os.Open(imagePath)
if err != nil {
@@ -123,12 +124,12 @@ func (self *ImageProbeTask) updateImageMetadata(
stat, err := fp.Stat()
if err != nil {
return err
return errors.Wrapf(err, "stat %s", imagePath)
}
chksum, err := fileutils2.MD5(imagePath)
if err != nil {
return err
return errors.Wrapf(err, "md5 %s", imagePath)
}
fastchksum, err := fileutils2.FastCheckSum(imagePath)