guest_eject_iso_task.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 GuestEjectISOTask struct {
  25. SGuestBaseTask
  26. }
  27. func init() {
  28. taskman.RegisterTask(GuestEjectISOTask{})
  29. }
  30. func (self *GuestEjectISOTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  31. self.startEjectIso(ctx, obj)
  32. }
  33. func (self *GuestEjectISOTask) startEjectIso(ctx context.Context, obj db.IStandaloneModel) {
  34. guest := obj.(*models.SGuest)
  35. cdromOrdinal, _ := self.Params.Int("cdrom_ordinal")
  36. if guest.EjectIso(cdromOrdinal, self.UserCred) && guest.Status == api.VM_RUNNING {
  37. self.SetStage("OnConfigSyncComplete", nil)
  38. drv, err := guest.GetDriver()
  39. if err != nil {
  40. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  41. return
  42. }
  43. drv.RequestGuestHotRemoveIso(ctx, guest, self)
  44. } else {
  45. self.SetStageComplete(ctx, nil)
  46. }
  47. }
  48. func (self *GuestEjectISOTask) OnConfigSyncComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  49. logclient.AddActionLogWithContext(ctx, obj, logclient.ACT_ISO_DETACH, nil, self.UserCred, true)
  50. self.SetStageComplete(ctx, nil)
  51. }
  52. func (self *GuestEjectISOTask) OnConfigSyncCompleteFailed(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  53. logclient.AddActionLogWithContext(ctx, obj, logclient.ACT_ISO_DETACH, nil, self.UserCred, false)
  54. self.SetStageFailed(ctx, nil)
  55. }