container_cache_images_task.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. "yunion.io/x/onecloud/pkg/httperrors"
  24. )
  25. func init() {
  26. taskman.RegisterTask(ContainerCacheImagesTask{})
  27. }
  28. type ContainerCacheImagesTask struct {
  29. ContainerBaseTask
  30. }
  31. func (t *ContainerCacheImagesTask) getInput() (*api.ContainerCacheImagesInput, error) {
  32. input := new(api.ContainerCacheImagesInput)
  33. if err := t.GetParams().Unmarshal(input); err != nil {
  34. return nil, err
  35. }
  36. return input, nil
  37. }
  38. func (t *ContainerCacheImagesTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  39. if err := t.startCacheImages(ctx, obj); err != nil {
  40. t.onError(ctx, obj.(*models.SContainer), jsonutils.NewString(err.Error()))
  41. return
  42. }
  43. }
  44. func (t *ContainerCacheImagesTask) onError(ctx context.Context, ctr *models.SContainer, reason jsonutils.JSONObject) {
  45. ctr.SetStatus(ctx, t.GetUserCred(), api.CONTAINER_STATUS_CACHE_IMAGE_FAILED, reason.String())
  46. t.SetStageFailed(ctx, reason)
  47. }
  48. func (t *ContainerCacheImagesTask) startCacheImages(ctx context.Context, obj db.IStandaloneModel) error {
  49. input, err := t.getInput()
  50. if err != nil {
  51. return errors.Wrapf(err, "getInput")
  52. }
  53. //caches := make([]db.IStandaloneModel, 0)
  54. //params := []*api.CacheImageInput{}
  55. t.SetStage("OnStorageCacheImageComplete", nil)
  56. for i, img := range input.Images {
  57. disk := models.DiskManager.FetchDiskById(img.DiskId)
  58. if disk == nil {
  59. return errors.Wrapf(httperrors.ErrNotFound, "disk not found %s", img.DiskId)
  60. }
  61. storage, _ := disk.GetStorage()
  62. storagecache := storage.GetStoragecache()
  63. if storagecache == nil {
  64. return errors.Wrapf(httperrors.ErrNotFound, "storage cache not found by %s", storage.GetId())
  65. }
  66. //caches = append(caches, storagecache)
  67. param := input.Images[i].Image
  68. param.ParentTaskId = t.GetTaskId()
  69. //params = append(params, param)
  70. if err := storagecache.StartImageCacheTask(ctx, t.GetUserCred(), *param); err != nil {
  71. return errors.Wrapf(err, "startImageCacheTask of param: %s", jsonutils.Marshal(param))
  72. }
  73. }
  74. return nil
  75. }
  76. func (t *ContainerCacheImagesTask) OnStorageCacheImageComplete(ctx context.Context, ctr *models.SContainer, data jsonutils.JSONObject) {
  77. if t.IsSubtask() {
  78. t.SetStageComplete(ctx, nil)
  79. return
  80. }
  81. ctr.StartSyncStatusTask(ctx, t.GetUserCred(), t.GetTaskId())
  82. t.SetStageComplete(ctx, nil)
  83. }
  84. func (t *ContainerCacheImagesTask) OnStorageCacheImageCompleteFailed(ctx context.Context, ctr *models.SContainer, data jsonutils.JSONObject) {
  85. t.onError(ctx, ctr, data)
  86. }