From e5aa26f0afbfadc847e4399a19ae798eafac0d57 Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Fri, 4 Jan 2019 17:36:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=EF=BC=9A1.=20storagecache.Ca?= =?UTF-8?q?cheImage=E6=8E=A5=E5=8F=A3=E9=9C=80=E8=A6=81=E6=8C=87=E5=AE=9Af?= =?UTF-8?q?ormat=202.=20PerformAction=E9=BB=98=E8=AE=A4=E8=BF=94=E5=9B=9EA?= =?UTF-8?q?ctionNotFound=20Error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/climc/shell/hosts.go | 7 ++++--- cmd/climc/shell/storagecaches.go | 10 +++++++--- cmd/climc/shell/storages.go | 10 +++++++--- pkg/cloudcommon/db/modelbase.go | 10 ++++++---- pkg/compute/guestdrivers/baremetals.go | 2 +- pkg/compute/guestdrivers/managedvirtual.go | 2 +- pkg/compute/guestdrivers/virtualization.go | 4 ++-- pkg/compute/hostdrivers/kvm.go | 3 +++ pkg/compute/models/guestdrivers.go | 2 +- pkg/compute/models/guests.go | 2 +- pkg/compute/models/hosts.go | 7 ++++--- pkg/compute/models/storagecaches.go | 9 +++++++-- pkg/compute/tasks/disk_create_task.go | 6 +++--- pkg/compute/tasks/guest_backup_tasks.go | 2 +- pkg/compute/tasks/guest_insert_iso_task.go | 2 +- pkg/compute/tasks/guest_live_migrate_task.go | 2 +- pkg/compute/tasks/guest_rebuild_root_task.go | 3 ++- pkg/compute/tasks/guest_start_task.go | 4 ++-- pkg/util/qemuimg/qemuimg.go | 2 +- 19 files changed, 55 insertions(+), 34 deletions(-) diff --git a/cmd/climc/shell/hosts.go b/cmd/climc/shell/hosts.go index a58877802d..19d472242c 100644 --- a/cmd/climc/shell/hosts.go +++ b/cmd/climc/shell/hosts.go @@ -419,9 +419,10 @@ func init() { }) type HostCacheImageActionOptions struct { - ID string `help:"ID or name of host"` - IMAGE string `help:"ID or name of image"` - Force bool `help:"Force refresh cache, even if the image exists in cache"` + ID string `help:"ID or name of host"` + IMAGE string `help:"ID or name of image"` + Force bool `help:"Force refresh cache, even if the image exists in cache"` + Format string `help:"image format" choices:"iso|vmdk|qcow2|vhd"` } R(&HostCacheImageActionOptions{}, "host-cache-image", "Ask a host to cache a image", func(s *mcclient.ClientSession, args *HostCacheImageActionOptions) error { params := jsonutils.NewDict() diff --git a/cmd/climc/shell/storagecaches.go b/cmd/climc/shell/storagecaches.go index 4a4772a177..baf58fc8d5 100644 --- a/cmd/climc/shell/storagecaches.go +++ b/cmd/climc/shell/storagecaches.go @@ -46,9 +46,10 @@ func init() { }) type StorageCacheImageActionOptions struct { - ID string `help:"ID or name of storage"` - IMAGE string `help:"ID or name of image"` - Force bool `help:"Force refresh cache, even if the image exists in cache"` + ID string `help:"ID or name of storage"` + IMAGE string `help:"ID or name of image"` + Force bool `help:"Force refresh cache, even if the image exists in cache"` + Format string `help:"image format" choices:"iso|qcow2|vmdk|vhd"` } R(&StorageCacheImageActionOptions{}, "storagecache-cache-image", "Ask a storage cache to cache a image", func(s *mcclient.ClientSession, args *StorageCacheImageActionOptions) error { params := jsonutils.NewDict() @@ -56,6 +57,9 @@ func init() { if args.Force { params.Add(jsonutils.JSONTrue, "is_force") } + if len(args.Format) > 0 { + params.Add(jsonutils.NewString(args.Format), "format") + } storage, err := modules.Storagecaches.PerformAction(s, args.ID, "cache-image", params) if err != nil { return err diff --git a/cmd/climc/shell/storages.go b/cmd/climc/shell/storages.go index 12be0e6244..67006b1eef 100644 --- a/cmd/climc/shell/storages.go +++ b/cmd/climc/shell/storages.go @@ -172,9 +172,10 @@ func init() { }) type StorageCacheImageActionOptions struct { - ID string `help:"ID or name of storage"` - IMAGE string `help:"ID or name of image"` - Force bool `help:"Force refresh cache, even if the image exists in cache"` + ID string `help:"ID or name of storage"` + IMAGE string `help:"ID or name of image"` + Force bool `help:"Force refresh cache, even if the image exists in cache"` + Format string `help:"Image force" choices:"iso|vmdk|qcow2|vhd"` } R(&StorageCacheImageActionOptions{}, "storage-cache-image", "Ask a storage to cache a image", func(s *mcclient.ClientSession, args *StorageCacheImageActionOptions) error { params := jsonutils.NewDict() @@ -182,6 +183,9 @@ func init() { if args.Force { params.Add(jsonutils.JSONTrue, "is_force") } + if len(args.Format) > 0 { + params.Add(jsonutils.NewString(args.Format), "format") + } storage, err := modules.Storages.PerformAction(s, args.ID, "cache-image", params) if err != nil { return err diff --git a/pkg/cloudcommon/db/modelbase.go b/pkg/cloudcommon/db/modelbase.go index 9ef211a4f3..1adfd7f702 100644 --- a/pkg/cloudcommon/db/modelbase.go +++ b/pkg/cloudcommon/db/modelbase.go @@ -7,9 +7,11 @@ import ( "net/http" "yunion.io/x/jsonutils" - "yunion.io/x/onecloud/pkg/appsrv" - "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/sqlchemy" + + "yunion.io/x/onecloud/pkg/appsrv" + "yunion.io/x/onecloud/pkg/httperrors" + "yunion.io/x/onecloud/pkg/mcclient" ) type SModelBase struct { @@ -149,7 +151,7 @@ func (manager *SModelBaseManager) AllowPerformAction(ctx context.Context, userCr } func (manager *SModelBaseManager) PerformAction(ctx context.Context, userCred mcclient.TokenCredential, action string, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) { - return nil, nil + return nil, httperrors.NewActionNotFoundError("Action %s not found", action) } func (manager *SModelBaseManager) AllowPerformCheckCreateData(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool { @@ -250,7 +252,7 @@ func (model *SModelBase) AllowPerformAction(ctx context.Context, userCred mcclie } func (model *SModelBase) PerformAction(ctx context.Context, userCred mcclient.TokenCredential, action string, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) { - return nil, nil + return nil, httperrors.NewActionNotFoundError("Action %s not found", action) } // update hooks diff --git a/pkg/compute/guestdrivers/baremetals.go b/pkg/compute/guestdrivers/baremetals.go index 6fe3c4814f..ef8dabb121 100644 --- a/pkg/compute/guestdrivers/baremetals.go +++ b/pkg/compute/guestdrivers/baremetals.go @@ -305,7 +305,7 @@ func (self *SBaremetalGuestDriver) PerformStart(ctx context.Context, userCred mc return guest.StartGueststartTask(ctx, userCred, data, "") } -func (self *SBaremetalGuestDriver) CheckDiskTemplateOnStorage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, storageId string, task taskman.ITask) error { +func (self *SBaremetalGuestDriver) CheckDiskTemplateOnStorage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, format string, storageId string, task taskman.ITask) error { task.ScheduleRun(nil) return nil } diff --git a/pkg/compute/guestdrivers/managedvirtual.go b/pkg/compute/guestdrivers/managedvirtual.go index ca15c56993..0f054e822a 100644 --- a/pkg/compute/guestdrivers/managedvirtual.go +++ b/pkg/compute/guestdrivers/managedvirtual.go @@ -119,7 +119,7 @@ func (self *SManagedVirtualizedGuestDriver) RequestGuestCreateAllDisks(ctx conte if storageCache == nil { return fmt.Errorf("no valid storage cache") } - return storageCache.StartImageCacheTask(ctx, task.GetUserCred(), imageId, false, task.GetTaskId()) + return storageCache.StartImageCacheTask(ctx, task.GetUserCred(), imageId, diskCat.Root.DiskFormat, false, task.GetTaskId()) } func (self *SManagedVirtualizedGuestDriver) RequestDetachDisk(ctx context.Context, guest *models.SGuest, task taskman.ITask) error { diff --git a/pkg/compute/guestdrivers/virtualization.go b/pkg/compute/guestdrivers/virtualization.go index 11fb8ab16c..3f0d8ee7ae 100644 --- a/pkg/compute/guestdrivers/virtualization.go +++ b/pkg/compute/guestdrivers/virtualization.go @@ -205,7 +205,7 @@ func (self *SVirtualizedGuestDriver) PerformStart(ctx context.Context, userCred return guest.StartGueststartTask(ctx, userCred, data, "") } -func (self *SVirtualizedGuestDriver) CheckDiskTemplateOnStorage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, storageId string, task taskman.ITask) error { +func (self *SVirtualizedGuestDriver) CheckDiskTemplateOnStorage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, format string, storageId string, task taskman.ITask) error { storage := models.StorageManager.FetchStorageById(storageId) if storage == nil { return fmt.Errorf("No such storage?? %s", storageId) @@ -214,7 +214,7 @@ func (self *SVirtualizedGuestDriver) CheckDiskTemplateOnStorage(ctx context.Cont if cache == nil { return fmt.Errorf("Cache is missing from storage") } - return cache.StartImageCacheTask(ctx, userCred, imageId, false, task.GetTaskId()) + return cache.StartImageCacheTask(ctx, userCred, imageId, format, false, task.GetTaskId()) } func (self *SVirtualizedGuestDriver) CanKeepDetachDisk() bool { diff --git a/pkg/compute/hostdrivers/kvm.go b/pkg/compute/hostdrivers/kvm.go index 0b1ab86992..fb028d7705 100644 --- a/pkg/compute/hostdrivers/kvm.go +++ b/pkg/compute/hostdrivers/kvm.go @@ -39,6 +39,7 @@ func (self *SKVMHostDriver) CheckAndSetCacheImage(ctx context.Context, host *mod if err != nil { return err } + format, _ := params.GetString("format") isForce := jsonutils.QueryBoolean(params, "is_force", false) obj, err := models.CachedimageManager.FetchById(imageId) if err != nil { @@ -52,6 +53,7 @@ func (self *SKVMHostDriver) CheckAndSetCacheImage(ctx context.Context, host *mod type contentStruct struct { ImageId string + Format string SrcUrl string IsForce bool StoragecacheId string @@ -59,6 +61,7 @@ func (self *SKVMHostDriver) CheckAndSetCacheImage(ctx context.Context, host *mod content := contentStruct{} content.ImageId = imageId + content.Format = format if srcHostCacheImage != nil { err = srcHostCacheImage.AddDownloadRefcount() diff --git a/pkg/compute/models/guestdrivers.go b/pkg/compute/models/guestdrivers.go index 2e388036fc..cb7e7845f8 100644 --- a/pkg/compute/models/guestdrivers.go +++ b/pkg/compute/models/guestdrivers.go @@ -84,7 +84,7 @@ type IGuestDriver interface { PerformStart(ctx context.Context, userCred mcclient.TokenCredential, guest *SGuest, data *jsonutils.JSONDict) error - CheckDiskTemplateOnStorage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, storageId string, task taskman.ITask) error + CheckDiskTemplateOnStorage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, format string, storageId string, task taskman.ITask) error GetGuestVncInfo(ctx context.Context, userCred mcclient.TokenCredential, guest *SGuest, host *SHost) (*jsonutils.JSONDict, error) diff --git a/pkg/compute/models/guests.go b/pkg/compute/models/guests.go index 3bfef2176a..efad6370ae 100644 --- a/pkg/compute/models/guests.go +++ b/pkg/compute/models/guests.go @@ -742,7 +742,7 @@ func (manager *SGuestManager) ValidateCreateData(ctx context.Context, userCred m } if len(diskConfig.ImageId) == 0 && len(diskConfig.SnapshotId) == 0 && !data.Contains("cdrom") { - return nil, httperrors.NewBadRequestError("Miss operate system???") + return nil, httperrors.NewBadRequestError("Miss operating system???") } if len(diskConfig.Backend) == 0 { diff --git a/pkg/compute/models/hosts.go b/pkg/compute/models/hosts.go index fd1df2e48d..1c09187c90 100644 --- a/pkg/compute/models/hosts.go +++ b/pkg/compute/models/hosts.go @@ -3235,15 +3235,16 @@ func (self *SHost) PerformCacheImage(ctx context.Context, userCred mcclient.Toke return nil, httperrors.NewInvalidStatusError("Cannot cache image with no checksum") } isForce := jsonutils.QueryBoolean(data, "is_force", false) - return nil, self.StartImageCacheTask(ctx, userCred, img.Id, isForce) + format, _ := data.GetString("format") + return nil, self.StartImageCacheTask(ctx, userCred, img.Id, format, isForce) } -func (self *SHost) StartImageCacheTask(ctx context.Context, userCred mcclient.TokenCredential, imageId string, isForce bool) error { +func (self *SHost) StartImageCacheTask(ctx context.Context, userCred mcclient.TokenCredential, imageId string, format string, isForce bool) error { sc := self.GetLocalStoragecache() if sc == nil { return fmt.Errorf("No local storage cache found") } - return sc.StartImageCacheTask(ctx, userCred, imageId, isForce, "") + return sc.StartImageCacheTask(ctx, userCred, imageId, format, isForce, "") } func (self *SHost) AllowPerformConvertHypervisor(ctx context.Context, diff --git a/pkg/compute/models/storagecaches.go b/pkg/compute/models/storagecaches.go index 946a800612..534ff8234a 100644 --- a/pkg/compute/models/storagecaches.go +++ b/pkg/compute/models/storagecaches.go @@ -238,10 +238,13 @@ func (self *SStoragecache) getMoreDetails(extra *jsonutils.JSONDict) *jsonutils. return extra } -func (self *SStoragecache) StartImageCacheTask(ctx context.Context, userCred mcclient.TokenCredential, imageId string, isForce bool, parentTaskId string) error { +func (self *SStoragecache) StartImageCacheTask(ctx context.Context, userCred mcclient.TokenCredential, imageId string, format string, isForce bool, parentTaskId string) error { StoragecachedimageManager.Register(ctx, userCred, self.Id, imageId) data := jsonutils.NewDict() data.Add(jsonutils.NewString(imageId), "image_id") + if len(format) > 0 { + data.Add(jsonutils.NewString(format), "format") + } image, _ := CachedimageManager.GetImageById(ctx, userCred, imageId, false) @@ -402,6 +405,8 @@ func (self *SStoragecache) PerformCacheImage(ctx context.Context, userCred mccli return nil, httperrors.NewInvalidStatusError("Cannot cache image with no checksum") } - err = self.StartImageCacheTask(ctx, userCred, image.Id, isForce, "") + format, _ := data.GetString("format") + + err = self.StartImageCacheTask(ctx, userCred, image.Id, format, isForce, "") return nil, err } diff --git a/pkg/compute/tasks/disk_create_task.go b/pkg/compute/tasks/disk_create_task.go index 10323654e0..5d86465954 100644 --- a/pkg/compute/tasks/disk_create_task.go +++ b/pkg/compute/tasks/disk_create_task.go @@ -27,7 +27,7 @@ func (self *DiskCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, } else { self.SetStage("OnStorageCacheImageComplete", nil) } - storagecache.StartImageCacheTask(ctx, self.UserCred, imageId, false, self.GetTaskId()) + storagecache.StartImageCacheTask(ctx, self.UserCred, imageId, disk.DiskFormat, false, self.GetTaskId()) } else { self.OnStorageCacheImageComplete(ctx, disk, nil) } @@ -38,7 +38,7 @@ func (self *DiskCreateTask) OnMasterStorageCacheImageComplete(ctx context.Contex storagecache := storage.GetStoragecache() imageId := disk.GetTemplateId() self.SetStage("OnStorageCacheImageComplete", nil) - storagecache.StartImageCacheTask(ctx, self.UserCred, imageId, false, self.GetTaskId()) + storagecache.StartImageCacheTask(ctx, self.UserCred, imageId, disk.DiskFormat, false, self.GetTaskId()) } func (self *DiskCreateTask) OnStorageCacheImageComplete(ctx context.Context, disk *models.SDisk, data jsonutils.JSONObject) { @@ -117,7 +117,7 @@ func (self *DiskCreateBackupTask) OnInit(ctx context.Context, obj db.IStandalone imageId := disk.GetTemplateId() if len(imageId) > 0 { self.SetStage("OnMasterStorageCreateDiskComplete", nil) - storagecache.StartImageCacheTask(ctx, self.UserCred, imageId, false, self.GetTaskId()) + storagecache.StartImageCacheTask(ctx, self.UserCred, imageId, disk.DiskFormat, false, self.GetTaskId()) } else { self.OnMasterStorageCreateDiskComplete(ctx, disk, nil) } diff --git a/pkg/compute/tasks/guest_backup_tasks.go b/pkg/compute/tasks/guest_backup_tasks.go index 4bc6425fc4..1e34ddbdf6 100644 --- a/pkg/compute/tasks/guest_backup_tasks.go +++ b/pkg/compute/tasks/guest_backup_tasks.go @@ -88,7 +88,7 @@ func (self *GuestStartAndSyncToBackupTask) OnInit(ctx context.Context, obj db.IS func (self *GuestStartAndSyncToBackupTask) checkTemplete(ctx context.Context, guest *models.SGuest) { diskCat := guest.CategorizeDisks() if diskCat.Root != nil && len(diskCat.Root.GetTemplateId()) > 0 { - err := guest.GetDriver().CheckDiskTemplateOnStorage(ctx, self.UserCred, diskCat.Root.GetTemplateId(), + err := guest.GetDriver().CheckDiskTemplateOnStorage(ctx, self.UserCred, diskCat.Root.GetTemplateId(), diskCat.Root.DiskFormat, diskCat.Root.BackupStorageId, self) if err != nil { self.SetStageFailed(ctx, err.Error()) diff --git a/pkg/compute/tasks/guest_insert_iso_task.go b/pkg/compute/tasks/guest_insert_iso_task.go index cd92dc93d6..aa01c1028d 100644 --- a/pkg/compute/tasks/guest_insert_iso_task.go +++ b/pkg/compute/tasks/guest_insert_iso_task.go @@ -37,7 +37,7 @@ func (self *GuestInsertIsoTask) prepareIsoImage(ctx context.Context, obj db.ISta storageCache := host.GetLocalStoragecache() if storageCache != nil { self.SetStage("OnIsoPrepareComplete", nil) - storageCache.StartImageCacheTask(ctx, self.UserCred, imageId, false, self.GetTaskId()) + storageCache.StartImageCacheTask(ctx, self.UserCred, imageId, "iso", false, self.GetTaskId()) } else { guest.EjectIso(self.UserCred) db.OpsLog.LogEvent(obj, db.ACT_ISO_PREPARE_FAIL, imageId, self.UserCred) diff --git a/pkg/compute/tasks/guest_live_migrate_task.go b/pkg/compute/tasks/guest_live_migrate_task.go index bfeecc6beb..ad970302dc 100644 --- a/pkg/compute/tasks/guest_live_migrate_task.go +++ b/pkg/compute/tasks/guest_live_migrate_task.go @@ -85,7 +85,7 @@ func (self *GuestMigrateTask) SaveScheduleResult(ctx context.Context, obj ISched if isLocalStorage { targetStorageCache := targetHost.GetLocalStoragecache() if targetStorageCache != nil { - targetStorageCache.StartImageCacheTask(ctx, self.UserCred, disk.TemplateId, false, self.GetTaskId()) + targetStorageCache.StartImageCacheTask(ctx, self.UserCred, disk.TemplateId, disk.DiskFormat, false, self.GetTaskId()) } } else { self.OnSrcPrepareComplete(ctx, guest, nil) diff --git a/pkg/compute/tasks/guest_rebuild_root_task.go b/pkg/compute/tasks/guest_rebuild_root_task.go index 627e52354f..78c2780660 100644 --- a/pkg/compute/tasks/guest_rebuild_root_task.go +++ b/pkg/compute/tasks/guest_rebuild_root_task.go @@ -221,11 +221,12 @@ func (self *ManagedGuestRebuildRootTask) OnInit(ctx context.Context, obj db.ISta diskCat := guest.CategorizeDisks() imageId := diskCat.Root.GetTemplateId() + format := diskCat.Root.DiskFormat storage := diskCat.Root.GetStorage() cache := storage.GetStoragecache() self.SetStage("OnHostCacheImageComplete", nil) - cache.StartImageCacheTask(ctx, self.UserCred, imageId, false, self.GetTaskId()) + cache.StartImageCacheTask(ctx, self.UserCred, imageId, format, false, self.GetTaskId()) } func (self *ManagedGuestRebuildRootTask) OnHostCacheImageComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { diff --git a/pkg/compute/tasks/guest_start_task.go b/pkg/compute/tasks/guest_start_task.go index 899aec02c7..78aae1a97a 100644 --- a/pkg/compute/tasks/guest_start_task.go +++ b/pkg/compute/tasks/guest_start_task.go @@ -32,7 +32,7 @@ func (self *GuestStartTask) checkTemplate(ctx context.Context, guest *models.SGu } else { self.SetStage("OnStartTemplateReady", nil) } - guest.GetDriver().CheckDiskTemplateOnStorage(ctx, self.UserCred, diskCat.Root.GetTemplateId(), diskCat.Root.StorageId, self) + guest.GetDriver().CheckDiskTemplateOnStorage(ctx, self.UserCred, diskCat.Root.GetTemplateId(), diskCat.Root.DiskFormat, diskCat.Root.StorageId, self) } else { self.startStart(ctx, guest) }*/ @@ -42,7 +42,7 @@ func (self *GuestStartTask) checkTemplate(ctx context.Context, guest *models.SGu func (self *GuestStartTask) OnMasterHostTemplateReady(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) { self.SetStage("OnStartTemplateReady", nil) diskCat := guest.CategorizeDisks() - err := guest.GetDriver().CheckDiskTemplateOnStorage(ctx, self.UserCred, diskCat.Root.GetTemplateId(), + err := guest.GetDriver().CheckDiskTemplateOnStorage(ctx, self.UserCred, diskCat.Root.GetTemplateId(), diskCat.Root.DiskFormat, diskCat.Root.BackupStorageId, self) if err != nil { self.SetStageFailed(ctx, err.Error()) diff --git a/pkg/util/qemuimg/qemuimg.go b/pkg/util/qemuimg/qemuimg.go index 36cdff48f0..89419f0f23 100644 --- a/pkg/util/qemuimg/qemuimg.go +++ b/pkg/util/qemuimg/qemuimg.go @@ -135,7 +135,7 @@ func (img *SQemuImage) parse() error { if img.Format == RAW { // test if it is an ISO blkType := fileutils2.GetBlkidType(img.Path) - if blkType == "iso9660" { + if utils.IsInStringArray(blkType, []string{"iso9660", "udf"}) { img.Format = ISO } }