-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathpackages.go
332 lines (284 loc) · 9.67 KB
/
packages.go
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package builder
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/magefile/mage/sh"
"github.com/elastic/elastic-package/internal/environment"
"github.com/elastic/elastic-package/internal/files"
"github.com/elastic/elastic-package/internal/logger"
"github.com/elastic/elastic-package/internal/packages"
"github.com/elastic/elastic-package/internal/validation"
)
const builtPackagesFolder = "packages"
const licenseTextFileName = "LICENSE.txt"
var repositoryLicenseEnv = environment.WithElasticPackagePrefix("REPOSITORY_LICENSE")
type BuildOptions struct {
PackageRoot string
CreateZip bool
SignPackage bool
SkipValidation bool
}
// BuildDirectory function locates the target build directory. If the directory doesn't exist, it will create it.
func BuildDirectory() (string, error) {
buildDir, found, err := findBuildDirectory()
if err != nil {
return "", fmt.Errorf("can't locate build directory: %w", err)
}
if !found {
buildDir, err = createBuildDirectory()
if err != nil {
return "", fmt.Errorf("can't create new build directory: %w", err)
}
}
return buildDir, nil
}
func findBuildDirectory() (string, bool, error) {
workDir, err := os.Getwd()
if err != nil {
return "", false, fmt.Errorf("can't locate build directory: %w", err)
}
dir := workDir
// required for multi platform support
root := fmt.Sprintf("%s%c", filepath.VolumeName(dir), os.PathSeparator)
for dir != "." {
path := filepath.Join(dir, "build")
fileInfo, err := os.Stat(path)
if err == nil && fileInfo.IsDir() {
return path, true, nil
}
if dir == root {
break
}
dir = filepath.Dir(dir)
}
return "", false, nil
}
// BuildPackagesDirectory function locates the target build directory for the package.
func BuildPackagesDirectory(packageRoot string) (string, error) {
buildDir, err := buildPackagesRootDirectory()
if err != nil {
return "", fmt.Errorf("can't locate build packages root directory: %w", err)
}
m, err := packages.ReadPackageManifestFromPackageRoot(packageRoot)
if err != nil {
return "", fmt.Errorf("reading package manifest failed (path: %s): %w", packageRoot, err)
}
return filepath.Join(buildDir, m.Name, m.Version), nil
}
// buildPackagesZipPath function locates the target zipped package path.
func buildPackagesZipPath(packageRoot string) (string, error) {
buildDir, err := buildPackagesRootDirectory()
if err != nil {
return "", fmt.Errorf("can't locate build packages root directory: %w", err)
}
m, err := packages.ReadPackageManifestFromPackageRoot(packageRoot)
if err != nil {
return "", fmt.Errorf("reading package manifest failed (path: %s): %w", packageRoot, err)
}
return ZippedBuiltPackagePath(buildDir, *m), nil
}
// ZippedBuiltPackagePath function returns the path to zipped built package.
func ZippedBuiltPackagePath(buildDir string, m packages.PackageManifest) string {
return filepath.Join(buildDir, fmt.Sprintf("%s-%s.zip", m.Name, m.Version))
}
func buildPackagesRootDirectory() (string, error) {
buildDir, found, err := FindBuildPackagesDirectory()
if err != nil {
return "", fmt.Errorf("can't locate build directory: %w", err)
}
if !found {
buildDir, err = createBuildDirectory(builtPackagesFolder)
if err != nil {
return "", fmt.Errorf("can't create new build directory: %w", err)
}
}
return buildDir, nil
}
// FindBuildPackagesDirectory function locates the target build directory for packages.
func FindBuildPackagesDirectory() (string, bool, error) {
buildDir, found, err := findBuildDirectory()
if err != nil {
return "", false, err
}
if found {
path := filepath.Join(buildDir, builtPackagesFolder)
fileInfo, err := os.Stat(path)
if errors.Is(err, os.ErrNotExist) {
return "", false, nil
}
if err != nil {
return "", false, err
}
if fileInfo.IsDir() {
return path, true, nil
}
}
return "", false, nil
}
// BuildPackage function builds the package.
func BuildPackage(options BuildOptions) (string, error) {
destinationDir, err := BuildPackagesDirectory(options.PackageRoot)
if err != nil {
return "", fmt.Errorf("can't locate build directory: %w", err)
}
logger.Debugf("Build directory: %s\n", destinationDir)
logger.Debugf("Clear target directory (path: %s)", destinationDir)
err = files.ClearDir(destinationDir)
if err != nil {
return "", fmt.Errorf("clearing package contents failed: %w", err)
}
logger.Debugf("Copy package content (source: %s)", options.PackageRoot)
err = files.CopyWithoutDev(options.PackageRoot, destinationDir)
if err != nil {
return "", fmt.Errorf("copying package contents failed: %w", err)
}
logger.Debug("Copy license file if needed")
err = copyLicenseTextFile(filepath.Join(destinationDir, licenseTextFileName))
if err != nil {
return "", fmt.Errorf("copying license text file: %w", err)
}
logger.Debug("Encode dashboards")
err = encodeDashboards(destinationDir)
if err != nil {
return "", fmt.Errorf("encoding dashboards failed: %w", err)
}
logger.Debug("Resolve external fields")
err = resolveExternalFields(options.PackageRoot, destinationDir)
if err != nil {
return "", fmt.Errorf("resolving external fields failed: %w", err)
}
err = addDynamicMappings(options.PackageRoot, destinationDir)
if err != nil {
return "", fmt.Errorf("adding dynamic mappings: %w", err)
}
logger.Debug("Include linked files")
links, err := files.IncludeLinkedFiles(options.PackageRoot, destinationDir)
if err != nil {
return "", fmt.Errorf("including linked files failed: %w", err)
}
for _, l := range links {
logger.Debugf("Linked file included (path: %s)", l.TargetFilePath(destinationDir))
}
if options.CreateZip {
return buildZippedPackage(options, destinationDir)
}
if options.SkipValidation {
logger.Debug("Skip validation of the built package")
return destinationDir, nil
}
logger.Debugf("Validating built package (path: %s)", destinationDir)
errs, skipped := validation.ValidateAndFilterFromPath(destinationDir)
if skipped != nil {
logger.Infof("Skipped errors: %v", skipped)
}
if errs != nil {
return "", fmt.Errorf("invalid content found in built package: %w", errs)
}
return destinationDir, nil
}
func buildZippedPackage(options BuildOptions, destinationDir string) (string, error) {
logger.Debug("Build zipped package")
zippedPackagePath, err := buildPackagesZipPath(options.PackageRoot)
if err != nil {
return "", fmt.Errorf("can't evaluate path for the zipped package: %w", err)
}
err = files.Zip(destinationDir, zippedPackagePath)
if err != nil {
return "", fmt.Errorf("can't compress the built package (compressed file path: %s): %w", zippedPackagePath, err)
}
if options.SkipValidation {
logger.Debug("Skip validation of the built .zip package")
} else {
logger.Debugf("Validating built .zip package (path: %s)", zippedPackagePath)
errs, skipped := validation.ValidateAndFilterFromZip(zippedPackagePath)
if skipped != nil {
logger.Infof("Skipped errors: %v", skipped)
}
if errs != nil {
return "", fmt.Errorf("invalid content found in built zip package: %w", errs)
}
}
if options.SignPackage {
err := signZippedPackage(options, zippedPackagePath)
if err != nil {
return "", err
}
}
return zippedPackagePath, nil
}
func signZippedPackage(options BuildOptions, zippedPackagePath string) error {
logger.Debug("Sign the package")
m, err := packages.ReadPackageManifestFromPackageRoot(options.PackageRoot)
if err != nil {
return fmt.Errorf("reading package manifest failed (path: %s): %w", options.PackageRoot, err)
}
err = files.Sign(zippedPackagePath, files.SignOptions{
PackageName: m.Name,
PackageVersion: m.Version,
})
if err != nil {
return fmt.Errorf("can't sign the zipped package (path: %s): %w", zippedPackagePath, err)
}
return nil
}
func copyLicenseTextFile(licensePath string) error {
_, err := os.Stat(licensePath)
if err == nil {
logger.Debug("License file in the package will be used")
return nil
}
repositoryLicenseTextFileName, userDefined := os.LookupEnv(repositoryLicenseEnv)
if !userDefined {
repositoryLicenseTextFileName = licenseTextFileName
}
sourceLicensePath, err := findRepositoryLicense(repositoryLicenseTextFileName)
if !userDefined && errors.Is(err, os.ErrNotExist) {
logger.Debug("No license text file is included in package")
return nil
}
if err != nil {
return fmt.Errorf("failure while looking for license %q in repository: %w", repositoryLicenseTextFileName, err)
}
logger.Infof("License text found in %q will be included in package", sourceLicensePath)
err = sh.Copy(licensePath, sourceLicensePath)
if err != nil {
return fmt.Errorf("can't copy license from repository: %w", err)
}
return nil
}
func createBuildDirectory(dirs ...string) (string, error) {
dir, err := files.FindRepositoryRootDirectory()
if errors.Is(err, os.ErrNotExist) {
return "", errors.New("package can be only built inside of a Git repository (.git folder is used as reference point)")
}
if err != nil {
return "", err
}
p := []string{dir, "build"}
if len(dirs) > 0 {
p = append(p, dirs...)
}
buildDir := filepath.Join(p...)
err = os.MkdirAll(buildDir, 0755)
if err != nil {
return "", fmt.Errorf("mkdir failed (path: %s): %w", buildDir, err)
}
return buildDir, nil
}
func findRepositoryLicense(licenseTextFileName string) (string, error) {
dir, err := files.FindRepositoryRootDirectory()
if err != nil {
return "", err
}
sourceFileName := filepath.Join(dir, licenseTextFileName)
_, err = os.Stat(sourceFileName)
if err != nil {
return "", fmt.Errorf("failed to find repository license: %w", err)
}
return sourceFileName, nil
}