mirrored from https://chromium.googlesource.com/infra/luci/luci-go
/
pkgdef.go
339 lines (299 loc) · 9.65 KB
/
pkgdef.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
333
334
335
336
337
338
339
// Copyright 2015 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package builder
import (
"io"
"path/filepath"
"regexp"
"sort"
"gopkg.in/yaml.v2"
"go.chromium.org/luci/common/errors"
"go.chromium.org/luci/cipd/client/cipd/fs"
"go.chromium.org/luci/cipd/client/cipd/pkg"
"go.chromium.org/luci/cipd/common"
"go.chromium.org/luci/cipd/common/cipderr"
)
// PackageDef defines how exactly to build a package.
//
// It specified what files to put into it, how to name them, how to name
// the package itself, etc. It is loaded from *.yaml file.
type PackageDef struct {
// Package defines a name of the package.
Package string
// Root defines where to search for files. It may either be an absolute path,
// or it may be a path relative to the package file itself. If omitted, it
// defaults to "." (i.e., the same directory as the package file)
Root string
// InstallMode defines how to deploy the package file: "copy" or "symlink".
InstallMode pkg.InstallMode `yaml:"install_mode"`
// PreserveModTime instructs CIPD to preserve the mtime of the files.
PreserveModTime bool `yaml:"preserve_mtime"`
// PreserveWritable instructs CIPD to preserve the user-writable permission
// mode on the files.
PreserveWritable bool `yaml:"preserve_writable"`
// Data describes what is deployed with the package.
Data []PackageChunkDef
}
// PackageChunkDef represents one entry in 'data' section of package definition.
//
// It is either a single file, or a recursively scanned directory (with optional
// list of regexps for files to skip).
type PackageChunkDef struct {
// Dir is a directory to add to the package (recursively).
Dir string
// File is a single file to add to the package.
File string
// VersionFile defines where to drop JSON file with package version.
VersionFile string `yaml:"version_file"`
// Exclude is a list of regexp patterns to exclude when scanning a directory.
Exclude []string
}
// LoadPackageDef loads package definition from a YAML source code.
//
// It substitutes %{...} strings in the definition with corresponding values
// from 'vars' map.
func LoadPackageDef(r io.Reader, vars map[string]string) (PackageDef, error) {
data, err := io.ReadAll(r)
if err != nil {
return PackageDef{}, errors.Annotate(err, "reading package definition file").Tag(cipderr.IO).Err()
}
out := PackageDef{}
if err = yaml.Unmarshal(data, &out); err != nil {
return PackageDef{}, errors.Annotate(err, "bad package definition file").Tag(cipderr.BadArgument).Err()
}
// Substitute variables in all strings.
for _, str := range out.strings() {
*str, err = subVars(*str, vars)
if err != nil {
return PackageDef{}, err
}
}
// Validate global package properties.
if err = common.ValidatePackageName(out.Package); err != nil {
return PackageDef{}, err
}
if err = pkg.ValidateInstallMode(out.InstallMode); err != nil {
return PackageDef{}, err
}
versionFile := ""
for i, chunk := range out.Data {
// Make sure 'dir' and 'file' etc. aren't used together.
has := make([]string, 0, 3)
if chunk.File != "" {
has = append(has, "file")
}
if chunk.VersionFile != "" {
has = append(has, "version_file")
}
if chunk.Dir != "" {
has = append(has, "dir")
}
if len(has) == 0 {
return out, errors.Reason("files entry #%d needs 'file', 'dir' or 'version_file' key", i).Tag(cipderr.BadArgument).Err()
}
if len(has) != 1 {
return out, errors.Reason("files entry #%d should have only one key, got %q", i, has).Tag(cipderr.BadArgument).Err()
}
//'version_file' can appear only once, it must be a clean relative path.
if chunk.VersionFile != "" {
if versionFile != "" {
return out, errors.Reason("'version_file' entry can be used only once").Tag(cipderr.BadArgument).Err()
}
versionFile = chunk.VersionFile
if !fs.IsCleanSlashPath(versionFile) {
return out, errors.Reason("'version_file' must be a path relative to the package root: %s", versionFile).Tag(cipderr.BadArgument).Err()
}
}
}
// Default 'root' to a directory with the package def file.
if out.Root == "" {
out.Root = "."
}
return out, nil
}
// FindFiles scans files system and returns files to be added to the package.
//
// It uses a path to package definition file directory ('cwd' argument) to find
// a root of the package.
func (def *PackageDef) FindFiles(cwd string) ([]fs.File, error) {
// Root of the package is defined relative to package def YAML file.
absCwd, err := filepath.Abs(cwd)
if err != nil {
return nil, errors.Annotate(err, "bad input directory").Tag(cipderr.BadArgument).Err()
}
root := filepath.Clean(def.Root)
if !filepath.IsAbs(root) {
root = filepath.Join(absCwd, root)
}
// Helper to get absolute path to a file given path relative to root.
makeAbs := func(p string) string {
return filepath.Join(root, filepath.FromSlash(p))
}
// Used to skip duplicates.
seen := map[string]fs.File{}
add := func(f fs.File) {
if seen[f.Name()] == nil {
seen[f.Name()] = f
}
}
scanOpts := fs.ScanOptions{
PreserveModTime: def.PreserveModTime,
PreserveWritable: def.PreserveWritable,
}
for _, chunk := range def.Data {
// Handled elsewhere.
if chunk.VersionFile != "" {
continue
}
// Individual file.
if chunk.File != "" {
file, err := fs.WrapFile(makeAbs(chunk.File), root, nil, scanOpts)
if err != nil {
return nil, err
}
add(file)
continue
}
// A subdirectory to scan (with filtering).
if chunk.Dir != "" {
// Absolute path to directory to scan.
startDir := makeAbs(chunk.Dir)
// Exclude files as specified in 'exclude' section.
exclude, err := makeExclusionFilter(chunk.Exclude)
if err != nil {
return nil, errors.Annotate(err, "dir %q", chunk.Dir).Err()
}
// Run the scan.
files, err := fs.ScanFileSystem(startDir, root, exclude, scanOpts)
if err != nil {
return nil, errors.Annotate(err, "dir %q", chunk.Dir).Err()
}
for _, f := range files {
add(f)
}
continue
}
// LoadPackageDef does validation, so this should not happen.
return nil, errors.Reason("unexpected definition: %v", chunk).Tag(cipderr.BadArgument).Err()
}
// Sort by Name().
names := make([]string, 0, len(seen))
for n := range seen {
names = append(names, n)
}
sort.Strings(names)
// Final sorted array of fs.File.
out := make([]fs.File, 0, len(names))
for _, n := range names {
out = append(out, seen[n])
}
return out, nil
}
// VersionFile defines where to drop JSON file with package version.
func (def *PackageDef) VersionFile() string {
// It is already validated by LoadPackageDef, so just return it.
for _, chunk := range def.Data {
if chunk.VersionFile != "" {
return chunk.VersionFile
}
}
return ""
}
// makeExclusionFilter produces a predicate that checks a relative file path
// against a list of regexps and returns true to exclude it.
//
// Note that regexps are defined against slash-separated relative paths (to make
// the package definition YAML platform-agnostic).
func makeExclusionFilter(patterns []string) (fs.ScanFilter, error) {
if len(patterns) == 0 {
return nil, nil
}
// Compile regular expressions. Note that we want to verify that each
// individual pattern is a valid regexp. For that reason we don't just
// concatenate them in a single uber-regexp and compile it afterwards.
exps := []*regexp.Regexp{}
for _, expr := range patterns {
if expr == "" {
continue
}
if expr[0] != '^' {
expr = "^" + expr
}
if expr[len(expr)-1] != '$' {
expr = expr + "$"
}
re, err := regexp.Compile(expr)
if err != nil {
return nil, errors.Annotate(err, "bad exclusion pattern").Tag(cipderr.BadArgument).Err()
}
exps = append(exps, re)
}
return func(rel string) bool {
rel = filepath.ToSlash(rel)
for _, exp := range exps {
if exp.MatchString(rel) {
return true
}
}
return false
}, nil
}
////////////////////////////////////////////////////////////////////////////////
// Variable substitution.
var subVarsRe = regexp.MustCompile(`\$\{[^\}]+\}`)
// strings return array of pointers to all strings in PackageDef that can
// contain ${var} variables.
func (def *PackageDef) strings() []*string {
out := []*string{
&def.Package,
&def.Root,
}
// Important to use index here, to get a point to a real object, not its copy.
for i := range def.Data {
out = append(out, def.Data[i].strings()...)
}
return out
}
// strings return array of pointers to all strings in PackageChunkDef that can
// contain ${var} variables.
func (def *PackageChunkDef) strings() []*string {
out := []*string{
&def.Dir,
&def.File,
&def.VersionFile,
}
for i := range def.Exclude {
out = append(out, &def.Exclude[i])
}
return out
}
// subVars replaces "${key}" in strings with values from 'vars' map. Returns
// error if some keys weren't found in 'vars' map.
func subVars(s string, vars map[string]string) (string, error) {
var badKeys []string
res := subVarsRe.ReplaceAllStringFunc(s, func(match string) string {
// Strip '${' and '}'.
key := match[2 : len(match)-1]
val, ok := vars[key]
if !ok {
badKeys = append(badKeys, key)
return match
}
return val
})
if len(badKeys) != 0 {
return res, errors.Reason("values for some variables are not provided: %v", badKeys).Tag(cipderr.BadArgument).Err()
}
return res, nil
}