baremetal_sync_config_task.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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/onecloud/pkg/cloudcommon/db"
  19. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  20. "yunion.io/x/onecloud/pkg/compute/models"
  21. "yunion.io/x/onecloud/pkg/util/logclient"
  22. )
  23. type BaremetalSyncConfigTask struct {
  24. SBaremetalBaseTask
  25. }
  26. func init() {
  27. taskman.RegisterTask(BaremetalSyncConfigTask{})
  28. }
  29. func (self *BaremetalSyncConfigTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  30. baremetal := obj.(*models.SHost)
  31. if baremetal.IsBaremetal {
  32. self.DoSyncConfig(ctx, baremetal)
  33. } else {
  34. self.SetStageComplete(ctx, nil)
  35. }
  36. }
  37. func (self *BaremetalSyncConfigTask) DoSyncConfig(ctx context.Context, baremetal *models.SHost) {
  38. self.SetStage("OnSyncConfigComplete", nil)
  39. drv, err := baremetal.GetHostDriver()
  40. if err != nil {
  41. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  42. return
  43. }
  44. err = drv.RequestSyncBaremetalHostConfig(ctx, self.GetUserCred(), baremetal, self)
  45. if err != nil {
  46. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  47. }
  48. }
  49. func (self *BaremetalSyncConfigTask) OnSyncConfigComplete(ctx context.Context, baremetal *models.SHost, body jsonutils.JSONObject) {
  50. logclient.AddActionLogWithStartable(self, baremetal, logclient.ACT_SYNC_CONF, "", self.UserCred, true)
  51. notSyncStatus, _ := self.Params.Bool("not_sync_status")
  52. if notSyncStatus {
  53. self.SetStageComplete(ctx, nil)
  54. return
  55. }
  56. self.SetStage("OnSyncstatusComplete", nil)
  57. baremetal.StartSyncstatus(ctx, self.UserCred, self.GetTaskId())
  58. }
  59. func (self *BaremetalSyncConfigTask) OnSyncstatusComplete(ctx context.Context, baremetal *models.SHost, body jsonutils.JSONObject) {
  60. self.SetStageComplete(ctx, nil)
  61. }