| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
use crate::capability::CapabilitySet; |
| 9 |
use crate::remote::{LogSink, RunOutput}; |
| 10 |
use crate::step::{ObserveKind, Step}; |
| 11 |
use anyhow::Result; |
| 12 |
use async_trait::async_trait; |
| 13 |
use std::path::Path; |
| 14 |
use tokio::process::Command; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#[derive(Clone, Debug, Default)] |
| 21 |
pub struct SyncOpts { |
| 22 |
|
| 23 |
|
| 24 |
pub delete: bool, |
| 25 |
|
| 26 |
pub chmod: Option<String>, |
| 27 |
} |
| 28 |
|
| 29 |
impl SyncOpts { |
| 30 |
|
| 31 |
|
| 32 |
pub fn release_mirror() -> Self { |
| 33 |
Self { delete: true, chmod: Some("Du=rwx,Dgo=rx,Fu=rw,Fgo=r,F+X".into()) } |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
#[derive(Clone, Debug, PartialEq)] |
| 41 |
pub enum ObserveEvent { |
| 42 |
ProcessExited { unit: String, code: i32 }, |
| 43 |
ResourceSample { cpu: f64, rss: u64, disk: u64 }, |
| 44 |
JournalLine { unit: String, line: String }, |
| 45 |
HealthChanged { check: String, from: String, to: String }, |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
pub type EventStream = tokio::sync::mpsc::Receiver<ObserveEvent>; |
| 50 |
|
| 51 |
#[async_trait] |
| 52 |
pub trait Executor: Send + Sync { |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
async fn run_streaming(&self, step: &Step, sink: &mut dyn LogSink) -> Result<RunOutput>; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
async fn pull(&self, remote: &Path, local: &Path, opts: &SyncOpts) -> Result<()>; |
| 63 |
|
| 64 |
|
| 65 |
async fn push(&self, local: &Path, remote: &Path, opts: &SyncOpts) -> Result<()>; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
fn observe(&self) -> Option<EventStream> { |
| 70 |
None |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
fn capabilities(&self) -> &CapabilitySet; |
| 75 |
|
| 76 |
|
| 77 |
fn can_observe(&self, kind: &ObserveKind) -> bool { |
| 78 |
self.capabilities().permits_observe(kind) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
pub(crate) async fn run_command_into_sink( |
| 90 |
mut cmd: Command, |
| 91 |
sink: &mut dyn LogSink, |
| 92 |
) -> Result<RunOutput> { |
| 93 |
use std::process::Stdio; |
| 94 |
use tokio::io::AsyncReadExt; |
| 95 |
|
| 96 |
cmd.stdout(Stdio::piped()); |
| 97 |
cmd.stderr(Stdio::piped()); |
| 98 |
cmd.kill_on_drop(true); |
| 99 |
let mut child = cmd.spawn().map_err(|e| anyhow::anyhow!("spawning command: {e}"))?; |
| 100 |
|
| 101 |
let mut out = child.stdout.take(); |
| 102 |
let mut err = child.stderr.take(); |
| 103 |
let mut stdout_buf = Vec::new(); |
| 104 |
let mut stderr_buf = Vec::new(); |
| 105 |
let mut ob = [0u8; 4096]; |
| 106 |
let mut eb = [0u8; 4096]; |
| 107 |
let mut out_done = out.is_none(); |
| 108 |
let mut err_done = err.is_none(); |
| 109 |
|
| 110 |
while !(out_done && err_done) { |
| 111 |
tokio::select! { |
| 112 |
r = async { out.as_mut().unwrap().read(&mut ob).await }, if !out_done => { |
| 113 |
match r { |
| 114 |
Ok(0) | Err(_) => out_done = true, |
| 115 |
Ok(n) => { stdout_buf.extend_from_slice(&ob[..n]); sink.write_chunk(&ob[..n]).await; } |
| 116 |
} |
| 117 |
} |
| 118 |
r = async { err.as_mut().unwrap().read(&mut eb).await }, if !err_done => { |
| 119 |
match r { |
| 120 |
Ok(0) | Err(_) => err_done = true, |
| 121 |
Ok(n) => { stderr_buf.extend_from_slice(&eb[..n]); sink.write_chunk(&eb[..n]).await; } |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
let status = child.wait().await.map_err(|e| anyhow::anyhow!("waiting on child: {e}"))?; |
| 128 |
Ok(RunOutput { status, stdout: stdout_buf, stderr: stderr_buf }) |
| 129 |
} |
| 130 |
|