baremetal_server_start_task.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. "fmt"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  23. "yunion.io/x/onecloud/pkg/compute/models"
  24. )
  25. type BaremetalServerStartTask struct {
  26. SGuestBaseTask
  27. }
  28. func init() {
  29. taskman.RegisterTask(BaremetalServerStartTask{})
  30. }
  31. func (self *BaremetalServerStartTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  32. guest := obj.(*models.SGuest)
  33. guest.SetStatus(ctx, self.UserCred, api.VM_START_START, "")
  34. db.OpsLog.LogEvent(guest, db.ACT_STARTING, "", self.UserCred)
  35. baremetal, _ := guest.GetHost()
  36. if baremetal == nil {
  37. self.OnStartCompleteFailed(ctx, guest, jsonutils.NewString("Baremetal is None"))
  38. return
  39. }
  40. desc := guest.GetJsonDescAtBaremetal(ctx, baremetal)
  41. config := jsonutils.NewDict()
  42. config.Set("desc", jsonutils.Marshal(desc))
  43. url := fmt.Sprintf("/baremetals/%s/servers/%s/start", baremetal.Id, guest.Id)
  44. headers := self.GetTaskRequestHeader()
  45. self.SetStage("OnStartComplete", nil)
  46. _, err := baremetal.BaremetalSyncRequest(ctx, "POST", url, headers, config)
  47. if err != nil {
  48. log.Errorln(err)
  49. self.OnStartCompleteFailed(ctx, guest, jsonutils.NewString(err.Error()))
  50. }
  51. }
  52. func (self *BaremetalServerStartTask) OnStartComplete(ctx context.Context, guest *models.SGuest, body jsonutils.JSONObject) {
  53. guest.SetStatus(ctx, self.UserCred, api.VM_RUNNING, "BaremetalServerStartTask.OnStartComplete")
  54. baremetal, _ := guest.GetHost()
  55. baremetal.SetStatus(ctx, self.UserCred, api.BAREMETAL_RUNNING, "")
  56. db.OpsLog.LogEvent(guest, db.ACT_START, guest.GetShortDesc(ctx), self.UserCred)
  57. }
  58. func (self *BaremetalServerStartTask) OnStartCompleteFailed(ctx context.Context, guest *models.SGuest, body jsonutils.JSONObject) {
  59. guest.SetStatus(ctx, self.UserCred, api.VM_START_FAILED, body.String())
  60. db.OpsLog.LogEvent(guest, db.ACT_START_FAIL, body, self.UserCred)
  61. baremetal, _ := guest.GetHost()
  62. baremetal.SetStatus(ctx, self.UserCred, api.BAREMETAL_START_FAIL, body.String())
  63. self.SetStageFailed(ctx, body)
  64. }