storageconnection.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package azure
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "sync"
  10. "time"
  11. "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
  12. "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container"
  13. "github.com/opencost/opencost/core/pkg/log"
  14. "github.com/opencost/opencost/pkg/cloud"
  15. )
  16. // StorageConnection provides access to Azure Storage
  17. type StorageConnection struct {
  18. StorageConfiguration
  19. lock sync.Mutex
  20. ConnectionStatus cloud.ConnectionStatus
  21. }
  22. func (sc *StorageConnection) GetStatus() cloud.ConnectionStatus {
  23. // initialize status if it has not done so; this can happen if the integration is inactive
  24. if sc.ConnectionStatus.String() == "" {
  25. sc.ConnectionStatus = cloud.InitialStatus
  26. }
  27. return sc.ConnectionStatus
  28. }
  29. func (sc *StorageConnection) Equals(config cloud.Config) bool {
  30. thatConfig, ok := config.(*StorageConnection)
  31. if !ok {
  32. return false
  33. }
  34. return sc.StorageConfiguration.Equals(&thatConfig.StorageConfiguration)
  35. }
  36. // getBlobURLTemplate returns the correct BlobUrl for whichever Cloud storage account is specified by the AzureCloud configuration
  37. // defaults to the Public Cloud template
  38. func (sc *StorageConnection) getBlobURLTemplate() string {
  39. // Use gov cloud blob url if gov is detected in AzureCloud
  40. if strings.Contains(strings.ToLower(sc.Cloud), "gov") {
  41. return "https://%s.blob.core.usgovcloudapi.net/%s"
  42. } else if strings.Contains(strings.ToLower(sc.Cloud), "china") {
  43. // Use China cloud blob url if china is detected in AzureCloud
  44. return "https://%s.blob.core.chinacloudapi.cn/%s"
  45. }
  46. // default to Public Cloud template
  47. return "https://%s.blob.core.windows.net/%s"
  48. }
  49. // DownloadBlob downloads the Azure Billing CSV into a byte slice
  50. func (sc *StorageConnection) DownloadBlob(blobName string, client *azblob.Client, ctx context.Context) ([]byte, error) {
  51. log.Infof("Azure Storage: retrieving blob: %v", blobName)
  52. downloadResponse, err := client.DownloadStream(ctx, sc.Container, blobName, nil)
  53. if err != nil {
  54. return nil, fmt.Errorf("Azure: DownloadBlob: failed to download %w", err)
  55. }
  56. // NOTE: automatically retries are performed if the connection fails
  57. retryReader := downloadResponse.NewRetryReader(ctx, &azblob.RetryReaderOptions{})
  58. defer retryReader.Close()
  59. // read the body into a buffer
  60. downloadedData := bytes.Buffer{}
  61. _, err = downloadedData.ReadFrom(retryReader)
  62. if err != nil {
  63. return nil, fmt.Errorf("Azure: DownloadBlob: failed to read downloaded data %w", err)
  64. }
  65. return downloadedData.Bytes(), nil
  66. }
  67. // StreamBlob returns an io.Reader for the given blob which uses a re-usable double buffer approach to stream directly
  68. // from blob storage.
  69. func (sc *StorageConnection) StreamBlob(blobName string, client *azblob.Client) (*StreamReader, error) {
  70. return NewStreamReader(client, sc.Container, blobName)
  71. }
  72. // DownloadBlobToFile downloads the Azure Billing CSV to a local file
  73. func (sc *StorageConnection) DownloadBlobToFile(localFilePath string, blob container.BlobItem, client *azblob.Client, ctx context.Context) error {
  74. // Lock to prevent accessing a file which may not be fully downloaded
  75. sc.lock.Lock()
  76. defer sc.lock.Unlock()
  77. blobName := *blob.Name
  78. // Check if file already exists
  79. if fileInfo, err := os.Stat(localFilePath); err == nil {
  80. blobModTime := *blob.Properties.LastModified
  81. // Check if the blob was last modified before the file was modified, indicating that the
  82. // file is the most recent version of the blob
  83. if blobModTime.Before(fileInfo.ModTime()) {
  84. log.Debugf("CloudCost: Azure: DownloadBlobToFile: file %s is more recent than correspondig blob %s", localFilePath, blobName)
  85. return nil
  86. }
  87. }
  88. // Create filepath
  89. dir := filepath.Dir(localFilePath)
  90. if err := os.MkdirAll(dir, os.ModePerm); err != nil {
  91. return fmt.Errorf("CloudCost: Azure: DownloadBlobToFile: failed to create directory %w", err)
  92. }
  93. fp, err := os.Create(localFilePath)
  94. if err != nil {
  95. return fmt.Errorf("CloudCost: Azure: DownloadBlobToFile: failed to create file %w", err)
  96. }
  97. defer fp.Close()
  98. // Download newest Azure Billing CSV to disk
  99. // Time out to prevent deadlock on download
  100. timeoutCtx, cancel := context.WithTimeout(ctx, 30*time.Minute)
  101. defer cancel()
  102. log.Infof("CloudCost: Azure: DownloadBlobToFile: retrieving blob: %v", blobName)
  103. filesize, err := client.DownloadFile(timeoutCtx, sc.Container, blobName, fp, nil)
  104. if err != nil {
  105. // Clean up file from failed download
  106. err2 := os.Remove(localFilePath)
  107. if err2 != nil {
  108. log.Errorf("CloudCost: Azure: DownloadBlobToFile: failed to remove file %s after failed download %s", localFilePath, err2.Error())
  109. }
  110. return fmt.Errorf("CloudCost: Azure: DownloadBlobToFile: failed to download %w", err)
  111. }
  112. log.Infof("CloudCost: Azure: DownloadBlobToFile: retrieved %v of size %dMB", blobName, filesize/1024/1024)
  113. return nil
  114. }
  115. // deleteFilesOlderThan7d recursively walks the directory specified and deletes
  116. // files which have not been modified in the last 7 days. Returns a list of
  117. // files deleted.
  118. func (sc *StorageConnection) deleteFilesOlderThan7d(localPath string) ([]string, error) {
  119. sc.lock.Lock()
  120. defer sc.lock.Unlock()
  121. duration := 7 * 24 * time.Hour
  122. cleaned := []string{}
  123. errs := []string{}
  124. if _, err := os.Stat(localPath); err != nil {
  125. return cleaned, nil // localPath does not exist
  126. }
  127. filepath.Walk(localPath, func(path string, info os.FileInfo, err error) error {
  128. if err != nil {
  129. errs = append(errs, err.Error())
  130. return err
  131. }
  132. if time.Since(info.ModTime()) > duration {
  133. err := os.Remove(path)
  134. if err != nil {
  135. errs = append(errs, err.Error())
  136. }
  137. cleaned = append(cleaned, path)
  138. }
  139. return nil
  140. })
  141. if len(errs) == 0 {
  142. return cleaned, nil
  143. } else {
  144. return cleaned, fmt.Errorf("deleteFilesOlderThan7d: %v", errs)
  145. }
  146. }