-
Notifications
You must be signed in to change notification settings - Fork 18
/
cache.go
241 lines (194 loc) · 7.06 KB
/
cache.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
package command
import (
"fmt"
"github.com/cirruslabs/cirrus-ci-agent/api"
"github.com/cirruslabs/cirrus-cli/pkg/parser/nameable"
"github.com/cirruslabs/cirrus-cli/pkg/parser/node"
"github.com/cirruslabs/cirrus-cli/pkg/parser/parseable"
"github.com/cirruslabs/cirrus-cli/pkg/parser/parserkit"
"github.com/cirruslabs/cirrus-cli/pkg/parser/schema"
jsschema "github.com/lestrrat-go/jsschema"
"strings"
)
type CacheCommand struct {
proto *api.Command
instruction *api.CacheInstruction
reuploadOnChangesExplicitlySet bool
folder string
folders []string
parseable.DefaultParser
}
func NewCacheCommand(mergedEnv map[string]string, parserKit *parserkit.ParserKit) *CacheCommand {
cache := &CacheCommand{
proto: &api.Command{},
instruction: &api.CacheInstruction{
ReuploadOnChanges: true,
},
}
cache.OptionalField(nameable.NewSimpleNameable("name"), schema.String(""), func(node *node.Node) error {
name, err := node.GetExpandedStringValue(mergedEnv)
if err != nil {
return err
}
cache.proto.Name = name
return nil
})
folderSchema := schema.String("Path of a folder to cache.")
cache.OptionalField(nameable.NewSimpleNameable("folder"), folderSchema, func(node *node.Node) error {
folder, err := node.GetExpandedStringValue(mergedEnv)
if err != nil {
return err
}
cache.folder = folder
return nil
})
foldersSchema := schema.StringOrListOfStrings("A list of folders to cache.")
cache.OptionalField(nameable.NewSimpleNameable("folders"), foldersSchema, func(node *node.Node) error {
folders, err := node.GetSliceOfExpandedStrings(mergedEnv)
if err != nil {
return err
}
cache.folders = folders
return nil
})
fpScriptSchema := schema.Script("Script that is used to calculate cache key.")
cache.OptionalField(nameable.NewSimpleNameable("fingerprint_script"), fpScriptSchema, func(node *node.Node) error {
scripts, err := node.GetScript()
if err != nil {
return err
}
cache.instruction.FingerprintScripts = scripts
// Disable the default "dumb" re-upload behavior unless otherwise specified by the user since
// we now have a better way of figuring out whether we need to upload the cache or not
if !cache.reuploadOnChangesExplicitlySet {
cache.instruction.ReuploadOnChanges = false
}
return nil
})
fpKeySchema := schema.String("Cache key in it's raw form.")
cache.OptionalField(nameable.NewSimpleNameable("fingerprint_key"), fpKeySchema, func(node *node.Node) error {
key, err := node.GetExpandedStringValue(mergedEnv)
if err != nil {
return err
}
cache.instruction.FingerprintKey = key
// Disable the default "dumb" re-upload behavior unless otherwise specified by the user since
// we now have a better way of figuring out whether we need to upload the cache or not
if !cache.reuploadOnChangesExplicitlySet {
cache.instruction.ReuploadOnChanges = false
}
return nil
})
populateSchema := schema.Script("In case of a cache miss this script will be executed.")
cache.OptionalField(nameable.NewSimpleNameable("populate_script"), populateSchema, func(node *node.Node) error {
scripts, err := node.GetScript()
if err != nil {
return err
}
cache.instruction.PopulateScripts = scripts
return nil
})
reuploadSchema := schema.Condition("A flag to check if contents of folder has changed after a cache hit.")
cache.OptionalField(nameable.NewSimpleNameable("reupload_on_changes"), reuploadSchema, func(node *node.Node) error {
evaluation, err := node.GetBoolValue(mergedEnv, parserKit.Boolevator)
if err != nil {
return err
}
cache.instruction.ReuploadOnChanges = evaluation
cache.reuploadOnChangesExplicitlySet = true
return nil
})
return cache
}
func (cache *CacheCommand) Parse(node *node.Node, parserKit *parserkit.ParserKit) error {
if err := cache.DefaultParser.Parse(node, parserKit); err != nil {
return err
}
if cache.proto.Name == "" {
cacheNameable := nameable.NewRegexNameable("^(.*)cache$")
cache.proto.Name = cacheNameable.FirstGroupOrDefault(node.Name, "main")
}
// Deal with cache folders
if cache.folder == "" && len(cache.folders) == 0 {
return node.ParserError("please specify the folders to cache, with either folder: or folders:")
} else if cache.folder != "" && len(cache.folders) != 0 {
return node.ParserError("please specify either folder: or folders: but not both, to avoid ambiguity")
}
var folders []string
if cache.folder != "" {
folders = []string{cache.folder}
} else {
folders = cache.folders
}
cache.instruction.Folders = fixFolders(folders)
// fingerprint_script and fingerprint_key are mutually exclusive,
// ensure that we don't allow such ambiguities as early as possible
// (in terms of build pipeline, not this method)
if len(cache.instruction.FingerprintScripts) != 0 && cache.instruction.FingerprintKey != "" {
return node.ParserError("please either use fingerprint_script: or fingerprint_key, since otherwise " +
"there's ambiguity about which one to prefer for cache key calculation")
}
return nil
}
func (cache *CacheCommand) Proto() *api.Command {
cache.proto.Instruction = &api.Command_CacheInstruction{
CacheInstruction: cache.instruction,
}
return cache.proto
}
func (cache *CacheCommand) Schema() *jsschema.Schema {
modifiedSchema := cache.DefaultParser.Schema()
modifiedSchema.Type = jsschema.PrimitiveTypes{jsschema.ObjectType}
modifiedSchema.Description = "Folder Cache Definition."
modifiedSchema.OneOf = jsschema.SchemaList{
&jsschema.Schema{
Required: []string{"folder"},
AdditionalItems: &jsschema.AdditionalItems{Schema: nil},
AdditionalProperties: &jsschema.AdditionalProperties{Schema: nil},
},
&jsschema.Schema{
Required: []string{"folders"},
AdditionalItems: &jsschema.AdditionalItems{Schema: nil},
AdditionalProperties: &jsschema.AdditionalProperties{Schema: nil},
},
}
return modifiedSchema
}
func GenUploadCacheCmds(commands []*api.Command) (result []*api.Command) {
// Don't generate any cache upload instructions if "upload_caches" instruction was used,
// in which case it'd insert at least one cache upload instruction.
for _, command := range commands {
if _, ok := command.Instruction.(*api.Command_UploadCacheInstruction); ok {
return []*api.Command{}
}
}
for _, command := range commands {
_, ok := command.Instruction.(*api.Command_CacheInstruction)
if !ok {
continue
}
uploadCommand := &api.Command{
Name: fmt.Sprintf("Upload '%s' cache", command.Name),
Instruction: &api.Command_UploadCacheInstruction{
UploadCacheInstruction: &api.UploadCacheInstruction{
CacheName: command.Name,
},
},
ExecutionBehaviour: command.ExecutionBehaviour,
}
result = append(result, uploadCommand)
}
return
}
func fixFolders(folders []string) (result []string) {
for _, folder := range folders {
// https://github.com/cirruslabs/cirrus-ci-agent/issues/47
const homePrefix = "~/"
folder = strings.TrimSpace(folder)
if strings.HasPrefix(folder, homePrefix) {
folder = "$HOME/" + strings.TrimPrefix(folder, homePrefix)
}
result = append(result, folder)
}
return
}