From 80a235e3a78a8962c6552204ecb5b47998d1c6e0 Mon Sep 17 00:00:00 2001 From: TangBin Date: Sat, 6 Feb 2021 12:24:55 +0800 Subject: [PATCH] imagetools NormalizeImageInfo update --- pkg/apis/image/consts.go | 6 ++++++ pkg/multicloud/aliyun/image.go | 21 ++++++++++++++----- pkg/multicloud/apsara/image.go | 21 ++++++++++++++----- pkg/multicloud/aws/image.go | 21 +++++++++++++++---- pkg/multicloud/ctyun/image.go | 20 ++++++++++++++---- pkg/multicloud/google/image.go | 20 ++++++++++++++---- pkg/multicloud/huawei/image.go | 24 ++++++++++++++------- pkg/multicloud/openstack/image.go | 35 +++++++++++++------------------ pkg/multicloud/qcloud/image.go | 25 ++++++++++++++-------- pkg/multicloud/ucloud/image.go | 24 ++++++++++++++------- pkg/multicloud/zstack/image.go | 22 ++++++++++++------- pkg/util/imagetools/imagetools.go | 19 ++++++++++++----- 12 files changed, 178 insertions(+), 80 deletions(-) diff --git a/pkg/apis/image/consts.go b/pkg/apis/image/consts.go index c154e1c840..bab51fcdef 100644 --- a/pkg/apis/image/consts.go +++ b/pkg/apis/image/consts.go @@ -56,6 +56,12 @@ const ( IMAGE_STATUS_UPDATING = "updating" ) +const ( + IMAGE_OS_ARCH_X86 = "x86" // x86 32位 + IMAGE_OS_ARCH_X86_64 = "x86_64" // + IMAGE_OS_ARCH_ARM = "aarch64" // arm 64位 little endian +) + var ( ImageDeadStatus = []string{IMAGE_STATUS_DEACTIVATED, IMAGE_STATUS_KILLED, IMAGE_STATUS_DELETED, IMAGE_STATUS_PENDING_DELETE} ) diff --git a/pkg/multicloud/aliyun/image.go b/pkg/multicloud/aliyun/image.go index b31da1a41f..56a86083a9 100644 --- a/pkg/multicloud/aliyun/image.go +++ b/pkg/multicloud/aliyun/image.go @@ -24,7 +24,6 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/log" - "yunion.io/x/pkg/utils" api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudprovider" @@ -61,6 +60,9 @@ type SImage struct { multicloud.SImageBase storageCache *SStoragecache + // normalized image info + imgInfo *imagetools.ImageInfo + Architecture string CreationTime time.Time Description string @@ -193,19 +195,28 @@ func (self *SImage) GetSizeByte() int64 { } func (self *SImage) GetOsType() string { - return utils.Capitalize(self.OSType) + return self.getNormalizedImageInfo().OsType } func (self *SImage) GetOsDist() string { - return self.Platform + return self.getNormalizedImageInfo().OsDistro +} + +func (self *SImage) getNormalizedImageInfo() *imagetools.ImageInfo { + if self.imgInfo == nil { + imgInfo := imagetools.NormalizeImageInfo(self.OSName, self.Architecture, self.OSType, self.Platform, "") + self.imgInfo = &imgInfo + } + + return self.imgInfo } func (self *SImage) GetOsVersion() string { - return imagetools.NormalizeImageInfo(self.OSName, "", "", "", "").OsVersion + return self.getNormalizedImageInfo().OsVersion } func (self *SImage) GetOsArch() string { - return self.Architecture + return self.getNormalizedImageInfo().OsArch } func (self *SImage) GetMinOsDiskSizeGb() int { diff --git a/pkg/multicloud/apsara/image.go b/pkg/multicloud/apsara/image.go index e211f9b558..7804bbd4c1 100644 --- a/pkg/multicloud/apsara/image.go +++ b/pkg/multicloud/apsara/image.go @@ -24,7 +24,6 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/log" - "yunion.io/x/pkg/utils" api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudprovider" @@ -61,6 +60,9 @@ type SImage struct { multicloud.SImageBase storageCache *SStoragecache + // normalized image info + imgInfo *imagetools.ImageInfo + Architecture string CreationTime time.Time Description string @@ -192,20 +194,29 @@ func (self *SImage) GetSizeByte() int64 { return int64(self.Size) * 1024 * 1024 * 1024 } +func (self *SImage) getNormalizedImageInfo() *imagetools.ImageInfo { + if self.imgInfo == nil { + imgInfo := imagetools.NormalizeImageInfo(self.OSName, self.Architecture, self.OSType, self.Platform, "") + self.imgInfo = &imgInfo + } + + return self.imgInfo +} + func (self *SImage) GetOsType() string { - return utils.Capitalize(self.OSType) + return self.getNormalizedImageInfo().OsType } func (self *SImage) GetOsDist() string { - return self.Platform + return self.getNormalizedImageInfo().OsDistro } func (self *SImage) GetOsVersion() string { - return imagetools.NormalizeImageInfo(self.OSName, "", "", "", "").OsVersion + return self.getNormalizedImageInfo().OsVersion } func (self *SImage) GetOsArch() string { - return self.Architecture + return self.getNormalizedImageInfo().OsArch } func (self *SImage) GetMinOsDiskSizeGb() int { diff --git a/pkg/multicloud/aws/image.go b/pkg/multicloud/aws/image.go index 019bf21683..951bcfaa90 100644 --- a/pkg/multicloud/aws/image.go +++ b/pkg/multicloud/aws/image.go @@ -29,6 +29,7 @@ import ( api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudprovider" "yunion.io/x/onecloud/pkg/multicloud" + "yunion.io/x/onecloud/pkg/util/imagetools" ) type ImageStatusType string @@ -79,6 +80,9 @@ type SImage struct { multicloud.SImageBase storageCache *SStoragecache + // normalized image info + imgInfo *imagetools.ImageInfo + Architecture string CreationTime time.Time Description string @@ -222,20 +226,29 @@ func (self *SImage) GetSizeByte() int64 { return int64(self.SizeGB) * 1024 * 1024 * 1024 } +func (self *SImage) getNormalizedImageInfo() *imagetools.ImageInfo { + if self.imgInfo == nil { + imgInfo := imagetools.NormalizeImageInfo("", self.Architecture, self.OSType, self.OSDist, self.OSVersion) + self.imgInfo = &imgInfo + } + + return self.imgInfo +} + func (self *SImage) GetOsType() string { - return self.OSType + return self.getNormalizedImageInfo().OsType } func (self *SImage) GetOsArch() string { - return self.Architecture + return self.getNormalizedImageInfo().OsArch } func (self *SImage) GetOsDist() string { - return self.OSDist + return self.getNormalizedImageInfo().OsDistro } func (self *SImage) GetOsVersion() string { - return self.OSVersion + return self.getNormalizedImageInfo().OsVersion } func (self *SImage) GetMinOsDiskSizeGb() int { diff --git a/pkg/multicloud/ctyun/image.go b/pkg/multicloud/ctyun/image.go index 7b92712963..d2cc99aa25 100644 --- a/pkg/multicloud/ctyun/image.go +++ b/pkg/multicloud/ctyun/image.go @@ -39,6 +39,9 @@ type SImage struct { multicloud.SImageBase storageCache *SStoragecache + // normalized image info + imgInfo *imagetools.ImageInfo + ID string `json:"id"` OSType string `json:"osType"` Platform string `json:"platform"` @@ -146,20 +149,29 @@ func (self *SImage) GetImageStatus() string { return cloudprovider.IMAGE_STATUS_ACTIVE } +func (self *SImage) getNormalizedImageInfo() *imagetools.ImageInfo { + if self.imgInfo == nil { + imgInfo := imagetools.NormalizeImageInfo(self.OSVersion, strconv.Itoa(int(self.OSBit)), self.OSType, self.Platform, "") + self.imgInfo = &imgInfo + } + + return self.imgInfo +} + func (self *SImage) GetOsType() string { - return self.OSType + return self.getNormalizedImageInfo().OsType } func (self *SImage) GetOsDist() string { - return self.Platform + return self.getNormalizedImageInfo().OsDistro } func (self *SImage) GetOsVersion() string { - return imagetools.NormalizeImageInfo(self.OSVersion, "", "", self.Platform, "").OsVersion + return self.getNormalizedImageInfo().OsVersion } func (self *SImage) GetOsArch() string { - return strconv.Itoa(int(self.OSBit)) + return self.getNormalizedImageInfo().OsArch } func (self *SImage) GetMinOsDiskSizeGb() int { diff --git a/pkg/multicloud/google/image.go b/pkg/multicloud/google/image.go index 0140f74002..713fb1245f 100644 --- a/pkg/multicloud/google/image.go +++ b/pkg/multicloud/google/image.go @@ -44,6 +44,9 @@ type SImage struct { storagecache *SStoragecache SResourceBase + // normalized image info + imgInfo *imagetools.ImageInfo + Id string CreationTimestamp time.Time Description string @@ -161,20 +164,29 @@ func (image *SImage) GetSizeByte() int64 { return image.ArchiveSizeBytes } +func (image *SImage) getNormalizedImageInfo() *imagetools.ImageInfo { + if image.imgInfo == nil { + imgInfo := imagetools.NormalizeImageInfo(image.Name, "", "", "", "") + image.imgInfo = &imgInfo + } + + return image.imgInfo +} + func (image *SImage) GetOsType() string { - return imagetools.NormalizeImageInfo(image.Name, "", "", "", "").OsType + return image.getNormalizedImageInfo().OsType } func (image *SImage) GetOsDist() string { - return imagetools.NormalizeImageInfo(image.Name, "", "", "", "").OsDistro + return image.getNormalizedImageInfo().OsDistro } func (image *SImage) GetOsVersion() string { - return imagetools.NormalizeImageInfo(image.Name, "", "", "", "").OsVersion + return image.getNormalizedImageInfo().OsVersion } func (image *SImage) GetOsArch() string { - return imagetools.NormalizeImageInfo(image.Name, "", "", "", "").OsArch + return image.getNormalizedImageInfo().OsArch } func (image *SImage) GetMinOsDiskSizeGb() int { diff --git a/pkg/multicloud/huawei/image.go b/pkg/multicloud/huawei/image.go index 4f66ed9b3d..f3861146b5 100644 --- a/pkg/multicloud/huawei/image.go +++ b/pkg/multicloud/huawei/image.go @@ -54,6 +54,9 @@ type SImage struct { multicloud.SImageBase storageCache *SStoragecache + // normalized image info + imgInfo *imagetools.ImageInfo + Schema string `json:"schema"` MinDiskGB int64 `json:"min_disk"` CreatedAt time.Time `json:"created_at"` @@ -160,24 +163,29 @@ func (self *SImage) GetSizeByte() int64 { return int64(self.MinDiskGB) * 1024 * 1024 * 1024 } +func (self *SImage) getNormalizedImageInfo() *imagetools.ImageInfo { + if self.imgInfo == nil { + imgInfo := imagetools.NormalizeImageInfo(self.ImageSourceType, self.OSType, self.OSType, self.Platform, "") + self.imgInfo = &imgInfo + } + + return self.imgInfo +} + func (self *SImage) GetOsType() string { - return self.OSType + return self.getNormalizedImageInfo().OsType } func (self *SImage) GetOsDist() string { - return self.Platform + return self.getNormalizedImageInfo().OsDistro } func (self *SImage) GetOsVersion() string { - return imagetools.NormalizeImageInfo(self.ImageSourceType, "", "", "", "").OsVersion + return self.getNormalizedImageInfo().OsVersion } func (self *SImage) GetOsArch() string { - if self.OSType == "32" { - return "x86" - } else { - return "x86_64" - } + return self.getNormalizedImageInfo().OsArch } func (self *SImage) GetMinOsDiskSizeGb() int { diff --git a/pkg/multicloud/openstack/image.go b/pkg/multicloud/openstack/image.go index a5ee0c1a55..cce90c7edd 100644 --- a/pkg/multicloud/openstack/image.go +++ b/pkg/multicloud/openstack/image.go @@ -25,7 +25,6 @@ import ( "github.com/pkg/errors" "yunion.io/x/jsonutils" - "yunion.io/x/pkg/util/osprofile" api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudprovider" @@ -51,6 +50,9 @@ type SImage struct { multicloud.SImageBase storageCache *SStoragecache + // normalized image info + imgInfo *imagetools.ImageInfo + Status string Name string Tags []string @@ -208,30 +210,21 @@ func (image *SImage) GetSizeByte() int64 { return int64(image.Size) } -func (image *SImage) GetOsType() string { - switch image.OsType { - case "linux": - return osprofile.OS_TYPE_LINUX - case "windows": - return osprofile.OS_TYPE_WINDOWS - default: - osType := imagetools.NormalizeImageInfo(image.Name, "", "", "", "").OsType - if len(osType) > 0 { - return osType - } +func (self *SImage) getNormalizedImageInfo() *imagetools.ImageInfo { + if self.imgInfo == nil { + imgInfo := imagetools.NormalizeImageInfo(self.Name, "", self.OsType, "", "") + self.imgInfo = &imgInfo } - return "Linux" + + return self.imgInfo +} + +func (image *SImage) GetOsType() string { + return image.getNormalizedImageInfo().OsType } func (image *SImage) GetOsDist() string { - if len(image.OsDistro) > 0 { - return image.OsDistro - } - osDist := imagetools.NormalizeImageInfo(image.Name, "", "", "", "").OsDistro - if len(osDist) > 0 { - return osDist - } - return "Linux" + return image.getNormalizedImageInfo().OsDistro } func (image *SImage) GetOsVersion() string { diff --git a/pkg/multicloud/qcloud/image.go b/pkg/multicloud/qcloud/image.go index 6498e386ad..86f1a8a907 100644 --- a/pkg/multicloud/qcloud/image.go +++ b/pkg/multicloud/qcloud/image.go @@ -46,6 +46,9 @@ type SImage struct { multicloud.SImageBase storageCache *SStoragecache + // normalized image info + imgInfo *imagetools.ImageInfo + ImageId string // 镜像ID OsName string // 镜像操作系统 ImageType string // 镜像类型 @@ -182,25 +185,29 @@ func (self *SImage) GetSizeByte() int64 { return int64(self.ImageSize) * 1024 * 1024 * 1024 } -func (self *SImage) GetOsType() string { - switch self.Platform { - case "Windows", "FreeBSD": - return self.Platform - default: - return "Linux" +func (self *SImage) getNormalizedImageInfo() *imagetools.ImageInfo { + if self.imgInfo == nil { + imgInfo := imagetools.NormalizeImageInfo(self.OsName, self.Architecture, self.Platform, self.Platform, "") + self.imgInfo = &imgInfo } + + return self.imgInfo +} + +func (self *SImage) GetOsType() string { + return self.getNormalizedImageInfo().OsType } func (self *SImage) GetOsDist() string { - return self.Platform + return self.getNormalizedImageInfo().OsDistro } func (self *SImage) GetOsVersion() string { - return imagetools.NormalizeImageInfo(self.OsName, "", "", "", "").OsVersion + return self.getNormalizedImageInfo().OsVersion } func (self *SImage) GetOsArch() string { - return self.Architecture + return self.getNormalizedImageInfo().OsArch } func (self *SImage) GetMinOsDiskSizeGb() int { diff --git a/pkg/multicloud/ucloud/image.go b/pkg/multicloud/ucloud/image.go index 5e650a5a24..cd5744003f 100644 --- a/pkg/multicloud/ucloud/image.go +++ b/pkg/multicloud/ucloud/image.go @@ -31,6 +31,9 @@ type SImage struct { multicloud.SImageBase storageCache *SStoragecache + // normalized image info + imgInfo *imagetools.ImageInfo + Zone string `json:"Zone"` ImageDescription string `json:"ImageDescription"` OSName string `json:"OsName"` @@ -164,24 +167,29 @@ func (self *SImage) GetImageStatus() string { } } +func (self *SImage) getNormalizedImageInfo() *imagetools.ImageInfo { + if self.imgInfo == nil { + imgInfo := imagetools.NormalizeImageInfo(self.ImageName, self.ImageName, self.ImageName, "", "") + self.imgInfo = &imgInfo + } + + return self.imgInfo +} + func (self *SImage) GetOsType() string { - imageInfo := imagetools.NormalizeImageInfo(self.ImageName, "", "", "", "") - return imageInfo.OsType + return self.getNormalizedImageInfo().OsType } func (self *SImage) GetOsDist() string { - imageInfo := imagetools.NormalizeImageInfo(self.ImageName, "", "", "", "") - return imageInfo.OsDistro + return self.getNormalizedImageInfo().OsDistro } func (self *SImage) GetOsVersion() string { - imageInfo := imagetools.NormalizeImageInfo(self.ImageName, "", "", "", "") - return imageInfo.OsVersion + return self.getNormalizedImageInfo().OsVersion } func (self *SImage) GetOsArch() string { - imageInfo := imagetools.NormalizeImageInfo(self.ImageName, "", "", "", "") - return imageInfo.OsArch + return self.getNormalizedImageInfo().OsArch } func (self *SImage) GetMinOsDiskSizeGb() int { diff --git a/pkg/multicloud/zstack/image.go b/pkg/multicloud/zstack/image.go index 76997691de..b975aece95 100644 --- a/pkg/multicloud/zstack/image.go +++ b/pkg/multicloud/zstack/image.go @@ -48,6 +48,9 @@ type SImage struct { multicloud.SImageBase storageCache *SStoragecache + // normalized image info + imgInfo *imagetools.ImageInfo + BackupStorageRefs []SBackupStorageRef `json:"backupStorageRefs"` ActualSize int `json:"actualSize"` CreateDate time.Time `json:"createDate"` @@ -156,20 +159,25 @@ func (image *SImage) GetSizeByte() int64 { return int64(image.Size) } +func (self *SImage) getNormalizedImageInfo() *imagetools.ImageInfo { + if self.imgInfo == nil { + imgInfo := imagetools.NormalizeImageInfo(self.URL, "", self.Platform, self.Platform, "") + self.imgInfo = &imgInfo + } + + return self.imgInfo +} + func (image *SImage) GetOsType() string { - return image.Platform + return image.getNormalizedImageInfo().OsType } func (image *SImage) GetOsDist() string { - osDist := imagetools.NormalizeImageInfo(image.URL, "", "", "", "").OsDistro - if len(osDist) > 0 { - return osDist - } - return image.Platform + return image.getNormalizedImageInfo().OsDistro } func (image *SImage) GetOsVersion() string { - return imagetools.NormalizeImageInfo(image.Name, "", "", "", "").OsVersion + return image.getNormalizedImageInfo().OsVersion } func (image *SImage) GetOsArch() string { diff --git a/pkg/util/imagetools/imagetools.go b/pkg/util/imagetools/imagetools.go index 0ca200dfa0..ca34c9333e 100644 --- a/pkg/util/imagetools/imagetools.go +++ b/pkg/util/imagetools/imagetools.go @@ -14,14 +14,23 @@ package imagetools -import "strings" +import ( + "strings" + + api "yunion.io/x/onecloud/pkg/apis/image" +) func normalizeOsArch(osArch string, osType string, osDist string) string { if len(osArch) > 0 { - if strings.ToLower(osArch) == "x86_64" { - return "x86_64" - } else { - return "i386" + switch strings.ToLower(osArch) { + case "x86_64", "64": + return api.IMAGE_OS_ARCH_X86_64 + case "x86", "x86_32", "32": + return api.IMAGE_OS_ARCH_X86 + case "arm", "arm64", "aarch", "aarch64": + return api.IMAGE_OS_ARCH_ARM + default: + return osArch } } else { if osType == "linux" {