Skip to content

Commit

Permalink
testutil/compose: safe clean (#601)
Browse files Browse the repository at this point in the history
Make `compose clean` safe, by checking if config.json is present (and double check that no go files are present).

Note: I deleted my whole charon repo by running `compose clean` in the wrong folder  🤦 

Also pull latest docker image in define step.

category: bug 
ticket: #568
  • Loading branch information
corverroos authored May 24, 2022
1 parent 6e537c4 commit 5c4dd41
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions testutil/compose/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
defaultNumNodes = 4
defaultThreshold = 3

charonImage = "ghcr.io/obolnetwork/charon"
localBinary = "/compose/charon"
containerBinary = "/usr/local/bin/charon"
cmdRun = "run"
Expand Down
41 changes: 41 additions & 0 deletions testutil/compose/define.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ func Clean(ctx context.Context, dir string) error {
return errors.Wrap(err, "glob dir")
}

// Make sure we ONLY delete compose artifacts.
var (
configFound bool
goFound bool
)
for _, file := range files {
if file == configFile {
configFound = true
} else if strings.HasSuffix(file, ".go") || strings.HasPrefix(file, "go.") {
goFound = true
}
}
if !configFound {
log.Info(ctx, "Not cleaning since config.json not found")
return nil
} else if goFound {
return errors.New("go files found, compose dir incorrect", z.Str("dir", dir))
}

log.Info(ctx, "Cleaning compose dir", z.Int("files", len(files)))

for _, file := range files {
Expand Down Expand Up @@ -79,6 +98,12 @@ func Define(ctx context.Context, dir string) error {
}
}

if conf.ImageTag == "latest" {
if err := pullLatest(ctx); err != nil {
return err
}
}

var data tmplData
if conf.KeyGen == keyGenDKG {
log.Info(ctx, "Creating node*/p2pkey for ENRs required for charon create dkg")
Expand Down Expand Up @@ -138,6 +163,7 @@ func Define(ctx context.Context, dir string) error {

log.Info(ctx, "Creating config.json")

conf.Step = stepDefined
if err := writeConfig(dir, conf); err != nil {
return err
}
Expand All @@ -152,6 +178,21 @@ func Define(ctx context.Context, dir string) error {
return writeDockerCompose(dir, data)
}

// pullLatest pulls the latest charon docker image.
func pullLatest(ctx context.Context) error {
log.Info(ctx, "Pulling latest charon docker image")

cmd := exec.CommandContext(ctx, "docker", "pull", charonImage+":latest")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

if err := cmd.Run(); err != nil {
return errors.Wrap(err, "run docker pull")
}

return nil
}

// buildLocal builds a local charon binary and writes it to the cluster dir. Note this requires CHARON_REPO env var.
func buildLocal(ctx context.Context, dir string) error {
repo, ok := os.LookupEnv("CHARON_REPO")
Expand Down
4 changes: 4 additions & 0 deletions testutil/compose/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import (

// New creates a new compose config file from flags.
func New(ctx context.Context, dir string, conf Config) error {
if err := Clean(ctx, dir); err != nil {
return err
}

conf.Step = stepNew

log.Info(ctx, "Writing config to compose dir",
Expand Down

0 comments on commit 5c4dd41

Please sign in to comment.