qemu_guest_agent.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 models
  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/httperrors"
  23. "yunion.io/x/onecloud/pkg/mcclient"
  24. "yunion.io/x/onecloud/pkg/util/seclib2"
  25. )
  26. func (self *SGuest) UpdateQgaStatus(status string) error {
  27. _, err := db.Update(self, func() error {
  28. self.QgaStatus = status
  29. return nil
  30. })
  31. if err != nil {
  32. return errors.Wrap(err, "Update QgaStatus")
  33. }
  34. return nil
  35. }
  36. func (self *SGuest) PerformQgaSetPassword(
  37. ctx context.Context,
  38. userCred mcclient.TokenCredential,
  39. query jsonutils.JSONObject,
  40. input *api.ServerQgaSetPasswordInput,
  41. ) (jsonutils.JSONObject, error) {
  42. if self.Status != api.VM_RUNNING {
  43. return nil, httperrors.NewBadRequestError("can't use qga in vm status: %s", self.Status)
  44. }
  45. if input.Username == "" {
  46. return nil, httperrors.NewMissingParameterError("username")
  47. }
  48. if input.Password == "" {
  49. return nil, httperrors.NewMissingParameterError("password")
  50. }
  51. err := seclib2.ValidatePassword(input.Password)
  52. if err != nil {
  53. return nil, err
  54. }
  55. self.SetStatus(ctx, userCred, api.VM_QGA_SET_PASSWORD, "")
  56. params := jsonutils.Marshal(input).(*jsonutils.JSONDict)
  57. task, err := taskman.TaskManager.NewTask(ctx, "GuestQgaSetPasswordTask", self, userCred, params, "", "", nil)
  58. if err != nil {
  59. return nil, err
  60. }
  61. task.ScheduleRun(nil)
  62. return nil, nil
  63. }
  64. func (self *SGuest) PerformQgaPing(
  65. ctx context.Context,
  66. userCred mcclient.TokenCredential,
  67. query jsonutils.JSONObject,
  68. input *api.ServerQgaTimeoutInput,
  69. ) (jsonutils.JSONObject, error) {
  70. if self.PowerStates != api.VM_POWER_STATES_ON {
  71. return nil, httperrors.NewBadRequestError("can't use qga in vm status: %s", self.Status)
  72. }
  73. res := jsonutils.NewDict()
  74. host, err := self.GetHost()
  75. if err != nil {
  76. return nil, err
  77. }
  78. drv, err := self.GetDriver()
  79. if err != nil {
  80. return nil, err
  81. }
  82. err = drv.QgaRequestGuestPing(ctx, mcclient.GetTokenHeaders(userCred), host, self, false, input)
  83. if err != nil {
  84. res.Set("ping_error", jsonutils.NewString(err.Error()))
  85. }
  86. return res, nil
  87. }
  88. func (self *SGuest) PerformQgaCommand(
  89. ctx context.Context,
  90. userCred mcclient.TokenCredential,
  91. query jsonutils.JSONObject,
  92. input *api.ServerQgaCommandInput,
  93. ) (jsonutils.JSONObject, error) {
  94. if self.PowerStates != api.VM_POWER_STATES_ON {
  95. return nil, httperrors.NewBadRequestError("can't use qga in vm status: %s", self.Status)
  96. }
  97. if input.Command == "" {
  98. return nil, httperrors.NewMissingParameterError("command")
  99. }
  100. host, err := self.GetHost()
  101. if err != nil {
  102. return nil, err
  103. }
  104. drv, err := self.GetDriver()
  105. if err != nil {
  106. return nil, err
  107. }
  108. return drv.RequestQgaCommand(ctx, userCred, jsonutils.Marshal(input), host, self)
  109. }
  110. func (self *SGuest) PerformQgaGuestInfoTask(
  111. ctx context.Context,
  112. userCred mcclient.TokenCredential,
  113. query jsonutils.JSONObject,
  114. input *api.ServerQgaGuestInfoTaskInput,
  115. ) (jsonutils.JSONObject, error) {
  116. if self.PowerStates != api.VM_POWER_STATES_ON {
  117. return nil, httperrors.NewBadRequestError("can't use qga in vm status: %s", self.Status)
  118. }
  119. host, err := self.GetHost()
  120. if err != nil {
  121. return nil, err
  122. }
  123. drv, err := self.GetDriver()
  124. if err != nil {
  125. return nil, err
  126. }
  127. return drv.QgaRequestGuestInfoTask(ctx, userCred, nil, host, self)
  128. }
  129. func (self *SGuest) PerformQgaGetNetwork(
  130. ctx context.Context,
  131. userCred mcclient.TokenCredential,
  132. query jsonutils.JSONObject,
  133. input *api.ServerQgaGetNetworkInput,
  134. ) (jsonutils.JSONObject, error) {
  135. if self.PowerStates != api.VM_POWER_STATES_ON {
  136. return nil, httperrors.NewBadRequestError("can't use qga in vm status: %s", self.Status)
  137. }
  138. host, err := self.GetHost()
  139. if err != nil {
  140. return nil, err
  141. }
  142. drv, err := self.GetDriver()
  143. if err != nil {
  144. return nil, err
  145. }
  146. return drv.QgaRequestGetNetwork(ctx, userCred, nil, host, self)
  147. }
  148. func (self *SGuest) startQgaSyncOsInfoTask(ctx context.Context, userCred mcclient.TokenCredential, parentTaskId string) error {
  149. self.SetStatus(ctx, userCred, api.VM_QGA_SYNC_OS_INFO, "")
  150. kwargs := jsonutils.NewDict()
  151. task, err := taskman.TaskManager.NewTask(ctx, "GuestQgaSyncOsInfoTask", self, userCred, kwargs, parentTaskId, "", nil)
  152. if err != nil {
  153. return err
  154. }
  155. task.ScheduleRun(nil)
  156. return nil
  157. }