image_copy_from_url_task.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 tasks
  15. import (
  16. "archive/tar"
  17. "compress/gzip"
  18. "context"
  19. "io"
  20. "net/http"
  21. "yunion.io/x/jsonutils"
  22. "yunion.io/x/log"
  23. "yunion.io/x/pkg/errors"
  24. "yunion.io/x/pkg/util/httputils"
  25. "yunion.io/x/pkg/utils"
  26. "yunion.io/x/onecloud/pkg/apis"
  27. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  28. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  29. "yunion.io/x/onecloud/pkg/image/models"
  30. "yunion.io/x/onecloud/pkg/image/options"
  31. )
  32. type ImageCopyFromUrlTask struct {
  33. taskman.STask
  34. }
  35. func init() {
  36. taskman.RegisterTask(ImageCopyFromUrlTask{})
  37. }
  38. func (self *ImageCopyFromUrlTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  39. image := obj.(*models.SImage)
  40. copyFrom, _ := self.Params.GetString("copy_from")
  41. compress, _ := self.Params.GetString("compress_format")
  42. log.Infof("Copy image from %s with compress %s", copyFrom, compress)
  43. self.SetStage("OnImageImportComplete", nil)
  44. taskman.LocalTaskRun(self, func() (jsonutils.JSONObject, error) {
  45. header := http.Header{}
  46. client := httputils.GetTimeoutClient(0)
  47. transport := httputils.GetTransport(true)
  48. transport.Proxy = options.Options.HttpTransportProxyFunc()
  49. client.Transport = transport
  50. resp, err := httputils.Request(client, ctx, httputils.GET, copyFrom, header, nil, false)
  51. if err != nil {
  52. return nil, err
  53. }
  54. defer resp.Body.Close()
  55. var reader io.Reader = resp.Body
  56. if utils.IsInStringArray(compress, []string{apis.COMPRESS_FORMAT_GZIP, apis.COMPRESS_FORMAT_TAR_GZ}) {
  57. gr, err := gzip.NewReader(resp.Body)
  58. if err != nil {
  59. return nil, errors.Wrapf(err, "gzip.NewReader")
  60. }
  61. reader = gr
  62. defer gr.Close()
  63. if compress == apis.COMPRESS_FORMAT_TAR_GZ {
  64. tr := tar.NewReader(gr)
  65. // 读取文件
  66. _, err := tr.Next()
  67. if err != nil && err != io.EOF {
  68. return nil, errors.Wrapf(err, "tr.Next")
  69. }
  70. reader = tr
  71. }
  72. }
  73. err = image.SaveImageFromStream(reader, resp.ContentLength, false)
  74. if err != nil {
  75. return nil, err
  76. }
  77. md5 := resp.Header.Get("x-oss-meta-yunion-os-checksum")
  78. if len(md5) > 0 {
  79. db.Update(image, func() error {
  80. image.OssChecksum = md5
  81. return nil
  82. })
  83. }
  84. return nil, nil
  85. })
  86. }
  87. func (self *ImageCopyFromUrlTask) OnImageImportComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  88. image := obj.(*models.SImage)
  89. image.OnSaveTaskSuccess(self, self.UserCred, "create upload success")
  90. image.StartImagePipeline(ctx, self.UserCred, false)
  91. self.SetStageComplete(ctx, nil)
  92. }
  93. func (self *ImageCopyFromUrlTask) OnImageImportCompleteFailed(ctx context.Context, obj db.IStandaloneModel, err jsonutils.JSONObject) {
  94. image := obj.(*models.SImage)
  95. copyFrom, _ := self.Params.GetString("copy_from")
  96. msg := jsonutils.NewDict()
  97. msg.Add(err, "reason")
  98. msg.Add(jsonutils.NewString(copyFrom), "copy_from")
  99. image.OnSaveTaskFailed(self, self.UserCred, msg)
  100. self.SetStageFailed(ctx, msg)
  101. }