From 419a33958c5296c8cdbef4851e51da53d029d651 Mon Sep 17 00:00:00 2001 From: rainzm Date: Fri, 1 Apr 2022 18:48:29 +0800 Subject: [PATCH] feat(image): notify when image checksum test fail --- pkg/image/models/image_subs.go | 15 ++++++++++++-- pkg/image/models/images.go | 37 +++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/pkg/image/models/image_subs.go b/pkg/image/models/image_subs.go index c8745c0e7e..354c64ae85 100644 --- a/pkg/image/models/image_subs.go +++ b/pkg/image/models/image_subs.go @@ -22,11 +22,14 @@ import ( "path/filepath" "strings" + "yunion.io/x/jsonutils" "yunion.io/x/log" "yunion.io/x/pkg/errors" api "yunion.io/x/onecloud/pkg/apis/image" + noapi "yunion.io/x/onecloud/pkg/apis/notify" "yunion.io/x/onecloud/pkg/cloudcommon/db" + "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient" "yunion.io/x/onecloud/pkg/image/options" "yunion.io/x/onecloud/pkg/image/torrent" "yunion.io/x/onecloud/pkg/mcclient" @@ -312,11 +315,19 @@ func (self *SImageSubformat) GetDetails() SImageSubformatDetails { } func (self *SImageSubformat) isActive(useFast bool) bool { - return isActive(self.GetLocalLocation(), self.Size, self.Checksum, self.FastHash, useFast) + active, reason := isActive(self.GetLocalLocation(), self.Size, self.Checksum, self.FastHash, useFast) + if active || reason != FileChecksumMismatch { + return active + } + data := jsonutils.NewDict() + data.Set("name", jsonutils.NewString(self.ImageId)) + notifyclient.SystemExceptionNotifyWithResult(context.TODO(), noapi.ActionChecksumTest, noapi.TOPIC_RESOURCE_IMAGE, noapi.ResultFailed, data) + return false } func (self *SImageSubformat) isTorrentActive() bool { - return isActive(self.getLocalTorrentLocation(), self.TorrentSize, self.TorrentChecksum, "", false) + active, _ := isActive(self.getLocalTorrentLocation(), self.TorrentSize, self.TorrentChecksum, "", false) + return active } func (self *SImageSubformat) SetStatus(status string) error { diff --git a/pkg/image/models/images.go b/pkg/image/models/images.go index a423e06f7b..b520f2ce8f 100644 --- a/pkg/image/models/images.go +++ b/pkg/image/models/images.go @@ -37,6 +37,7 @@ import ( "yunion.io/x/onecloud/pkg/apis" api "yunion.io/x/onecloud/pkg/apis/image" + noapi "yunion.io/x/onecloud/pkg/apis/notify" "yunion.io/x/onecloud/pkg/appsrv" "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/cloudcommon/db/quotas" @@ -1340,40 +1341,49 @@ func (manager *SImageManager) QueryDistinctExtraField(q *sqlchemy.SQuery, field return q, httperrors.ErrNotFound } -func isActive(localPath string, size int64, chksum string, fastHash string, useFastHash bool) bool { +type sUnactiveReason int + +const ( + FileNoExists sUnactiveReason = iota + FileSizeMismatch + FileChecksumMismatch + Others +) + +func isActive(localPath string, size int64, chksum string, fastHash string, useFastHash bool) (bool, sUnactiveReason) { if len(localPath) == 0 || !fileutils2.Exists(localPath) { log.Errorf("invalid file: %s", localPath) - return false + return false, FileNoExists } if size != fileutils2.FileSize(localPath) { log.Errorf("size mistmatch: %s", localPath) - return false + return false, FileSizeMismatch } if len(chksum) == 0 || len(fastHash) == 0 { - return true + return true, Others } if useFastHash && len(fastHash) > 0 { fhash, err := fileutils2.FastCheckSum(localPath) if err != nil { log.Errorf("IsActive fastChecksum fail %s for %s", err, localPath) - return false + return false, Others } if fastHash != fhash { log.Errorf("IsActive fastChecksum mismatch for %s", localPath) - return false + return false, FileChecksumMismatch } } else { md5sum, err := fileutils2.MD5(localPath) if err != nil { log.Errorf("IsActive md5 fail %s for %s", err, localPath) - return false + return false, Others } if chksum != md5sum { log.Errorf("IsActive checksum mismatch: %s", localPath) - return false + return false, FileChecksumMismatch } } - return true + return true, Others } func (self *SImage) IsIso() bool { @@ -1381,7 +1391,14 @@ func (self *SImage) IsIso() bool { } func (self *SImage) isActive(useFast bool) bool { - return isActive(self.GetLocalLocation(), self.Size, self.Checksum, self.FastHash, useFast) + active, reason := isActive(self.GetLocalLocation(), self.Size, self.Checksum, self.FastHash, useFast) + if active || reason != FileChecksumMismatch { + return active + } + data := jsonutils.NewDict() + data.Set("name", jsonutils.NewString(self.Name)) + notifyclient.SystemExceptionNotifyWithResult(context.TODO(), noapi.ActionChecksumTest, noapi.TOPIC_RESOURCE_IMAGE, noapi.ResultFailed, data) + return false } func (self *SImage) DoCheckStatus(ctx context.Context, userCred mcclient.TokenCredential, useFast bool) {