aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/internal/vespa/application.go
blob: cb43578af32c576c275cd50a68929cf5966b3637 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package vespa

import (
	"archive/zip"
	"errors"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"strings"

	"github.com/vespa-engine/vespa/client/go/internal/util"
)

type ApplicationPackage struct {
	Path     string
	TestPath string
}

func (ap *ApplicationPackage) HasCertificate() bool { return ap.hasFile("security", "clients.pem") }

func (ap *ApplicationPackage) HasDeploymentSpec() bool { return ap.hasFile("deployment.xml", "") }

func (ap *ApplicationPackage) hasFile(pathSegment ...string) bool {
	if !ap.IsZip() {
		return util.PathExists(filepath.Join(append([]string{ap.Path}, pathSegment...)...))
	}
	zipName := filepath.Join(pathSegment...)
	return ap.hasZipEntry(func(name string) bool { return zipName == name })
}

func (ap *ApplicationPackage) hasZipEntry(matcher func(zipName string) bool) bool {
	r, err := zip.OpenReader(ap.Path)
	if err != nil {
		return false
	}
	defer r.Close()
	for _, f := range r.File {
		if matcher(f.Name) {
			return true
		}
	}
	return false
}

func (ap *ApplicationPackage) IsZip() bool { return isZip(ap.Path) }

func (ap *ApplicationPackage) IsJava() bool {
	if ap.IsZip() {
		return ap.hasZipEntry(func(name string) bool { return filepath.Ext(name) == ".jar" })
	}
	return util.PathExists(filepath.Join(ap.Path, "pom.xml"))
}

func (ap *ApplicationPackage) Validate() error {
	if !ap.IsZip() {
		return nil
	}
	invalidPath := ""
	invalid := ap.hasZipEntry(func(name string) bool {
		if !validPath(name) {
			invalidPath = name
			return true
		}
		return false
	})
	if invalid {
		return fmt.Errorf("found invalid path inside zip: %s", invalidPath)
	}
	return nil
}

func isZip(filename string) bool { return filepath.Ext(filename) == ".zip" }

func zipDir(dir string, destination string) error {
	if !util.PathExists(dir) {
		message := "'" + dir + "' should be an application package zip or dir, but does not exist"
		return errors.New(message)
	}
	if !util.IsDirectory(dir) {
		message := "'" + dir + "' should be an application package dir, but is a (non-zip) file"
		return errors.New(message)
	}

	file, err := os.Create(destination)
	if err != nil {
		message := "Could not create a temporary zip file for the application package: " + err.Error()
		return errors.New(message)
	}
	defer file.Close()

	w := zip.NewWriter(file)
	defer w.Close()

	walker := func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if ignorePackageFile(filepath.Base(path)) {
			if info.IsDir() {
				return filepath.SkipDir
			}
			return nil
		}
		if info.IsDir() {
			return nil
		}
		file, err := os.Open(path)
		if err != nil {
			return err
		}
		defer file.Close()

		zippath, err := filepath.Rel(dir, path)
		if err != nil {
			return err
		}
		zipfile, err := w.Create(zippath)
		if err != nil {
			return err
		}

		_, err = io.Copy(zipfile, file)
		if err != nil {
			return err
		}
		return nil
	}
	return filepath.Walk(dir, walker)
}

func ignorePackageFile(name string) bool {
	switch name {
	case ".DS_Store":
		return true
	}
	return false
}

func (ap *ApplicationPackage) zipReader(test bool) (io.ReadCloser, error) {
	zipFile := ap.Path
	if test {
		zipFile = ap.TestPath
	}
	if !ap.IsZip() {
		tempZip, err := os.CreateTemp("", "vespa")
		if err != nil {
			return nil, fmt.Errorf("could not create a temporary zip file for the application package: %w", err)
		}
		defer func() {
			tempZip.Close()
			os.Remove(tempZip.Name())
			// TODO: Caller must remove temporary file
		}()
		if err := zipDir(zipFile, tempZip.Name()); err != nil {
			return nil, err
		}
		zipFile = tempZip.Name()
	}
	f, err := os.Open(zipFile)
	if err != nil {
		return nil, fmt.Errorf("could not open application package at %s: %w", ap.Path, err)
	}
	return f, nil
}

