Skip to content

Commit

Permalink
take-out implement zero byte files
Browse files Browse the repository at this point in the history
  • Loading branch information
metron2 committed Jan 11, 2022
1 parent 018c189 commit 7292287
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
8 changes: 4 additions & 4 deletions cmd/opts/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,10 @@ type InterpolateArgs struct {
}

type TakeOutOpts struct {
Args TakeOutArgs `positional-args:"true" required:"true"`
MirrorPrefix string `long:"mirror-prefix" short:"m" description:"Mirror prefix" optional:"true" default:"file:"`
StemcellType string `long:"stemcell-type" short:"t" description:"Stemcell type" optional:"true" default:"vsphere-esxi"`

Args TakeOutArgs `positional-args:"true" required:"true"`
MirrorPrefix string `long:"mirror-prefix" short:"m" description:"Mirror prefix" optional:"true" default:"file:"`
StemcellType string `long:"stemcell-type" short:"t" description:"Stemcell type" optional:"true" default:"vsphere-esxi"`
CreatePlaceholders bool `long:"create-placeholders" short:"p" description:"Create zero byte release and stemfill files (useful for diffs)"`
VarFlags
OpsFlags

Expand Down
4 changes: 2 additions & 2 deletions cmd/take_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ func (c TakeOutCmd) Run(opts TakeOutOpts) error {
c.ui.PrintLinef("Release does not have a URL for take_out; Name: %s / %s", r.Name, r.Version)
return bosherr.WrapErrorf(nil, "Provide an opsfile that has a URL or removes this release") // TODO
} else {
o, err := c.to.TakeOutRelease(r, c.ui, opts.MirrorPrefix)
o, err := c.to.TakeOutRelease(r, c.ui, opts)
if err != nil {
return err
}
releaseChanges = append(releaseChanges, o)
}
}
for _, s := range deployment.Stemcells {
err := c.to.TakeOutStemcell(s, c.ui, opts.StemcellType)
err := c.to.TakeOutStemcell(s, c.ui, opts)
if err != nil {

}
Expand Down
7 changes: 4 additions & 3 deletions takeout/interfaces.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package takeout

import (
"github.com/cloudfoundry/bosh-cli/cmd/opts"
boshdir "github.com/cloudfoundry/bosh-cli/director"
boshui "github.com/cloudfoundry/bosh-cli/ui"
)
Expand All @@ -23,14 +24,14 @@ type DeploymentReader interface {
}

type ReleaseDownloader interface {
RetrieveRelease(r boshdir.ManifestRelease, ui boshui.UI, localFileName string) (err error)
RetrieveRelease(r boshdir.ManifestRelease, ui boshui.UI, localFileName string, opts opts.TakeOutOpts) (err error)
}

type StemcellDownloader interface {
TakeOutStemcell(s boshdir.ManifestReleaseStemcell, ui boshui.UI, stemCellType string) (err error)
TakeOutStemcell(s boshdir.ManifestReleaseStemcell, ui boshui.UI, opts opts.TakeOutOpts) (err error)
RetrieveStemcell(ui boshui.UI, s boshdir.ManifestReleaseStemcell, localFileName string, stemCellType string) (err error)
}

type OpFileGenerator interface {
TakeOutRelease(r boshdir.ManifestRelease, ui boshui.UI, mirrorPrefix string) (entry OpEntry, err error)
TakeOutRelease(r boshdir.ManifestRelease, ui boshui.UI, opts opts.TakeOutOpts) (entry OpEntry, err error)
}
3 changes: 2 additions & 1 deletion takeout/takeoutfakes/takeoutfakes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package takeoutfakes
import (
"errors"
"fmt"
. "github.com/cloudfoundry/bosh-cli/cmd/opts"
boshdir "github.com/cloudfoundry/bosh-cli/director"
"github.com/cloudfoundry/bosh-cli/takeout"
boshui "github.com/cloudfoundry/bosh-cli/ui"
Expand All @@ -17,7 +18,7 @@ func (c FakeUtensils) Reset() {
c.RetrieveMap = make(map[boshdir.ManifestRelease]string)
}

func (c FakeUtensils) RetrieveRelease(r boshdir.ManifestRelease, ui boshui.UI, localFileName string) (err error) {
func (c FakeUtensils) RetrieveRelease(r boshdir.ManifestRelease, _ boshui.UI, localFileName string, _ TakeOutOpts) (err error) {
if val, ok := c.RetrieveMap[r]; ok {
return errors.New(fmt.Sprintf("Release already in download map: %s", val))
}
Expand Down
25 changes: 16 additions & 9 deletions takeout/takeoutrelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package takeout
import (
"crypto/sha1"
"fmt"
"github.com/cloudfoundry/bosh-cli/cmd/opts"
boshdir "github.com/cloudfoundry/bosh-cli/director"
boshui "github.com/cloudfoundry/bosh-cli/ui"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
Expand All @@ -18,12 +19,11 @@ var BadChar = regexp.MustCompile("[?=\"]")
type RealUtensils struct {
}

func (c RealUtensils) RetrieveRelease(r boshdir.ManifestRelease, ui boshui.UI, localFileName string) (err error) {
func (c RealUtensils) RetrieveRelease(r boshdir.ManifestRelease, ui boshui.UI, localFileName string, _ opts.TakeOutOpts) (err error) {
ui.PrintLinef("Downloading release: %s / %s -> %s", r.Name, r.Version, localFileName)

tempFileName := localFileName + ".download"


resp, err := http.Get(r.URL)

if resp != nil {
Expand Down Expand Up @@ -69,13 +69,16 @@ func (c RealUtensils) RetrieveRelease(r boshdir.ManifestRelease, ui boshui.UI, l
return nil
}

func (c RealUtensils) TakeOutStemcell(s boshdir.ManifestReleaseStemcell, ui boshui.UI, stemCellType string) (err error) {
func (c RealUtensils) TakeOutStemcell(s boshdir.ManifestReleaseStemcell, ui boshui.UI, opts opts.TakeOutOpts) (err error) {

localFileName := fmt.Sprintf("bosh-%s-%s-go_agent-stemcell_v%s.tgz", stemCellType, s.OS, s.Version)
localFileName := fmt.Sprintf("bosh-%s-%s-go_agent-stemcell_v%s.tgz", opts.StemcellType, s.OS, s.Version)

if _, err := os.Stat(localFileName); os.IsNotExist(err) {

err := c.RetrieveStemcell(ui, s, localFileName, stemCellType)
if opts.CreatePlaceholders {
_, err = os.Create(localFileName)
} else {
err = c.RetrieveStemcell(ui, s, localFileName, opts.StemcellType)
}
if err != nil {
return err
}
Expand Down Expand Up @@ -121,13 +124,17 @@ func (c RealUtensils) RetrieveStemcell(ui boshui.UI, s boshdir.ManifestReleaseSt
return err
}

func (c RealUtensils) TakeOutRelease(r boshdir.ManifestRelease, ui boshui.UI, mirrorPrefix string) (entry OpEntry, err error) {
func (c RealUtensils) TakeOutRelease(r boshdir.ManifestRelease, ui boshui.UI, opts opts.TakeOutOpts) (entry OpEntry, err error) {

// generate a local file name that's safe
localFileName := BadChar.ReplaceAllString(fmt.Sprintf("%s_v%s.tgz", r.Name, r.Version), "_")

if _, err := os.Stat(localFileName); os.IsNotExist(err) {
err = c.RetrieveRelease(r, ui, localFileName)
if opts.CreatePlaceholders {
_, err = os.Create(localFileName)
} else {
err = c.RetrieveRelease(r, ui, localFileName, opts)
}
if err != nil {
return OpEntry{}, err
}
Expand All @@ -136,7 +143,7 @@ func (c RealUtensils) TakeOutRelease(r boshdir.ManifestRelease, ui boshui.UI, mi
}
if len(r.Name) > 0 {
path := fmt.Sprintf("/releases/name=%s/url", r.Name)
localFile := fmt.Sprintf("%s%s", mirrorPrefix, localFileName)
localFile := fmt.Sprintf("%s%s", opts.MirrorPrefix, localFileName)
entry = OpEntry{Type: "replace", Path: path, Value: localFile}
}
return entry, err
Expand Down

0 comments on commit 7292287

Please sign in to comment.