prepare.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/log"
  18. "yunion.io/x/onecloud/pkg/baremetal/status"
  19. "yunion.io/x/onecloud/pkg/mcclient/auth"
  20. "yunion.io/x/onecloud/pkg/util/ssh"
  21. )
  22. type SBaremetalServerPrepareTask struct {
  23. SBaremetalTaskBase
  24. }
  25. func NewBaremetalServerPrepareTask(
  26. baremetal IBaremetal,
  27. ) *SBaremetalServerPrepareTask {
  28. task := &SBaremetalServerPrepareTask{
  29. SBaremetalTaskBase: newBaremetalTaskBase(auth.AdminCredential(), baremetal, "", nil),
  30. }
  31. task.SetVirtualObject(task)
  32. task.SetSSHStage(task.OnPXEBootRequest)
  33. return task
  34. }
  35. func (self *SBaremetalServerPrepareTask) NeedPXEBoot() bool {
  36. return true
  37. }
  38. func (self *SBaremetalServerPrepareTask) GetName() string {
  39. return "BaremetalServerPrepareTask"
  40. }
  41. // OnPXEBootRequest called by notify api handler
  42. func (self *SBaremetalServerPrepareTask) OnPXEBootRequest(ctx context.Context, cli *ssh.Client, args interface{}) error {
  43. err := newBaremetalPrepareTask(self.Baremetal, self.userCred).DoPrepare(ctx, cli)
  44. if err != nil {
  45. log.Errorf("Prepare failed: %v", err)
  46. self.Baremetal.SyncStatus(ctx, status.PREPARE_FAIL, err.Error())
  47. return err
  48. }
  49. self.Baremetal.AutoSyncStatus(ctx)
  50. SetTaskComplete(self, nil)
  51. return nil
  52. }