From 0717498d54c4e7adb9c026bdfea556b3d703b5f0 Mon Sep 17 00:00:00 2001 From: ioito Date: Sat, 12 Oct 2019 16:07:02 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20openstack=E4=B8=8A=E4=BC=A0=E9=95=9C?= =?UTF-8?q?=E5=83=8F=E6=97=B6=E4=B9=9F=E6=8C=87=E5=AE=9Aos=5Ftype=E5=8F=8A?= =?UTF-8?q?=E9=A2=9D=E5=A4=96=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/multicloud/openstack/image.go | 26 ++++++++++++++++++++++-- pkg/multicloud/openstack/shell/image.go | 8 ++++++-- pkg/multicloud/openstack/storagecache.go | 11 +++++++++- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/pkg/multicloud/openstack/image.go b/pkg/multicloud/openstack/image.go index b35ed6a88d..9e7f3f3896 100644 --- a/pkg/multicloud/openstack/image.go +++ b/pkg/multicloud/openstack/image.go @@ -17,10 +17,12 @@ package openstack import ( "context" "net/url" + "strings" "time" "github.com/pkg/errors" "yunion.io/x/jsonutils" + "yunion.io/x/pkg/util/osprofile" "yunion.io/x/pkg/utils" api "yunion.io/x/onecloud/pkg/apis/compute" @@ -60,6 +62,8 @@ type SImage struct { OsHashAlgo string OsHashValue string OsHidden bool + OsDistro string + OsType string Owner string Size int MinRAM int @@ -170,10 +174,24 @@ func (image *SImage) GetSizeByte() int64 { } 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 + } + } return "Linux" } 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 @@ -243,11 +261,15 @@ func (region *SRegion) GetImageByName(name string) (*SImage, error) { return &images[0], nil } -func (region *SRegion) CreateImage(imageName string) (*SImage, error) { - params := map[string]string{ +func (region *SRegion) CreateImage(imageName string, osType string, osDist string, minDiskGb int, minRam int) (*SImage, error) { + params := map[string]interface{}{ "container_format": "bare", "disk_format": "vmdk", "name": imageName, + "min_disk": minDiskGb, + "min_ram": minRam, + "os_type": strings.ToLower(osType), + "os_distro": osDist, "hw_qemu_guest_agent": "yes", } diff --git a/pkg/multicloud/openstack/shell/image.go b/pkg/multicloud/openstack/shell/image.go index 87c5503d11..323144b1c1 100644 --- a/pkg/multicloud/openstack/shell/image.go +++ b/pkg/multicloud/openstack/shell/image.go @@ -52,11 +52,15 @@ func init() { }) type ImageCreateOptions struct { - NAME string + NAME string + OsType string `help:"os type" default:"linux" choices:"linux|windows"` + OsDistro string + MinDiskSizeGB int + MinRamMb int } shellutils.R(&ImageCreateOptions{}, "image-create", "Create image", func(cli *openstack.SRegion, args *ImageCreateOptions) error { - image, err := cli.CreateImage(args.NAME) + image, err := cli.CreateImage(args.NAME, args.OsType, args.OsDistro, args.MinDiskSizeGB, args.MinRamMb) if err != nil { return err } diff --git a/pkg/multicloud/openstack/storagecache.go b/pkg/multicloud/openstack/storagecache.go index 36f6e1520e..2e0e78554e 100644 --- a/pkg/multicloud/openstack/storagecache.go +++ b/pkg/multicloud/openstack/storagecache.go @@ -142,7 +142,16 @@ func (cache *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.T nameIdx++ } - img, err := cache.region.CreateImage(imageName) + minDiskSizeMb, _ := meta.Int("min_disk") + minRamMb, _ := meta.Int("min_ram") + osType, _ := meta.GetString("properties", "os_type") + osDist, _ := meta.GetString("properties", "os_distribution") + minDiskSizeGB := minDiskSizeMb / 1024 + if minDiskSizeMb%1024 > 0 { + minDiskSizeGB += 1 + } + + img, err := cache.region.CreateImage(imageName, osType, osDist, int(minDiskSizeGB), int(minRamMb)) if err != nil { return "", err }