fix(region): support shutdown mode (#17137)

This commit is contained in:
屈轩
2023-05-24 06:24:49 +08:00
committed by GitHub
parent 5032c219be
commit 6f4bdbbfa8
9 changed files with 50 additions and 1 deletions

View File

@@ -208,6 +208,11 @@ const (
VM_POWER_STATES_UNKNOWN = "unknown"
)
const (
VM_SHUTDOWN_MODE_KEEP_CHARGING = "keep_charging"
VM_SHUTDOWN_MODE_STOP_CHARGING = "stop_charging"
)
const (
QGA_STATUS_UNKNOWN = "unknown"
QGA_STATUS_EXCUTING = "executing"

View File

@@ -137,6 +137,7 @@ type ServerRebuildRootInput struct {
// swagger: ignore
Image string `json:"image" yunion-deprecated-by:"image_id"`
// 关机且停机不收费情况下不允许重装系统
// 镜像 id
// required: true
ImageId string `json:"image_id"`
@@ -726,6 +727,7 @@ type ServerDetachDiskInput struct {
}
type ServerChangeConfigInput struct {
// 关机且停机不收费情况下不允许调整配置
// 实例类型, 优先级高于vcpu_count和vmem_size
InstanceType string `json:"instance_type"`
// swagger: ignore

View File

@@ -216,6 +216,10 @@ func (self *SAliyunGuestDriver) IsSupportedBillingCycle(bc billing.SBillingCycle
return false
}
func (self *SAliyunGuestDriver) IsSupportShutdownMode() bool {
return true
}
func (self *SAliyunGuestDriver) IsSupportPublicipToEip() bool {
return true
}

View File

@@ -301,6 +301,10 @@ func (self *SBaseGuestDriver) IsSupportPostpaidExpire() bool {
return true
}
func (self *SBaseGuestDriver) IsSupportShutdownMode() bool {
return false
}
func (self *SBaseGuestDriver) RequestRenewInstance(ctx context.Context, guest *models.SGuest, bc billing.SBillingCycle) (time.Time, error) {
return time.Time{}, nil
}

View File

@@ -368,6 +368,10 @@ func (self *SKVMGuestDriver) IsSupportEip() bool {
return true
}
func (self *SKVMGuestDriver) IsSupportShutdownMode() bool {
return true
}
func (self *SKVMGuestDriver) ValidateCreateEip(ctx context.Context, userCred mcclient.TokenCredential, input api.ServerCreateEipInput) error {
return nil
}

View File

@@ -233,6 +233,10 @@ func (self *SQcloudGuestDriver) IsSupportdDcryptPasswordFromSecretKey() bool {
return false
}
func (self *SQcloudGuestDriver) IsSupportShutdownMode() bool {
return true
}
func (self *SQcloudGuestDriver) GetUserDataType() string {
return cloudprovider.CLOUD_SHELL
}

View File

@@ -1177,7 +1177,19 @@ func (self *SGuest) StartGuestStopTask(ctx context.Context, userCred mcclient.To
if len(parentTaskId) > 0 {
params.Add(jsonutils.JSONTrue, "subtask")
}
return self.GetDriver().StartGuestStopTask(self, ctx, userCred, params, parentTaskId)
driver := self.GetDriver()
shutdownMode := api.VM_SHUTDOWN_MODE_KEEP_CHARGING
if stopCharging && driver.IsSupportShutdownMode() {
shutdownMode = api.VM_SHUTDOWN_MODE_STOP_CHARGING
}
_, err := db.Update(self, func() error {
self.ShutdownMode = shutdownMode
return nil
})
if err != nil {
return errors.Wrapf(err, "db.Update")
}
return driver.StartGuestStopTask(self, ctx, userCred, params, parentTaskId)
}
func (self *SGuest) insertIso(imageId string, cdromOrdinal int64) bool {
@@ -1618,6 +1630,10 @@ func (self *SGuest) PerformRebuildRoot(ctx context.Context, userCred mcclient.To
return nil, httperrors.NewInvalidStatusError("Cannot reset root in status %s", self.Status)
}
if self.Status == api.VM_READY && self.ShutdownMode == api.VM_SHUTDOWN_MODE_STOP_CHARGING {
return nil, httperrors.NewInvalidStatusError("Cannot reset root with %s", self.ShutdownMode)
}
autoStart := false
if input.AutoStart != nil {
autoStart = *input.AutoStart
@@ -2556,6 +2572,10 @@ func (self *SGuest) PerformChangeConfig(ctx context.Context, userCred mcclient.T
return nil, httperrors.NewInvalidStatusError("Cannot change config in %s for %s, requires %s", self.Status, self.GetHypervisor(), changeStatus)
}
if self.Status == api.VM_READY && self.ShutdownMode == api.VM_SHUTDOWN_MODE_STOP_CHARGING {
return nil, httperrors.NewInvalidStatusError("Cannot change config with %s", self.ShutdownMode)
}
_, err = self.GetHost()
if err != nil {
return nil, errors.Wrapf(err, "GetHost")

View File

@@ -55,6 +55,7 @@ type IGuestDriver interface {
IsSupportedBillingCycle(bc billing.SBillingCycle) bool
IsSupportPostpaidExpire() bool
IsSupportShutdownMode() bool
RequestRenewInstance(ctx context.Context, guest *SGuest, bc billing.SBillingCycle) (time.Time, error)

View File

@@ -133,6 +133,10 @@ type SGuest struct {
// example: stop
ShutdownBehavior string `width:"16" charset:"ascii" default:"stop" list:"user" update:"user" create:"optional"`
// 关机收费模式
// example: keep_charging, stop_charging
ShutdownMode string `width:"16" charset:"ascii" default:"keep_charging" list:"user"`
// 秘钥对Id
KeypairId string `width:"36" charset:"ascii" nullable:"true" list:"user" create:"optional"`
@@ -5119,6 +5123,7 @@ func (self *SGuest) GetShortDesc(ctx context.Context) *jsonutils.JSONDict {
desc.Set("cpu", jsonutils.NewInt(int64(self.VcpuCount)))
desc.Set("status", jsonutils.NewString(self.Status))
desc.Set("shutdown_mode", jsonutils.NewString(self.ShutdownMode))
address := jsonutils.NewString(strings.Join(self.GetRealIPs(), ","))
desc.Set("ip_addr", address)