modelarts_pool_create_task.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package modelarts
  15. import (
  16. "context"
  17. "strings"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  21. api "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  25. "yunion.io/x/onecloud/pkg/compute/models"
  26. "yunion.io/x/onecloud/pkg/util/logclient"
  27. )
  28. type ModelartsPoolCreateTask struct {
  29. taskman.STask
  30. }
  31. func init() {
  32. taskman.RegisterTask(ModelartsPoolCreateTask{})
  33. }
  34. func (modelartsCreateTask *ModelartsPoolCreateTask) taskFailed(ctx context.Context, pool *models.SModelartsPool, status string, err error) {
  35. pool.SetStatus(ctx, modelartsCreateTask.UserCred, status, err.Error())
  36. db.OpsLog.LogEvent(pool, db.ACT_ALLOCATE, err, modelartsCreateTask.UserCred)
  37. logclient.AddActionLogWithStartable(modelartsCreateTask, pool, logclient.ACT_ALLOCATE, err, modelartsCreateTask.UserCred, false)
  38. modelartsCreateTask.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  39. }
  40. func (modelartsCreateTask *ModelartsPoolCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  41. pool := obj.(*models.SModelartsPool)
  42. opts := &cloudprovider.ModelartsPoolCreateOption{
  43. Name: pool.Name,
  44. InstanceType: pool.InstanceType,
  45. WorkType: pool.WorkType,
  46. NodeCount: pool.NodeCount,
  47. Cidr: pool.Cidr,
  48. }
  49. modelartsCreateTask.SetStage("OnModelartsPoolCreateComplete", nil)
  50. // withDelay
  51. modelartsCreateTask.WaitStatus(ctx, opts, pool)
  52. }
  53. func (modelartsCreateTask *ModelartsPoolCreateTask) taskComplete(ctx context.Context, pool *models.SModelartsPool) {
  54. pool.SetStatus(ctx, modelartsCreateTask.GetUserCred(), api.MODELARTS_POOL_STATUS_RUNNING, "")
  55. notifyclient.EventNotify(ctx, modelartsCreateTask.UserCred, notifyclient.SEventNotifyParam{
  56. Obj: modelartsCreateTask,
  57. Action: notifyclient.ActionCreate,
  58. })
  59. }
  60. func (modelartsCreateTask *ModelartsPoolCreateTask) WaitStatus(ctx context.Context, opts *cloudprovider.ModelartsPoolCreateOption, pool *models.SModelartsPool) error {
  61. taskman.LocalTaskRun(modelartsCreateTask, func() (jsonutils.JSONObject, error) {
  62. iRegion, err := pool.GetIRegion()
  63. if err != nil {
  64. return nil, errors.Wrapf(err, "pool.GetIRegion")
  65. }
  66. callback := func(id string) {
  67. db.SetExternalId(pool, modelartsCreateTask.GetUserCred(), id)
  68. }
  69. ipool, err := iRegion.CreateIModelartsPool(opts, callback)
  70. if err != nil {
  71. return nil, errors.Wrapf(err, "iRegion.CreateIModelartsPool")
  72. }
  73. switch ipool.GetStatus() {
  74. case api.MODELARTS_POOL_STATUS_RUNNING:
  75. return nil, nil
  76. case api.MODELARTS_POOL_STATUS_CREATE_FAILED:
  77. return nil, errors.Errorf("%s", ipool.GetStatusMessage())
  78. default:
  79. return nil, errors.Errorf("%s", ipool.GetStatus())
  80. }
  81. })
  82. return nil
  83. }
  84. func (modelartsCreateTask *ModelartsPoolCreateTask) OnModelartsPoolCreateCompleteFailed(ctx context.Context, modelarts *models.SModelartsPool, err jsonutils.JSONObject) {
  85. if strings.Contains(err.String(), errors.ErrTimeout.Error()) {
  86. modelartsCreateTask.taskFailed(ctx, modelarts, api.MODELARTS_POOL_STATUS_TIMEOUT, errors.Errorf("%s", err.String()))
  87. } else {
  88. modelartsCreateTask.taskFailed(ctx, modelarts, api.MODELARTS_POOL_STATUS_CREATE_FAILED, errors.Errorf("%s", err.String()))
  89. }
  90. }
  91. func (modelartsCreateTask *ModelartsPoolCreateTask) OnModelartsPoolCreateComplete(ctx context.Context, modelarts *models.SModelartsPool, body jsonutils.JSONObject) {
  92. modelartsCreateTask.taskComplete(ctx, modelarts)
  93. }