diff options
author | Martin Polden <mpolden@mpolden.no> | 2021-08-25 14:17:52 +0200 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2021-08-25 18:56:03 +0200 |
commit | dfa5970405e3f9182bed4fc9f4422eb81aae13d2 (patch) | |
tree | 44d7ebf5969f5dd0e7bd9d7b09d5b40031e1e7f5 /client | |
parent | 1d7f47a7d48fb36d27d6c86d7d0aa8ebdabb753f (diff) |
Extract ApplicationPackage
Diffstat (limited to 'client')
-rw-r--r-- | client/go/vespa/deploy.go | 75 | ||||
-rw-r--r-- | client/go/vespa/deploy_test.go | 46 |
2 files changed, 91 insertions, 30 deletions
diff --git a/client/go/vespa/deploy.go b/client/go/vespa/deploy.go index 26af5112420..dec46a92b00 100644 --- a/client/go/vespa/deploy.go +++ b/client/go/vespa/deploy.go @@ -7,7 +7,7 @@ package vespa import ( "archive/zip" "errors" - "github.com/vespa-engine/vespa/util" + "fmt" "io" "io/ioutil" "net/http" @@ -16,19 +16,56 @@ import ( "path/filepath" "strings" "time" + + "github.com/vespa-engine/vespa/util" ) +type ApplicationPackage struct { + Path string +} + +// Find application package relative given name, which is the path to a file or directory. +func FindApplicationPackage(name string) (ApplicationPackage, error) { + if isZip(name) { + return ApplicationPackage{Path: name}, nil + } + candidates := []string{ + filepath.Join(name, "target", "application.zip"), + filepath.Join(name, "src", "main", "application", "services.xml"), + filepath.Join(name, "services.xml"), + } + for _, path := range candidates { + if !util.PathExists(path) { + continue + } + if !isZip(path) { + path = filepath.Dir(path) + } + return ApplicationPackage{Path: path}, nil + } + return ApplicationPackage{}, fmt.Errorf("no application package found in %s", name) +} + +func (ap *ApplicationPackage) IsZip() bool { return isZip(ap.Path) } + +func (ap *ApplicationPackage) HasCertificate() bool { + if ap.IsZip() { + return true // TODO: Consider looking inside zip to verify + } + return util.PathExists(filepath.Join(ap.Path, "security", "clients.pem")) +} + +func isZip(filename string) bool { return filepath.Ext(filename) == ".zip" } + func Deploy(prepare bool, application string, target string) { - source, noSourceError := determineSource(application) + pkg, noSourceError := FindApplicationPackage(application) if noSourceError != nil { util.Error(noSourceError.Error()) return } - var zippedSource string - if filepath.Ext(source) == ".zip" { - zippedSource = source - } else { // create zip + zippedSource := pkg.Path + if !pkg.IsZip() { // create zip tempZip, error := ioutil.TempFile("", "application.zip") if error != nil { util.Error("Could not create a temporary zip file for the application package") @@ -36,7 +73,7 @@ func Deploy(prepare bool, application string, target string) { return } - error = zipDir(source, tempZip.Name()) + error = zipDir(pkg.Path, tempZip.Name()) if error != nil { util.Error(error.Error()) return @@ -47,7 +84,7 @@ func Deploy(prepare bool, application string, target string) { zipFileReader, zipFileError := os.Open(zippedSource) if zipFileError != nil { - util.Error("Could not open application package at " + source) + util.Error("Could not open application package at " + pkg.Path) util.Detail(zipFileError.Error()) return } @@ -87,28 +124,6 @@ func Deploy(prepare bool, application string, target string) { } } -// Use heuristics to determine the source (directory or zip) of an application package deployment -func determineSource(application string) (string, error) { - if filepath.Ext(application) == ".zip" { - return application, nil - } - if util.PathExists(filepath.Join(application, "target")) { - source := filepath.Join(application, "target", "application.zip") - if !util.PathExists(source) { - return "", errors.New("target/ exists but have no application.zip. Run mvn package first") - } else { - return source, nil - } - } - if util.PathExists(filepath.Join(application, "src", "main", "application")) { - return filepath.Join(application, "src", "main", "application"), nil - } - if util.PathExists(filepath.Join(application, "services.xml")) { - return application, nil - } - return "", errors.New("Could not find an application package source in '" + application + "'") -} - func zipDir(dir string, destination string) error { if filepath.IsAbs(dir) { message := "Path must be relative, but '" + dir + "'" diff --git a/client/go/vespa/deploy_test.go b/client/go/vespa/deploy_test.go new file mode 100644 index 00000000000..cfc52cf92a9 --- /dev/null +++ b/client/go/vespa/deploy_test.go @@ -0,0 +1,46 @@ +package vespa + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFindApplicationPackage(t *testing.T) { + dir := t.TempDir() + var tests = []struct { + in string + out string + fail bool + }{ + {filepath.Join(dir, "foo"), "", true}, + {filepath.Join(dir, "services.xml"), dir, false}, + {filepath.Join(dir, "src", "main", "application", "services.xml"), filepath.Join(dir, "src", "main", "application"), false}, + {filepath.Join(dir, "target", "application.zip"), filepath.Join(dir, "target", "application.zip"), false}, + } + + zipFile := filepath.Join(dir, "application.zip") + writeFile(t, zipFile) + pkg, err := FindApplicationPackage(zipFile) + assert.Nil(t, err) + assert.Equal(t, zipFile, pkg.Path) + + for i, tt := range tests { + writeFile(t, tt.in) + pkg, err := FindApplicationPackage(dir) + if tt.fail { + assert.NotNil(t, err) + } else if pkg.Path != tt.out { + t.Errorf("#%d: FindApplicationPackage(%q) = (%q, %s), want (%q, nil)", i, dir, pkg.Path, err, tt.out) + } + } +} + +func writeFile(t *testing.T, filename string) { + err := os.MkdirAll(filepath.Dir(filename), 0755) + assert.Nil(t, err) + err = os.WriteFile(filename, []byte{0}, 0644) + assert.Nil(t, err) +} |