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 "fmt" 8 "log" 9 "reflect" 10 "strings" 11 "time" 12 13 "go.skia.org/infra/go/cipd" 14 "go.skia.org/infra/task_scheduler/go/specs" 15) 16 17// taskBuilder is a helper for creating a task. 18type taskBuilder struct { 19 *jobBuilder 20 parts 21 Name string 22 Spec *specs.TaskSpec 23 recipeProperties map[string]string 24} 25 26// newTaskBuilder returns a taskBuilder instance. 27func newTaskBuilder(b *jobBuilder, name string) *taskBuilder { 28 parts, err := b.jobNameSchema.ParseJobName(name) 29 if err != nil { 30 log.Fatal(err) 31 } 32 return &taskBuilder{ 33 jobBuilder: b, 34 parts: parts, 35 Name: name, 36 Spec: &specs.TaskSpec{}, 37 recipeProperties: map[string]string{}, 38 } 39} 40 41// attempts sets the desired MaxAttempts for this task. 42func (b *taskBuilder) attempts(a int) { 43 b.Spec.MaxAttempts = a 44} 45 46// cache adds the given caches to the task. 47func (b *taskBuilder) cache(caches ...*specs.Cache) { 48 for _, c := range caches { 49 alreadyHave := false 50 for _, exist := range b.Spec.Caches { 51 if c.Name == exist.Name { 52 if !reflect.DeepEqual(c, exist) { 53 log.Fatalf("Already have cache %s with a different definition!", c.Name) 54 } 55 alreadyHave = true 56 break 57 } 58 } 59 if !alreadyHave { 60 b.Spec.Caches = append(b.Spec.Caches, c) 61 } 62 } 63} 64 65// cmd sets the command for the task. 66func (b *taskBuilder) cmd(c ...string) { 67 b.Spec.Command = c 68} 69 70// dimension adds the given dimensions to the task. 71func (b *taskBuilder) dimension(dims ...string) { 72 for _, dim := range dims { 73 if !In(dim, b.Spec.Dimensions) { 74 b.Spec.Dimensions = append(b.Spec.Dimensions, dim) 75 } 76 } 77} 78 79// expiration sets the expiration of the task. 80func (b *taskBuilder) expiration(e time.Duration) { 81 b.Spec.Expiration = e 82} 83 84// idempotent marks the task as idempotent. 85func (b *taskBuilder) idempotent() { 86 b.Spec.Idempotent = true 87} 88 89// cas sets the CasSpec used by the task. 90func (b *taskBuilder) cas(casSpec string) { 91 b.Spec.CasSpec = casSpec 92} 93 94// env sets the value for the given environment variable for the task. 95func (b *taskBuilder) env(key, value string) { 96 if b.Spec.Environment == nil { 97 b.Spec.Environment = map[string]string{} 98 } 99 b.Spec.Environment[key] = value 100} 101 102// envPrefixes appends the given values to the given environment variable for 103// the task. 104func (b *taskBuilder) envPrefixes(key string, values ...string) { 105 if b.Spec.EnvPrefixes == nil { 106 b.Spec.EnvPrefixes = map[string][]string{} 107 } 108 for _, value := range values { 109 if !In(value, b.Spec.EnvPrefixes[key]) { 110 b.Spec.EnvPrefixes[key] = append(b.Spec.EnvPrefixes[key], value) 111 } 112 } 113} 114 115// addToPATH adds the given locations to PATH for the task. 116func (b *taskBuilder) addToPATH(loc ...string) { 117 b.envPrefixes("PATH", loc...) 118} 119 120// output adds the given paths as outputs to the task, which results in their 121// contents being uploaded to the isolate server. 122func (b *taskBuilder) output(paths ...string) { 123 for _, path := range paths { 124 if !In(path, b.Spec.Outputs) { 125 b.Spec.Outputs = append(b.Spec.Outputs, path) 126 } 127 } 128} 129 130// serviceAccount sets the service account for this task. 131func (b *taskBuilder) serviceAccount(sa string) { 132 b.Spec.ServiceAccount = sa 133} 134 135// timeout sets the timeout(s) for this task. 136func (b *taskBuilder) timeout(timeout time.Duration) { 137 b.Spec.ExecutionTimeout = timeout 138 b.Spec.IoTimeout = timeout // With kitchen, step logs don't count toward IoTimeout. 139} 140 141// dep adds the given tasks as dependencies of this task. 142func (b *taskBuilder) dep(tasks ...string) { 143 for _, task := range tasks { 144 if !In(task, b.Spec.Dependencies) { 145 b.Spec.Dependencies = append(b.Spec.Dependencies, task) 146 } 147 } 148} 149 150// cipd adds the given CIPD packages to the task. 151func (b *taskBuilder) cipd(pkgs ...*specs.CipdPackage) { 152 for _, pkg := range pkgs { 153 alreadyHave := false 154 for _, exist := range b.Spec.CipdPackages { 155 if pkg.Name == exist.Name { 156 if !reflect.DeepEqual(pkg, exist) { 157 log.Fatalf("Already have package %s with a different definition!", pkg.Name) 158 } 159 alreadyHave = true 160 break 161 } 162 } 163 if !alreadyHave { 164 b.Spec.CipdPackages = append(b.Spec.CipdPackages, pkg) 165 } 166 } 167} 168 169// useIsolatedAssets returns true if this task should use assets which are 170// isolated rather than downloading directly from CIPD. 171func (b *taskBuilder) useIsolatedAssets() bool { 172 // Only do this on the RPIs for now. Other, faster machines shouldn't 173 // see much benefit and we don't need the extra complexity, for now. 174 if b.os("ChromeOS", "iOS") || b.matchOs("Android") { 175 return true 176 } 177 return false 178} 179 180// uploadAssetCASCfg represents a task which copies a CIPD package into 181// isolate. 182type uploadAssetCASCfg struct { 183 alwaysIsolate bool 184 uploadTaskName string 185 path string 186} 187 188// assetWithVersion adds the given asset with the given version number to the 189// task as a CIPD package. 190func (b *taskBuilder) assetWithVersion(assetName string, version int) { 191 pkg := &specs.CipdPackage{ 192 Name: fmt.Sprintf("skia/bots/%s", assetName), 193 Path: assetName, 194 Version: fmt.Sprintf("version:%d", version), 195 } 196 b.cipd(pkg) 197} 198 199// asset adds the given assets to the task as CIPD packages. 200func (b *taskBuilder) asset(assets ...string) { 201 shouldIsolate := b.useIsolatedAssets() 202 pkgs := make([]*specs.CipdPackage, 0, len(assets)) 203 for _, asset := range assets { 204 if cfg, ok := ISOLATE_ASSET_MAPPING[asset]; ok && (cfg.alwaysIsolate || shouldIsolate) { 205 b.dep(b.uploadCIPDAssetToCAS(asset)) 206 } else { 207 pkgs = append(pkgs, b.MustGetCipdPackageFromAsset(asset)) 208 } 209 } 210 b.cipd(pkgs...) 211} 212 213// usesCCache adds attributes to tasks which use ccache. 214func (b *taskBuilder) usesCCache() { 215 b.cache(CACHES_CCACHE...) 216} 217 218// usesGit adds attributes to tasks which use git. 219func (b *taskBuilder) usesGit() { 220 b.cache(CACHES_GIT...) 221 if b.matchOs("Win") || b.matchExtraConfig("Win") { 222 b.cipd(specs.CIPD_PKGS_GIT_WINDOWS_AMD64...) 223 } else if b.matchOs("Mac") || b.matchExtraConfig("Mac") { 224 b.cipd(specs.CIPD_PKGS_GIT_MAC_AMD64...) 225 } else { 226 b.cipd(specs.CIPD_PKGS_GIT_LINUX_AMD64...) 227 } 228} 229 230// usesGo adds attributes to tasks which use go. Recipes should use 231// "with api.context(env=api.infra.go_env)". 232func (b *taskBuilder) usesGo() { 233 b.usesGit() // Go requires Git. 234 b.cache(CACHES_GO...) 235 pkg := b.MustGetCipdPackageFromAsset("go") 236 if b.matchOs("Win") || b.matchExtraConfig("Win") { 237 pkg = b.MustGetCipdPackageFromAsset("go_win") 238 pkg.Path = "go" 239 } 240 b.cipd(pkg) 241 b.addToPATH(pkg.Path + "/go/bin") 242 b.envPrefixes("GOROOT", pkg.Path+"/go") 243} 244 245// usesDocker adds attributes to tasks which use docker. 246func (b *taskBuilder) usesDocker() { 247 b.dimension("docker_installed:true") 248 249 // The "docker" binary reads its config from $HOME/.docker/config.json which, after running 250 // "gcloud auth configure-docker", typically looks like this: 251 // 252 // { 253 // "credHelpers": { 254 // "gcr.io": "gcloud", 255 // "us.gcr.io": "gcloud", 256 // "eu.gcr.io": "gcloud", 257 // "asia.gcr.io": "gcloud", 258 // "staging-k8s.gcr.io": "gcloud", 259 // "marketplace.gcr.io": "gcloud" 260 // } 261 // } 262 // 263 // This instructs "docker" to get its GCR credentials from a credential helper [1] program 264 // named "docker-credential-gcloud" [2], which is part of the Google Cloud SDK. This program is 265 // a shell script that invokes the "gcloud" command, which is itself a shell script that probes 266 // the environment to find a viable Python interpreter, and then invokes 267 // /usr/lib/google-cloud-sdk/lib/gcloud.py. For some unknown reason, sometimes "gcloud" decides 268 // to use "/b/s/w/ir/cache/vpython/875f1a/bin/python" as the Python interpreter (exact path may 269 // vary), which causes gcloud.py to fail with the following error: 270 // 271 // ModuleNotFoundError: No module named 'contextlib' 272 // 273 // Fortunately, "gcloud" supports specifying a Python interpreter via the GCLOUDSDK_PYTHON 274 // environment variable. 275 // 276 // [1] https://docs.docker.com/engine/reference/commandline/login/#credential-helpers 277 // [2] See /usr/bin/docker-credential-gcloud on your gLinux system, which is provided by the 278 // google-cloud-sdk package. 279 b.envPrefixes("CLOUDSDK_PYTHON", "cipd_bin_packages/cpython3/bin/python3") 280 281 // As mentioned, Docker uses gcloud for authentication against GCR, and gcloud requires Python. 282 b.usesPython() 283} 284 285// usesGSUtil adds the gsutil dependency from CIPD and puts it on PATH. 286func (b *taskBuilder) usesGSUtil() { 287 b.asset("gsutil") 288 b.addToPATH("gsutil/gsutil") 289} 290 291// recipeProp adds the given recipe property key/value pair. Panics if 292// getRecipeProps() was already called. 293func (b *taskBuilder) recipeProp(key, value string) { 294 if b.recipeProperties == nil { 295 log.Fatal("taskBuilder.recipeProp() cannot be called after taskBuilder.getRecipeProps()!") 296 } 297 b.recipeProperties[key] = value 298} 299 300// recipeProps calls recipeProp for every key/value pair in the given map. 301// Panics if getRecipeProps() was already called. 302func (b *taskBuilder) recipeProps(props map[string]string) { 303 for k, v := range props { 304 b.recipeProp(k, v) 305 } 306} 307 308// getRecipeProps returns JSON-encoded recipe properties. Subsequent calls to 309// recipeProp[s] will panic, to prevent accidentally adding recipe properties 310// after they have been added to the task. 311func (b *taskBuilder) getRecipeProps() string { 312 props := make(map[string]interface{}, len(b.recipeProperties)+2) 313 // TODO(borenet): I'm not sure why we supply the original task name 314 // and not the upload task name. We should investigate whether this is 315 // needed. 316 buildername := b.Name 317 if b.role("Upload") { 318 buildername = strings.TrimPrefix(buildername, "Upload-") 319 } 320 props["buildername"] = buildername 321 props["$kitchen"] = struct { 322 DevShell bool `json:"devshell"` 323 GitAuth bool `json:"git_auth"` 324 }{ 325 DevShell: true, 326 GitAuth: true, 327 } 328 for k, v := range b.recipeProperties { 329 props[k] = v 330 } 331 b.recipeProperties = nil 332 return marshalJson(props) 333} 334 335// cipdPlatform returns the CIPD platform for this task. 336func (b *taskBuilder) cipdPlatform() string { 337 if b.role("Upload") { 338 return cipd.PlatformLinuxAmd64 339 } else if b.matchOs("Win") || b.matchExtraConfig("Win") { 340 return cipd.PlatformWindowsAmd64 341 } else if b.matchOs("Mac") { 342 return cipd.PlatformMacAmd64 343 } else if b.matchArch("Arm64") { 344 return cipd.PlatformLinuxArm64 345 } else if b.matchOs("Android", "ChromeOS") { 346 return cipd.PlatformLinuxArm64 347 } else if b.matchOs("iOS") { 348 return cipd.PlatformLinuxArm64 349 } else { 350 return cipd.PlatformLinuxAmd64 351 } 352} 353 354// usesPython adds attributes to tasks which use python. 355func (b *taskBuilder) usesPython() { 356 pythonPkgs := removePython2(cipd.PkgsPython[b.cipdPlatform()]) 357 b.cipd(pythonPkgs...) 358 b.addToPATH( 359 "cipd_bin_packages/cpython3", 360 "cipd_bin_packages/cpython3/bin", 361 ) 362 b.cache(&specs.Cache{ 363 Name: "vpython", 364 Path: "cache/vpython", 365 }) 366 b.envPrefixes("VPYTHON_VIRTUALENV_ROOT", "cache/vpython") 367 b.env("VPYTHON_LOG_TRACE", "1") 368} 369 370// removePython2 removes all python2 packages from a list of CIPD packages. This can be used to 371// enforce the lack of Python2 dependencies in our tests. 372func removePython2(pyPackages []*cipd.Package) []*cipd.Package { 373 var python3Pkgs []*cipd.Package 374 for _, p := range pyPackages { 375 if strings.HasPrefix(p.Version, "version:2@2.7") { 376 continue 377 } 378 python3Pkgs = append(python3Pkgs, p) 379 } 380 return python3Pkgs 381} 382 383func (b *taskBuilder) usesNode() { 384 // It is very important when including node via CIPD to also add it to the PATH of the 385 // taskdriver or mysterious things can happen when subprocesses try to resolve node/npm. 386 b.asset("node") 387 b.addToPATH("node/node/bin") 388} 389 390func (b *taskBuilder) needsLottiesWithAssets() { 391 // This CIPD package was made by hand with the following invocation: 392 // cipd create -name skia/internal/lotties_with_assets -in ./lotties/ -tag version:2 393 // cipd acl-edit skia/internal/lotties_with_assets -reader group:project-skia-external-task-accounts 394 // cipd acl-edit skia/internal/lotties_with_assets -reader user:pool-skia@chromium-swarm.iam.gserviceaccount.com 395 // Where lotties is a hand-selected set of lottie animations and (optionally) assets used in 396 // them (e.g. fonts, images). 397 // Each test case is in its own folder, with a data.json file and an optional images/ subfolder 398 // with any images/fonts/etc loaded by the animation. 399 // Note: If you are downloading the existing package to update them, remove the CIPD-generated 400 // .cipdpkg subfolder before trying to re-upload it. 401 // Note: It is important that the folder names do not special characters like . (), &, as 402 // the Android filesystem does not support folders with those names well. 403 b.cipd(&specs.CipdPackage{ 404 Name: "skia/internal/lotties_with_assets", 405 Path: "lotties_with_assets", 406 Version: "version:4", 407 }) 408} 409