remotefile.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 remotefile
  15. import (
  16. "compress/zlib"
  17. "context"
  18. "fmt"
  19. "io"
  20. "net/http"
  21. "os"
  22. "strconv"
  23. "syscall"
  24. "time"
  25. "yunion.io/x/jsonutils"
  26. "yunion.io/x/log"
  27. "yunion.io/x/pkg/errors"
  28. "yunion.io/x/pkg/util/httputils"
  29. "yunion.io/x/onecloud/pkg/mcclient/auth"
  30. "yunion.io/x/onecloud/pkg/util/fileutils2"
  31. "yunion.io/x/onecloud/pkg/util/pb"
  32. "yunion.io/x/onecloud/pkg/util/sparsefile"
  33. )
  34. type SImageDesc struct {
  35. Name string `json:"name"`
  36. Format string `json:"format"`
  37. Id string `json:"id"`
  38. Chksum string `json:"chksum"`
  39. Path string `json:"path"`
  40. SizeMb int64 `json:"size"`
  41. AccessAt time.Time `json:"access_at"`
  42. }
  43. type SRemoteFile struct {
  44. ctx context.Context
  45. url string
  46. downloadUrl string
  47. localPath string
  48. tmpPath string
  49. preChksum string
  50. compress bool
  51. timeout time.Duration
  52. extraHeaders map[string]string
  53. chksum string
  54. format string
  55. name string
  56. s3Info *S3RemoteFileInfo
  57. }
  58. func NewRemoteFile(
  59. ctx context.Context, url, localPath string, compress bool,
  60. PreChksum string, timeout int, extraHeaders map[string]string,
  61. tmpPath string, downloadUrl string,
  62. ) *SRemoteFile {
  63. if timeout <= 0 {
  64. timeout = 24 * 3600 //24 hours
  65. }
  66. if len(tmpPath) == 0 {
  67. tmpPath = localPath
  68. }
  69. return &SRemoteFile{
  70. ctx: ctx,
  71. url: url,
  72. localPath: localPath,
  73. compress: compress,
  74. preChksum: PreChksum,
  75. timeout: time.Duration(timeout) * time.Second,
  76. extraHeaders: extraHeaders,
  77. tmpPath: tmpPath,
  78. downloadUrl: downloadUrl,
  79. }
  80. }
  81. func (r *SRemoteFile) GetFormat() string {
  82. return r.format
  83. }
  84. func (r *SRemoteFile) Fetch(callback func(progress, progressMbps float64, totalSizeMb int64)) error {
  85. if len(r.preChksum) > 0 {
  86. log.Infof("Fetch remote file with precheck sum: %s", r.preChksum)
  87. return r.fetch(r.preChksum, callback)
  88. }
  89. if fileutils2.Exists(r.localPath) {
  90. if r.preChksum != "" {
  91. err := r.VerifyIntegrity(callback)
  92. if err != nil {
  93. log.Warningf("Local path %s file mistmatch, refetch", r.localPath)
  94. return r.fetch("", callback)
  95. }
  96. } else {
  97. if err := r.FillAttributes(callback); err != nil {
  98. return errors.Wrap(err, "fetch remote attribute")
  99. }
  100. }
  101. return nil
  102. }
  103. log.Infof("Fetch remote file %q %q to %q", r.url, r.downloadUrl, r.tmpPath)
  104. return r.fetch("", callback)
  105. }
  106. func (r *SRemoteFile) GetInfo() (*SImageDesc, error) {
  107. fi, err := os.Stat(r.localPath)
  108. if err != nil {
  109. return nil, errors.Wrapf(err, "os.Stat(%s)", r.localPath)
  110. }
  111. var atime time.Time
  112. if fi.Sys() != nil {
  113. atm := fi.Sys().(*syscall.Stat_t).Atim
  114. atime = time.Unix(atm.Sec, atm.Nsec)
  115. }
  116. return &SImageDesc{
  117. Name: r.name,
  118. Format: r.format,
  119. Chksum: r.chksum,
  120. Path: r.localPath,
  121. SizeMb: fi.Size() / 1024 / 1024,
  122. AccessAt: atime,
  123. }, nil
  124. }
  125. func (r *SRemoteFile) VerifyIntegrity(callback func(progress, progressMbps float64, totalSizeMb int64)) error {
  126. localChksum, err := fileutils2.MD5(r.localPath)
  127. if err != nil {
  128. return errors.Wrapf(err, "fileutils2.MD5(%s)", r.localPath)
  129. }
  130. if r.preChksum != "" {
  131. if localChksum == r.preChksum {
  132. return nil
  133. }
  134. }
  135. err = r.download(false, "", callback)
  136. if err == nil && localChksum == r.chksum {
  137. return nil
  138. }
  139. log.Warningf("Integrity mistmatch, fetch from remote")
  140. return r.fetch("", callback)
  141. }
  142. func (r *SRemoteFile) fetch(preChksum string, callback func(progress, progressMbps float64, totalSizeMb int64)) error {
  143. var err error
  144. for i := 0; i < 3; i++ {
  145. err = r.download(true, preChksum, callback)
  146. if err == nil {
  147. if len(r.chksum) > 0 && fileutils2.Exists(r.tmpPath) {
  148. localChksum, err := fileutils2.MD5(r.tmpPath)
  149. if err != nil {
  150. log.Errorf("TmpPath MD5 %s fail %s", r.tmpPath, err)
  151. return errors.Wrapf(err, "TmpPath fileutils2.MD5(%s)", r.tmpPath)
  152. }
  153. if r.chksum != localChksum {
  154. log.Errorf("remote checksume %s != local checksum %s", r.chksum, localChksum)
  155. return fmt.Errorf("remote checksum %s != local checksum %s", r.chksum, localChksum)
  156. }
  157. }
  158. log.Debugf("localPath %s tmpPath %s", r.localPath, r.tmpPath)
  159. if r.localPath != r.tmpPath {
  160. syscall.Unlink(r.localPath)
  161. return syscall.Rename(r.tmpPath, r.localPath)
  162. }
  163. return nil
  164. }
  165. }
  166. return errors.Wrapf(err, "download")
  167. }
  168. func (r *SRemoteFile) FillAttributes(callback func(progress, progressMbps float64, totalSizeMb int64)) error {
  169. if err := r.download(false, "", callback); err != nil {
  170. return errors.Wrap(err, "download attribute data")
  171. }
  172. return nil
  173. }
  174. // retry download
  175. func (r *SRemoteFile) download(getData bool, preChksum string, callback func(progress, progressMbps float64, totalSizeMb int64)) error {
  176. if getData {
  177. // fetch image headers and set resource properties
  178. err := r.downloadInternal(false, preChksum, callback)
  179. if err != nil {
  180. log.Errorf("fetch image properties failed %v", err)
  181. }
  182. }
  183. err := r.downloadInternal(getData, preChksum, callback)
  184. if err == nil {
  185. return nil
  186. }
  187. log.Errorf("download from cached url %s failed, try direct download from %s ...", r.downloadUrl, r.url)
  188. r.downloadUrl = ""
  189. if getData {
  190. // fetch image headers and set resource properties
  191. err = r.downloadInternal(false, preChksum, callback)
  192. if err != nil {
  193. log.Errorf("fetch image properties failed error: %v", err)
  194. }
  195. }
  196. return r.downloadInternal(getData, preChksum, callback)
  197. }
  198. func (r *SRemoteFile) downloadS3(callback func(progress, progressMbps float64, totalSizeMb int64)) error {
  199. defer func() {
  200. // clear s3 info
  201. r.s3Info = nil
  202. }()
  203. err := r.s3Info.download(r.ctx, r.tmpPath, callback)
  204. if err != nil {
  205. return errors.Wrap(err, "download s3")
  206. }
  207. return nil
  208. }
  209. func (r *SRemoteFile) downloadInternal(getData bool, preChksum string, callback func(progress, progressMbps float64, totalSizeMb int64)) error {
  210. if getData && r.s3Info != nil {
  211. return r.downloadS3(callback)
  212. }
  213. var header = http.Header{}
  214. header.Set("X-Auth-Token", auth.GetTokenString())
  215. if len(preChksum) > 0 {
  216. header.Set("X-Image-Meta-Checksum", preChksum)
  217. }
  218. if r.compress {
  219. header.Set("X-Compress-Content", "zlib")
  220. }
  221. header.Set("X-Sparse-Content", "true")
  222. if len(r.extraHeaders) > 0 {
  223. for k, v := range r.extraHeaders {
  224. header.Set(k, v)
  225. }
  226. }
  227. var method, url = "HEAD", r.url
  228. if len(r.downloadUrl) > 0 {
  229. url = r.downloadUrl
  230. }
  231. if getData {
  232. method = "GET"
  233. }
  234. httpCli := httputils.GetTimeoutClient(r.timeout)
  235. resp, err := httputils.Request(httpCli, r.ctx,
  236. httputils.THttpMethod(method), url, header, nil, false)
  237. if err != nil {
  238. return errors.Wrapf(err, "request %s %s", method, url)
  239. }
  240. totalSize, _ := strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64)
  241. sparseHeader, _ := strconv.ParseInt(resp.Header.Get("X-Sparse-Header"), 10, 64)
  242. defer resp.Body.Close()
  243. if resp.StatusCode < 300 {
  244. if getData {
  245. var fi *os.File
  246. if r.tmpPath == r.localPath && fileutils2.Exists(r.localPath) {
  247. fi, err = os.Open(r.tmpPath)
  248. if err != nil {
  249. return errors.Wrapf(err, "os.Open(%s)", r.tmpPath)
  250. }
  251. } else {
  252. fi, err = os.Create(r.tmpPath)
  253. if err != nil {
  254. return errors.Wrapf(err, "os.Create(%s)", r.tmpPath)
  255. }
  256. }
  257. defer fi.Close()
  258. var reader = resp.Body
  259. if r.compress {
  260. zlibRC, err := zlib.NewReader(resp.Body)
  261. if err != nil {
  262. return errors.Wrapf(err, "zlib.NewReader")
  263. }
  264. defer zlibRC.Close()
  265. reader = zlibRC
  266. }
  267. var writer io.Writer = fi
  268. if sparseHeader > 0 {
  269. writer = sparsefile.NewSparseFileWriter(fi, sparseHeader, totalSize)
  270. fileSize, _ := strconv.ParseInt(resp.Header.Get("X-File-Size"), 10, 64)
  271. if fileSize > 0 {
  272. err = fi.Truncate(fileSize)
  273. if err != nil {
  274. return errors.Wrapf(err, "failed truncate file")
  275. }
  276. }
  277. }
  278. pb := pb.NewProxyReader(reader, totalSize)
  279. pb.SetCallback(func() {
  280. if callback != nil {
  281. go func() {
  282. callback(pb.Percent(), pb.Rate(), totalSize/1024/1024)
  283. }()
  284. }
  285. log.Infof("written file %s rate: %.2f MiB p/s percent: %.2f%%", r.tmpPath, pb.Rate(), pb.Percent())
  286. })
  287. _, err = io.Copy(writer, pb)
  288. if err != nil {
  289. return errors.Wrapf(err, "io.Copy to tmpPath %s from reader", r.tmpPath)
  290. }
  291. }
  292. r.setProperties(resp.Header)
  293. return nil
  294. } else if resp.StatusCode == 304 {
  295. if fileutils2.Exists(r.tmpPath) {
  296. if err := os.Remove(r.tmpPath); err != nil {
  297. log.Errorf("Fail to remove file %s", r.tmpPath)
  298. }
  299. }
  300. return nil
  301. }
  302. return fmt.Errorf("Remote file fetch %s %s error %d", method, url, resp.StatusCode)
  303. }
  304. func (r *SRemoteFile) setProperties(header http.Header) {
  305. log.Debugf("remoteFile headers: %s", jsonutils.Marshal(header))
  306. if chksum := header.Get("X-Image-Meta-Checksum"); len(chksum) > 0 {
  307. r.chksum = chksum
  308. }
  309. if format := header.Get("X-Image-Meta-Disk_format"); len(format) > 0 {
  310. r.format = format
  311. }
  312. if name := header.Get("X-Image-Meta-Name"); len(name) > 0 {
  313. r.name = name
  314. }
  315. if s3Url := header.Get("X-Image-Meta-S3_info_url"); len(s3Url) > 0 {
  316. r.s3Info = &S3RemoteFileInfo{}
  317. r.s3Info.Url = s3Url
  318. if s3AccessKey := header.Get("X-Image-Meta-S3_info_access_key"); len(s3AccessKey) > 0 {
  319. r.s3Info.AcessKey = s3AccessKey
  320. }
  321. if s3Secret := header.Get("X-Image-Meta-S3_info_secret"); len(s3Secret) > 0 {
  322. r.s3Info.Secret = s3Secret
  323. }
  324. if s3Key := header.Get("X-Image-Meta-S3_info_key"); len(s3Key) > 0 {
  325. r.s3Info.Key = s3Key
  326. }
  327. if s3SignVer := header.Get("X-Image-Meta-S3_info_sign_ver"); len(s3SignVer) > 0 {
  328. r.s3Info.SignVer = s3SignVer
  329. }
  330. if s3Bucket := header.Get("X-Image-Meta-S3_info_bucket"); len(s3Bucket) > 0 {
  331. r.s3Info.Bucket = s3Bucket
  332. }
  333. }
  334. }