container_commit_task.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. api "yunion.io/x/onecloud/pkg/apis/compute"
  19. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  21. "yunion.io/x/onecloud/pkg/compute/models"
  22. )
  23. func init() {
  24. taskman.RegisterTask(ContainerCommitTask{})
  25. }
  26. type ContainerCommitTask struct {
  27. ContainerBaseTask
  28. }
  29. func (t *ContainerCommitTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  30. t.requestCommitImage(ctx, obj.(*models.SContainer))
  31. }
  32. func (t *ContainerCommitTask) requestCommitImage(ctx context.Context, container *models.SContainer) {
  33. t.SetStage("OnCommitted", nil)
  34. if err := t.GetPodDriver().RequestCommitContainer(ctx, t.GetUserCred(), t); err != nil {
  35. t.OnCommittedFailed(ctx, container, jsonutils.NewString(err.Error()))
  36. return
  37. }
  38. }
  39. func (t *ContainerCommitTask) OnCommitted(ctx context.Context, container *models.SContainer, data jsonutils.JSONObject) {
  40. t.SetStage("OnSyncStatus", nil)
  41. container.StartSyncStatusTask(ctx, t.GetUserCred(), t.GetTaskId())
  42. }
  43. func (t *ContainerCommitTask) OnCommittedFailed(ctx context.Context, container *models.SContainer, reason jsonutils.JSONObject) {
  44. container.SetStatus(ctx, t.GetUserCred(), api.CONTAINER_STATUS_COMMIT_FAILED, reason.String())
  45. t.SetStageFailed(ctx, reason)
  46. }
  47. func (t *ContainerCommitTask) OnSyncStatus(ctx context.Context, container *models.SContainer, data jsonutils.JSONObject) {
  48. t.SetStageComplete(ctx, nil)
  49. }
  50. func (t *ContainerCommitTask) OnSyncStatusFailed(ctx context.Context, container *models.SContainer, reason jsonutils.JSONObject) {
  51. t.SetStageFailed(ctx, reason)
  52. }