baremetal_maintenance_task.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. 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 BaremetalMaintenanceTask struct {
  25. SBaremetalBaseTask
  26. }
  27. func init() {
  28. taskman.RegisterTask(BaremetalMaintenanceTask{})
  29. }
  30. func (self *BaremetalMaintenanceTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  31. baremetal := obj.(*models.SHost)
  32. self.SetStage("OnEnterMaintenantModeSucc", nil)
  33. drv, err := baremetal.GetHostDriver()
  34. if err != nil {
  35. self.OnEnterMaintenantModeSuccFailed(ctx, baremetal, jsonutils.NewString(err.Error()))
  36. return
  37. }
  38. err = drv.RequestBaremetalMaintence(ctx, self.GetUserCred(), baremetal, self)
  39. if err != nil {
  40. self.OnEnterMaintenantModeSuccFailed(ctx, baremetal, jsonutils.NewString(err.Error()))
  41. return
  42. }
  43. baremetal.SetStatus(ctx, self.UserCred, api.BAREMETAL_MAINTAINING, "")
  44. }
  45. func (self *BaremetalMaintenanceTask) OnEnterMaintenantModeSucc(ctx context.Context, baremetal *models.SHost, body jsonutils.JSONObject) {
  46. action := self.Action()
  47. if len(action) > 0 {
  48. logclient.AddActionLogWithStartable(self, baremetal, action, "", self.UserCred, true)
  49. }
  50. if action != logclient.ACT_VM_START {
  51. db.Update(baremetal, func() error {
  52. baremetal.IsMaintenance = true
  53. return nil
  54. })
  55. }
  56. username, _ := body.Get("username")
  57. password, _ := body.Get("password")
  58. ip, _ := body.Get("ip")
  59. metadatas := map[string]interface{}{
  60. "__maint_username": username,
  61. "__maint_password": password,
  62. "__maint_ip": ip,
  63. }
  64. guestRunning, err := body.Get("guest_running")
  65. if err != nil {
  66. metadatas["__maint_guest_running"] = guestRunning
  67. }
  68. baremetal.SetAllMetadata(ctx, metadatas, self.UserCred)
  69. baremetal.StartSyncConfig(ctx, self.GetUserCred(), "")
  70. self.SetStageComplete(ctx, nil)
  71. }
  72. func (self *BaremetalMaintenanceTask) OnEnterMaintenantModeSuccFailed(ctx context.Context, baremetal *models.SHost, body jsonutils.JSONObject) {
  73. self.SetStageFailed(ctx, body)
  74. baremetal.StartSyncstatus(ctx, self.GetUserCred(), "")
  75. guest := baremetal.GetBaremetalServer()
  76. if guest != nil {
  77. guest.StartSyncstatus(ctx, self.UserCred, "")
  78. }
  79. action := self.Action()
  80. if len(action) > 0 {
  81. logclient.AddActionLogWithStartable(self, baremetal, action, body, self.UserCred, false)
  82. }
  83. }