@@ -326,10 +326,10 @@ func (manager *SImageManager) FetchCustomizeColumns(
326326 return rows
327327}
328328
329- func (self * SImage ) GetExtraDetailsHeaders (ctx context.Context , userCred mcclient.TokenCredential , query jsonutils.JSONObject ) map [string ]string {
329+ func (img * SImage ) GetExtraDetailsHeaders (ctx context.Context , userCred mcclient.TokenCredential , query jsonutils.JSONObject ) map [string ]string {
330330 headers := make (map [string ]string )
331331
332- details := ImageManager .FetchCustomizeColumns (ctx , userCred , query , []interface {}{self }, nil , false )
332+ details := ImageManager .FetchCustomizeColumns (ctx , userCred , query , []interface {}{img }, nil , false )
333333 extra := jsonutils .Marshal (details [0 ]).(* jsonutils.JSONDict )
334334 for _ , k := range extra .SortedKeys () {
335335 if k == "properties" {
@@ -341,8 +341,8 @@ func (self *SImage) GetExtraDetailsHeaders(ctx context.Context, userCred mcclien
341341 }
342342 }
343343
344- jsonDict := jsonutils .Marshal (self ).(* jsonutils.JSONDict )
345- fields , _ := db .GetDetailFields (self .GetModelManager (), userCred )
344+ jsonDict := jsonutils .Marshal (img ).(* jsonutils.JSONDict )
345+ fields , _ := db .GetDetailFields (img .GetModelManager (), userCred )
346346 for _ , k := range jsonDict .SortedKeys () {
347347 if utils .IsInStringArray (k , fields ) {
348348 val , _ := jsonDict .GetString (k )
@@ -352,16 +352,24 @@ func (self *SImage) GetExtraDetailsHeaders(ctx context.Context, userCred mcclien
352352 }
353353 }
354354
355+ // none of subimage business
356+ var ossChksum = img .OssChecksum
357+ if len (img .OssChecksum ) == 0 {
358+ ossChksum = img .Checksum
359+ }
360+ headers [fmt .Sprintf ("%s%s" , modules .IMAGE_META , "oss_checksum" )] = ossChksum
361+
355362 formatStr := jsonutils .GetAnyString (query , []string {"format" , "disk_format" })
356363 if len (formatStr ) > 0 {
357- subimg := ImageSubformatManager .FetchSubImage (self .Id , formatStr )
364+ subimg := ImageSubformatManager .FetchSubImage (img .Id , formatStr )
358365 if subimg != nil {
359366 headers [fmt .Sprintf ("%s%s" , modules .IMAGE_META , "disk_format" )] = formatStr
360367 isTorrent := jsonutils .QueryBoolean (query , "torrent" , false )
361368 if ! isTorrent {
362369 headers [fmt .Sprintf ("%s%s" , modules .IMAGE_META , "status" )] = subimg .Status
363370 headers [fmt .Sprintf ("%s%s" , modules .IMAGE_META , "size" )] = fmt .Sprintf ("%d" , subimg .Size )
364371 headers [fmt .Sprintf ("%s%s" , modules .IMAGE_META , "checksum" )] = subimg .Checksum
372+ headers [fmt .Sprintf ("%s%s" , modules .IMAGE_META , "oss_checksum" )] = subimg .Checksum
365373 } else {
366374 headers [fmt .Sprintf ("%s%s" , modules .IMAGE_META , "status" )] = subimg .TorrentStatus
367375 headers [fmt .Sprintf ("%s%s" , modules .IMAGE_META , "size" )] = fmt .Sprintf ("%d" , subimg .TorrentSize )
@@ -370,31 +378,24 @@ func (self *SImage) GetExtraDetailsHeaders(ctx context.Context, userCred mcclien
370378 }
371379 }
372380
373- // none of subimage business
374- var ossChksum = self .OssChecksum
375- if len (self .OssChecksum ) == 0 {
376- ossChksum = self .Checksum
377- }
378- headers [fmt .Sprintf ("%s%s" , modules .IMAGE_META , "oss_checksum" )] = ossChksum
379-
380- properties , _ := ImagePropertyManager .GetProperties (self .Id )
381+ properties , _ := ImagePropertyManager .GetProperties (img .Id )
381382 if len (properties ) > 0 {
382383 for k , v := range properties {
383384 headers [fmt .Sprintf ("%s%s" , modules .IMAGE_META_PROPERTY , k )] = v
384385 }
385386 }
386387
387- if self .PendingDeleted {
388- pendingDeletedAt := self .PendingDeletedAt .Add (time .Second * time .Duration (options .Options .PendingDeleteExpireSeconds ))
388+ if img .PendingDeleted {
389+ pendingDeletedAt := img .PendingDeletedAt .Add (time .Second * time .Duration (options .Options .PendingDeleteExpireSeconds ))
389390 headers [fmt .Sprintf ("%s%s" , modules .IMAGE_META , "auto_delete_at" )] = timeutils .FullIsoTime (pendingDeletedAt )
390391 }
391392
392- if strings .HasPrefix (self .Location , api .S3Prefix ) {
393+ if strings .HasPrefix (img .Location , api .S3Prefix ) {
393394 headers [fmt .Sprintf ("%s%s" , modules .IMAGE_META , "s3_info_url" )] = s3 .GetEndpoint (options .Options .S3Endpoint , options .Options .S3UseSSL )
394395 headers [fmt .Sprintf ("%s%s" , modules .IMAGE_META , "s3_info_access_key" )] = options .Options .S3AccessKey
395396 headers [fmt .Sprintf ("%s%s" , modules .IMAGE_META , "s3_info_secret" )] = options .Options .S3SecretKey
396397 headers [fmt .Sprintf ("%s%s" , modules .IMAGE_META , "s3_info_bucket" )] = options .Options .S3BucketName
397- headers [fmt .Sprintf ("%s%s" , modules .IMAGE_META , "s3_info_key" )] = imagePathToName (self .Location )
398+ headers [fmt .Sprintf ("%s%s" , modules .IMAGE_META , "s3_info_key" )] = imagePathToName (img .Location )
398399 headers [fmt .Sprintf ("%s%s" , modules .IMAGE_META , "s3_info_sign_ver" )] = options .Options .S3SignVersion
399400 }
400401
@@ -1084,44 +1085,44 @@ func (self *SImage) newSubformat(ctx context.Context, format qemuimgfmt.TImageFo
10841085 return nil
10851086}
10861087
1087- func (self * SImage ) migrateSubImage (ctx context.Context ) error {
1088+ func (img * SImage ) migrateSubImage (ctx context.Context ) error {
10881089 log .Debugf ("migrateSubImage" )
1089- if ! qemuimgfmt .IsSupportedImageFormat (self .DiskFormat ) {
1090- log .Warningf ("Unsupported image format %s, no need to migrate" , self .DiskFormat )
1090+ if ! qemuimgfmt .IsSupportedImageFormat (img .DiskFormat ) {
1091+ log .Warningf ("Unsupported image format %s, no need to migrate" , img .DiskFormat )
10911092 return nil
10921093 }
10931094
1094- subimg := ImageSubformatManager .FetchSubImage (self .Id , self .DiskFormat )
1095+ subimg := ImageSubformatManager .FetchSubImage (img .Id , img .DiskFormat )
10951096 if subimg != nil {
10961097 return nil
10971098 }
10981099
1099- imgInst , err := self .getQemuImage ()
1100+ imgInst , err := img .getQemuImage ()
11001101 if err != nil {
11011102 return errors .Wrap (err , "getQemuImage" )
11021103 }
1103- if self .GetImageType () != api .ImageTypeISO && imgInst .IsSparse () && utils .IsInStringArray (self .DiskFormat , options .Options .TargetImageFormats ) {
1104+ if img .GetImageType () != api .ImageTypeISO && imgInst .IsSparse () && utils .IsInStringArray (img .DiskFormat , options .Options .TargetImageFormats ) {
11041105 // need to convert again
11051106 log .Debugf ("migrateImage: image is not iso but sparse, need to convert the image" )
1106- return self .newSubformat (ctx , qemuimgfmt .String2ImageFormat (self .DiskFormat ), false )
1107+ return img .newSubformat (ctx , qemuimgfmt .String2ImageFormat (img .DiskFormat ), false )
11071108 } else {
11081109 log .Debugf ("migrateImage: no need to convert the image" )
1109- localPath := self .GetLocalLocation ()
1110- if ! strings .HasSuffix (localPath , fmt .Sprintf (".%s" , self .DiskFormat )) {
1111- newLocalpath := fmt .Sprintf ("%s.%s" , localPath , self .DiskFormat )
1110+ localPath := img .GetLocalLocation ()
1111+ if ! strings .HasSuffix (localPath , fmt .Sprintf (".%s" , img .DiskFormat )) {
1112+ newLocalpath := fmt .Sprintf ("%s.%s" , localPath , img .DiskFormat )
11121113 out , err := procutils .NewCommand ("mv" , "-f" , localPath , newLocalpath ).Output ()
11131114 if err != nil {
11141115 return errors .Wrapf (err , "rename file failed %s" , out )
11151116 }
1116- _ , err = db .Update (self , func () error {
1117- self .Location = self .GetNewLocation (newLocalpath )
1117+ _ , err = db .Update (img , func () error {
1118+ img .Location = img .GetNewLocation (newLocalpath )
11181119 return nil
11191120 })
11201121 if err != nil {
11211122 return err
11221123 }
11231124 }
1124- return self .newSubformat (ctx , qemuimgfmt .String2ImageFormat (self .DiskFormat ), true )
1125+ return img .newSubformat (ctx , qemuimgfmt .String2ImageFormat (img .DiskFormat ), true )
11251126 }
11261127}
11271128
@@ -1914,7 +1915,7 @@ func (image *SImage) doUploadPermanentStorage(ctx context.Context, userCred mccl
19141915 if ! subimgs [i ].isLocal () {
19151916 continue
19161917 }
1917- if subimgs [i ].Format == image .DiskFormat {
1918+ if subimgs [i ].Format == image .DiskFormat && subimgs [ i ]. Checksum == image . Checksum {
19181919 _ , err := db .Update (& subimgs [i ], func () error {
19191920 subimgs [i ].Location = image .Location
19201921 subimgs [i ].Status = api .IMAGE_STATUS_ACTIVE
@@ -1926,7 +1927,7 @@ func (image *SImage) doUploadPermanentStorage(ctx context.Context, userCred mccl
19261927 } else {
19271928 imagePath := subimgs [i ].GetLocalLocation ()
19281929 storage := GetStorage ()
1929- location , err := GetStorage () .SaveImage (ctx , imagePath , nil )
1930+ location , err := storage .SaveImage (ctx , imagePath , nil )
19301931 if err != nil {
19311932 log .Errorf ("Failed save image to sepcific storage %s" , err )
19321933 subimgs [i ].SetStatus (api .IMAGE_STATUS_SAVE_FAIL )
0 commit comments