Merge pull request #15122 from ioito/hotfix/qx-modelarts-change-config

fix(region): modelarts change config
This commit is contained in:
Zexi Li
2022-10-08 14:45:47 +08:00
committed by GitHub
10 changed files with 166 additions and 10 deletions
@@ -27,4 +27,5 @@ func init() {
cmd.Delete(&options.BaseIdOptions{})
cmd.Create(&compute.ModelartsPoolCreateOption{})
cmd.Perform("syncstatus", &compute.ModelartsPoolSyncstatusOption{})
cmd.Perform("change-config", &compute.ModelartsPoolChangeConfigOption{})
}
+11 -6
View File
@@ -19,12 +19,13 @@ import (
)
const (
MODELARTS_POOL_STATUS_RUNNING = "running"
MODELARTS_POOL_STATUS_ABNORMAL = "abnormal"
MODELARTS_POOL_STATUS_CREATING = "creating"
MODELARTS_POOL_STATUS_DELETING = "deleting"
MODELARTS_POOL_STATUS_ERROR = "error"
MODELARTS_POOL_STATUS_UNKNOWN = "unknown"
MODELARTS_POOL_STATUS_RUNNING = "running"
MODELARTS_POOL_STATUS_ABNORMAL = "abnormal"
MODELARTS_POOL_STATUS_CREATING = "creating"
MODELARTS_POOL_STATUS_DELETING = "deleting"
MODELARTS_POOL_STATUS_CHANGE_CONFIG = "change_config"
MODELARTS_POOL_STATUS_ERROR = "error"
MODELARTS_POOL_STATUS_UNKNOWN = "unknown"
)
type ModelartsPoolCreateInput struct {
@@ -80,3 +81,7 @@ type ModelartsPoolListInput struct {
type ModelartsPoolSyncstatusInput struct {
}
type ModelartsPoolChangeConfigInput struct {
NodeCount int
}
+1
View File
@@ -944,6 +944,7 @@ type ICloudModelartsPool interface {
GetInstanceType() string
GetWorkType() string
GetNodeCount() int
ChangeConfig(opts *ModelartsPoolChangeConfigOptions) error
}
type ICloudModelartsPoolSku interface {
+4
View File
@@ -31,3 +31,7 @@ type Azs struct {
Az string `json:"az"`
Count int `json:"count"`
}
type ModelartsPoolChangeConfigOptions struct {
NodeCount int
}
+17
View File
@@ -365,6 +365,23 @@ func (self *SModelartsPool) StartDeleteTask(ctx context.Context, userCred mcclie
return nil
}
func (self *SModelartsPool) PerformChangeConfig(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.ModelartsPoolChangeConfigInput) (jsonutils.JSONObject, error) {
if input.NodeCount == self.NodeCount {
return nil, nil
}
return nil, self.StartChangeConfigTask(ctx, userCred, input)
}
func (self *SModelartsPool) StartChangeConfigTask(ctx context.Context, userCred mcclient.TokenCredential, input api.ModelartsPoolChangeConfigInput) error {
params := jsonutils.Marshal(input).(*jsonutils.JSONDict)
task, err := taskman.TaskManager.NewTask(ctx, "ModelartsPoolChangeConfigTask", self, userCred, params, "", "", nil)
if err != nil {
return err
}
self.SetStatus(userCred, api.MODELARTS_POOL_STATUS_CHANGE_CONFIG, "")
return task.ScheduleRun(nil)
}
func (self *SModelartsPool) GetIRegion() (cloudprovider.ICloudRegion, error) {
provider, err := self.GetDriver(context.Background())
if err != nil {
@@ -0,0 +1,79 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tasks
import (
"context"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
"yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
"yunion.io/x/onecloud/pkg/cloudprovider"
"yunion.io/x/onecloud/pkg/compute/models"
"yunion.io/x/onecloud/pkg/util/logclient"
)
type ModelartsPoolChangeConfigTask struct {
taskman.STask
}
func init() {
taskman.RegisterTask(ModelartsPoolChangeConfigTask{})
}
func (self *ModelartsPoolChangeConfigTask) taskFailed(ctx context.Context, mp *models.SModelartsPool, err error) {
mp.SetStatus(self.UserCred, api.MODELARTS_POOL_STATUS_UNKNOWN, err.Error())
db.OpsLog.LogEvent(mp, db.ACT_CHANGE_CONFIG, err, self.UserCred)
logclient.AddActionLogWithStartable(self, mp, logclient.ACT_CHANGE_CONFIG, err, self.UserCred, false)
self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
}
func (self *ModelartsPoolChangeConfigTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
pool := obj.(*models.SModelartsPool)
iMp, err := pool.GetIModelartsPool()
if err != nil {
self.taskFailed(ctx, pool, errors.Wrapf(err, "iMp.GetIModelartsPoolById"))
return
}
input := &api.ModelartsPoolChangeConfigInput{}
self.GetParams().Unmarshal(input)
opts := &cloudprovider.ModelartsPoolChangeConfigOptions{}
opts.NodeCount = input.NodeCount
err = iMp.ChangeConfig(opts)
if err != nil {
self.taskFailed(ctx, pool, errors.Wrapf(err, "iMp.ChangeConfig"))
return
}
err = cloudprovider.WaitStatusWithDelay(iMp, api.MODELARTS_POOL_STATUS_RUNNING, 30*time.Second, 15*time.Second, 30*time.Minute)
if err != nil {
self.taskFailed(ctx, pool, errors.Wrapf(err, "WaitStatusWithDelay"))
return
}
pool.SyncWithCloudModelartsPool(ctx, self.GetUserCred(), iMp)
self.taskComplete(ctx, pool)
}
func (self *ModelartsPoolChangeConfigTask) taskComplete(ctx context.Context, pool *models.SModelartsPool) {
notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
Obj: self,
Action: notifyclient.ActionChangeConfig,
})
self.SetStageComplete(ctx, nil)
}
@@ -21,7 +21,7 @@ import (
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
api "yunion.io/x/onecloud/pkg/apis/compute"
apis "yunion.io/x/onecloud/pkg/apis"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
"yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
@@ -39,7 +39,7 @@ func init() {
}
func (self *ModelartsPoolDeleteTask) taskFailed(ctx context.Context, mp *models.SModelartsPool, err error) {
mp.SetStatus(self.UserCred, api.NAS_STATUS_DELETE_FAILED, err.Error())
mp.SetStatus(self.UserCred, apis.STATUS_DELETE_FAILED, err.Error())
db.OpsLog.LogEvent(mp, db.ACT_DELETE_FAIL, err, self.UserCred)
logclient.AddActionLogWithStartable(self, mp, logclient.ACT_DELOCATE, err, self.UserCred, false)
self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
+15 -2
View File
@@ -65,7 +65,7 @@ func (opts *ModelartsPoolUpdateOption) GetId() string {
}
func (opts *ModelartsPoolUpdateOption) Params() (jsonutils.JSONObject, error) {
return options.ListStructToParams(opts)
return options.StructToParams(opts)
}
type ModelartsPoolSyncstatusOption struct {
@@ -77,5 +77,18 @@ func (opts *ModelartsPoolSyncstatusOption) GetId() string {
}
func (opts *ModelartsPoolSyncstatusOption) Params() (jsonutils.JSONObject, error) {
return options.ListStructToParams(opts)
return options.StructToParams(opts)
}
type ModelartsPoolChangeConfigOption struct {
ID string `help:"Id"`
NodeCount int
}
func (opts *ModelartsPoolChangeConfigOption) GetId() string {
return opts.ID
}
func (opts *ModelartsPoolChangeConfigOption) Params() (jsonutils.JSONObject, error) {
return options.StructToParams(opts)
}
+18
View File
@@ -374,3 +374,21 @@ func (self *SModelartsPool) GetNodeCount() int {
}
return self.Spec.Resource[0].Count
}
func (self *SModelartsPool) ChangeConfig(opts *cloudprovider.ModelartsPoolChangeConfigOptions) error {
//{"spec":{"resources":[{"flavor":"modelarts.kat1.8xlarge","count":2}]}}
res := []map[string]interface{}{}
for _, re := range self.Spec.Resource {
res = append(res, map[string]interface{}{
"flavor": re.Flavor,
"count": opts.NodeCount,
})
}
params := map[string]interface{}{
"spec": map[string]interface{}{
"resources": res,
},
}
_, err := self.region.client.modelartsPoolUpdate(self.Metadata.Name, params)
return err
}
+18
View File
@@ -373,3 +373,21 @@ func (self *SModelartsPool) GetNodeCount() int {
}
return self.Spec.Resource[0].Count
}
func (self *SModelartsPool) ChangeConfig(opts *cloudprovider.ModelartsPoolChangeConfigOptions) error {
//{"spec":{"resources":[{"flavor":"modelarts.kat1.8xlarge","count":2}]}}
res := []map[string]interface{}{}
for _, re := range self.Spec.Resource {
res = append(res, map[string]interface{}{
"flavor": re.Flavor,
"count": opts.NodeCount,
})
}
params := map[string]interface{}{
"spec": map[string]interface{}{
"resources": res,
},
}
_, err := self.region.client.modelartsPoolUpdate(self.Metadata.Name, params)
return err
}