Skip to content
This repository has been archived by the owner on Oct 29, 2023. It is now read-only.

Commit

Permalink
Fix bug during 'dbdeployer init'
Browse files Browse the repository at this point in the history
When the sandbox-binary directory existed, but empty, the download was skipped.
  • Loading branch information
datacharmer committed Aug 7, 2020
1 parent 95edf9b commit 2fce607
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 13 deletions.
21 changes: 21 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ package cmd

import (
"fmt"
"io/ioutil"
"os"
"path"
"runtime"
"strings"

Expand Down Expand Up @@ -79,6 +81,25 @@ func initEnvironment(cmd *cobra.Command, args []string) error {
fmt.Printf("\n%s\n", globals.DashLine)
if common.DirExists(sandboxBinary) {
fmt.Printf("Directory %s ($SANDBOX_BINARY) already exists\n", sandboxBinary)
files, err := ioutil.ReadDir(sandboxBinary)
if err != nil {
return fmt.Errorf("error reading sandbox binary directory %s: %s", sandboxBinary, err)
}
// Sandbox binary directory exists.
// We now check whether there is any expanded tarball directory
numSandboxes := 0
for _, f := range files {
if f.IsDir() {
bin := path.Join(sandboxBinary, f.Name(), "bin")
if common.DirExists(bin) {
numSandboxes++
}
}
}
if numSandboxes == 0 {
needDownload = true
}

} else {

if !dryRun {
Expand Down
17 changes: 16 additions & 1 deletion common/strutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,29 @@ func ReplaceLiteralHome(path string) string {
// for example, if "$HOME" resolves to "/home/goofy" the string "/home/goofy/some/path" would become "$HOME/some/path"
func ReplaceLiteralEnvVar(name string, envVar string) string {
value := os.Getenv(envVar)
re := regexp.MustCompile(value)
// If the environment variable is empty, we return the initial name
if value == "" {
return name
}
// If the current location is at the top of the directory tree, we don't want to do any replacement
if value == "/" {
return name
}
// If there is already a variable in the name, no further replacement is needed
if strings.Contains(name, "$") {
return name
}
re := regexp.MustCompile(`^` + value)
return re.ReplaceAllString(name, "$$"+envVar)
}

// Replaces the environment variable `envVar` with its value
// for example, if "$HOME" resolves to "/home/goofy" the string "$HOME/some/path" would become "/home/goofy/some/path"
func ReplaceEnvVar(name string, envVar string) string {
value := os.Getenv(envVar)
if value == "" || value == "/" {
return name
}
re := regexp.MustCompile(`\$` + envVar + `\b`)
return re.ReplaceAllString(name, value)
}
Expand Down
27 changes: 18 additions & 9 deletions common/strutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
)

type pathInfo struct {
home string
pwd string
value string
envVar string
expected string
Expand All @@ -33,36 +35,43 @@ type pathInfo struct {
func TestReplaceLiteralHome(t *testing.T) {
saveHome := os.Getenv("HOME")
savePWD := os.Getenv("PWD")
os.Setenv("HOME", "/home/Groucho")
os.Setenv("PWD", "/var/lib/MarxBrothers")
groucho := "/home/Groucho"
brothers := "/var/lib/MarxBrothers"
var paths = []pathInfo{
{"/home/Groucho/", "HOME", "$HOME/"},
{"/home/Groucho/path1/path2", "HOME", "$HOME/path1/path2"},
{"/home/Harpo/path1/path2", "HOME", "/home/Harpo/path1/path2"},
{"/var/lib/MarxBrothers/path1/path2", "PWD", "$PWD/path1/path2"},
{"/var/lib/MarxCousins/path1/path2", "PWD", "/var/lib/MarxCousins/path1/path2"},
{groucho, brothers, "/home/Groucho/", "HOME", "$HOME/"},
{groucho, brothers, "/home/Groucho/path1/path2", "HOME", "$HOME/path1/path2"},
{groucho, brothers, "/home/Harpo/path1/path2", "HOME", "/home/Harpo/path1/path2"},
{groucho, brothers, "/var/lib/MarxBrothers/path1/path2", "PWD", "$PWD/path1/path2"},
{groucho, brothers, "/var/lib/MarxCousins/path1/path2", "PWD", "/var/lib/MarxCousins/path1/path2"},
{groucho, "/", "/var/lib/MarxCousins/path1/path2", "PWD", "/var/lib/MarxCousins/path1/path2"},
{groucho, "", "/var/lib/MarxCousins/path1/path2", "PWD", "/var/lib/MarxCousins/path1/path2"},
{groucho, brothers, "$PWD/home/Groucho/path2", "HOME", "$PWD/home/Groucho/path2"},
}
for _, p := range paths {
os.Setenv("HOME", p.home)
os.Setenv("PWD", p.pwd)
value := p.value
envVar := p.envVar
expected := p.expected
canary := ReplaceLiteralEnvVar(value, envVar)
if expected == canary {
t.Logf("ok %-35s %-10s =--> %-25s\n", value, "("+envVar+")", expected)
} else {
t.Logf("NOT OK %-35s %-10s =--> %-25s\n", value, "("+envVar+")", expected)
t.Logf("NOT OK - got %-35s %-10s =--> want %-25s\n", value, "("+envVar+")", expected)
t.Fail()
}
}
for _, p := range paths {
os.Setenv("HOME", p.home)
os.Setenv("PWD", p.pwd)
value := p.expected
envVar := p.envVar
expected := p.value
canary := ReplaceEnvVar(value, envVar)
if expected == canary {
t.Logf("ok %-35s %-10s --=> %-25s\n", value, "("+envVar+")", expected)
} else {
t.Logf("NOT OK %-35s %-10s --=> %-25s\n", value, "("+envVar+")", expected)
t.Logf("NOT OK - got %-35s %-10s --=> want %-25s\n", value, "("+envVar+")", expected)
t.Fail()
}
}
Expand Down
6 changes: 3 additions & 3 deletions common/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
package common

// This file was generated during build. Do not edit.
// Build time: 2020-07-26 19:40
// Build time: 2020-08-07 10:52

var VersionDef string = "1.53.1" // 2020-07-26
var VersionDef string = "1.53.2" // 2020-08-07

// Compatible version is the version used to mark compatible archives (templates, configuration).
// It is usually major.minor.0, except when we are at version 0.x, when
// every revision may bring incompatibility
var CompatibleVersion string = "1.53.0" // 2020-07-26
var CompatibleVersion string = "1.53.0" // 2020-08-07

0 comments on commit 2fce607

Please sign in to comment.