Debian Codegraph @

infra/bots/task_drivers/recreate_skps/recreate_skps.go

raw
1 // Copyright 2021 Google Inc.
2 //
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 package main
7
8 import (
9 "context"
10 "flag"
11 "fmt"
12 "os"
13 "path/filepath"
14 "strings"
15
16 "go.skia.org/infra/go/common"
17 "go.skia.org/infra/go/depot_tools"
18 "go.skia.org/infra/go/exec"
19 "go.skia.org/infra/go/gerrit"
20 "go.skia.org/infra/go/git"
21 "go.skia.org/infra/go/git/git_common"
22 "go.skia.org/infra/go/skerr"
23 "go.skia.org/infra/go/sklog"
24 "go.skia.org/infra/promk/go/pushgateway"
25 "go.skia.org/infra/task_driver/go/lib/auth_steps"
26 "go.skia.org/infra/task_driver/go/lib/os_steps"
27 "go.skia.org/infra/task_driver/go/td"
28 )
29
30 const (
31 // Metric constants for pushgateway.
32 jobName = "recreate-skps"
33 buildFailureMetricName = "recreate_skps_build_failure"
34 creatingSKPsFailureMetricName = "recreate_skps_creation_failure"
35 metricValue_NoFailure = "0"
36 metricValue_Failure = "1"
37 )
38
39 func botUpdate(ctx context.Context, checkoutRoot, gitCacheDir, skiaRev, patchRef, depotToolsDir string, local bool) error {
40 return td.Do(ctx, td.Props("bot_update").Infra(), func(ctx context.Context) error {
41 tmp, err := os_steps.TempDir(ctx, "", "")
42 if err != nil {
43 return err
44 }
45 defer func() {
46 _ = os_steps.RemoveAll(ctx, tmp)
47 }()
48 ctx = td.WithEnv(ctx, []string{
49 "DEPOT_TOOLS_COLLECT_METRICS",
50 "DEPOT_TOOLS_UPDATE=0",
51 "GIT_CONFIG_NOSYSTEM: 1",
52 "GIT_HTTP_LOW_SPEED_LIMIT: 102400",
53 "GIT_HTTP_LOW_SPEED_TIME: 1800",
54 "GIT_TERMINAL_PROMPT: 0",
55 "GIT_USER_AGENT=",
56 })
57 if _, err := exec.RunCwd(ctx, ".", "which", "git"); err != nil {
58 return err
59 }
60 if _, err := exec.RunCwd(ctx, ".", "git", "--version"); err != nil {
61 return err
62 }
63 if !local {
64 if err := git_common.EnsureGitIsFromCIPD(ctx); err != nil {
65 return err
66 }
67 }
68
69 spec := `
70 solutions = [
71 { "name" : 'src',
72 "url" : 'https://chromium.googlesource.com/chromium/src.git',
73 "deps_file" : 'DEPS',
74 "managed" : False,
75 "custom_deps" : {
76 },
77 "custom_vars": {},
78 },
79 ]
80 `
81 specPath := filepath.Join(checkoutRoot, ".gclient")
82 if err := os_steps.WriteFile(ctx, specPath, []byte(spec), os.ModePerm); err != nil {
83 return err
84 }
85 skiaRepoURL := "https://skia.googlesource.com/skia.git"
86 botUpdateScript := filepath.Join(depotToolsDir, "recipes", "recipe_modules", "bot_update", "resources", "bot_update.py")
87 mainRepoName := "src"
88 patchRoot := "src/third_party/skia"
89 revision := "origin/main"
90 // These are required for some reason, despite our not using them.
91 outputJson := filepath.Join(tmp, "bot_update.json")
92 revMapFile := filepath.Join(tmp, "revmap")
93 revMap := `{"got_revision": "src"}`
94 if err := os_steps.WriteFile(ctx, revMapFile, []byte(revMap), os.ModePerm); err != nil {
95 return err
96 }
97 cleanupDir := filepath.Join(tmp, "cleanup")
98 if err := os_steps.MkdirAll(ctx, cleanupDir); err != nil {
99 return err
100 }
101
102 cmd := []string{
103 "vpython3", "-u", botUpdateScript,
104 "--spec-path", specPath,
105 "--patch_root", patchRoot,
106 "--revision_mapping_file", revMapFile,
107 "--cleanup-dir", cleanupDir,
108 "--output_json", outputJson,
109 "--revision", fmt.Sprintf("%s@%s", mainRepoName, revision),
110 "--revision", fmt.Sprintf("%s@%s", "https://skia.googlesource.com/skia.git", skiaRev),
111 }
112 if gitCacheDir != "" {
113 cmd = append(cmd, "--git-cache-dir", gitCacheDir)
114 }
115 if patchRef != "" {
116 patchRepoURL := skiaRepoURL
117 patchBaseRev := skiaRev
118 cmd = append(cmd, "--patch_ref", fmt.Sprintf("%s@%s:%s", patchRepoURL, patchBaseRev, patchRef))
119 }
120 if _, err := exec.RunCwd(ctx, checkoutRoot, cmd...); err != nil {
121 return err
122 }
123
124 if _, err := exec.RunCwd(ctx, checkoutRoot, "vpython3", "-u", filepath.Join(depotToolsDir, "gclient.py"), "runhooks"); err != nil {
125 return err
126 }
127
128 return nil
129 })
130 }
131
132 func main() {
133 var (
134 projectId = flag.String("project_id", "", "ID of the Google Cloud project.")
135 taskId = flag.String("task_id", "", "ID of this task.")
136 taskName = flag.String("task_name", "", "Name of the task.")
137 output = flag.String("o", "", "If provided, dump a JSON blob of step data to the given file. Prints to stdout if '-' is given.")
138 local = flag.Bool("local", true, "True if running locally (as opposed to on the bots)")
139 dryRun = flag.Bool("dry_run", false, "If set, generate SKPs but do not upload or commit them.")
140 skiaRev = flag.String("skia_revision", "origin/main", "Revision of Skia at which this task is running.")
141 patchRef = flag.String("patch_ref", "", "Patch ref, if any, associated with this task.")
142 skipSync = flag.Bool("skip-sync", false, "Skip sync. Helpful for running locally.")
143 skipBuild = flag.Bool("skip-build", false, "skip build. Helpful for running locally.")
144 gitCacheDirFlag = flag.String("git_cache", "", "Git cache directory.")
145 checkoutRootFlag = flag.String("checkout_root", "", "Directory to use for checkouts.")
146 dmPathFlag = flag.String("dm_path", "", "Path to the DM binary.")
147 )
148 ctx := td.StartRun(projectId, taskId, taskName, output, local)
149 defer td.EndRun(ctx)
150
151 // Setup.
152 client, _, err := auth_steps.InitHttpClient(ctx, *local, gerrit.AuthScope)
153 if err != nil {
154 td.Fatal(ctx, err)
155 }
156 cwd, err := os.Getwd()
157 if err != nil {
158 td.Fatal(ctx, err)
159 }
160 skiaDir := filepath.Join(cwd, "skia")
161 gitCacheDir := ""
162 if *gitCacheDirFlag != "" {
163 gitCacheDir = filepath.Join(cwd, *gitCacheDirFlag)
164 }
165 checkoutRoot := cwd
166 if *checkoutRootFlag != "" {
167 checkoutRoot = filepath.Join(cwd, *checkoutRootFlag)
168 }
169 if *dmPathFlag == "" {
170 td.Fatal(ctx, fmt.Errorf("Must specify --dm_path"))
171 }
172 dmPath := filepath.Join(cwd, *dmPathFlag)
173
174 // Fetch `sk`
175 if _, err := exec.RunCwd(ctx, skiaDir, "python3", filepath.Join("bin", "fetch-sk")); err != nil {
176 td.Fatal(ctx, err)
177 }
178
179 // Create a temporary directory.
180 tmp, err := os_steps.TempDir(ctx, "", "")
181 if err != nil {
182 td.Fatal(ctx, err)
183 }
184 defer func() {
185 _ = os_steps.RemoveAll(ctx, tmp)
186 }()
187
188 // Check out depot_tools at the exact revision expected by tests (defined in recipes.cfg), and
189 // make it available to tests by by adding it to the PATH.
190 var depotToolsDir string
191 if err := td.Do(ctx, td.Props("Check out depot_tools"), func(ctx context.Context) error {
192 var err error
193 co, err := git.NewCheckout(ctx, common.REPO_DEPOT_TOOLS, tmp)
194 if err != nil {
195 return skerr.Wrap(err)
196 }
197 depotToolsDir = co.Dir()
198 return err
199 }); err != nil {
200 td.Fatal(ctx, err)
201 }
202 ctx = td.WithEnv(ctx, depot_tools.Env(depotToolsDir))
203
204 // Sync Chrome.
205 if !*skipSync {
206 if err := botUpdate(ctx, checkoutRoot, gitCacheDir, *skiaRev, *patchRef, depotToolsDir, *local); err != nil {
207 td.Fatal(ctx, err)
208 }
209 }
210
211 // For updating metrics.
212 pg := pushgateway.New(client, jobName, pushgateway.DefaultPushgatewayURL)
213
214 chromiumDir := filepath.Join(checkoutRoot, "src")
215 outDir := filepath.Join(chromiumDir, "out", "Release")
216
217 // Build Chrome.
218 if !*skipBuild {
219 if err := td.Do(ctx, td.Props("Build Chrome"), func(ctx context.Context) error {
220 // Run "gn gen". This task driver only runs on Linux.
221 gn := filepath.Join(chromiumDir, "buildtools", "linux64", "gn")
222 if _, err := exec.RunCommand(ctx, &exec.Command{
223 Name: gn,
224 Args: []string{"gen", outDir},
225 Dir: chromiumDir,
226 Env: []string{
227 "CPPFLAGS=-DSK_ALLOW_CROSSPROCESS_PICTUREIMAGEFILTERS=1",
228 "GYP_GENERATORS=ninja",
229 },
230 InheritEnv: true,
231 InheritPath: true,
232 }); err != nil {
233 return err
234 }
235
236 // Perform the build.
237 ninja := filepath.Join(depotToolsDir, "ninja")
238 if _, err := exec.RunCwd(ctx, chromiumDir, ninja, "-C", outDir, "chrome"); err != nil {
239 return err
240 }
241 return nil
242 }); err != nil {
243 // Report that the build failed.
244 _ = pg.Push(ctx, buildFailureMetricName, metricValue_Failure)
245 td.Fatal(ctx, err)
246 }
247 // Report that the build was successful.
248 _ = pg.Push(ctx, buildFailureMetricName, metricValue_NoFailure)
249 }
250
251 // Capture and upload the SKPs.
252 script := filepath.Join(skiaDir, "infra", "bots", "assets", "skp", "create_and_upload.py")
253 cmd := []string{
254 "vpython3", "-u", script,
255 "--chrome_src_path", chromiumDir,
256 "--browser_executable", filepath.Join(outDir, "chrome"),
257 "--dm_path", dmPath,
258 }
259 if *dryRun {
260 cmd = append(cmd, "--dry_run")
261 } else {
262 cmd = append(cmd, "--upload_to_partner_bucket")
263 }
264
265 if *local {
266 cmd = append(cmd, "--local")
267 }
268 command := &exec.Command{
269 Name: filepath.Join(depotToolsDir, cmd[0]),
270 Args: cmd[1:],
271 Dir: skiaDir,
272 Env: []string{
273 fmt.Sprintf("PATH=%s:%s", os.Getenv("PATH"), depotToolsDir),
274 },
275 }
276 sklog.Infof("Running command: %s %s", command.Name, strings.Join(command.Args, " "))
277 if err := exec.Run(ctx, command); err != nil {
278 // Creating SKP asset in RecreateSKPs failed.
279 _ = pg.Push(ctx, creatingSKPsFailureMetricName, metricValue_Failure)
280 td.Fatal(ctx, err)
281 }
282 // Report that the asset creation was successful.
283 _ = pg.Push(ctx, creatingSKPsFailureMetricName, metricValue_NoFailure)
284 }