From 6ed2d8dfb749aec43cf486c736ef06ebf7542843 Mon Sep 17 00:00:00 2001 From: TangBin Date: Fri, 31 May 2019 11:18:42 +0800 Subject: [PATCH 1/2] ucloud image upload bugfix & storage type fix --- pkg/apis/compute/storage_const.go | 2 +- pkg/compute/hostdrivers/ucloud.go | 12 +++++++ pkg/util/multipart/reader.go | 6 ++-- pkg/util/multipart/reader_test.go | 4 +-- pkg/util/ucloud/host.go | 4 +-- pkg/util/ucloud/instance.go | 57 +++++++++++++++++++++++++------ pkg/util/ucloud/storagecache.go | 3 +- pkg/util/ucloud/zone.go | 2 ++ 8 files changed, 70 insertions(+), 20 deletions(-) diff --git a/pkg/apis/compute/storage_const.go b/pkg/apis/compute/storage_const.go index bd8cadc65d..f9b98c2638 100644 --- a/pkg/apis/compute/storage_const.go +++ b/pkg/apis/compute/storage_const.go @@ -82,7 +82,7 @@ const ( var ( DISK_TYPES = []string{DISK_TYPE_ROTATE, DISK_TYPE_SSD, DISK_TYPE_HYBRID} - STORAGE_LOCAL_TYPES = []string{STORAGE_LOCAL, STORAGE_BAREMETAL} + STORAGE_LOCAL_TYPES = []string{STORAGE_LOCAL, STORAGE_BAREMETAL, STORAGE_UCLOUD_LOCAL_NORMAL, STORAGE_UCLOUD_LOCAL_SSD, STORAGE_UCLOUD_EXCLUSIVE_LOCAL_DISK} STORAGE_SUPPORT_TYPES = STORAGE_LOCAL_TYPES STORAGE_ALL_TYPES = []string{ STORAGE_LOCAL, STORAGE_BAREMETAL, STORAGE_SHEEPDOG, diff --git a/pkg/compute/hostdrivers/ucloud.go b/pkg/compute/hostdrivers/ucloud.go index 18acbafbd0..49b469bdcc 100644 --- a/pkg/compute/hostdrivers/ucloud.go +++ b/pkg/compute/hostdrivers/ucloud.go @@ -43,6 +43,18 @@ func (self *SUCloudHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb if sizeGb < 20 || sizeGb > 4000 { return fmt.Errorf("The %s disk size must be in the range of 20G ~ 4000GB", storage.StorageType) } + } else if storage.StorageType == api.STORAGE_UCLOUD_LOCAL_SSD { + if sizeGb < 20 || sizeGb > 1000 { + return fmt.Errorf("The %s disk size must be in the range of 20G ~ 1000GB", storage.StorageType) + } + + return fmt.Errorf("Not support create/resize %s disk", storage.StorageType) + } else if storage.StorageType == api.STORAGE_UCLOUD_LOCAL_NORMAL { + if sizeGb < 20 || sizeGb > 2000 { + return fmt.Errorf("The %s disk size must be in the range of 20G ~ 2000GB", storage.StorageType) + } + + return fmt.Errorf("Not support create/resize %s disk", storage.StorageType) } else { return fmt.Errorf("Not support create %s disk", storage.StorageType) } diff --git a/pkg/util/multipart/reader.go b/pkg/util/multipart/reader.go index 2a591ce716..42fcd3216f 100644 --- a/pkg/util/multipart/reader.go +++ b/pkg/util/multipart/reader.go @@ -1,13 +1,13 @@ package multipart import ( - "io" + "bytes" "crypto/rand" "fmt" + "io" "net/textproto" - "strings" - "bytes" "sort" + "strings" ) type SReader struct { diff --git a/pkg/util/multipart/reader_test.go b/pkg/util/multipart/reader_test.go index 118d643461..1e54031089 100644 --- a/pkg/util/multipart/reader_test.go +++ b/pkg/util/multipart/reader_test.go @@ -1,10 +1,10 @@ package multipart import ( - "testing" - "strings" "bytes" "io" + "strings" + "testing" ) func TestReader(t *testing.T) { diff --git a/pkg/util/ucloud/host.go b/pkg/util/ucloud/host.go index 83580bfb40..2d787029a3 100644 --- a/pkg/util/ucloud/host.go +++ b/pkg/util/ucloud/host.go @@ -257,8 +257,8 @@ func (self *SHost) _createVM(name, imgId string, sysDisk cloudprovider.SDiskInfo log.Errorf("GetImage %s fail %s", imgId, err) return "", err } - if img.GetStatus() != cloudprovider.IMAGE_STATUS_ACTIVE { - log.Errorf("image %s status %s, expect %s", imgId, img.GetStatus(), cloudprovider.IMAGE_STATUS_ACTIVE) + if img.GetStatus() != api.CACHED_IMAGE_STATUS_READY { + log.Errorf("image %s status %s, expect %s", imgId, img.GetStatus(), api.CACHED_IMAGE_STATUS_READY) return "", fmt.Errorf("image not ready") } diff --git a/pkg/util/ucloud/instance.go b/pkg/util/ucloud/instance.go index 8eda93cbc9..e05a9e5064 100644 --- a/pkg/util/ucloud/instance.go +++ b/pkg/util/ucloud/instance.go @@ -20,9 +20,11 @@ import ( "fmt" "strings" "time" + "yunion.io/x/pkg/utils" "yunion.io/x/jsonutils" "yunion.io/x/log" + "yunion.io/x/onecloud/pkg/multicloud" "yunion.io/x/pkg/util/osprofile" billing_api "yunion.io/x/onecloud/pkg/apis/billing" @@ -93,7 +95,9 @@ func (self *SInstance) GetError() error { type DiskSet struct { DiskID string `json:"DiskId"` + DiskType string `json:"DiskType"` Drive string `json:"Drive"` + IsBoot bool `json:"IsBoot"` Size int `json:"Size"` Encrypted string `json:"Encrypted"` Type string `json:"Type"` @@ -220,10 +224,38 @@ func (self *SInstance) GetIHost() cloudprovider.ICloudHost { return self.host } +func (self *SInstance) GetLocalDisk(diskId, storageType string, sizeGB int, isBoot bool) SDisk { + diskType := "" + if isBoot { + diskType = "SystemDisk" + } + + disk := SDisk{ + SDisk: multicloud.SDisk{}, + Status: "Available", + UHostID: self.GetId(), + Name: diskId, + Zone: self.host.zone.GetId(), + DiskType: diskType, + UDiskID: diskId, + UHostName: self.GetName(), + CreateTime: self.CreateTime, + SizeGB: sizeGB, + } + + disk.storage = &SStorage{zone: self.host.zone, storageType: storageType} + return disk +} + func (self *SInstance) GetIDisks() ([]cloudprovider.ICloudDisk, error) { + localDisks := make([]SDisk, 0) diskIds := make([]string, 0) for _, disk := range self.DiskSet { - diskIds = append(diskIds, disk.DiskID) + if utils.IsInStringArray(disk.DiskType, []string{api.STORAGE_UCLOUD_LOCAL_NORMAL, api.STORAGE_UCLOUD_LOCAL_SSD}) { + localDisks = append(localDisks, self.GetLocalDisk(disk.DiskID, disk.DiskType, disk.Size, disk.IsBoot)) + } else { + diskIds = append(diskIds, disk.DiskID) + } } disks, err := self.host.zone.region.GetDisks("", "", diskIds) @@ -231,19 +263,22 @@ func (self *SInstance) GetIDisks() ([]cloudprovider.ICloudDisk, error) { return nil, err } + disks = append(disks, localDisks...) idisks := make([]cloudprovider.ICloudDisk, len(disks)) for i := 0; i < len(disks); i += 1 { - var category string - if strings.Contains(disks[i].DiskType, "SSD") { - category = api.STORAGE_UCLOUD_CLOUD_SSD - } else { - category = api.STORAGE_UCLOUD_CLOUD_NORMAL + if disks[i].storage == nil { + var category string + if strings.Contains(disks[i].DiskType, "SSD") { + category = api.STORAGE_UCLOUD_CLOUD_SSD + } else { + category = api.STORAGE_UCLOUD_CLOUD_NORMAL + } + storage, err := self.host.zone.getStorageByCategory(category) + if err != nil { + return nil, err + } + disks[i].storage = storage } - storage, err := self.host.zone.getStorageByCategory(category) - if err != nil { - return nil, err - } - disks[i].storage = storage idisks[i] = &disks[i] // 将系统盘放到第0个位置 if disks[i].GetDiskType() == api.DISK_TYPE_SYS { diff --git a/pkg/util/ucloud/storagecache.go b/pkg/util/ucloud/storagecache.go index 7d660d6e93..9d832471b3 100644 --- a/pkg/util/ucloud/storagecache.go +++ b/pkg/util/ucloud/storagecache.go @@ -22,6 +22,7 @@ import ( "time" "yunion.io/x/log" + api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/compute/options" "yunion.io/x/onecloud/pkg/mcclient/auth" "yunion.io/x/onecloud/pkg/mcclient/modules" @@ -124,7 +125,7 @@ func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.To if err != nil { log.Errorf("GetImageStatus error %s", err) } - if img.GetStatus() == cloudprovider.IMAGE_STATUS_ACTIVE && !isForce { + if img.GetStatus() == api.CACHED_IMAGE_STATUS_READY && !isForce { return image.ExternalId, nil } } else { diff --git a/pkg/util/ucloud/zone.go b/pkg/util/ucloud/zone.go index fbd4e3eb09..6dc57c271b 100644 --- a/pkg/util/ucloud/zone.go +++ b/pkg/util/ucloud/zone.go @@ -28,6 +28,8 @@ import ( var StorageTypes = []string{ api.STORAGE_UCLOUD_CLOUD_NORMAL, api.STORAGE_UCLOUD_CLOUD_SSD, + api.STORAGE_UCLOUD_LOCAL_NORMAL, // 本地盘 + api.STORAGE_UCLOUD_LOCAL_SSD, // 本地SSD盘 } type SZone struct { From 9fd5867483f7a7bece0363c0bd089ce25fa5fd17 Mon Sep 17 00:00:00 2001 From: TangBin Date: Thu, 6 Jun 2019 16:17:13 +0800 Subject: [PATCH 2/2] fmt import --- pkg/util/ucloud/instance.go | 2 +- pkg/util/ucloud/storagecache.go | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/pkg/util/ucloud/instance.go b/pkg/util/ucloud/instance.go index e05a9e5064..af08aef7ec 100644 --- a/pkg/util/ucloud/instance.go +++ b/pkg/util/ucloud/instance.go @@ -24,11 +24,11 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/log" - "yunion.io/x/onecloud/pkg/multicloud" "yunion.io/x/pkg/util/osprofile" billing_api "yunion.io/x/onecloud/pkg/apis/billing" api "yunion.io/x/onecloud/pkg/apis/compute" + "yunion.io/x/onecloud/pkg/multicloud" "yunion.io/x/onecloud/pkg/cloudprovider" "yunion.io/x/onecloud/pkg/util/billing" ) diff --git a/pkg/util/ucloud/storagecache.go b/pkg/util/ucloud/storagecache.go index 9d832471b3..9821b4480f 100644 --- a/pkg/util/ucloud/storagecache.go +++ b/pkg/util/ucloud/storagecache.go @@ -22,15 +22,14 @@ import ( "time" "yunion.io/x/log" + "yunion.io/x/jsonutils" + "yunion.io/x/pkg/utils" + api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/compute/options" "yunion.io/x/onecloud/pkg/mcclient/auth" "yunion.io/x/onecloud/pkg/mcclient/modules" "yunion.io/x/onecloud/pkg/util/qemuimg" - "yunion.io/x/pkg/utils" - - "yunion.io/x/jsonutils" - "yunion.io/x/onecloud/pkg/cloudprovider" "yunion.io/x/onecloud/pkg/mcclient" )