pod_start_task.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 guest
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. api "yunion.io/x/onecloud/pkg/apis/compute"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  22. "yunion.io/x/onecloud/pkg/compute/models"
  23. )
  24. type PodStartTask struct {
  25. SGuestBaseTask
  26. }
  27. func init() {
  28. taskman.RegisterTask(PodStartTask{})
  29. }
  30. func (t *PodStartTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  31. t.SetStage("OnPodStarted", nil)
  32. pod := obj.(*models.SGuest)
  33. err := pod.StartGueststartTask(ctx, t.GetUserCred(), jsonutils.NewDict(), t.GetTaskId())
  34. if err != nil {
  35. t.OnPodStartedFailed(ctx, pod, jsonutils.NewString(err.Error()))
  36. }
  37. }
  38. func (t *PodStartTask) OnPodStarted(ctx context.Context, pod *models.SGuest, _ jsonutils.JSONObject) {
  39. pod.SetStatus(ctx, t.GetUserCred(), api.POD_STATUS_STARTING_CONTAINER, "")
  40. // ctrs, err := models.GetContainerManager().GetContainersByPod(pod.GetId())
  41. // if err != nil {
  42. // t.OnContainerStartedFailed(ctx, pod, jsonutils.NewString(errors.Wrap(err, "GetContainersByPod").Error()))
  43. // return
  44. // }
  45. t.SetStage("OnContainerStarted", nil)
  46. task, err := taskman.TaskManager.NewTask(ctx, "PodStartContainerInDependencyOrderTask", pod, t.GetUserCred(), nil, t.GetTaskId(), "", nil)
  47. if err != nil {
  48. t.SetStageFailed(ctx, jsonutils.NewString(errors.Wrap(err, "New PodStartContainerInDependencyOrderTask").Error()))
  49. return
  50. }
  51. task.ScheduleRun(nil)
  52. }
  53. func (t *PodStartTask) OnPodStartedFailed(ctx context.Context, pod *models.SGuest, reason jsonutils.JSONObject) {
  54. pod.SetStatus(ctx, t.GetUserCred(), api.VM_START_FAILED, reason.String())
  55. t.SetStageFailed(ctx, reason)
  56. }
  57. func (t *PodStartTask) OnContainerStarted(ctx context.Context, pod *models.SGuest, data jsonutils.JSONObject) {
  58. pod.SetStatus(ctx, t.GetUserCred(), api.VM_RUNNING, "")
  59. t.SetStageComplete(ctx, nil)
  60. }
  61. func (t *PodStartTask) OnContainerStartedFailed(ctx context.Context, pod *models.SGuest, data jsonutils.JSONObject) {
  62. pod.SetStatus(ctx, t.GetUserCred(), api.POD_STATUS_START_CONTAINER_FAILED, data.String())
  63. t.SetStageFailed(ctx, data)
  64. }