baremetal_create_task.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 baremetal
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/log"
  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. "yunion.io/x/onecloud/pkg/util/logclient"
  24. )
  25. type BaremetalCreateTask struct {
  26. SBaremetalBaseTask
  27. }
  28. func init() {
  29. taskman.RegisterTask(BaremetalCreateTask{})
  30. }
  31. func (self *BaremetalCreateTask) taskComplete(ctx context.Context, baremetal *models.SHost) {
  32. logclient.AddActionLogWithStartable(self, baremetal, logclient.ACT_ALLOCATE, baremetal.GetShortDesc(ctx), self.UserCred, true)
  33. self.SetStageComplete(ctx, nil)
  34. }
  35. func (self *BaremetalCreateTask) taskFailed(ctx context.Context, baremetal *models.SHost, err string) {
  36. logclient.AddActionLogWithStartable(self, baremetal, logclient.ACT_ALLOCATE, err, self.UserCred, false)
  37. self.SetStageComplete(ctx, nil)
  38. }
  39. func (self *BaremetalCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  40. baremetal := obj.(*models.SHost)
  41. self.SetStage("OnIpmiProbeComplete", nil)
  42. baremetal.StartIpmiProbeTask(ctx, self.UserCred, self.GetTaskId())
  43. }
  44. func (self *BaremetalCreateTask) OnIpmiProbeComplete(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  45. baremetal := obj.(*models.SHost)
  46. ipmiInfo, _ := baremetal.GetIpmiInfo()
  47. if !ipmiInfo.Verified {
  48. self.taskComplete(ctx, baremetal)
  49. return
  50. }
  51. if jsonutils.QueryBoolean(self.Params, "no_prepare", false) {
  52. self.taskComplete(ctx, baremetal)
  53. return
  54. }
  55. if (baremetal.EnablePxeBoot.IsFalse() || !ipmiInfo.PxeBoot) && !ipmiInfo.CdromBoot {
  56. self.taskComplete(ctx, baremetal)
  57. return
  58. }
  59. if baremetal.AccessMac == "" && baremetal.Uuid == "" && !ipmiInfo.CdromBoot {
  60. msg := "Fail to find access_mac or uuid, host-prepare aborted. Please supply either access_mac or uuid and try host-prepare"
  61. log.Errorf("%s", msg)
  62. self.taskFailed(ctx, baremetal, msg)
  63. baremetal.SetStatus(ctx, self.UserCred, api.BAREMETAL_PREPARE_FAIL, msg)
  64. return
  65. }
  66. self.SetStage("OnPrepareComplete", nil)
  67. baremetal.StartPrepareTask(ctx, self.UserCred, "", self.GetTaskId())
  68. }
  69. func (self *BaremetalCreateTask) OnIpmiProbeCompleteFailed(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  70. baremetal := obj.(*models.SHost)
  71. self.taskFailed(ctx, baremetal, body.String())
  72. }
  73. func (self *BaremetalCreateTask) OnPrepareComplete(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  74. baremetal := obj.(*models.SHost)
  75. self.taskComplete(ctx, baremetal)
  76. }
  77. func (self *BaremetalCreateTask) OnPrepareCompleteFailed(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  78. baremetal := obj.(*models.SHost)
  79. self.taskFailed(ctx, baremetal, body.String())
  80. }