Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for already-existing image before building image #113

Merged
merged 1 commit into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"
"path"

"github.com/bravetools/bravetools/platform"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -40,7 +41,11 @@ func build(cmd *cobra.Command, args []string) {
}

err = host.BuildImage(bravefile)
if err != nil {
switch errType := err.(type) {
case nil:
case *platform.ImageExistsError:
log.Fatalf("image %q already exists - if you want to rebuild it, first delete the existing image with: `brave remove -i [IMAGE]`", errType.Name)
default:
log.Fatal(err)
}
}
6 changes: 6 additions & 0 deletions platform/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,9 @@ func checkUnits(unitName string, bh *BraveHost) error {

return nil
}

func imageExists(imageNameAndVersion string) bool {
homeDir, _ := os.UserHomeDir()
image := path.Join(homeDir, shared.ImageStore, imageNameAndVersion+".tar.gz")
return shared.FileExists(image)
}
14 changes: 14 additions & 0 deletions platform/host_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,14 @@ func (bh *BraveHost) DeleteUnit(name string) error {
return nil
}

type ImageExistsError struct {
Name string
}

func (e *ImageExistsError) Error() string {
return fmt.Sprintf("image %q already exists", e.Name)
}

// BuildImage creates an image based on Bravefile
func (bh *BraveHost) BuildImage(bravefile *shared.Bravefile) error {
if strings.ContainsAny(bravefile.PlatformService.Name, "/_. !@£$%^&*(){}:;`~,?") {
Expand All @@ -544,6 +552,9 @@ func (bh *BraveHost) BuildImage(bravefile *shared.Bravefile) error {
if err != nil {
return err
}
if imageExists(bravefile.PlatformService.Image) {
return &ImageExistsError{Name: bravefile.PlatformService.Image}
}

// Intercept SIGINT, propagate cancel and cleanup artefacts
var imageFingerprint string
Expand Down Expand Up @@ -797,6 +808,9 @@ func (bh *BraveHost) InitUnit(backend Backend, unitParams *shared.Bravefile) (er
if err != nil {
return err
}
if !imageExists(unitParams.PlatformService.Image) {
return fmt.Errorf("image %q does not exist", unitParams.PlatformService.Image)
}

var fingerprint string

Expand Down