guest_sync_isolated_device_task.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/compute/tasks/utils"
  23. "yunion.io/x/onecloud/pkg/util/logclient"
  24. )
  25. type GuestIsolatedDeviceSyncTask struct {
  26. SGuestBaseTask
  27. }
  28. func init() {
  29. taskman.RegisterTask(GuestIsolatedDeviceSyncTask{})
  30. }
  31. func (self *GuestIsolatedDeviceSyncTask) needStart() bool {
  32. return jsonutils.QueryBoolean(self.Params, "auto_start", false)
  33. }
  34. func (self *GuestIsolatedDeviceSyncTask) onTaskFail(ctx context.Context, guest *models.SGuest, err jsonutils.JSONObject) {
  35. self.SetStageFailed(ctx, err)
  36. guest.SetStatus(ctx, self.GetUserCred(), api.VM_SYNC_ISOLATED_DEVICE_FAILED, err.String())
  37. logclient.AddActionLogWithStartable(self, guest, logclient.ACT_VM_SYNC_ISOLATED_DEVICE, err, self.GetUserCred(), false)
  38. }
  39. func (self *GuestIsolatedDeviceSyncTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  40. guest := obj.(*models.SGuest)
  41. self.SetStage("OnSyncConfigComplete", nil)
  42. drv, err := guest.GetDriver()
  43. if err != nil {
  44. self.onTaskFail(ctx, guest, utils.JsonErrorObj(err))
  45. return
  46. }
  47. err = drv.RequestSyncIsolatedDevice(ctx, guest, self)
  48. if err != nil {
  49. self.onTaskFail(ctx, guest, utils.JsonErrorObj(err))
  50. return
  51. }
  52. }
  53. func (self *GuestIsolatedDeviceSyncTask) OnSyncConfigComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  54. if self.needStart() {
  55. self.SetStage("OnStartComplete", nil)
  56. guest.StartGueststartTask(ctx, self.GetUserCred(), nil, self.GetId())
  57. } else {
  58. self.OnStartComplete(ctx, guest, data)
  59. }
  60. }
  61. func (self *GuestIsolatedDeviceSyncTask) OnSyncConfigCompleteFailed(ctx context.Context, guest *models.SGuest, reason jsonutils.JSONObject) {
  62. self.onTaskFail(ctx, guest, reason)
  63. }
  64. func (self *GuestIsolatedDeviceSyncTask) OnStartComplete(ctx context.Context, obj *models.SGuest, data jsonutils.JSONObject) {
  65. logclient.AddActionLogWithStartable(self, obj, logclient.ACT_VM_SYNC_ISOLATED_DEVICE, nil, self.GetUserCred(), true)
  66. self.SetStageComplete(ctx, nil)
  67. }
  68. func (self *GuestIsolatedDeviceSyncTask) OnStartCompleteFailed(ctx context.Context, obj *models.SGuest, data jsonutils.JSONObject) {
  69. self.onTaskFail(ctx, obj, data)
  70. }