Skip to content

Commit

Permalink
download product should support blobstore caching
Browse files Browse the repository at this point in the history
Signed-off-by: Kira Boyle <kirab@vmware.com>
  • Loading branch information
JT Archie authored and kcboyle committed Jul 22, 2020
1 parent b056e87 commit bd54130
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ can be found in [Pivotal Documentation](docs.pivotal.io/platform-automation).
This ensures that commands are not kept in `bash` history.
The environment variable `OM_PASSWORD` will overwrite the password value in `env.yml`.

## 6.0.1

### Bug Fixes
- `download-product` will now correctly cache if downloading from a blobstore
when `CACHE_CLEANUP='I acknowledge this will delete files in the output directories'`
is set.

## 6.0.0

### Features
Expand Down
14 changes: 10 additions & 4 deletions commands/download_product.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,15 +381,15 @@ func (c *DownloadProduct) downloadProductFile(slug, version, glob, prefixPath st
if exist {
c.stderr.Printf("%s already exists, skip downloading", productFilePath)

err = c.cleanupCacheArtifacts(outputDir, glob, productFilePath)
err = c.cleanupCacheArtifacts(outputDir, glob, productFilePath, slug)
if err != nil {
return "", nil, fmt.Errorf("could not cleanup cache: %w", err)
}

return productFilePath, fileArtifact, nil
}

err = c.cleanupCacheArtifacts(outputDir, glob, productFilePath)
err = c.cleanupCacheArtifacts(outputDir, glob, productFilePath, slug)
if err != nil {
return "", nil, fmt.Errorf("could not cleanup cache: %w", err)
}
Expand Down Expand Up @@ -423,17 +423,23 @@ func (c *DownloadProduct) downloadProductFile(slug, version, glob, prefixPath st
return productFilePath, fileArtifact, nil
}

func (c *DownloadProduct) cleanupCacheArtifacts(outputDir string, glob string, productFilePath string) error {
func (c *DownloadProduct) cleanupCacheArtifacts(outputDir string, glob string, productFilePath string, slug string) error {
if c.Options.CacheCleanup == "I acknowledge this will delete files in the output directories" {
c.stderr.Println("Cleaning up cached artifacts...")
var prefixedGlob string
if c.Options.Source != "pivnet" || c.Options.Bucket == "" {
prefixedGlob = glob
} else {
prefixedGlob = fmt.Sprintf("\\[%s,*\\]%s", slug, glob)
}
outputDirContents, err := ioutil.ReadDir(outputDir)
if err != nil {
return err
}

for _, file := range outputDirContents {
dirFilePath := path.Join(outputDir, file.Name())
if matchGlob, _ := filepath.Match(glob, file.Name()); matchGlob {
if matchGlob, _ := filepath.Match(prefixedGlob, file.Name()); matchGlob {
if dirFilePath != productFilePath {
_ = os.Remove(dirFilePath)
}
Expand Down
34 changes: 34 additions & 0 deletions commands/download_product_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,40 @@ var _ = Describe("DownloadProduct", func() {
Expect(previousDownloadedStemcell).ToNot(BeAnExistingFile())
})
})

When("--s3 bucket is provided", func() {
BeforeEach(func() {
commandArgs = []string{
"--pivnet-api-token", "token",
"--file-glob", "cf*.pivotal",
"--pivnet-product-slug", "elastic-runtime",
"--product-version", "2.0.0",
"--output-directory", productOutputDir,
"--stemcell-output-directory", stemcellOutputDir,
"--stemcell-iaas", "google",
"--s3-bucket", "there once was a man from a",
}

})

It("only deletes files that match the glob of the product and stemcell(s), ignoring the download prefix", func() {
alreadyDownloadedProduct := tempFile(productOutputDir, "[elastic-runtime,1.0.0]cf*.pivotal")
alreadyDownloadedLightStemcell := tempFile(stemcellOutputDir, "[stemcells-ubuntu-xenial,96.00]light-bosh-google-*.tgz")
unknownFileWeDontOwn := tempFile(productOutputDir, "no-delete")

err = command.Execute(commandArgs)
Expect(err).ToNot(HaveOccurred())

downloadedFilePath := path.Join(productOutputDir, "[elastic-runtime,2.0.0]cf-2.0-build.1.pivotal")
downloadedStemcellFilePath := path.Join(stemcellOutputDir, "[stemcells-ubuntu-xenial,97.190]stemcell.tgz")
Expect(downloadedFilePath).To(BeAnExistingFile())
Expect(downloadedStemcellFilePath).To(BeAnExistingFile())

Expect(alreadyDownloadedProduct).ToNot(BeAnExistingFile())
Expect(alreadyDownloadedLightStemcell).ToNot(BeAnExistingFile())
Expect(unknownFileWeDontOwn).To(BeAnExistingFile())
})
})
})
})

Expand Down

0 comments on commit bd54130

Please sign in to comment.