func (ap *ApplicationPackage) Unzip(test bool) (string, error) {
	if !ap.IsZip() {
		return "", fmt.Errorf("can't unzip a package that is a directory structure")
	}
	cleanTemp := true
	tmp, err := os.MkdirTemp(os.TempDir(), "vespa-test-pkg")
	if err != nil {
		return "", err
	}
	defer func() {
		if cleanTemp {
			os.RemoveAll(tmp)
		}
	}()
	path := ap.Path
	if test {
		path = ap.TestPath
	}
	f, err := zip.OpenReader(path)
	if err != nil {
		return "", err
	}
	defer f.Close()
	for _, f := range f.File {
		dst := filepath.Join(tmp, f.Name)
		if f.FileInfo().IsDir() {
			if err := os.Mkdir(dst, f.FileInfo().Mode()); err != nil {
				return "", err
			}
			continue
		}
		if err := copyFile(f, dst); err != nil {
			return "", fmt.Errorf("copying %s to %s failed: %w", f.Name, dst, err)
		}
	}
	cleanTemp = false
	return tmp, nil
}

func (ap *ApplicationPackage) HasTests() bool { return ap.TestPath != "" }

func validPath(path string) bool {
	path = strings.TrimSuffix(path, "/")
	if filepath.Clean(path) != path {
		return false
	}
	for _, part := range strings.Split(path, "/") {
		if part == ".." {
			return false
		}
	}
	return true
}

func copyFile(src *zip.File, dst string) error {
	from, err := src.Open()
	if err != nil {
		return err
	}
	defer from.Close()
	to, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, src.FileInfo().Mode())
	if err != nil {
		return err
	}
	defer to.Close()
	_, err = io.Copy(to, from)
	return err
}

// FindApplicationPackage finds the path to an application package from the zip file or directory zipOrDir. If
// requirePackaging is true, the application package is required to be packaged with mvn package.
//
// Package to use is preferred in this order:
// 1. Given path, if it's a zip
// 2. target/application
// 3. src/main/application
// 4. Given path, if it contains services.xml
func FindApplicationPackage(zipOrDir string, requirePackaging bool) (ApplicationPackage, error) {
	pkg, err := findApplicationPackage(zipOrDir, requirePackaging)
	if err != nil {
		return ApplicationPackage{}, err
	}
	if err := pkg.Validate(); err != nil {
		return ApplicationPackage{}, err
	}
	return pkg, nil
}

func findApplicationPackage(zipOrDir string, requirePackaging bool) (ApplicationPackage, error) {
	if isZip(zipOrDir) {
		return ApplicationPackage{Path: zipOrDir}, nil
	}
	// Pre-packaged application. We prefer the uncompressed application because this allows us to add
	// security/clients.pem to the package on-demand
	hasPOM := util.PathExists(filepath.Join(zipOrDir, "pom.xml"))
	if hasPOM {
		path := filepath.Join(zipOrDir, "target", "application")
		if util.PathExists(path) {
			testPath := existingPath(filepath.Join(zipOrDir, "target", "application-test"))
			return ApplicationPackage{Path: path, TestPath: testPath}, nil
		}
		if requirePackaging {
			return ApplicationPackage{}, fmt.Errorf("found pom.xml, but %s does not exist: run 'mvn package' first", path)
		}
	}
	// Application with Maven directory structure, but with no POM or no hard requirement on packaging
	if path := filepath.Join(zipOrDir, "src", "main", "application"); util.PathExists(path) {
		testPath := existingPath(filepath.Join(zipOrDir, "src", "test", "application"))
		return ApplicationPackage{Path: path, TestPath: testPath}, nil
	}
	// Application without Java components
	if util.PathExists(filepath.Join(zipOrDir, "services.xml")) {
		testPath := ""
		if util.PathExists(filepath.Join(zipOrDir, "tests")) {
			testPath = zipOrDir
		}
		return ApplicationPackage{Path: zipOrDir, TestPath: testPath}, nil
	}
	return ApplicationPackage{}, fmt.Errorf("could not find an application package source in '%s'", zipOrDir)
}

func existingPath(path string) string {
	if util.PathExists(path) {
		return path
	}
	return ""
}