mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-19 10:46:58 +08:00
Automatic merge from release/2.4.0 -> release/2.5.0
* commit 'c09fd43c7d202f0ee2d48ef8743e6ca1708f62ec': add iso insert eject
This commit is contained in:
@@ -1227,8 +1227,8 @@ func fillDiskConfigByImage(ctx context.Context, userCred mcclient.TokenCredentia
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseIsoInfo(ctx context.Context, userCred mcclient.TokenCredential, info string) (*SImage, error) {
|
||||
image, err := CachedimageManager.getImageInfo(ctx, userCred, info, false)
|
||||
func parseIsoInfo(ctx context.Context, userCred mcclient.TokenCredential, imageId string) (*SImage, error) {
|
||||
image, err := CachedimageManager.getImageInfo(ctx, userCred, imageId, false)
|
||||
if err != nil {
|
||||
log.Errorf("getImageInfo fail %s", err)
|
||||
return nil, err
|
||||
|
||||
@@ -587,6 +587,66 @@ func (self *SGuest) GetDetailsIso(userCred mcclient.TokenCredential) jsonutils.J
|
||||
return desc
|
||||
}
|
||||
|
||||
func (self *SGuest) AllowPerformInsertiso(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
|
||||
return self.IsOwner(userCred)
|
||||
}
|
||||
|
||||
func (self *SGuest) PerformInsertiso(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
cdrom := self.getCdrom()
|
||||
if cdrom == nil {
|
||||
return nil, fmt.Errorf("Failed to get cdrom")
|
||||
}
|
||||
|
||||
if len(cdrom.ImageId) > 0 {
|
||||
return nil, httperrors.NewBadRequestError("CD-ROM not empty, please eject first")
|
||||
}
|
||||
imageId, _ := data.GetString("image_id")
|
||||
image, err := parseIsoInfo(ctx, userCred, imageId)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if utils.IsInStringArray(self.Status, []string{VM_RUNNING, VM_READY}) {
|
||||
err = self.StartInsertIsoTask(ctx, image.Id, self.HostId, userCred, "")
|
||||
return nil, err
|
||||
} else {
|
||||
return nil, httperrors.NewServerStatusError("Insert ISO not allowed in status %s", self.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SGuest) AllowPerformEjectiso(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
|
||||
return self.IsOwner(userCred)
|
||||
}
|
||||
|
||||
func (self *SGuest) PerformEjectiso(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
if self.Hypervisor != HYPERVISOR_KVM {
|
||||
return nil, httperrors.NewNotAcceptableError("Not allow for hypervisor %s", self.Hypervisor)
|
||||
}
|
||||
cdrom := self.getCdrom()
|
||||
if cdrom == nil {
|
||||
return nil, fmt.Errorf("Failed to get cdrom")
|
||||
}
|
||||
if len(cdrom.ImageId) == 0 {
|
||||
return nil, httperrors.NewBadRequestError("No ISO to eject")
|
||||
}
|
||||
if utils.IsInStringArray(self.Status, []string{VM_RUNNING, VM_READY}) {
|
||||
err := self.StartEjectisoTask(ctx, userCred, "")
|
||||
return nil, err
|
||||
} else {
|
||||
return nil, httperrors.NewServerStatusError("Eject ISO not allowed in status %s", self.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SGuest) StartEjectisoTask(ctx context.Context, userCred mcclient.TokenCredential, parentTaskId string) error {
|
||||
task, err := taskman.TaskManager.NewTask(ctx, "GuestEjectISOTask", self, userCred, nil, parentTaskId, "", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
task.ScheduleRun(nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SGuest) StartInsertIsoTask(ctx context.Context, imageId string, hostId string, userCred mcclient.TokenCredential, parentTaskId string) error {
|
||||
self.insertIso(imageId)
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
)
|
||||
|
||||
type GuestEjectISOTask struct {
|
||||
SGuestBaseTask
|
||||
}
|
||||
|
||||
func init() {
|
||||
taskman.RegisterTask(GuestEjectISOTask{})
|
||||
}
|
||||
|
||||
func (self *GuestEjectISOTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
|
||||
self.startEjectIso(ctx, obj)
|
||||
}
|
||||
|
||||
func (self *GuestEjectISOTask) startEjectIso(ctx context.Context, obj db.IStandaloneModel) {
|
||||
guest := obj.(*models.SGuest)
|
||||
if guest.EjectIso(self.UserCred) && guest.Status == models.VM_RUNNING {
|
||||
self.SetStage("OnConfigSyncComplete", nil)
|
||||
guest.StartSyncTask(ctx, self.UserCred, false, self.GetId())
|
||||
} else {
|
||||
self.SetStageComplete(ctx, nil)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *GuestEjectISOTask) OnConfigSyncComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
|
||||
self.SetStageComplete(ctx, nil)
|
||||
}
|
||||
Reference in New Issue
Block a user