container_start_task.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 container
  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 ContainerStartTask struct {
  25. ContainerBaseTask
  26. }
  27. func init() {
  28. taskman.RegisterTask(ContainerStartTask{})
  29. }
  30. func (t *ContainerStartTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  31. ctr := obj.(*models.SContainer)
  32. if err := t.startCacheImages(ctx, ctr); err != nil {
  33. t.OnStartedFailed(ctx, ctr, jsonutils.NewString(err.Error()))
  34. }
  35. }
  36. func (t *ContainerStartTask) startCacheImages(ctx context.Context, ctr *models.SContainer) error {
  37. t.SetStage("OnCacheImagesComplete", nil)
  38. input, err := t.GetContainerCacheImagesInput(ctr)
  39. if err != nil {
  40. return errors.Wrap(err, "GetContainerCacheImagesInput")
  41. }
  42. if len(input.Images) == 0 {
  43. t.OnCacheImagesComplete(ctx, ctr, nil)
  44. return nil
  45. }
  46. return ctr.StartCacheImagesTask(ctx, t.GetUserCred(), input, t.GetTaskId())
  47. }
  48. func (t *ContainerStartTask) OnCacheImagesComplete(ctx context.Context, ctr *models.SContainer, data jsonutils.JSONObject) {
  49. t.requestSyncConf(ctx, ctr)
  50. }
  51. func (t *ContainerStartTask) OnCacheImagesCompleteFailed(ctx context.Context, ctr *models.SContainer, data jsonutils.JSONObject) {
  52. t.OnStartedFailed(ctx, ctr, jsonutils.NewString(data.String()))
  53. }
  54. func (t *ContainerStartTask) requestSyncConf(ctx context.Context, container *models.SContainer) {
  55. // sync configuration to make server of host to refresh desc file
  56. t.SetStage("OnSyncConf", nil)
  57. if err := container.GetPod().StartSyncTaskWithoutSyncstatus(ctx, t.GetUserCred(), false, t.GetTaskId()); err != nil {
  58. t.OnSyncConfFailed(ctx, container, jsonutils.NewString(err.Error()))
  59. return
  60. }
  61. }
  62. func (t *ContainerStartTask) OnSyncConf(ctx context.Context, container *models.SContainer, data jsonutils.JSONObject) {
  63. t.requestStart(ctx, container)
  64. }
  65. func (t *ContainerStartTask) OnSyncConfFailed(ctx context.Context, container *models.SContainer, reason jsonutils.JSONObject) {
  66. container.SetStatus(ctx, t.GetUserCred(), api.CONTAINER_STATUS_SYNC_CONF_FAILED, reason.String())
  67. t.SetStageFailed(ctx, reason)
  68. }
  69. func (t *ContainerStartTask) requestStart(ctx context.Context, container *models.SContainer) {
  70. t.SetStage("OnStarted", nil)
  71. if err := t.GetPodDriver().RequestStartContainer(ctx, t.GetUserCred(), t); err != nil {
  72. t.OnStartedFailed(ctx, container, jsonutils.NewString(err.Error()))
  73. return
  74. }
  75. }
  76. func (t *ContainerStartTask) OnStarted(ctx context.Context, container *models.SContainer, data jsonutils.JSONObject) {
  77. t.SetStage("OnSyncStatus", nil)
  78. container.StartSyncStatusTask(ctx, t.GetUserCred(), t.GetTaskId())
  79. }
  80. func (t *ContainerStartTask) OnStartedFailed(ctx context.Context, container *models.SContainer, reason jsonutils.JSONObject) {
  81. container.SetStatus(ctx, t.GetUserCred(), api.CONTAINER_STATUS_START_FAILED, reason.String())
  82. t.SetStageFailed(ctx, reason)
  83. }
  84. func (t *ContainerStartTask) OnSyncStatus(ctx context.Context, container *models.SContainer, data jsonutils.JSONObject) {
  85. t.SetStageComplete(ctx, nil)
  86. }
  87. func (t *ContainerStartTask) OnSyncStatusFailed(ctx context.Context, container *models.SContainer, reason jsonutils.JSONObject) {
  88. t.SetStageFailed(ctx, reason)
  89. }