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