解决冲突

This commit is contained in:
屈轩
2018-12-04 20:27:12 +08:00
10 changed files with 212 additions and 26 deletions
+1 -1
View File
@@ -91,7 +91,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
@@ -32,6 +32,7 @@ func (self *SManagedVirtualizationHostDriver) CheckAndSetCacheImage(ctx context.
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()
@@ -47,7 +48,7 @@ func (self *SManagedVirtualizationHostDriver) CheckAndSetCacheImage(ctx context.
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
@@ -103,7 +103,7 @@ func (self *SStoragecache) GetPath() string {
return ""
}
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)
+7 -5
View File
@@ -2,12 +2,9 @@ package aws
import (
"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"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"strings"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/onecloud/pkg/cloudprovider"
@@ -15,6 +12,11 @@ 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"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
type SStoragecache struct {
@@ -102,7 +104,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
@@ -97,7 +97,7 @@ func (self *SStoragecache) GetPath() string {
return ""
}
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)
+1 -1
View File
@@ -127,6 +127,6 @@ func (self *SDatastoreImageCache) DownloadImage(userCred mcclient.TokenCredentia
return nil, cloudprovider.ErrNotImplemented
}
func (self *SDatastoreImageCache) UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist string, extId string, isForce bool) (string, error) {
func (self *SDatastoreImageCache) UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) {
return "", cloudprovider.ErrNotImplemented
}
+82 -12
View File
@@ -3,12 +3,14 @@ package qcloud
import (
"fmt"
"strconv"
"strings"
"time"
"context"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/onecloud/pkg/cloudprovider"
"yunion.io/x/pkg/utils"
)
type ImageStatusType string
@@ -163,23 +165,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)
+3
View File
@@ -91,6 +91,9 @@ func (self *SRegion) GetClient() *SQcloudClient {
}
func (self *SRegion) GetIEipById(eipId string) (cloudprovider.ICloudEIP, error) {
if len(eipId) == 0 {
return nil, cloudprovider.ErrNotFound
}
eips, total, err := self.GetEips(eipId, "", 0, 1)
if err != nil {
return nil, err
+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
@@ -125,7 +125,7 @@ func (self *SStoragecache) GetPath() string {
return ""
}
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)
@@ -139,7 +139,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 {
@@ -147,7 +147,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, "")
@@ -214,7 +214,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