mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-01 15:07:17 +08:00
normalize镜像版本
This commit is contained in:
@@ -236,10 +236,11 @@ func (self *SStoragecache) StartImageCacheTask(ctx context.Context, userCred mcc
|
||||
|
||||
if image != nil {
|
||||
imgInfo := imagetools.NormalizeImageInfo(image.Name, image.Properties["os_arch"], image.Properties["os_type"],
|
||||
image.Properties["os_distribution"])
|
||||
image.Properties["os_distribution"], image.Properties["os_version"])
|
||||
data.Add(jsonutils.NewString(imgInfo.OsType), "os_type")
|
||||
data.Add(jsonutils.NewString(imgInfo.OsArch), "os_arch")
|
||||
data.Add(jsonutils.NewString(imgInfo.OsDistro), "os_distribution")
|
||||
data.Add(jsonutils.NewString(imgInfo.OsVersion), "os_version")
|
||||
}
|
||||
|
||||
if isForce {
|
||||
|
||||
@@ -3,12 +3,12 @@ package imagetools
|
||||
import "testing"
|
||||
|
||||
func TestNormalizeImageInfo(t *testing.T) {
|
||||
info := NormalizeImageInfo("rhel67_20180816.qcow2", "", "", "")
|
||||
info := NormalizeImageInfo("rhel67_20180816.qcow2", "", "", "", "")
|
||||
t.Logf("%#v", info)
|
||||
|
||||
info = NormalizeImageInfo("Ubuntu_16.04.3_amd64_qingcloud_20180817.qcow2", "", "", "")
|
||||
info = NormalizeImageInfo("Ubuntu_16.04.3_amd64_qingcloud_20180817.qcow2", "", "", "", "")
|
||||
t.Logf("%#v", info)
|
||||
|
||||
info = NormalizeImageInfo("windows-server-2008-dc-cn-20180717", "", "", "")
|
||||
info = NormalizeImageInfo("windows-server-2008-dc-cn-20180717", "", "", "", "")
|
||||
t.Logf("%#v", info)
|
||||
}
|
||||
|
||||
@@ -38,21 +38,23 @@ func normalizeOsDistribution(osDist string, imageName string) string {
|
||||
osDist = imageName
|
||||
}
|
||||
osDist = strings.ToLower(osDist)
|
||||
if strings.HasPrefix(osDist, "centos") || strings.HasPrefix(osDist, "redhat") || strings.HasPrefix(osDist, "rhel") {
|
||||
if strings.Contains(osDist, "centos") || strings.Contains(osDist, "redhat") || strings.Contains(osDist, "rhel") {
|
||||
return "CentOS"
|
||||
} else if strings.HasPrefix(osDist, "ubuntu") {
|
||||
} else if strings.Contains(osDist, "ubuntu") {
|
||||
return "Ubuntu"
|
||||
} else if strings.HasPrefix(osDist, "suse") {
|
||||
} else if strings.Contains(osDist, "suse") {
|
||||
return "SUSE"
|
||||
} else if strings.HasPrefix(osDist, "opensuse") {
|
||||
} else if strings.Contains(osDist, "opensuse") {
|
||||
return "OpenSUSE"
|
||||
} else if strings.HasPrefix(osDist, "debian") {
|
||||
} else if strings.Contains(osDist, "debian") {
|
||||
return "Debian"
|
||||
} else if strings.HasPrefix(osDist, "coreos") {
|
||||
} else if strings.Contains(osDist, "coreos") {
|
||||
return "CoreOS"
|
||||
} else if strings.HasPrefix(osDist, "aliyun") {
|
||||
} else if strings.Contains(osDist, "aliyun") {
|
||||
return "Aliyun"
|
||||
} else if strings.HasPrefix(osDist, "windows") {
|
||||
} else if strings.Contains(osDist, "freebsd") {
|
||||
return "FreeBSD"
|
||||
} else if strings.Contains(osDist, "windows") {
|
||||
if strings.Contains(osDist, "2003") {
|
||||
return "Windows Server 2003"
|
||||
} else if strings.Contains(osDist, "2008") {
|
||||
@@ -69,18 +71,45 @@ func normalizeOsDistribution(osDist string, imageName string) string {
|
||||
}
|
||||
}
|
||||
|
||||
type ImageInfo struct {
|
||||
Name string
|
||||
OsArch string
|
||||
OsType string
|
||||
OsDistro string
|
||||
var imageVersions = map[string][]string{
|
||||
"CentOS": {"5", "6", "7"},
|
||||
"FreeBSD": {"10"},
|
||||
"Ubuntu": {"10", "12", "14", "16"},
|
||||
"OpenSUSE": {"11", "12"},
|
||||
"SUSE": {"10", "11", "12", "13"},
|
||||
"Debian": {"6", "7", "8", "9"},
|
||||
"CoreOS": {"7"},
|
||||
"Aliyun": {},
|
||||
}
|
||||
|
||||
func NormalizeImageInfo(imageName, osArch, osType, osDist string) ImageInfo {
|
||||
func normalizeOsVersion(imageName string, osDist string, osVersion string) string {
|
||||
if versions, ok := imageVersions[osDist]; ok {
|
||||
for _, version := range versions {
|
||||
if strings.HasPrefix(osVersion, version) {
|
||||
return version
|
||||
}
|
||||
}
|
||||
if len(versions) > 0 {
|
||||
return versions[0]
|
||||
}
|
||||
}
|
||||
return "-"
|
||||
}
|
||||
|
||||
type ImageInfo struct {
|
||||
Name string
|
||||
OsArch string
|
||||
OsType string
|
||||
OsDistro string
|
||||
OsVersion string
|
||||
}
|
||||
|
||||
func NormalizeImageInfo(imageName, osArch, osType, osDist, osVersion string) ImageInfo {
|
||||
info := ImageInfo{}
|
||||
info.Name = imageName
|
||||
info.OsDistro = normalizeOsDistribution(osDist, imageName)
|
||||
info.OsType = normalizeOsType(osType, info.OsDistro)
|
||||
info.OsArch = normalizeOsArch(osArch, info.OsType, info.OsDistro)
|
||||
info.OsVersion = normalizeOsVersion(imageName, info.OsDistro, osVersion)
|
||||
return info
|
||||
}
|
||||
|
||||
@@ -226,6 +226,9 @@ func (self *SRegion) GetImportImageParams(name string, osArch, osDist, osVersion
|
||||
}
|
||||
if !utils.IsInStringArray(osVersion, _imageSet.OsVersions) {
|
||||
osVersion = "-"
|
||||
if len(_imageSet.OsVersions) > 0 {
|
||||
osVersion = _imageSet.OsVersions[0]
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user