feat(region,host): create or update server add machine option (#12605)

This commit is contained in:
Zexi Li
2021-11-04 22:26:47 +08:00
committed by GitHub
parent 2196265a68
commit d0cbc99423
9 changed files with 46 additions and 3 deletions

View File

@@ -396,6 +396,10 @@ type ServerCreateInput struct {
// emulate: BIOS, UEFI
Bios string `json:"bios"`
// Machine类型
// emulate: pc, q35
Machine string `json:"machine"`
// 启动顺序
// c: cdrome
// d: disk

View File

@@ -180,6 +180,11 @@ const (
CPU_MODE_HOST = "host"
)
const (
VM_MACHINE_TYPE_PC = "pc"
VM_MACHINE_TYPE_Q35 = "q35"
)
var VM_RUNNING_STATUS = []string{VM_START_START, VM_STARTING, VM_RUNNING, VM_BLOCK_STREAM, VM_BLOCK_STREAM_FAIL}
var VM_CREATING_STATUS = []string{VM_CREATE_NETWORK, VM_CREATE_DISK, VM_START_DEPLOY, VM_DEPLOYING}

View File

@@ -631,7 +631,7 @@ type GuestJsonDesc struct {
Cpu int `json:"cpu"`
Vga string `json:"vga"`
Vdi string `json:"vdi"`
Machine string `json:"machie"`
Machine string `json:"machine"`
Bios string `json:"bios"`
BootOrder string `json:"boot_order"`
SrcIpCheck bool `json:"src_ip_check"`

View File

@@ -411,6 +411,10 @@ func (self *SBaseGuestDriver) RequestLiveMigrate(ctx context.Context, guest *mod
return fmt.Errorf("Not Implement RequestLiveMigrate")
}
func (self *SBaseGuestDriver) ValidateUpdateData(ctx context.Context, userCred mcclient.TokenCredential, input api.ServerUpdateInput) error {
return nil
}
func (self *SBaseGuestDriver) RequestRemoteUpdate(ctx context.Context, guest *models.SGuest, userCred mcclient.TokenCredential, replaceTags bool) error {
// nil ops
return nil

View File

@@ -265,10 +265,31 @@ func (self *SVirtualizedGuestDriver) RequestStopGuestForDelete(ctx context.Conte
return nil
}
func (self *SVirtualizedGuestDriver) ValidateMachineType(machine string) error {
if !utils.IsInStringArray(machine, []string{api.VM_MACHINE_TYPE_PC, api.VM_MACHINE_TYPE_Q35}) {
return httperrors.NewBadRequestError("Invalid machine %q", machine)
}
return nil
}
func (self *SVirtualizedGuestDriver) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, input *api.ServerCreateInput) (*api.ServerCreateInput, error) {
if input.Machine != "" {
if err := self.ValidateMachineType(input.Machine); err != nil {
return nil, err
}
}
return input, nil
}
func (self *SVirtualizedGuestDriver) ValidateUpdateData(ctx context.Context, userCred mcclient.TokenCredential, input api.ServerUpdateInput) error {
if input.Machine != nil {
if err := self.ValidateMachineType(*input.Machine); err != nil {
return err
}
}
return nil
}
func (self *SVirtualizedGuestDriver) ValidateCreateDataOnHost(ctx context.Context, userCred mcclient.TokenCredential, bmName string, host *models.SHost, input *api.ServerCreateInput) (*api.ServerCreateInput, error) {
if host.HostStatus != api.HOST_ONLINE {
return nil, httperrors.NewInvalidStatusError("Host %s is not online", bmName)

View File

@@ -205,6 +205,7 @@ type IGuestDriver interface {
RequestMigrate(ctx context.Context, guest *SGuest, userCred mcclient.TokenCredential, data *jsonutils.JSONDict, task taskman.ITask) error
RequestLiveMigrate(ctx context.Context, guest *SGuest, userCred mcclient.TokenCredential, data *jsonutils.JSONDict, task taskman.ITask) error
ValidateUpdateData(ctx context.Context, userCred mcclient.TokenCredential, input api.ServerUpdateInput) error
RequestRemoteUpdate(ctx context.Context, guest *SGuest, userCred mcclient.TokenCredential, replaceTags bool) error
RequestOpenForward(ctx context.Context, userCred mcclient.TokenCredential, guest *SGuest, req *guestdriver_types.OpenForwardRequest) (*guestdriver_types.OpenForwardResponse, error)

View File

@@ -1017,6 +1017,10 @@ func (self *SGuest) ValidateUpdateData(ctx context.Context, userCred mcclient.To
}
}
if err := self.GetDriver().ValidateUpdateData(ctx, userCred, input); err != nil {
return input, err
}
var err error
input.VirtualResourceBaseUpdateInput, err = self.SVirtualResourceBase.ValidateUpdateData(ctx, userCred, query, input.VirtualResourceBaseUpdateInput)
if err != nil {

View File

@@ -29,6 +29,7 @@ import (
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/utils"
api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/hostman/options"
"yunion.io/x/onecloud/pkg/hostman/storageman"
"yunion.io/x/onecloud/pkg/util/fileutils2"
@@ -120,7 +121,7 @@ func (s *SKVMGuestInstance) isWindows10() bool {
func (s *SKVMGuestInstance) getMachine() string {
machine, err := s.Desc.GetString("machine")
if err != nil {
machine = "pc"
machine = api.VM_MACHINE_TYPE_PC
}
return machine
}
@@ -134,7 +135,7 @@ func (s *SKVMGuestInstance) getBios() string {
}
func (s *SKVMGuestInstance) isQ35() bool {
return s.getMachine() == "q35"
return s.getMachine() == api.VM_MACHINE_TYPE_Q35
}
func (s *SKVMGuestInstance) GetVdiProtocol() string {

View File

@@ -365,6 +365,7 @@ type ServerCreateOptionalOptions struct {
Vga string `help:"VGA driver" choices:"std|vmware|cirrus|qxl"`
Vdi string `help:"VDI protocool" choices:"vnc|spice"`
Bios string `help:"BIOS" choices:"BIOS|UEFI"`
Machine string `help:"Machine type" choices:"pc|q35"`
Desc string `help:"Description" metavar:"<DESCRIPTION>" json:"description"`
Boot string `help:"Boot device" metavar:"<BOOT_DEVICE>" choices:"disk|cdrom" json:"-"`
EnableCloudInit bool `help:"Enable cloud-init service"`
@@ -468,6 +469,7 @@ func (opts *ServerCreateOptionalOptions) OptionalParams() (*computeapi.ServerCre
Vga: opts.Vga,
Vdi: opts.Vdi,
Bios: opts.Bios,
Machine: opts.Machine,
ShutdownBehavior: opts.ShutdownBehavior,
AutoStart: opts.AutoStart,
Duration: opts.Duration,
@@ -583,6 +585,7 @@ type ServerUpdateOptions struct {
Boot string `help:"Boot device" choices:"disk|cdrom"`
Delete string `help:"Lock server to prevent from deleting" choices:"enable|disable" json:"-"`
ShutdownBehavior string `help:"Behavior after VM server shutdown" choices:"stop|terminate"`
Machine string `help:"Machine type" choices:"q35|pc"`
}
func (opts *ServerUpdateOptions) Params() (jsonutils.JSONObject, error) {