container_pull_image_task.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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(ContainerPullImageTask{})
  25. }
  26. type ContainerPullImageTask struct {
  27. ContainerBaseTask
  28. }
  29. func (t *ContainerPullImageTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  30. t.requestPullImage(ctx, obj.(*models.SContainer))
  31. }
  32. func (t *ContainerPullImageTask) requestPullImage(ctx context.Context, container *models.SContainer) {
  33. t.SetStage("OnPulled", nil)
  34. if err := t.GetPodDriver().RequestPullContainerImage(ctx, t.GetUserCred(), t); err != nil {
  35. t.OnPulledFailed(ctx, container, jsonutils.NewString(err.Error()))
  36. return
  37. }
  38. }
  39. func (t *ContainerPullImageTask) OnPulledFailed(ctx context.Context, container *models.SContainer, reason jsonutils.JSONObject) {
  40. container.SetStatus(ctx, t.GetUserCred(), api.CONTAINER_STATUS_PULL_IMAGE_FAILED, reason.String())
  41. t.SetStageFailed(ctx, reason)
  42. }
  43. func (t *ContainerPullImageTask) OnPulled(ctx context.Context, container *models.SContainer, data jsonutils.JSONObject) {
  44. container.SetStatus(ctx, t.GetUserCred(), api.CONTAINER_STATUS_PULLED_IMAGE, "")
  45. t.SetStageComplete(ctx, nil)
  46. }