diff --git a/crates/engine/src/cmd/deploy.rs b/crates/engine/src/cmd/deploy.rs index 17dade1..3a44168 100644 --- a/crates/engine/src/cmd/deploy.rs +++ b/crates/engine/src/cmd/deploy.rs @@ -29,6 +29,7 @@ use std::sync::mpsc; use std::sync::{Arc, Mutex, PoisonError}; use std::time::{Duration, SystemTime, UNIX_EPOCH}; +use lutra_deploy::browse::{self, Target}; use lutra_deploy::known_hosts::{self, HostKey}; use lutra_deploy::secrets::account; use lutra_deploy::{ @@ -75,6 +76,7 @@ pub fn register(registry: &mut Registry) -> Result<(), EngineError> { registry.register(Box::new(CancelJob))?; registry.register(Box::new(Log))?; registry.register(Box::new(ClearLog))?; + super::deploy_remote::register(registry)?; Ok(()) } @@ -171,6 +173,9 @@ pub struct DeployState { next_id: u64, /// Uploads on save waiting for the slot, in order, without duplicates. queue: VecDeque, + /// Set while `deploy.upload` saves buffers first, so those saves queue + /// no upload on save of their own. + saving: bool, } impl Default for DeployState { @@ -182,10 +187,18 @@ impl Default for DeployState { log: VecDeque::new(), next_id: 1, queue: VecDeque::new(), + saving: false, } } } +impl State { + /// Whether no deployment job runs. + pub fn deploy_idle(&self) -> bool { + self.deploy.active.is_none() + } +} + impl DeployState { /// Uses `store` for passwords and passphrases instead of the keychain. pub fn set_secret_store(&mut self, store: Arc) { @@ -315,13 +328,29 @@ fn drain(state: &mut State) { /// What a job does. #[derive(Debug, Clone)] -enum Work { +pub(super) enum Work { /// Test the connection. Test, /// Upload these paths. Upload(Vec), /// Download these paths. Download(Vec), + /// List a browser folder. + Browse(String), + /// Read a file as text. + Read(Target), + /// Write a browser file. + Write(String, String), + /// Rename a browser path. + Rename(String, String), + /// Create a browser folder. + Mkdir(String), + /// Delete a browser path (confirmed). + Delete(String), + /// Compare project paths with the server. + Compare(Vec), + /// Upload these files and delete those (confirmed) on the server. + Sync(Vec, Vec), } impl Work { @@ -331,6 +360,13 @@ impl Work { Self::Test => "test", Self::Upload(_) => "upload", Self::Download(_) => "download", + Self::Browse(_) | Self::Read(_) => "browse", + Self::Write(..) => "save", + Self::Rename(..) => "rename", + Self::Mkdir(_) => "mkdir", + Self::Delete(_) => "delete", + Self::Compare(_) => "compare", + Self::Sync(..) => "sync", } } } @@ -431,7 +467,7 @@ fn spawn( /// Starts a job for a command and returns its pending outcome. The thread /// ignores a failed wake-up send: it only means the outcome was dropped /// because the host stopped waiting. -fn start( +pub(super) fn start( state: &mut State, command: &'static str, server_id: Option<&str>, @@ -444,11 +480,7 @@ fn start( let (tx, rx) = mpsc::channel(); let filled = Arc::clone(&slot); spawn(state, work, job, None, move |job, work| { - let value = match work { - Work::Test => test_value(job::test_connection(job)), - Work::Upload(paths) => summary_value(job::upload(job, paths)), - Work::Download(paths) => summary_value(job::download(job, paths)), - }; + let value = run_work(command, job, work); *filled.lock().unwrap_or_else(PoisonError::into_inner) = Some(value); let _ = tx.send(Ok(Value::Null)); })?; @@ -464,6 +496,44 @@ fn start( }))) } +/// Runs `work` on the job's thread and turns the result into the +/// command's value; the caller's mistakes (bad paths) become +/// `invalid_params` for `command`. +fn run_work(command: &'static str, job: &Job, work: &Work) -> Result { + let fail = |e: DeployError| engine_error(command, e); + match work { + Work::Test => test_value(job::test_connection(job)), + Work::Upload(paths) => summary_value(job::upload(job, paths)), + Work::Download(paths) => summary_value(job::download(job, paths)), + Work::Browse(rel) => { + let entries: Vec = browse::list(job, rel) + .map_err(fail)? + .into_iter() + .map(|e| json!({"name": e.name, "isDir": e.is_dir, "size": e.size})) + .collect(); + Ok(json!({"path": rel, "entries": entries})) + } + Work::Read(target) => { + let read = browse::read(job, target).map_err(fail)?; + Ok(json!({"text": read.text, "binary": read.binary, "tooLarge": read.too_large})) + } + Work::Write(rel, text) => browse::write(job, rel, text.as_bytes()) + .map(|()| json!({})) + .map_err(fail), + Work::Rename(from, to) => browse::rename(job, from, to) + .map(|()| json!({})) + .map_err(fail), + Work::Mkdir(rel) => browse::mkdir(job, rel).map(|()| json!({})).map_err(fail), + Work::Delete(rel) => browse::delete(job, rel) + .map(|n| json!({"deleted": n})) + .map_err(fail), + Work::Compare(paths) => job::compare(job, paths) + .map(|d| json!({"differences": d})) + .map_err(fail), + Work::Sync(upload, delete) => summary_value(job::sync(job, upload, delete)), + } +} + /// An upload or download summary as the command's value. fn summary_value(result: Result) -> Result { result @@ -563,7 +633,7 @@ fn start_queued(state: &mut State) { /// `file.saveAll`. A configuration that cannot be read is logged and /// otherwise ignored, so saving never fails because of deployment. pub fn saved(state: &mut State, path: &str) { - if state.project.is_none() || state.config.is_none() { + if state.project.is_none() || state.config.is_none() || state.deploy.saving { return; } let Ok((_, deployment)) = load(state, "file.save") else { @@ -1052,7 +1122,7 @@ impl Command for TrustHostKey { /// Params of `deploy.upload` and `deploy.download`. #[derive(Debug, Deserialize)] -#[serde(deny_unknown_fields)] +#[serde(deny_unknown_fields, rename_all = "camelCase")] struct TransferParams { /// Project-relative (or absolute, inside the project) files or /// folders; `""` is the whole project. @@ -1060,10 +1130,52 @@ struct TransferParams { /// The server's id; the default server when absent. #[serde(default)] id: Option, + /// Upload only: save the dirty buffers under `paths` first (default + /// true). Ignored by download. + #[serde(default)] + save_first: Option, +} + +/// Saves every dirty buffer whose file lies under one of the +/// project-relative `paths` (`""` is the whole project), without queueing +/// uploads on save for them. +/// +/// # Errors +/// The first save that fails; the upload does not start then. +fn save_under(state: &mut State, paths: &[String]) -> Result<(), EngineError> { + let ids: Vec<_> = state + .buffers + .dirty_ids() + .into_iter() + .filter(|id| { + state.buffers.get(*id).is_some_and(|open| { + let rel = open.path.to_string_lossy().replace('\\', "/"); + paths.iter().any(|p| { + p.is_empty() + || rel == *p + || rel + .strip_prefix(p.as_str()) + .is_some_and(|rest| rest.starts_with('/')) + }) + }) + }) + .collect(); + state.deploy.saving = true; + let mut result = Ok(()); + for id in ids { + if let Err(e) = super::file::save_buffer(state, "deploy.upload", id) { + result = Err(EngineError::Failed(format!( + "saving before the upload failed, so nothing was uploaded: {e}" + ))); + break; + } + } + state.deploy.saving = false; + result } /// Turns client paths into project-relative ones. -fn relative_paths( +pub(super) fn relative_paths( state: &State, command: &str, given: &[String], @@ -1097,7 +1209,7 @@ impl Command for Upload { label: "Deployment: Upload to Server", menu_path: None, default_shortcut: None, - params: "{ paths: [project-relative file or folder, \"\" for the whole project], id?: server id (default server when absent) }; returns { files, bytes, skipped, failed }; progress as deployProgress, lines as deployLog; deploy.cancel stops it", + params: "{ paths: [project-relative file or folder, \"\" for the whole project], id?: server id (default server when absent), saveFirst?: boolean (default true: save the dirty buffers under paths first) }; returns { files, bytes, skipped, failed, deleted }; progress as deployProgress, lines as deployLog; deploy.cancel stops it", } } @@ -1109,6 +1221,9 @@ impl Command for Upload { let command = self.spec().id; let p: TransferParams = parse(command, params)?; let paths = relative_paths(state, command, &p.paths)?; + if state.deploy.active.is_none() && p.save_first.unwrap_or(true) { + save_under(state, &paths)?; + } start(state, command, p.id.as_deref(), Work::Upload(paths)) } } diff --git a/crates/engine/src/cmd/deploy_remote.rs b/crates/engine/src/cmd/deploy_remote.rs new file mode 100644 index 0000000..9e33b18 --- /dev/null +++ b/crates/engine/src/cmd/deploy_remote.rs @@ -0,0 +1,413 @@ +//! Remote browser, compare and sync commands (C9 D2). They share the job +//! slot of [`super::deploy`]: each runs as a pending outcome on a +//! `lutra-deploy` thread, one at a time. +//! +//! **Browser paths** are relative to the server's root folder with `/` +//! (`""` is the root); absolute paths and `..` are `invalid_params`. +//! +//! **Nothing is deleted on the server without an explicit confirmation:** +//! `deploy.deleteRemote` needs `confirm: true`, and `deploy.sync` needs +//! `confirmDelete: true` whenever its `delete` list is not empty. Without +//! it they fail with `invalid_params` before connecting. + +use serde::Deserialize; +use serde_json::Value; + +use super::deploy::{Work, relative_paths, start}; +use super::params::parse; +use crate::{Command, CommandSpec, EngineError, Outcome, Registry, State}; +use lutra_deploy::browse::Target; + +/// Longest text `deploy.writeRemote` accepts. +const MAX_WRITE: usize = 4 * 1024 * 1024; + +/// Registers the remote `deploy.*` commands. +/// +/// # Errors +/// [`EngineError::Internal`] if an id is taken. +pub fn register(registry: &mut Registry) -> Result<(), EngineError> { + registry.register(Box::new(Browse))?; + registry.register(Box::new(ReadRemote))?; + registry.register(Box::new(WriteRemote))?; + registry.register(Box::new(RenameRemote))?; + registry.register(Box::new(MkdirRemote))?; + registry.register(Box::new(DeleteRemote))?; + registry.register(Box::new(Compare))?; + registry.register(Box::new(Sync))?; + Ok(()) +} + +/// Ids of this module's commands. +#[cfg(test)] +pub(crate) const COMMAND_IDS: &[&str] = &[ + "deploy.browse", + "deploy.compare", + "deploy.deleteRemote", + "deploy.mkdirRemote", + "deploy.readRemote", + "deploy.renameRemote", + "deploy.sync", + "deploy.writeRemote", +]; + +/// Whether a job may start: a project and a free slot. +fn idle(state: &State) -> bool { + state.has_project() && state.deploy_idle() +} + +/// Params naming a browser path. +#[derive(Debug, Deserialize)] +#[serde(deny_unknown_fields)] +struct PathParams { + /// The browser path. + #[serde(default)] + path: String, + /// The server's id; the default server when absent. + #[serde(default)] + id: Option, +} + +/// `deploy.browse`. +struct Browse; + +impl Command for Browse { + fn spec(&self) -> &CommandSpec { + &CommandSpec { + id: "deploy.browse", + label: "Deployment: List Server Folder", + menu_path: None, + default_shortcut: None, + params: "{ path?: folder relative to the server's root (\"\" for the root), id?: server id }; returns { path, entries: [{ name, isDir, size? }] } folders first", + } + } + + fn enabled(&self, state: &State) -> bool { + idle(state) + } + + fn run(&self, state: &mut State, params: Value) -> Result { + let p: PathParams = parse(self.spec().id, params)?; + start(state, self.spec().id, p.id.as_deref(), Work::Browse(p.path)) + } +} + +/// Params of `deploy.readRemote`. +#[derive(Debug, Deserialize)] +#[serde(deny_unknown_fields, rename_all = "camelCase")] +struct ReadParams { + /// A project-relative path, mapped to the server. + #[serde(default)] + path: Option, + /// A browser path, relative to the server's root. + #[serde(default)] + remote_path: Option, + /// The server's id; the default server when absent. + #[serde(default)] + id: Option, +} + +/// `deploy.readRemote`. +struct ReadRemote; + +impl Command for ReadRemote { + fn spec(&self) -> &CommandSpec { + &CommandSpec { + id: "deploy.readRemote", + label: "Deployment: Read Server File", + menu_path: None, + default_shortcut: None, + params: "{ path: project-relative file (mapped) | remotePath: file relative to the server's root, id?: server id }; returns { text: string|null (null when missing), binary, tooLarge } (at most 4 MiB)", + } + } + + fn enabled(&self, state: &State) -> bool { + idle(state) + } + + fn run(&self, state: &mut State, params: Value) -> Result { + let command = self.spec().id; + let p: ReadParams = parse(command, params)?; + let target = match (p.path, p.remote_path) { + (Some(path), None) => { + let rel = relative_paths(state, command, &[path])?; + Target::Mapped(rel.into_iter().next().unwrap_or_default()) + } + (None, Some(remote)) => Target::Browser(remote), + _ => { + return Err(EngineError::invalid_params( + command, + "give exactly one of path and remotePath", + )); + } + }; + start(state, command, p.id.as_deref(), Work::Read(target)) + } +} + +/// Params of `deploy.writeRemote`. +#[derive(Debug, Deserialize)] +#[serde(deny_unknown_fields, rename_all = "camelCase")] +struct WriteParams { + /// The browser path of the file. + remote_path: String, + /// Its new text. + text: String, + /// The server's id; the default server when absent. + #[serde(default)] + id: Option, +} + +/// `deploy.writeRemote`. +struct WriteRemote; + +impl Command for WriteRemote { + fn spec(&self) -> &CommandSpec { + &CommandSpec { + id: "deploy.writeRemote", + label: "Deployment: Save Server File", + menu_path: None, + default_shortcut: None, + params: "{ remotePath: file relative to the server's root, text: at most 4 MiB, id?: server id }; writes the file on the server (creating its folder); returns {}", + } + } + + fn enabled(&self, state: &State) -> bool { + idle(state) + } + + fn run(&self, state: &mut State, params: Value) -> Result { + let command = self.spec().id; + let p: WriteParams = parse(command, params)?; + if p.text.len() > MAX_WRITE || p.remote_path.trim().is_empty() { + return Err(EngineError::invalid_params( + command, + "remotePath must name a file and text may hold at most 4 MiB", + )); + } + start( + state, + command, + p.id.as_deref(), + Work::Write(p.remote_path, p.text), + ) + } +} + +/// Params of `deploy.renameRemote`. +#[derive(Debug, Deserialize)] +#[serde(deny_unknown_fields)] +struct RenameParams { + /// The browser path to rename. + from: String, + /// Its new browser path. + to: String, + /// The server's id; the default server when absent. + #[serde(default)] + id: Option, +} + +/// `deploy.renameRemote`. +struct RenameRemote; + +impl Command for RenameRemote { + fn spec(&self) -> &CommandSpec { + &CommandSpec { + id: "deploy.renameRemote", + label: "Deployment: Rename on Server", + menu_path: None, + default_shortcut: None, + params: "{ from, to: paths relative to the server's root, id?: server id }; returns {}", + } + } + + fn enabled(&self, state: &State) -> bool { + idle(state) + } + + fn run(&self, state: &mut State, params: Value) -> Result { + let command = self.spec().id; + let p: RenameParams = parse(command, params)?; + if p.from.trim().is_empty() || p.to.trim().is_empty() { + return Err(EngineError::invalid_params( + command, + "from and to must name paths", + )); + } + start(state, command, p.id.as_deref(), Work::Rename(p.from, p.to)) + } +} + +/// `deploy.mkdirRemote`. +struct MkdirRemote; + +impl Command for MkdirRemote { + fn spec(&self) -> &CommandSpec { + &CommandSpec { + id: "deploy.mkdirRemote", + label: "Deployment: New Folder on Server", + menu_path: None, + default_shortcut: None, + params: "{ path: new folder relative to the server's root, id?: server id }; creates it and its parents; returns {}", + } + } + + fn enabled(&self, state: &State) -> bool { + idle(state) + } + + fn run(&self, state: &mut State, params: Value) -> Result { + let command = self.spec().id; + let p: PathParams = parse(command, params)?; + if p.path.trim().is_empty() { + return Err(EngineError::invalid_params( + command, + "path must name the new folder", + )); + } + start(state, command, p.id.as_deref(), Work::Mkdir(p.path)) + } +} + +/// Params of `deploy.deleteRemote`. +#[derive(Debug, Deserialize)] +#[serde(deny_unknown_fields)] +struct DeleteParams { + /// The browser path to delete. + path: String, + /// The user confirmed the deletion. + #[serde(default)] + confirm: bool, + /// The server's id; the default server when absent. + #[serde(default)] + id: Option, +} + +/// `deploy.deleteRemote`. +struct DeleteRemote; + +impl Command for DeleteRemote { + fn spec(&self) -> &CommandSpec { + &CommandSpec { + id: "deploy.deleteRemote", + label: "Deployment: Delete on Server", + menu_path: None, + default_shortcut: None, + params: "{ path: file or folder relative to the server's root (never the root), confirm: true (required: the user agreed), id?: server id }; a folder goes with everything inside; returns { deleted: count }", + } + } + + fn enabled(&self, state: &State) -> bool { + idle(state) + } + + fn run(&self, state: &mut State, params: Value) -> Result { + let command = self.spec().id; + let p: DeleteParams = parse(command, params)?; + if !p.confirm { + return Err(EngineError::invalid_params( + command, + "deleting on the server needs confirm: true after the user agreed", + )); + } + start(state, command, p.id.as_deref(), Work::Delete(p.path)) + } +} + +/// Params of `deploy.compare`. +#[derive(Debug, Deserialize)] +#[serde(deny_unknown_fields)] +struct CompareParams { + /// Project-relative files or folders; `""` is the whole project. + paths: Vec, + /// The server's id; the default server when absent. + #[serde(default)] + id: Option, +} + +/// `deploy.compare`. +struct Compare; + +impl Command for Compare { + fn spec(&self) -> &CommandSpec { + &CommandSpec { + id: "deploy.compare", + label: "Deployment: Compare with Server", + menu_path: None, + default_shortcut: None, + params: "{ paths: [project-relative file or folder, \"\" for the whole project], id?: server id }; returns { differences: [{ path, status: localOnly|remoteOnly|different, localSize?, remoteSize? }] } sorted by path; equal files are left out", + } + } + + fn enabled(&self, state: &State) -> bool { + idle(state) + } + + fn run(&self, state: &mut State, params: Value) -> Result { + let command = self.spec().id; + let p: CompareParams = parse(command, params)?; + let paths = relative_paths(state, command, &p.paths)?; + start(state, command, p.id.as_deref(), Work::Compare(paths)) + } +} + +/// Params of `deploy.sync`. +#[derive(Debug, Deserialize)] +#[serde(deny_unknown_fields, rename_all = "camelCase")] +struct SyncParams { + /// Project-relative files to upload. + #[serde(default)] + upload: Vec, + /// Project-relative files to delete on the server. + #[serde(default)] + delete: Vec, + /// The user confirmed the deletions. + #[serde(default)] + confirm_delete: bool, + /// The server's id; the default server when absent. + #[serde(default)] + id: Option, +} + +/// `deploy.sync`. +struct Sync; + +impl Command for Sync { + fn spec(&self) -> &CommandSpec { + &CommandSpec { + id: "deploy.sync", + label: "Deployment: Sync with Server", + menu_path: None, + default_shortcut: None, + params: "{ upload?: [project-relative files], delete?: [project-relative files to delete on the server], confirmDelete: true (required when delete is not empty), id?: server id }; returns { files, bytes, skipped, failed, deleted }", + } + } + + fn enabled(&self, state: &State) -> bool { + idle(state) + } + + fn run(&self, state: &mut State, params: Value) -> Result { + let command = self.spec().id; + let p: SyncParams = parse(command, params)?; + if !p.delete.is_empty() && !p.confirm_delete { + return Err(EngineError::invalid_params( + command, + "deleting on the server needs confirmDelete: true after the user agreed", + )); + } + if p.upload.is_empty() && p.delete.is_empty() { + return Err(EngineError::invalid_params(command, "nothing to sync")); + } + let upload = if p.upload.is_empty() { + Vec::new() + } else { + relative_paths(state, command, &p.upload)? + }; + let delete = if p.delete.is_empty() { + Vec::new() + } else { + relative_paths(state, command, &p.delete)? + }; + start(state, command, p.id.as_deref(), Work::Sync(upload, delete)) + } +} diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs index d93a635..5905bea 100644 --- a/crates/engine/src/cmd/file.rs +++ b/crates/engine/src/cmd/file.rs @@ -112,6 +112,19 @@ fn save(state: &mut State, command: &str, id: BufferId) -> Result Result { + save(state, command, id) +} + /// Params of `file.save`. #[derive(Deserialize)] #[serde(deny_unknown_fields)] diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs index 916e2d3..0706c22 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -3,6 +3,7 @@ mod app; mod buffer; pub mod deploy; +mod deploy_remote; mod file; pub mod git; pub mod git_blame; @@ -98,6 +99,7 @@ pub(crate) const BUILTIN_COMMAND_IDS: &[&[&str]] = &[ app::COMMAND_IDS, buffer::COMMAND_IDS, deploy::COMMAND_IDS, + deploy_remote::COMMAND_IDS, file::COMMAND_IDS, git::COMMAND_IDS, git_blame::COMMAND_IDS, diff --git a/crates/engine/tests/deploy.rs b/crates/engine/tests/deploy.rs index 0a57f57..4781f40 100644 --- a/crates/engine/tests/deploy.rs +++ b/crates/engine/tests/deploy.rs @@ -384,3 +384,122 @@ fn bad_params_and_no_project() { Err(EngineError::Disabled(_)) )); } + +#[test] +fn remote_browser_compare_sync_and_save_first() { + let tmp = tempfile::tempdir().unwrap(); + let remote = tmp.path().join("remote"); + std::fs::create_dir_all(remote.join("www/public")).unwrap(); + std::fs::write(remote.join("www/public/old.php"), "