basedeploy.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. "yunion.io/x/jsonutils"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/utils"
  21. "yunion.io/x/onecloud/pkg/baremetal/utils/uefi"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/types"
  23. "yunion.io/x/onecloud/pkg/mcclient"
  24. "yunion.io/x/onecloud/pkg/util/ssh"
  25. )
  26. type IServerBaseDeployTask interface {
  27. IPXEBootTask
  28. RemoveEFIOSEntry() bool
  29. DoDeploys(ctx context.Context, term *ssh.Client) (jsonutils.JSONObject, error)
  30. PostDeploys(ctx context.Context, term *ssh.Client) error
  31. }
  32. type SBaremetalServerBaseDeployTask struct {
  33. SBaremetalPXEBootTaskBase
  34. needPXEBoot bool
  35. }
  36. func newBaremetalServerBaseDeployTask(
  37. userCred mcclient.TokenCredential,
  38. baremetal IBaremetal,
  39. taskId string,
  40. data jsonutils.JSONObject,
  41. ) SBaremetalServerBaseDeployTask {
  42. task := SBaremetalServerBaseDeployTask{
  43. SBaremetalPXEBootTaskBase: newBaremetalPXEBootTaskBase(userCred, baremetal, taskId, data),
  44. needPXEBoot: true,
  45. }
  46. // any inheritance must call:
  47. // task.SetStage(task.InitPXEBootTask)
  48. return task
  49. }
  50. func (self *SBaremetalServerBaseDeployTask) IServerBaseDeployTask() IServerBaseDeployTask {
  51. return self.GetVirtualObject().(IServerBaseDeployTask)
  52. }
  53. func (self *SBaremetalServerBaseDeployTask) GetName() string {
  54. return "BaremetalServerBaseDeployTask"
  55. }
  56. func (self *SBaremetalServerBaseDeployTask) NeedPXEBoot() bool {
  57. return self.needPXEBoot
  58. }
  59. func (self *SBaremetalServerBaseDeployTask) IsDisableImageCache() bool {
  60. return jsonutils.QueryBoolean(self.data, "disable_image_cache", false)
  61. }
  62. func (self *SBaremetalServerBaseDeployTask) GetFinishAction() string {
  63. if self.data != nil {
  64. action, _ := self.data.GetString("on_finish")
  65. return action
  66. }
  67. return ""
  68. }
  69. func (self *SBaremetalServerBaseDeployTask) RemoveEFIOSEntry() bool {
  70. return false
  71. }
  72. func (self *SBaremetalServerBaseDeployTask) DoDeploys(ctx context.Context, _ *ssh.Client) (jsonutils.JSONObject, error) {
  73. return nil, nil
  74. }
  75. func (self *SBaremetalServerBaseDeployTask) PostDeploys(_ context.Context, _ *ssh.Client) error {
  76. return nil
  77. }
  78. func (self *SBaremetalServerBaseDeployTask) OnPXEBoot(ctx context.Context, term *ssh.Client, args interface{}) error {
  79. log.Infof("%s called on stage pxeboot, args: %v", self.GetName(), args)
  80. if self.IServerBaseDeployTask().RemoveEFIOSEntry() {
  81. if err := uefi.RemoteTryRemoveOSBootEntry(term); err != nil {
  82. return errors.Wrap(err, "Remote uefi boot entry")
  83. }
  84. }
  85. result, err := self.IServerBaseDeployTask().DoDeploys(ctx, term)
  86. if err != nil {
  87. return errors.Wrap(err, "Do deploy")
  88. }
  89. if err := AdjustUEFIBootOrder(ctx, term, self.Baremetal); err != nil {
  90. return errors.Wrap(err, "Adjust UEFI boot order")
  91. }
  92. _, err = term.Run(
  93. "/bin/sync",
  94. "/sbin/sysctl -w vm.drop_caches=3",
  95. )
  96. if err != nil {
  97. return errors.Wrap(err, "Sync disk")
  98. }
  99. if err := self.IServerBaseDeployTask().PostDeploys(ctx, term); err != nil {
  100. return errors.Wrap(err, "post deploy")
  101. }
  102. onFinishAction := self.GetFinishAction()
  103. if utils.IsInStringArray(onFinishAction, []string{"restart", "shutdown"}) {
  104. if self.Baremetal.HasBMC() {
  105. if err := self.EnsurePowerShutdown(false); err != nil {
  106. return errors.Wrap(err, "Ensure power off")
  107. }
  108. if onFinishAction == "restart" {
  109. if err := self.EnsurePowerUp(); err != nil {
  110. return errors.Wrap(err, "Ensure power up")
  111. }
  112. }
  113. self.Baremetal.AutoSyncAllStatus(ctx)
  114. } else {
  115. if onFinishAction == "shutdown" {
  116. log.Infof("None BMC baremetal can't shutdown when deploying")
  117. /*
  118. * if err := self.Baremetal.SSHShutdown(); err != nil {
  119. * return errors.Wrap(err, "Try ssh shutdown")
  120. * }
  121. */
  122. } else {
  123. // do restart
  124. // hack: ssh reboot to disk
  125. self.needPXEBoot = false
  126. if err := self.EnsureSSHReboot(ctx); err != nil {
  127. return errors.Wrap(err, "Try ssh reboot")
  128. }
  129. }
  130. }
  131. self.Baremetal.SyncAllStatus(ctx, types.POWER_STATUS_ON)
  132. }
  133. SetTaskComplete(self, result)
  134. return nil
  135. }