1// Copyright 2020 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4package gen_tasks_logic 5 6import ( 7 "log" 8 "reflect" 9 "strings" 10 "time" 11 12 "go.skia.org/infra/go/cipd" 13 "go.skia.org/infra/task_scheduler/go/specs" 14) 15 16// taskBuilder is a helper for creating a task. 17type taskBuilder struct { 18 *jobBuilder 19 parts 20 Name string 21 Spec *specs.TaskSpec 22 recipeProperties map[string]string 23} 24 25// newTaskBuilder returns a taskBuilder instance. 26func newTaskBuilder(b *jobBuilder, name string) *taskBuilder { 27 parts, err := b.jobNameSchema.ParseJobName(name) 28 if err != nil { 29 log.Fatal(err) 30 } 31 return &taskBuilder{ 32 jobBuilder: b, 33 parts: parts, 34 Name: name, 35 Spec: &specs.TaskSpec{}, 36 recipeProperties: map[string]string{}, 37 } 38} 39 40// attempts sets the desired MaxAttempts for this task. 41func (b *taskBuilder) attempts(a int) { 42 b.Spec.MaxAttempts = a 43} 44 45// cache adds the given caches to the task. 46func (b *taskBuilder) cache(caches ...*specs.Cache) { 47 for _, c := range caches { 48 alreadyHave := false 49 for _, exist := range b.Spec.Caches { 50 if c.Name == exist.Name { 51 if !reflect.DeepEqual(c, exist) { 52 log.Fatalf("Already have cache %s with a different definition!", c.Name) 53 } 54 alreadyHave = true 55 break 56 } 57 } 58 if !alreadyHave { 59 b.Spec.Caches = append(b.Spec.Caches, c) 60 } 61 } 62} 63 64// cmd sets the command for the task. 65func (b *taskBuilder) cmd(c ...string) { 66 b.Spec.Command = c 67} 68 69// dimension adds the given dimensions to the task. 70func (b *taskBuilder) dimension(dims ...string) { 71 for _, dim := range dims { 72 if !In(dim, b.Spec.Dimensions) { 73 b.Spec.Dimensions = append(b.Spec.Dimensions, dim) 74 } 75 } 76} 77 78// expiration sets the expiration of the task. 79func (b *taskBuilder) expiration(e time.Duration) { 80 b.Spec.Expiration = e 81} 82 83// idempotent marks the task as idempotent. 84func (b *taskBuilder) idempotent() { 85 b.Spec.Idempotent = true 86} 87 88// cas sets the CasSpec used by the task. 89func (b *taskBuilder) cas(casSpec string) { 90 b.Spec.CasSpec = casSpec 91} 92 93// env appends the given values to the given environment variable for the task. 94func (b *taskBuilder) env(key string, values ...string) { 95 if b.Spec.EnvPrefixes == nil { 96 b.Spec.EnvPrefixes = map[string][]string{} 97 } 98 for _, value := range values { 99 if !In(value, b.Spec.EnvPrefixes[key]) { 100 b.Spec.EnvPrefixes[key] = append(b.Spec.EnvPrefixes[key], value) 101 } 102 } 103} 104 105// addToPATH adds the given locations to PATH for the task. 106func (b *taskBuilder) addToPATH(loc ...string) { 107 b.env("PATH", loc...) 108} 109 110// output adds the given paths as outputs to the task, which results in their 111// contents being uploaded to the isolate server. 112func (b *taskBuilder) output(paths ...string) { 113 for _, path := range paths { 114 if !In(path, b.Spec.Outputs) { 115 b.Spec.Outputs = append(b.Spec.Outputs, path) 116 } 117 } 118} 119 120// serviceAccount sets the service account for this task. 121func (b *taskBuilder) serviceAccount(sa string) { 122 b.Spec.ServiceAccount = sa 123} 124 125// timeout sets the timeout(s) for this task. 126func (b *taskBuilder) timeout(timeout time.Duration) { 127 b.Spec.ExecutionTimeout = timeout 128 b.Spec.IoTimeout = timeout // With kitchen, step logs don't count toward IoTimeout. 129} 130 131// dep adds the given tasks as dependencies of this task. 132func (b *taskBuilder) dep(tasks ...string) { 133 for _, task := range tasks { 134 if !In(task, b.Spec.Dependencies) { 135 b.Spec.Dependencies = append(b.Spec.Dependencies, task) 136 } 137 } 138} 139 140// cipd adds the given CIPD packages to the task. 141func (b *taskBuilder) cipd(pkgs ...*specs.CipdPackage) { 142 for _, pkg := range pkgs { 143 alreadyHave := false 144 for _, exist := range b.Spec.CipdPackages { 145 if pkg.Name == exist.Name { 146 if !reflect.DeepEqual(pkg, exist) { 147 log.Fatalf("Already have package %s with a different definition!", pkg.Name) 148 } 149 alreadyHave = true 150 break 151 } 152 } 153 if !alreadyHave { 154 b.Spec.CipdPackages = append(b.Spec.CipdPackages, pkg) 155 } 156 } 157} 158 159// useIsolatedAssets returns true if this task should use assets which are 160// isolated rather than downloading directly from CIPD. 161func (b *taskBuilder) useIsolatedAssets() bool { 162 // Only do this on the RPIs for now. Other, faster machines shouldn't 163 // see much benefit and we don't need the extra complexity, for now. 164 if b.os("Android", "ChromeOS", "iOS") { 165 return true 166 } 167 return false 168} 169 170// uploadAssetCASCfg represents a task which copies a CIPD package into 171// isolate. 172type uploadAssetCASCfg struct { 173 alwaysIsolate bool 174 uploadTaskName string 175 path string 176} 177 178// asset adds the given assets to the task as CIPD packages. 179func (b *taskBuilder) asset(assets ...string) { 180 shouldIsolate := b.useIsolatedAssets() 181 pkgs := make([]*specs.CipdPackage, 0, len(assets)) 182 for _, asset := range assets { 183 if cfg, ok := ISOLATE_ASSET_MAPPING[asset]; ok && (cfg.alwaysIsolate || shouldIsolate) { 184 b.dep(b.uploadCIPDAssetToCAS(asset)) 185 } else { 186 pkgs = append(pkgs, b.MustGetCipdPackageFromAsset(asset)) 187 } 188 } 189 b.cipd(pkgs...) 190} 191 192// usesCCache adds attributes to tasks which use ccache. 193func (b *taskBuilder) usesCCache() { 194 b.cache(CACHES_CCACHE...) 195} 196 197// usesGit adds attributes to tasks which use git. 198func (b *taskBuilder) usesGit() { 199 b.cache(CACHES_GIT...) 200 if b.matchOs("Win") || b.matchExtraConfig("Win") { 201 b.cipd(specs.CIPD_PKGS_GIT_WINDOWS_AMD64...) 202 } else if b.matchOs("Mac") || b.matchExtraConfig("Mac") { 203 b.cipd(specs.CIPD_PKGS_GIT_MAC_AMD64...) 204 } else { 205 b.cipd(specs.CIPD_PKGS_GIT_LINUX_AMD64...) 206 } 207} 208 209// usesGo adds attributes to tasks which use go. Recipes should use 210// "with api.context(env=api.infra.go_env)". 211func (b *taskBuilder) usesGo() { 212 b.usesGit() // Go requires Git. 213 b.cache(CACHES_GO...) 214 pkg := b.MustGetCipdPackageFromAsset("go") 215 if b.matchOs("Win") || b.matchExtraConfig("Win") { 216 pkg = b.MustGetCipdPackageFromAsset("go_win") 217 pkg.Path = "go" 218 } 219 b.cipd(pkg) 220 b.addToPATH(pkg.Path + "/go/bin") 221 b.env("GOROOT", pkg.Path+"/go") 222} 223 224// usesDocker adds attributes to tasks which use docker. 225func (b *taskBuilder) usesDocker() { 226 b.dimension("docker_installed:true") 227} 228 229// recipeProp adds the given recipe property key/value pair. Panics if 230// getRecipeProps() was already called. 231func (b *taskBuilder) recipeProp(key, value string) { 232 if b.recipeProperties == nil { 233 log.Fatal("taskBuilder.recipeProp() cannot be called after taskBuilder.getRecipeProps()!") 234 } 235 b.recipeProperties[key] = value 236} 237 238// recipeProps calls recipeProp for every key/value pair in the given map. 239// Panics if getRecipeProps() was already called. 240func (b *taskBuilder) recipeProps(props map[string]string) { 241 for k, v := range props { 242 b.recipeProp(k, v) 243 } 244} 245 246// getRecipeProps returns JSON-encoded recipe properties. Subsequent calls to 247// recipeProp[s] will panic, to prevent accidentally adding recipe properties 248// after they have been added to the task. 249func (b *taskBuilder) getRecipeProps() string { 250 props := make(map[string]interface{}, len(b.recipeProperties)+2) 251 // TODO(borenet): I'm not sure why we supply the original task name 252 // and not the upload task name. We should investigate whether this is 253 // needed. 254 buildername := b.Name 255 if b.role("Upload") { 256 buildername = strings.TrimPrefix(buildername, "Upload-") 257 } 258 props["buildername"] = buildername 259 props["$kitchen"] = struct { 260 DevShell bool `json:"devshell"` 261 GitAuth bool `json:"git_auth"` 262 }{ 263 DevShell: true, 264 GitAuth: true, 265 } 266 for k, v := range b.recipeProperties { 267 props[k] = v 268 } 269 b.recipeProperties = nil 270 return marshalJson(props) 271} 272 273// cipdPlatform returns the CIPD platform for this task. 274func (b *taskBuilder) cipdPlatform() string { 275 if b.role("Upload") { 276 return cipd.PlatformLinuxAmd64 277 } else if b.matchOs("Win") || b.matchExtraConfig("Win") { 278 return cipd.PlatformWindowsAmd64 279 } else if b.matchOs("Mac") { 280 return cipd.PlatformMacAmd64 281 } else if b.matchArch("Arm64") { 282 return cipd.PlatformLinuxArm64 283 } else if b.matchOs("Android", "ChromeOS") { 284 return cipd.PlatformLinuxArm64 285 } else if b.matchOs("iOS") { 286 return cipd.PlatformLinuxArmv6l 287 } else { 288 return cipd.PlatformLinuxAmd64 289 } 290} 291 292// usesPython adds attributes to tasks which use python. 293func (b *taskBuilder) usesPython() { 294 pythonPkgs := cipd.PkgsPython[b.cipdPlatform()] 295 b.cipd(pythonPkgs...) 296 b.addToPATH( 297 "cipd_bin_packages/cpython", 298 "cipd_bin_packages/cpython/bin", 299 "cipd_bin_packages/cpython3", 300 "cipd_bin_packages/cpython3/bin", 301 ) 302 b.cache(&specs.Cache{ 303 Name: "vpython", 304 Path: "cache/vpython", 305 }) 306 b.env("VPYTHON_VIRTUALENV_ROOT", "cache/vpython") 307 b.env("VPYTHON_LOG_TRACE", "1") 308} 309 310func (b *taskBuilder) usesNode() { 311 // It is very important when including node via CIPD to also add it to the PATH of the 312 // taskdriver or mysterious things can happen when subprocesses try to resolve node/npm. 313 b.asset("node") 314 b.addToPATH("node/node/bin") 315} 316