|
| 1 | +use crate::fs_digest::*; |
| 2 | +use bazel_remote_apis::build::bazel::remote::execution::v2::{ |
| 3 | + command, platform, Action, ActionResult, Command, Digest, ExecutedActionMetadata, |
| 4 | +}; |
| 5 | +use miette::IntoDiagnostic; |
| 6 | +use moon_action::Operation; |
| 7 | +use moon_task::Task; |
| 8 | +use std::collections::BTreeMap; |
| 9 | +use std::path::Path; |
| 10 | + |
| 11 | +pub struct ActionState<'task> { |
| 12 | + task: &'task Task, |
| 13 | + |
| 14 | + // RE API |
| 15 | + pub action: Option<Action>, |
| 16 | + pub action_result: Option<ActionResult>, |
| 17 | + pub command: Option<Command>, |
| 18 | + pub digest: Digest, |
| 19 | + |
| 20 | + // To upload |
| 21 | + pub blobs: Vec<Blob>, |
| 22 | +} |
| 23 | + |
| 24 | +impl ActionState<'_> { |
| 25 | + pub fn new(digest: Digest, task: &Task) -> ActionState<'_> { |
| 26 | + ActionState { |
| 27 | + task, |
| 28 | + action: None, |
| 29 | + action_result: None, |
| 30 | + command: None, |
| 31 | + digest, |
| 32 | + blobs: vec![], |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + pub fn create_action_from_task(&mut self) { |
| 37 | + let mut action = Action { |
| 38 | + command_digest: Some(self.digest.clone()), |
| 39 | + do_not_cache: !self.task.options.cache, |
| 40 | + input_root_digest: None, // TODO? |
| 41 | + ..Default::default() |
| 42 | + }; |
| 43 | + |
| 44 | + // https://github.com/bazelbuild/remote-apis/blob/main/build/bazel/remote/execution/v2/platform.md |
| 45 | + if let Some(os_list) = &self.task.options.os { |
| 46 | + let platform = action.platform.get_or_insert_default(); |
| 47 | + |
| 48 | + for os in os_list { |
| 49 | + platform.properties.push(platform::Property { |
| 50 | + name: "OSFamily".into(), |
| 51 | + value: os.to_string(), |
| 52 | + }); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Since we don't support (or plan to) remote execution, |
| 57 | + // then we can ignore all the working directory logic |
| 58 | + let mut command = Command { |
| 59 | + arguments: vec![self.task.command.clone()], |
| 60 | + output_paths: vec![], // TODO |
| 61 | + ..Default::default() |
| 62 | + }; |
| 63 | + |
| 64 | + command.arguments.extend(self.task.args.clone()); |
| 65 | + |
| 66 | + for (name, value) in BTreeMap::from_iter(self.task.env.clone()) { |
| 67 | + command |
| 68 | + .environment_variables |
| 69 | + .push(command::EnvironmentVariable { name, value }); |
| 70 | + } |
| 71 | + |
| 72 | + self.action = Some(action); |
| 73 | + self.command = Some(command); |
| 74 | + } |
| 75 | + |
| 76 | + pub fn create_action_result_from_operation( |
| 77 | + &mut self, |
| 78 | + operation: &Operation, |
| 79 | + ) -> miette::Result<()> { |
| 80 | + let mut result = ActionResult { |
| 81 | + execution_metadata: Some(ExecutedActionMetadata { |
| 82 | + worker: "moon".into(), |
| 83 | + execution_start_timestamp: create_timestamp_from_naive(operation.started_at), |
| 84 | + execution_completed_timestamp: operation |
| 85 | + .finished_at |
| 86 | + .and_then(create_timestamp_from_naive), |
| 87 | + ..Default::default() |
| 88 | + }), |
| 89 | + ..Default::default() |
| 90 | + }; |
| 91 | + |
| 92 | + if let Some(exec) = operation.get_output() { |
| 93 | + result.exit_code = exec.exit_code.unwrap_or_default(); |
| 94 | + |
| 95 | + if let Some(stderr) = &exec.stderr { |
| 96 | + let blob = Blob::new(stderr.as_bytes().to_owned()); |
| 97 | + |
| 98 | + result.stderr_digest = Some(blob.digest.clone()); |
| 99 | + self.blobs.push(blob); |
| 100 | + } |
| 101 | + |
| 102 | + if let Some(stdout) = &exec.stdout { |
| 103 | + let blob = Blob::new(stdout.as_bytes().to_owned()); |
| 104 | + |
| 105 | + result.stdout_digest = Some(blob.digest.clone()); |
| 106 | + self.blobs.push(blob); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + self.action_result = Some(result); |
| 111 | + |
| 112 | + Ok(()) |
| 113 | + } |
| 114 | + |
| 115 | + pub fn set_action_result(&mut self, result: ActionResult) { |
| 116 | + self.action_result = Some(result); |
| 117 | + } |
| 118 | + |
| 119 | + pub fn compute_outputs(&mut self, workspace_root: &Path) -> miette::Result<()> { |
| 120 | + let mut outputs = OutputDigests::default(); |
| 121 | + |
| 122 | + for path in self.task.get_output_files(workspace_root, true)? { |
| 123 | + outputs.insert_relative_path(path, workspace_root)?; |
| 124 | + } |
| 125 | + |
| 126 | + if let Some(result) = &mut self.action_result { |
| 127 | + result.output_files = outputs.files; |
| 128 | + result.output_symlinks = outputs.symlinks; |
| 129 | + result.output_directories = outputs.dirs; |
| 130 | + self.blobs.extend(outputs.blobs); |
| 131 | + } |
| 132 | + |
| 133 | + Ok(()) |
| 134 | + } |
| 135 | + |
| 136 | + pub fn get_command_as_bytes(&self) -> miette::Result<Vec<u8>> { |
| 137 | + bincode::serialize(&self.command).into_diagnostic() |
| 138 | + } |
| 139 | + |
| 140 | + pub fn prepare_for_upload(&mut self) -> Option<(ActionResult, Vec<Blob>)> { |
| 141 | + self.action_result |
| 142 | + .take() |
| 143 | + .map(|result| (result, self.blobs.drain(0..).collect::<Vec<_>>())) |
| 144 | + } |
| 145 | +} |
0 commit comments