guest_cpuset_task.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 guest
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. api "yunion.io/x/onecloud/pkg/apis/compute"
  19. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  21. "yunion.io/x/onecloud/pkg/compute/models"
  22. "yunion.io/x/onecloud/pkg/util/logclient"
  23. )
  24. type GuestCPUSetTask struct {
  25. SGuestBaseTask
  26. }
  27. func init() {
  28. taskman.RegisterTask(GuestCPUSetTask{})
  29. }
  30. func (self *GuestCPUSetTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  31. guest := obj.(*models.SGuest)
  32. self.SetStage("OnSyncComplete", nil)
  33. db.OpsLog.LogEvent(guest, db.ACT_GUEST_CPUSET, self.GetParams(), self.UserCred)
  34. if err := guest.StartSyncTask(ctx, self.GetUserCred(), true, self.GetTaskId()); err != nil {
  35. self.setStageFailed(ctx, guest, data)
  36. }
  37. }
  38. func (self *GuestCPUSetTask) setStageFailed(ctx context.Context, obj *models.SGuest, data jsonutils.JSONObject) {
  39. logclient.AddActionLogWithStartable(self, obj, logclient.ACT_VM_CPUSET, data, self.UserCred, false)
  40. db.OpsLog.LogEvent(obj, db.ACT_GUEST_CPUSET_FAIL, data, self.UserCred)
  41. self.SetStageFailed(ctx, data)
  42. }
  43. func (self *GuestCPUSetTask) OnSyncComplete(ctx context.Context, obj *models.SGuest, data jsonutils.JSONObject) {
  44. host, _ := obj.GetHost()
  45. input := new(api.ServerCPUSetInput)
  46. self.GetParams().Unmarshal(input)
  47. drv, err := obj.GetDriver()
  48. if err != nil {
  49. self.setStageFailed(ctx, obj, jsonutils.NewString(err.Error()))
  50. return
  51. }
  52. _, err = drv.RequestCPUSet(ctx, self.GetUserCred(), host, obj, input)
  53. if err != nil {
  54. self.setStageFailed(ctx, obj, jsonutils.NewString(err.Error()))
  55. return
  56. }
  57. logclient.AddActionLogWithStartable(self, obj, logclient.ACT_VM_CPUSET, input, self.UserCred, true)
  58. self.SetStageComplete(ctx, nil)
  59. }
  60. func (self *GuestCPUSetTask) OnSyncCompleteFailed(ctx context.Context, obj *models.SGuest, data jsonutils.JSONObject) {
  61. self.setStageFailed(ctx, obj, data)
  62. }