腾讯云导入镜像适配

This commit is contained in:
屈轩
2018-12-04 18:50:58 +08:00
parent e0fe689607
commit 3fa55efe42
11 changed files with 213 additions and 27 deletions
+1 -1
View File
@@ -86,7 +86,7 @@ type ICloudStoragecache interface {
DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error)
UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist string, extId string, isForce bool) (string, error)
UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error)
}
type ICloudStorage interface {
+2 -1
View File
@@ -54,6 +54,7 @@ func (self *SAliyunHostDriver) CheckAndSetCacheImage(ctx context.Context, host *
osArch, _ := params.GetString("os_arch")
osType, _ := params.GetString("os_type")
osDist, _ := params.GetString("os_distribution")
osVersion, _ := params.GetString("os_version")
isForce := jsonutils.QueryBoolean(params, "is_force", false)
userCred := task.GetUserCred()
@@ -69,7 +70,7 @@ func (self *SAliyunHostDriver) CheckAndSetCacheImage(ctx context.Context, host *
return nil, err
}
extImgId, err := iStorageCache.UploadImage(userCred, imageId, osArch, osType, osDist, scimg.ExternalId, isForce)
extImgId, err := iStorageCache.UploadImage(userCred, imageId, osArch, osType, osDist, osVersion, scimg.ExternalId, isForce)
if err != nil {
return nil, err
+2 -1
View File
@@ -57,6 +57,7 @@ func (self *SAwsHostDriver) CheckAndSetCacheImage(ctx context.Context, host *mod
osArch, _ := params.GetString("os_arch")
osType, _ := params.GetString("os_type")
osDist, _ := params.GetString("os_distribution")
osVersion, _ := params.GetString("os_version")
isForce := jsonutils.QueryBoolean(params, "is_force", false)
userCred := task.GetUserCred()
@@ -72,7 +73,7 @@ func (self *SAwsHostDriver) CheckAndSetCacheImage(ctx context.Context, host *mod
return nil, err
}
extImgId, err := iStorageCache.UploadImage(userCred, imageId, osArch, osType, osDist, scimg.ExternalId, isForce)
extImgId, err := iStorageCache.UploadImage(userCred, imageId, osArch, osType, osDist, osVersion, scimg.ExternalId, isForce)
if err != nil {
return nil, err
+2 -1
View File
@@ -58,6 +58,7 @@ func (self *SAzureHostDriver) CheckAndSetCacheImage(ctx context.Context, host *m
osArch, _ := params.GetString("os_arch")
osType, _ := params.GetString("os_type")
osDist, _ := params.GetString("os_distribution")
osVersion, _ := params.GetString("os_version")
isForce := jsonutils.QueryBoolean(params, "is_force", false)
userCred := task.GetUserCred()
@@ -71,7 +72,7 @@ func (self *SAzureHostDriver) CheckAndSetCacheImage(ctx context.Context, host *m
return nil, err
}
extImgId, err := iStorageCache.UploadImage(userCred, imageId, osArch, osType, osDist, scimg.ExternalId, isForce)
extImgId, err := iStorageCache.UploadImage(userCred, imageId, osArch, osType, osDist, osVersion, scimg.ExternalId, isForce)
if err != nil {
return nil, err
} else {
+2 -1
View File
@@ -60,6 +60,7 @@ func (self *SQcloudHostDriver) CheckAndSetCacheImage(ctx context.Context, host *
osArch, _ := params.GetString("os_arch")
osType, _ := params.GetString("os_type")
osDist, _ := params.GetString("os_distribution")
osVersion, _ := params.GetString("os_version")
isForce := jsonutils.QueryBoolean(params, "is_force", false)
userCred := task.GetUserCred()
@@ -73,7 +74,7 @@ func (self *SQcloudHostDriver) CheckAndSetCacheImage(ctx context.Context, host *
return nil, err
}
extImgId, err := iStorageCache.UploadImage(userCred, imageId, osArch, osType, osDist, scimg.ExternalId, isForce)
extImgId, err := iStorageCache.UploadImage(userCred, imageId, osArch, osType, osDist, osVersion, scimg.ExternalId, isForce)
if err != nil {
return nil, err
}
+1 -1
View File
@@ -87,7 +87,7 @@ func (self *SStoragecache) GetIImages() ([]cloudprovider.ICloudImage, error) {
return self.iimages, nil
}
func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist string, extId string, isForce bool) (string, error) {
func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) {
if len(extId) > 0 {
log.Debugf("UploadImage: Image external ID exists %s", extId)
+6 -4
View File
@@ -3,12 +3,10 @@ package aws
import (
"bytes"
"fmt"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/s3"
"io/ioutil"
"strings"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/onecloud/pkg/cloudprovider"
@@ -16,6 +14,10 @@ import (
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/auth"
"yunion.io/x/onecloud/pkg/mcclient/modules"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/s3"
)
type SStoragecache struct {
@@ -87,7 +89,7 @@ func (self *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imag
return self.downloadImage(userCred, imageId, extId)
}
func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist string, extId string, isForce bool) (string, error) {
func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) {
if len(extId) > 0 {
log.Debugf("UploadImage: Image external ID exists %s", extId)
+1 -1
View File
@@ -84,7 +84,7 @@ func (self *SStoragecache) GetIImages() ([]cloudprovider.ICloudImage, error) {
return self.iimages, nil
}
func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist string, extId string, isForce bool) (string, error) {
func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) {
if len(extId) > 0 {
log.Debugf("UploadImage: Image external ID exists %s", extId)
status, err := self.region.GetImageStatus(extId)
+82 -12
View File
@@ -3,11 +3,13 @@ package qcloud
import (
"fmt"
"strconv"
"strings"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/onecloud/pkg/cloudprovider"
"yunion.io/x/pkg/utils"
)
type ImageStatusType string
@@ -162,23 +164,91 @@ func (self *SRegion) GetImageByName(name string) (*SImage, error) {
return &images[0], nil
}
func (self *SRegion) ImportImage(name string, osArch string, osType string, osVersion string, imageUrl string) (*SImage, error) {
params := make(map[string]string)
params["ImageName"] = name
if _, err := strconv.Atoi(osVersion); len(osVersion) == 0 || err != nil {
type ImportImageOsListSupported struct {
Linux []string //"CentOS|Ubuntu|Debian|OpenSUSE|SUSE|CoreOS|FreeBSD|Other Linux"
Windows []string //"Windows Server 2008|Windows Server 2012|Windows Server 2016"
}
type ImportImageOsVersionSet struct {
Architecture []string
OsName string //"CentOS|Ubuntu|Debian|OpenSUSE|SUSE|CoreOS|FreeBSD|Other Linux|Windows Server 2008|Windows Server 2012|Windows Server 2016"
OsVersions []string
}
type SupportImageSet struct {
ImportImageOsListSupported ImportImageOsListSupported
ImportImageOsVersionSet []ImportImageOsVersionSet
}
func (self *SRegion) GetSupportImageSet() (*SupportImageSet, error) {
body, err := self.cvmRequest("DescribeImportImageOs", map[string]string{})
if err != nil {
return nil, err
}
imageSet := SupportImageSet{}
return &imageSet, body.Unmarshal(&imageSet)
}
func (self *SRegion) GetImportImageParams(name string, osArch, osDist, osVersion string, imageUrl string) (map[string]string, error) {
params := map[string]string{}
imageSet, err := self.GetSupportImageSet()
if err != nil {
return nil, err
}
osType := ""
for _, _imageSet := range imageSet.ImportImageOsVersionSet {
if strings.ToLower(osDist) == strings.ToLower(_imageSet.OsName) { //Linux一般可正常匹配
osType = _imageSet.OsName
} else if strings.Contains(strings.ToLower(_imageSet.OsName), "windows") && strings.Contains(strings.ToLower(osDist), "windows") {
info := strings.Split(_imageSet.OsName, " ")
_osVersion := "2008"
for _, version := range info {
if _, err := strconv.Atoi(version); err == nil {
_osVersion = version
break
}
}
if strings.Contains(osDist+osVersion, _osVersion) {
osType = _imageSet.OsName
}
}
if len(osType) == 0 {
continue
}
if !utils.IsInStringArray(osArch, _imageSet.Architecture) {
osArch = "x86_64"
}
for _, _osVersion := range _imageSet.OsVersions {
if strings.HasPrefix(osVersion, _osVersion) {
osVersion = _osVersion
break
}
}
if !utils.IsInStringArray(osVersion, _imageSet.OsVersions) {
osVersion = "-"
}
break
}
if len(osType) == 0 {
osType = "Other Linux"
osArch = "x86_64"
osVersion = "-"
}
params["OsVersion"] = osVersion // "6|7|8|-"
if len(osType) == 0 || osType == "linux" {
osType = "Other Linux"
}
params["OsType"] = osType // "CentOS|Ubuntu|Debian|OpenSUSE|SUSE|CoreOS|FreeBSD|Other Linux|Windows Server 2008|Windows Server 2012|Windows Server 2016"
if len(osArch) == 0 {
osArch = "x86_64"
}
params["ImageName"] = name
params["OsType"] = osType
params["OsVersion"] = osVersion
params["Architecture"] = osArch // "x86_64|i386"
params["ImageUrl"] = imageUrl
params["Force"] = "true"
return params, nil
}
func (self *SRegion) ImportImage(name string, osArch, osDist, osVersion string, imageUrl string) (*SImage, error) {
params, err := self.GetImportImageParams(name, osArch, osDist, osVersion, imageUrl)
if err != nil {
return nil, err
}
log.Debugf("Upload image with params %#v", params)
+110
View File
@@ -1,6 +1,9 @@
package shell
import (
"fmt"
"yunion.io/x/log"
"yunion.io/x/onecloud/pkg/util/qcloud"
"yunion.io/x/onecloud/pkg/util/shellutils"
)
@@ -52,4 +55,111 @@ func init() {
return cli.DeleteImage(args.ID)
})
type ImageSupportSetOptions struct {
}
shellutils.R(&ImageSupportSetOptions{}, "image-support-set", "Show image support set", func(cli *qcloud.SRegion, args *ImageSupportSetOptions) error {
imageSet, err := cli.GetSupportImageSet()
if err != nil {
return err
}
printObject(imageSet)
return nil
})
type ImageParam struct {
osArch string
osDist string
osVersion string
OutputArch string
OutputDist string
OutputOsVersion string
}
shellutils.R(&ImageSupportSetOptions{}, "image-import-test", "Test image import params", func(cli *qcloud.SRegion, args *ImageSupportSetOptions) error {
imageParams := []ImageParam{
{
osArch: "test",
osDist: "Centos",
osVersion: "3.4",
OutputArch: "x86_64",
OutputDist: "CentOS",
OutputOsVersion: "-",
},
{
osArch: "i386",
osDist: "Centos",
osVersion: "6.9",
OutputArch: "i386",
OutputDist: "CentOS",
OutputOsVersion: "6",
},
{
osArch: "i386",
osDist: "Centos",
osVersion: "7.1.1503",
OutputArch: "i386",
OutputDist: "CentOS",
OutputOsVersion: "7",
},
{
osArch: "",
osDist: "",
osVersion: "7.1",
OutputArch: "x86_64",
OutputDist: "Other Linux",
OutputOsVersion: "-",
},
{
osArch: "x86_64",
osDist: "Windows Server",
osVersion: "2008 R2 Datacenter Evaluation",
OutputArch: "x86_64",
OutputDist: "Windows Server 2008",
OutputOsVersion: "-",
},
{
osArch: "x86_64",
osDist: "Ubuntu",
osVersion: "16.04.5",
OutputArch: "x86_64",
OutputDist: "Ubuntu",
OutputOsVersion: "16",
},
{
osArch: "x86_64",
osDist: "Windows%20Server%202008%20R2%20Datacenter",
osVersion: "6.1",
OutputArch: "x86_64",
OutputDist: "Windows Server 2008",
OutputOsVersion: "-",
},
{
osArch: "x86_64",
osDist: "Windows Server 2012 R2 Datacenter Evaluation",
osVersion: "6.2",
OutputArch: "x86_64",
OutputDist: "Windows Server 2012",
OutputOsVersion: "-",
},
}
for _, imageParam := range imageParams {
log.Debugf("process %s", imageParam.osDist)
params, err := cli.GetImportImageParams("", imageParam.osArch, imageParam.osDist, imageParam.osVersion, "")
if err != nil {
return err
}
if params["OsType"] != imageParam.OutputDist {
return fmt.Errorf("params: %s osType should be %s not %s", imageParam, imageParam.OutputDist, params["OsType"])
}
if params["OsVersion"] != imageParam.OutputOsVersion {
return fmt.Errorf("params: %s OsVersion should be %s not %s", imageParam, imageParam.OutputOsVersion, params["OsVersion"])
}
if params["Architecture"] != imageParam.OutputArch {
return fmt.Errorf("params: %s Architecture should be %s not %s", imageParam, imageParam.OutputArch, params["Architecture"])
}
}
return nil
})
}
+4 -4
View File
@@ -109,7 +109,7 @@ func (self *SStoragecache) GetIImages() ([]cloudprovider.ICloudImage, error) {
return self.iimages, nil
}
func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist string, extId string, isForce bool) (string, error) {
func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) {
if len(extId) > 0 {
log.Debugf("UploadImage: Image external ID exists %s", extId)
@@ -123,7 +123,7 @@ func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageI
} else {
log.Debugf("UploadImage: no external ID")
}
return self.uploadImage(userCred, imageId, osArch, osType, osDist, isForce)
return self.uploadImage(userCred, imageId, osArch, osType, osDist, osVersion, isForce)
}
func (self *SRegion) getCosUrl(bucket, object string) string {
@@ -131,7 +131,7 @@ func (self *SRegion) getCosUrl(bucket, object string) string {
return fmt.Sprintf("http://%s-%s.cos.%s.myqcloud.com/%s", bucket, self.client.AppID, self.Region, object)
}
func (self *SStoragecache) uploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist string, isForce bool) (string, error) {
func (self *SStoragecache) uploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, isForce bool) (string, error) {
// first upload image to oss
s := auth.GetAdminSession(options.Options.Region, "")
@@ -198,7 +198,7 @@ func (self *SStoragecache) uploadImage(userCred mcclient.TokenCredential, imageI
}
log.Debugf("Import image %s", imageName)
if image, err := self.region.ImportImage(imageName, osArch, osType, osDist, self.region.getCosUrl(bucketName, imageId)); err != nil {
if image, err := self.region.ImportImage(imageName, osArch, osDist, osVersion, self.region.getCosUrl(bucketName, imageId)); err != nil {
return "", err
} else if cloudprovider.WaitStatus(image, string(ImageStatusAvailable), 15*time.Second, 3600*time.Second); err != nil {
return "", err