guest_remote_update_task.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. "yunion.io/x/pkg/errors"
  19. api "yunion.io/x/onecloud/pkg/apis/compute"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  22. "yunion.io/x/onecloud/pkg/compute/models"
  23. )
  24. type GuestRemoteUpdateTask struct {
  25. SGuestBaseTask
  26. }
  27. func init() {
  28. taskman.RegisterTask(GuestRemoteUpdateTask{})
  29. }
  30. func (self *GuestRemoteUpdateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  31. guest := obj.(*models.SGuest)
  32. self.SetStage("OnRemoteUpdateComplete", nil)
  33. replaceTags := jsonutils.QueryBoolean(self.Params, "replace_tags", false)
  34. taskman.LocalTaskRun(self, func() (jsonutils.JSONObject, error) {
  35. drv, err := guest.GetDriver()
  36. if err != nil {
  37. return nil, err
  38. }
  39. err = drv.RequestRemoteUpdate(ctx, guest, self.UserCred, replaceTags)
  40. if err != nil {
  41. return nil, errors.Wrap(err, "RequestRemoteUpdate")
  42. }
  43. return nil, nil
  44. })
  45. }
  46. func (self *GuestRemoteUpdateTask) OnRemoteUpdateComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  47. self.SetStage("OnSyncStatusComplete", nil)
  48. guest.StartSyncstatus(ctx, self.UserCred, self.GetTaskId())
  49. }
  50. func (self *GuestRemoteUpdateTask) OnRemoteUpdateCompleteFailed(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  51. guest.SetStatus(ctx, self.UserCred, api.VM_UPDATE_TAGS_FAILED, data.String())
  52. self.SetStageFailed(ctx, data)
  53. }
  54. func (self *GuestRemoteUpdateTask) OnSyncStatusComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  55. self.SetStageComplete(ctx, nil)
  56. }
  57. func (self *GuestRemoteUpdateTask) OnSyncStatusCompleteFailed(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  58. self.SetStageFailed(ctx, data)
  59. }