Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 1 | // Copyright 2020 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package remoteexec |
| 16 | |
| 17 | import ( |
Anas Sulaiman | 9c49364 | 2023-10-18 16:34:37 +0000 | [diff] [blame] | 18 | "fmt" |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 19 | "sort" |
| 20 | "strings" |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 21 | ) |
| 22 | |
| 23 | const ( |
| 24 | // ContainerImageKey is the key identifying the container image in the platform spec. |
| 25 | ContainerImageKey = "container-image" |
| 26 | |
| 27 | // PoolKey is the key identifying the pool to use for remote execution. |
| 28 | PoolKey = "Pool" |
| 29 | |
| 30 | // DefaultImage is the default container image used for Android remote execution. The |
| 31 | // image was built with the Dockerfile at |
| 32 | // https://android.googlesource.com/platform/prebuilts/remoteexecution-client/+/refs/heads/master/docker/Dockerfile |
Anas Sulaiman | 3f465a4 | 2023-11-15 20:26:34 +0000 | [diff] [blame] | 33 | DefaultImage = "docker://gcr.io/androidbuild-re-dockerimage/android-build-remoteexec-image@sha256:1eb7f64b9e17102b970bd7a1af7daaebdb01c3fb777715899ef462d6c6d01a45" |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 34 | |
| 35 | // DefaultWrapperPath is the default path to the remote execution wrapper. |
| 36 | DefaultWrapperPath = "prebuilts/remoteexecution-client/live/rewrapper" |
| 37 | |
| 38 | // DefaultPool is the name of the pool to use for remote execution when none is specified. |
| 39 | DefaultPool = "default" |
| 40 | |
| 41 | // LocalExecStrategy is the exec strategy to indicate that the action should be run locally. |
| 42 | LocalExecStrategy = "local" |
| 43 | |
| 44 | // RemoteExecStrategy is the exec strategy to indicate that the action should be run |
| 45 | // remotely. |
| 46 | RemoteExecStrategy = "remote" |
| 47 | |
| 48 | // RemoteLocalFallbackExecStrategy is the exec strategy to indicate that the action should |
| 49 | // be run remotely and fallback to local execution if remote fails. |
| 50 | RemoteLocalFallbackExecStrategy = "remote_local_fallback" |
| 51 | ) |
| 52 | |
| 53 | var ( |
Colin Cross | 95fad7a | 2021-06-09 12:48:53 -0700 | [diff] [blame] | 54 | defaultLabels = map[string]string{"type": "tool"} |
| 55 | defaultExecStrategy = LocalExecStrategy |
| 56 | defaultEnvironmentVariables = []string{ |
| 57 | // This is a subset of the allowlist in ui/build/ninja.go that makes sense remotely. |
| 58 | "LANG", |
| 59 | "LC_MESSAGES", |
| 60 | "PYTHONDONTWRITEBYTECODE", |
| 61 | } |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 62 | ) |
| 63 | |
| 64 | // REParams holds information pertinent to the remote execution of a rule. |
| 65 | type REParams struct { |
| 66 | // Platform is the key value pair used for remotely executing the action. |
| 67 | Platform map[string]string |
| 68 | // Labels is a map of labels that identify the rule. |
| 69 | Labels map[string]string |
| 70 | // ExecStrategy is the remote execution strategy: remote, local, or remote_local_fallback. |
| 71 | ExecStrategy string |
| 72 | // Inputs is a list of input paths or ninja variables. |
| 73 | Inputs []string |
Colin Cross | a4eafdd | 2021-03-24 14:09:28 -0700 | [diff] [blame] | 74 | // RSPFiles is the name of the files used by the rule as a placeholder for an rsp input. |
| 75 | RSPFiles []string |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 76 | // OutputFiles is a list of output file paths or ninja variables as placeholders for rule |
| 77 | // outputs. |
| 78 | OutputFiles []string |
Ramy Medhat | c7965cd | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 79 | // OutputDirectories is a list of output directories or ninja variables as placeholders for |
| 80 | // rule output directories. |
Kousik Kumar | 1372c1b | 2020-05-20 07:55:56 -0700 | [diff] [blame] | 81 | OutputDirectories []string |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 82 | // ToolchainInputs is a list of paths or ninja variables pointing to the location of |
| 83 | // toolchain binaries used by the rule. |
| 84 | ToolchainInputs []string |
Colin Cross | 31972dc | 2021-03-04 10:44:12 -0800 | [diff] [blame] | 85 | // EnvironmentVariables is a list of environment variables whose values should be passed through |
| 86 | // to the remote execution. |
| 87 | EnvironmentVariables []string |
Anas Sulaiman | 9c49364 | 2023-10-18 16:34:37 +0000 | [diff] [blame] | 88 | // Boolean indicating whether to compare chosen exec strategy with local execution. |
| 89 | Compare bool |
| 90 | // Number of times the action should be rerun locally. |
| 91 | NumLocalRuns int |
| 92 | // Number of times the action should be rerun remotely. |
| 93 | NumRemoteRuns int |
Anas Sulaiman | 9d7a36d | 2023-11-21 23:00:07 +0000 | [diff] [blame] | 94 | // Boolean indicating whether to update remote cache entry. Rewrapper defaults to true, so the name is negated here. |
| 95 | NoRemoteUpdateCache bool |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | func init() { |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 99 | } |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 100 | |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 101 | // Template generates the remote execution wrapper template to be added as a prefix to the rule's |
| 102 | // command. |
| 103 | func (r *REParams) Template() string { |
Colin Cross | 77cdcfd | 2021-03-12 11:28:25 -0800 | [diff] [blame] | 104 | return "${android.RBEWrapper}" + r.wrapperArgs() |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 105 | } |
| 106 | |
Ramy Medhat | c7965cd | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 107 | // NoVarTemplate generates the remote execution wrapper template without variables, to be used in |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 108 | // RuleBuilder. |
Colin Cross | 77cdcfd | 2021-03-12 11:28:25 -0800 | [diff] [blame] | 109 | func (r *REParams) NoVarTemplate(wrapper string) string { |
| 110 | return wrapper + r.wrapperArgs() |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | func (r *REParams) wrapperArgs() string { |
| 114 | args := "" |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 115 | var kvs []string |
| 116 | labels := r.Labels |
| 117 | if len(labels) == 0 { |
| 118 | labels = defaultLabels |
| 119 | } |
| 120 | for k, v := range labels { |
| 121 | kvs = append(kvs, k+"="+v) |
| 122 | } |
| 123 | sort.Strings(kvs) |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 124 | args += " --labels=" + strings.Join(kvs, ",") |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 125 | |
| 126 | var platform []string |
| 127 | for k, v := range r.Platform { |
| 128 | if v == "" { |
| 129 | continue |
| 130 | } |
| 131 | platform = append(platform, k+"="+v) |
| 132 | } |
| 133 | if _, ok := r.Platform[ContainerImageKey]; !ok { |
| 134 | platform = append(platform, ContainerImageKey+"="+DefaultImage) |
| 135 | } |
| 136 | if platform != nil { |
| 137 | sort.Strings(platform) |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 138 | args += " --platform=\"" + strings.Join(platform, ",") + "\"" |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | strategy := r.ExecStrategy |
| 142 | if strategy == "" { |
| 143 | strategy = defaultExecStrategy |
| 144 | } |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 145 | args += " --exec_strategy=" + strategy |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 146 | |
Anas Sulaiman | 9c49364 | 2023-10-18 16:34:37 +0000 | [diff] [blame] | 147 | if r.Compare && r.NumLocalRuns >= 0 && r.NumRemoteRuns >= 0 { |
| 148 | args += fmt.Sprintf(" --compare=true --num_local_reruns=%d --num_remote_reruns=%d", r.NumLocalRuns, r.NumRemoteRuns) |
| 149 | } |
| 150 | |
Anas Sulaiman | 9d7a36d | 2023-11-21 23:00:07 +0000 | [diff] [blame] | 151 | if r.NoRemoteUpdateCache { |
| 152 | args += " --remote_update_cache=false" |
| 153 | } |
| 154 | |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 155 | if len(r.Inputs) > 0 { |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 156 | args += " --inputs=" + strings.Join(r.Inputs, ",") |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 157 | } |
| 158 | |
Colin Cross | a4eafdd | 2021-03-24 14:09:28 -0700 | [diff] [blame] | 159 | if len(r.RSPFiles) > 0 { |
| 160 | args += " --input_list_paths=" + strings.Join(r.RSPFiles, ",") |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | if len(r.OutputFiles) > 0 { |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 164 | args += " --output_files=" + strings.Join(r.OutputFiles, ",") |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 165 | } |
| 166 | |
Kousik Kumar | 1372c1b | 2020-05-20 07:55:56 -0700 | [diff] [blame] | 167 | if len(r.OutputDirectories) > 0 { |
| 168 | args += " --output_directories=" + strings.Join(r.OutputDirectories, ",") |
| 169 | } |
| 170 | |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 171 | if len(r.ToolchainInputs) > 0 { |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 172 | args += " --toolchain_inputs=" + strings.Join(r.ToolchainInputs, ",") |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 173 | } |
| 174 | |
Colin Cross | 95fad7a | 2021-06-09 12:48:53 -0700 | [diff] [blame] | 175 | envVarAllowlist := append(r.EnvironmentVariables, defaultEnvironmentVariables...) |
| 176 | |
| 177 | if len(envVarAllowlist) > 0 { |
| 178 | args += " --env_var_allowlist=" + strings.Join(envVarAllowlist, ",") |
Colin Cross | 31972dc | 2021-03-04 10:44:12 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Ramy Medhat | 427683c | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 181 | return args + " -- " |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 182 | } |