| 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)] |
| 21 |
pub struct SyncOpts { |
| 22 |
|
| 23 |
|
| 24 |
pub delete: bool, |
| 25 |
|
| 26 |
pub chmod: Option<String>, |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
pub compress: bool, |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
pub partial: bool, |
| 37 |
} |
| 38 |
|
| 39 |
impl Default for SyncOpts { |
| 40 |
fn default() -> Self { |
| 41 |
Self { |
| 42 |
delete: false, |
| 43 |
chmod: None, |
| 44 |
compress: true, |
| 45 |
partial: true, |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
impl SyncOpts { |
| 51 |
|
| 52 |
|
| 53 |
pub fn release_mirror() -> Self { |
| 54 |
Self { |
| 55 |
delete: true, |
| 56 |
chmod: Some("Du=rwx,Dgo=rx,Fu=rw,Fgo=r,F+X".into()), |
| 57 |
..Self::default() |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
pub fn precompressed() -> Self { |
| 64 |
Self { |
| 65 |
compress: false, |
| 66 |
..Self::default() |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
#[derive(Clone, Debug, PartialEq)] |
| 75 |
pub enum ObserveEvent { |
| 76 |
ProcessExited { |
| 77 |
unit: String, |
| 78 |
code: i32, |
| 79 |
}, |
| 80 |
ResourceSample { |
| 81 |
cpu: f64, |
| 82 |
rss: u64, |
| 83 |
disk: u64, |
| 84 |
}, |
| 85 |
JournalLine { |
| 86 |
unit: String, |
| 87 |
line: String, |
| 88 |
}, |
| 89 |
HealthChanged { |
| 90 |
check: String, |
| 91 |
from: String, |
| 92 |
to: String, |
| 93 |
}, |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
pub type EventStream = tokio::sync::mpsc::Receiver<ObserveEvent>; |
| 98 |
|
| 99 |
#[async_trait] |
| 100 |
pub trait Executor: Send + Sync { |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
async fn run_streaming(&self, step: &Step, sink: &mut dyn LogSink) -> Result<RunOutput>; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
async fn pull_file(&self, remote: &Path, local: &Path, opts: &SyncOpts) -> Result<()>; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
async fn pull_dir(&self, remote: &Path, local: &Path, opts: &SyncOpts) -> Result<()>; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
async fn pull_glob(&self, remote_glob: &str, local_dir: &Path, opts: &SyncOpts) -> Result<()>; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
async fn push_dir(&self, local: &Path, remote: &Path, opts: &SyncOpts) -> Result<()>; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
fn observe(&self) -> Option<EventStream> { |
| 152 |
None |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
async fn preflight(&self) -> Result<()> { |
| 162 |
Ok(()) |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
fn capabilities(&self) -> &CapabilitySet; |
| 167 |
|
| 168 |
|
| 169 |
fn can_observe(&self, kind: &ObserveKind) -> bool { |
| 170 |
self.capabilities().permits_observe(kind) |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
pub(crate) async fn run_command_into_sink( |
| 187 |
mut cmd: Command, |
| 188 |
sink: &mut dyn LogSink, |
| 189 |
sentinel: Option<crate::remote::RcSentinel>, |
| 190 |
host: &str, |
| 191 |
) -> Result<RunOutput> { |
| 192 |
use std::process::Stdio; |
| 193 |
use tokio::io::AsyncReadExt; |
| 194 |
|
| 195 |
cmd.stdout(Stdio::piped()); |
| 196 |
cmd.stderr(Stdio::piped()); |
| 197 |
cmd.kill_on_drop(true); |
| 198 |
let mut child = cmd |
| 199 |
.spawn() |
| 200 |
.map_err(|e| anyhow::anyhow!("spawning command: {e}"))?; |
| 201 |
|
| 202 |
let mut out = child.stdout.take(); |
| 203 |
let mut err = child.stderr.take(); |
| 204 |
let mut stdout_buf = Vec::new(); |
| 205 |
let mut stderr_buf = Vec::new(); |
| 206 |
let mut ob = [0u8; 4096]; |
| 207 |
let mut eb = [0u8; 4096]; |
| 208 |
let mut out_done = out.is_none(); |
| 209 |
let mut err_done = err.is_none(); |
| 210 |
|
| 211 |
let mut filter = crate::remote::RcFilter::new(sentinel.clone()); |
| 212 |
|
| 213 |
while !(out_done && err_done) { |
| 214 |
tokio::select! { |
| 215 |
r = async { out.as_mut().unwrap().read(&mut ob).await }, if !out_done => { |
| 216 |
match r { |
| 217 |
Ok(0) | Err(_) => out_done = true, |
| 218 |
Ok(n) => { |
| 219 |
let chunk = filter.feed(&ob[..n]); |
| 220 |
if !chunk.is_empty() { |
| 221 |
crate::remote::push_bounded(&mut stdout_buf, &chunk, crate::remote::OUTPUT_TAIL_CAP); |
| 222 |
sink.write_chunk(&chunk).await; |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
r = async { err.as_mut().unwrap().read(&mut eb).await }, if !err_done => { |
| 228 |
match r { |
| 229 |
Ok(0) | Err(_) => err_done = true, |
| 230 |
Ok(n) => { crate::remote::push_bounded(&mut stderr_buf, &eb[..n], crate::remote::OUTPUT_TAIL_CAP); sink.write_chunk(&eb[..n]).await; } |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
let (rest, code) = filter.finish(); |
| 237 |
if !rest.is_empty() { |
| 238 |
crate::remote::push_bounded(&mut stdout_buf, &rest, crate::remote::OUTPUT_TAIL_CAP); |
| 239 |
sink.write_chunk(&rest).await; |
| 240 |
} |
| 241 |
|
| 242 |
let status = child |
| 243 |
.wait() |
| 244 |
.await |
| 245 |
.map_err(|e| anyhow::anyhow!("waiting on child: {e}"))?; |
| 246 |
let status = match sentinel { |
| 247 |
Some(_) => crate::remote::resolve_remote_status(host, status, code)?, |
| 248 |
None => status, |
| 249 |
}; |
| 250 |
Ok(RunOutput { |
| 251 |
status, |
| 252 |
stdout: stdout_buf, |
| 253 |
stderr: stderr_buf, |
| 254 |
}) |
| 255 |
} |
| 256 |
|