resetbmc.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 tasks
  15. import (
  16. "context"
  17. "time"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/onecloud/pkg/baremetal/utils/ipmitool"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/types"
  21. "yunion.io/x/onecloud/pkg/mcclient"
  22. "yunion.io/x/onecloud/pkg/util/ssh"
  23. )
  24. type SBaremetalResetBMCTask struct {
  25. SBaremetalPXEBootTaskBase
  26. term *ssh.Client
  27. }
  28. func NewBaremetalResetBMCTask(
  29. userCred mcclient.TokenCredential,
  30. baremetal IBaremetal,
  31. taskId string,
  32. data jsonutils.JSONObject,
  33. ) ITask {
  34. task := &SBaremetalResetBMCTask{
  35. SBaremetalPXEBootTaskBase: newBaremetalPXEBootTaskBase(userCred, baremetal, taskId, data),
  36. }
  37. task.SetVirtualObject(task)
  38. task.SetStage(task.InitPXEBootTask)
  39. return task
  40. }
  41. func (self *SBaremetalResetBMCTask) GetName() string {
  42. return "BaremetalResetBMCTask"
  43. }
  44. func (self *SBaremetalResetBMCTask) GetIPMITool() *ipmitool.SSHIPMI {
  45. return ipmitool.NewSSHIPMI(self.term)
  46. }
  47. func (self *SBaremetalResetBMCTask) OnPXEBoot(ctx context.Context, term *ssh.Client, args interface{}) error {
  48. self.term = term
  49. self.SetStage(self.WaitForBMCReady)
  50. err := ipmitool.DoBMCReset(self.GetIPMITool())
  51. if err != nil {
  52. return err
  53. }
  54. time.Sleep(10 * time.Second)
  55. ExecuteTask(self, nil)
  56. return nil
  57. }
  58. func (self *SBaremetalResetBMCTask) WaitForBMCReady(ctx context.Context, args interface{}) error {
  59. self.SetStage(self.OnBMCReady)
  60. status, err := ipmitool.GetChassisPowerStatus(self.GetIPMITool())
  61. if err != nil {
  62. return err
  63. }
  64. if status != "" && status == string(types.POWER_STATUS_ON) {
  65. ExecuteTask(self, nil)
  66. }
  67. return nil
  68. }
  69. func (self *SBaremetalResetBMCTask) OnBMCReady(ctx context.Context, args interface{}) error {
  70. time.Sleep(20 * time.Second)
  71. SetTaskComplete(self, nil)
  72. return nil
  73. }