From baefda005035ec2db276ccbe32c46e245dd33fce Mon Sep 17 00:00:00 2001 From: sergiudm Date: Wed, 18 Nov 2026 19:54:33 -0800 Subject: [PATCH] Install, update and uninstall apps from LaunchX MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A manifest can now say how to install its project from a checkout, in a top-level [install] table: the installer, an uninstaller, the tools it needs, and where the app keeps the user's data (SPEC §4.9). - lx install clones a repository into ~/.launchx/sources/ (a folder is installed where it is), shows the services, the installer and the data paths, asks, then stops what was running, runs the installer, registers the app and starts it again. - lx update pulls each app and reruns its installer when there is something new; --check only reports. The launcher also checks at start and every twelve hours. - lx uninstall stops the app, runs its uninstaller, unregisters it and deletes the checkout LaunchX cloned. The data stays. - installs.json records each app's source, checkout, commit and data. - The page gets Install app (with the plan shown first), Check for updates, and Update / Uninstall on each installed app's card. These run as one job at a time, its output streamed to the log panel. - Cards no longer overflow a phone-width screen. Co-Authored-By: Claude Opus 5.5 --- README.md | 9 +- SPEC.md | 61 ++++- src/gateway.rs | 38 +++ src/install.rs | 615 ++++++++++++++++++++++++++++++++++++++++++++++ src/jobs.rs | 225 +++++++++++++++++ src/main.rs | 281 ++++++++++++++++++--- src/manifest.rs | 72 +++++- src/supervisor.rs | 13 + tests/install.rs | 272 ++++++++++++++++++++ ui/app.js | 170 ++++++++++++- ui/index.html | 28 ++- ui/style.css | 41 +++- 12 files changed, 1785 insertions(+), 40 deletions(-) create mode 100644 src/install.rs create mode 100644 src/jobs.rs create mode 100644 tests/install.rs diff --git a/README.md b/README.md index 0ca3d26..0aa2767 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,8 @@ Needs Rust (cargo) and, on macOS, the Xcode command-line tools (swiftc). ```sh ./install.sh # macOS: LaunchX.app into ~/Applications, lx linked into ~/.local/bin lx up # start the launcher now and at every login -lx add ~/code/my-app # register a service by its directory +lx install AutoPhd-org/schedule-everything # install an app from its repository +lx add ~/code/my-app # or register one that is already on this computer ``` On Linux `install.sh` copies `lx` into `~/.local/bin`; use the browser page at `http://launchx.localhost:7600/` (`lx open`). @@ -86,6 +87,7 @@ On Linux `install.sh` copies `lx` into `~/.local/bin`; use the browser page at ` Open **LaunchX** from Launchpad, Spotlight or `~/Applications`. - **Main window:** every service as a card with its status, health, summary, version and port, plus Open, Start, Stop, Restart and Logs buttons. It is the launcher's web page, signed in automatically. +- **Install app** takes a repository or folder, shows what installing it will run, and installs it. Apps installed this way get **Update** and **Uninstall** on their card, and a note when their repository has new commits. - **Open** sends a service's page to your default browser, already signed in (starting the service first if needed). After that, a bookmark such as `http://vmc.localhost:7600/` signs in on its own for 30 days. - **Menu bar (paper plane):** each service with a coloured dot and Open / Start / Stop / Restart, without opening the window. - **When the launcher is not running,** the window offers a Start button (it runs `lx up`). @@ -100,6 +102,9 @@ LaunchX is the only app: services stay in the browser, where they get tabs, book | `lx start ` / `lx stop ` / `lx restart ` | Control a service. Start brings up what it requires; stop first stops what requires it. Services you started come back when the launcher restarts (at login, after an update) until you stop them. | | `lx open [id]` | Open the launcher, or a service's page, in the browser. | | `lx logs [-f] [-n N]` | Captured output, including the launcher's own notes (`[launchx]`). | +| `lx install ` | Install an app from a git URL, `owner/repo` on GitHub, or a folder: clone it (a folder stays where it is), run the installer its `launchx.toml` names, register it. Asks first; `--yes` doesn't. With no source, lists what is installed. | +| `lx update […]` | Pull each installed app and, when there is something new, run its installer again, restarting what was running. `--check` only reports. | +| `lx uninstall ` | Stop the app, run its uninstaller, unregister it and delete the checkout LaunchX cloned. Your data stays, and it says where. | | `lx add ` / `lx rm ` | Register or unregister a manifest. `rm` removes the link in `~/.launchx/services`, never the manifest. | | `lx check ` | Start a temporary copy on a spare port and test it against the level it claims. | | `lx state` | The launcher's full state document as JSON. | @@ -154,6 +159,8 @@ run/.json URL, token and pid of each running service (mode 0600) logs/.log captured output, rotated at 5 MB agents/skills/ skills published to your agents, linked from their skill folders memory/ your shared memory: pages/, history/ and log.jsonl +sources// checkouts lx install cloned +installs.json what lx install installed, from where, at which commit state.json assigned ports and tokens, the session secret, and the agents and memory switches (mode 0600) ``` diff --git a/SPEC.md b/SPEC.md index 8ffffa5..5bf072e 100644 --- a/SPEC.md +++ b/SPEC.md @@ -149,6 +149,35 @@ wants = "write" reason = "Keeps your schedule page current and suggests tasks that fit your profile" ``` +### 4.9 Install fields + +The optional top-level `[install]` table says how to build and install the project from a checkout of its repository, so that `lx install` and `lx update` can do it (§10.8). LaunchX reads it only from the `launchx.toml` at the root of a checkout; a copy of the manifest elsewhere may carry it and it is ignored there. + +| Key | Type | Default | Meaning | +|---|---|---|---| +| `run` | array of strings | none | The installer: command and arguments, run in the checkout. | +| `uninstall` | array of strings | none | Undoes what `run` did (commands, launch agents, installed files) and keeps the user's data. | +| `needs` | array of strings | empty | Executables that must be on `PATH` before `run`, for example `cargo`. | +| `data` | array of strings | empty | Where the app keeps the user's data. Shown on uninstall, and what to copy when moving to another machine. LaunchX never deletes it. | + +```toml +[install] +run = ["./install.sh", "--yes"] +uninstall = ["./uninstall.sh"] +needs = ["cargo"] +data = ["~/.schedule_management"] +``` + +An installer: + +- MUST NOT prompt. Its standard input is closed. +- MUST be safe to run again over an earlier install, since that is how updates run, and MUST keep the user's data. +- MAY register a manifest of its own with `lx add`, for example one written next to the files it installed. When it has registered none of the services the checkout's manifest defines, LaunchX registers the checkout's manifest. + +It runs with the user's login environment, the directory of `lx` first on `PATH`, `LAUNCHX_HOME`, and `LAUNCHX_INSTALL` set to `install` or `update`. + +Without `run`, installing only registers the checkout's manifest. That suits a project that runs from its checkout. + ## 5. Process contract (level 1) ### 5.1 Foreground @@ -337,6 +366,8 @@ run/.json one per running service, mode 0600 logs/.log captured output, rotated agents/skills/ skills published to agents (§10.7) memory/ the user's shared memory (§12.1) +sources// checkouts that lx install cloned (§10.8) +installs.json what was installed from where (§10.8) state.json assigned ports and tokens, which services the user left running, which are discoverable by agents and what each may do with memory; mode 0600 @@ -417,6 +448,12 @@ POST /api/memory/agents body {"access": "none"|"read"|"write"} ( * /api/memory/… the shared memory (§12.4) GET /api/services/{id}/logs?tail=200 GET /api/agents what agents are told (§10.7) +POST /api/install/plan body {"source", "name"?}: fetch and say what installing would do (§10.8) +POST /api/install body {"source", "name"?}: install, as a job +POST /api/packages/{name}/update body {"force"?}: update, as a job +POST /api/packages/{name}/uninstall remove, as a job +POST /api/packages/check look for updates now +GET /api/install/log?tail=200 output of the current or last job POST /api/link one-time sign-in link; body {"service": id?, "next": path?} POST /api/reload POST /api/shutdown bearer token only @@ -462,6 +499,9 @@ Its CLI: | `lx open [id]` | Open the launcher, or a service's UI, in the browser with a session. | | `lx logs [-f]` | Show or follow captured output. | | `lx add ` / `lx rm ` | Register or unregister a manifest. | +| `lx install []` | Install an app from a repository or folder, or with no source, list what is installed (§10.8). | +| `lx update […]` | Update installed apps, all of them by default; `--check` only reports. | +| `lx uninstall ` | Remove an installed app, keeping its data. | | `lx check ` | Verify the service against its declared level. | | `lx state` | Print the launcher's state document. | | `lx agents` | List the services agents can discover (§10.7). | @@ -494,6 +534,23 @@ Every published skill is a link from the agent's folder to `$LAUNCHX_HOME/agents The switch decides what agents are told, not what they can reach. An agent runs as the user, so it can still read run files and call a service it was not told about (§11). +### 10.8 Installing, updating and removing apps + +`lx install ` installs an app. The source is a git URL, `owner/repo` for a GitHub repository, or a folder on this machine. + +- **A URL** is cloned into `$LAUNCHX_HOME/sources/`, where the name is the repository's. LaunchX owns that checkout: it pulls it to update and deletes it on uninstall. +- **A folder** is installed where it is. LaunchX never pulls, changes or deletes it. This is how to install something that is not published, or a working copy. + +LaunchX then reads the checkout's `launchx.toml`, shows its services, the installer command (§4.9) and where the app keeps data, and asks before going on (`--yes` skips the question). It checks `needs`, stops the app's running services, runs the installer, registers the app (§4.9), records it, and starts again what it stopped. Installing an app that is already installed updates it. + +`lx update […]` pulls each cloned app with `git pull --ff-only` and, when there are new commits, runs its installer again (`--force` runs it anyway). An app installed from a folder is reinstalled from the folder as it is. Running services are stopped first and started again after. `lx update --check` fetches and reports without changing anything. The launcher also checks at start and every twelve hours, without pulling, and shows the apps that have updates. + +`lx uninstall ` stops the app's services, runs its `uninstall` command, unregisters its manifests, deletes the checkout if LaunchX cloned it, and forgets it. The app's data stays, and the command says where. A service id works in place of the name. + +`installs.json` records each app: its source, checkout, whether LaunchX cloned it, its services, the commit installed, its `data` paths, and when it was installed and updated. + +The launcher's page does the same: an Install button takes a source and shows the plan before installing, and each installed app's card has Update and Uninstall. On the page each of these runs as a job whose output streams to the log panel. One install, update or uninstall runs at a time, from the page or the command line. + ## 11. Security model | Threat | Handled by | @@ -507,6 +564,7 @@ The switch decides what agents are told, not what they can reach. An agent runs | One service using memory under another's name | Memory tokens are per service and never handed to dependents; writes are logged under the caller's id, whatever it claims. | | A lost update to a shared page | Every update names the revision it was based on; a stale one is refused (§12.5). | | Other devices on the network | Everything binds to loopback by default. | +| Installing runs code from a repository | LaunchX installs only from a source the user names, shows the installer command and asks first. The page's install API needs the launcher session, like every other action. | Out of scope: a hostile process running as the same user. It can read the run files, as it can read everything else the user owns. This includes an AI agent the user runs: hiding a service from agents (§10.7) keeps it out of what they are told, and a well-behaved agent stays away, but only running the agent as another user, or in a sandbox, keeps it out. The same holds for memory: its files are the user's, so a service that reads them directly, rather than through the API, is not stopped by the switch. @@ -642,7 +700,8 @@ Commands that read take `--json`. - **Action catalogue.** A machine-readable list of each service's actions, so one generic client (or one MCP server in the launcher) can drive every service. The likely next addition. - **Remote and phone access.** Exposing the gateway on a LAN or Tailscale with pairing, so every service gets it at once. -- **Install and update.** `lx install `. For now, installing is the job of the service's own installer. +- **Restoring on a new machine.** `installs.json` lists every source, so `lx install` could reinstall them all from it. +- **Pinned versions.** `lx install` follows a repository's default branch; installing a tag or branch is not there yet. - **Jobs.** One-shot or scheduled commands, as opposed to long-running services. - **Adopting standalone instances.** A service started by hand is not taken over by the launcher. - **Memory by page.** Access is per service, for all of memory. A switch per page or per group of pages (so a service could read `schedule` but not `resume`) is the likely next step. diff --git a/src/gateway.rs b/src/gateway.rs index b6ed58f..4ac74d0 100644 --- a/src/gateway.rs +++ b/src/gateway.rs @@ -27,6 +27,7 @@ use serde_json::{Value, json}; use tokio::net::TcpListener; use tokio::sync::{broadcast, watch}; +use crate::jobs; use crate::manifest; use crate::memory::{self, Access, Who}; use crate::supervisor::{Event, Launcher}; @@ -303,6 +304,36 @@ impl App { }; json_response(StatusCode::OK, json!({ "url": self.login_link(&self.launcher_host(), &landing) })) } + (&Method::GET, ["api", "install", "log"]) => { + let lines = query(request.uri()).get("tail").and_then(|n| n.parse().ok()).unwrap_or(200usize).min(5000); + json_response(StatusCode::OK, json!({ "lines": jobs::log_tail(launcher, lines) })) + } + (&Method::POST, ["api", "install", "plan"]) => { + let body = read_json(request).await; + let Some(source) = body.get("source").and_then(Value::as_str).map(str::to_string) else { + return error(StatusCode::BAD_REQUEST, "Send {\"source\": \"\"}"); + }; + let name = body.get("name").and_then(Value::as_str).filter(|n| !n.is_empty()).map(str::to_string); + match jobs::plan(launcher, source, name).await { + Ok(plan) => json_response(StatusCode::OK, plan), + Err(message) => error(StatusCode::UNPROCESSABLE_ENTITY, &message), + } + } + (&Method::POST, ["api", "install"]) => { + let body = read_json(request).await; + let Some(source) = body.get("source").and_then(Value::as_str).map(str::to_string) else { + return error(StatusCode::BAD_REQUEST, "Send {\"source\": \"\"}"); + }; + let name = body.get("name").and_then(Value::as_str).filter(|n| !n.is_empty()).map(str::to_string); + job_response(jobs::start_install(launcher, source, name)) + } + (&Method::POST, ["api", "packages", "check"]) => json_response(StatusCode::OK, jobs::check_updates(launcher).await), + (&Method::POST, ["api", "packages", name, "update"]) => { + let name = name.to_string(); + let force = read_json(request).await.get("force").and_then(Value::as_bool).unwrap_or(false); + job_response(jobs::start_update(launcher, &name, force)) + } + (&Method::POST, ["api", "packages", name, "uninstall"]) => job_response(jobs::start_uninstall(launcher, name)), (&Method::POST, ["api", "reload"]) => json_response(StatusCode::OK, launcher.reload().await), (&Method::POST, ["api", "shutdown"]) if bearer => { let _ = self.shutdown.send(true); @@ -609,6 +640,13 @@ async fn read_json_limit(request: Request, limit: usize) -> Value { serde_json::from_slice(&body.to_bytes()).unwrap_or_else(|_| json!({})) } +fn job_response(result: Result) -> Response { + match result { + Ok(job) => json_response(StatusCode::ACCEPTED, json!({ "job": job })), + Err(message) => error(StatusCode::CONFLICT, &message), + } +} + fn json_response(status: StatusCode, value: Value) -> Response { Response::builder() .status(status) diff --git a/src/install.rs b/src/install.rs new file mode 100644 index 0000000..b567a3e --- /dev/null +++ b/src/install.rs @@ -0,0 +1,615 @@ +//! Installing, updating and removing apps from their repositories (contract §4.9, §10.8). +//! +//! The work here is synchronous: fetching with git, running an app's installer, +//! registering its manifest and recording it in `installs.json`. Stopping and +//! starting services around that work is the caller's, through [`Services`], +//! since the command line does it through the daemon's API and the daemon does +//! it directly. + +use std::collections::BTreeMap; +use std::fs; +use std::future::Future; +use std::io::{BufRead, BufReader}; +use std::os::fd::AsRawFd; +use std::path::{Path, PathBuf}; +use std::process::{Command, Stdio}; +use std::sync::Arc; + +use anyhow::{Context, bail}; +use serde::{Deserialize, Serialize}; +use serde_json::{Value, json}; + +use crate::home::Home; +use crate::manifest::{self, InstallDef, Service}; +use crate::platform; + +/// Where a line of output goes: the terminal, or the job log. +pub type Out = Arc; + +// --- What is installed ---------------------------------------------------------- + +/// One installed app, as `installs.json` records it. +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +pub struct Package { + pub name: String, + /// A git URL, or the absolute path of a folder. + pub source: String, + /// The checkout the app was installed from. + pub dir: PathBuf, + /// Whether LaunchX cloned `dir`, and so may pull and delete it. + pub cloned: bool, + /// The services the checkout's manifest defines. + #[serde(default)] + pub services: Vec, + #[serde(default)] + pub commit: Option, + /// Where the app keeps the user's data (`[install] data`). + #[serde(default)] + pub data: Vec, + #[serde(default)] + pub installed_at: String, + #[serde(default)] + pub updated_at: Option, + /// New commits upstream at the last check, for a cloned app. + #[serde(default)] + pub behind: Option, + #[serde(default)] + pub checked_at: Option, +} + +impl Package { + pub fn to_json(&self) -> Value { + json!({ + "name": self.name, + "source": self.source, + "dir": self.dir, + "cloned": self.cloned, + "services": self.services, + "commit": self.commit, + "data": self.data, + "installed_at": self.installed_at, + "updated_at": self.updated_at, + "behind": self.behind, + "checked_at": self.checked_at, + }) + } +} + +#[derive(Clone, Debug, Default, Serialize, Deserialize)] +pub struct Installs { + #[serde(default)] + pub packages: BTreeMap, +} + +impl Installs { + pub fn path(home: &Home) -> PathBuf { + home.root.join("installs.json") + } + + /// What is installed; empty when nothing is, or the file cannot be read. + pub fn load(home: &Home) -> Installs { + fs::read_to_string(Self::path(home)).ok().and_then(|text| serde_json::from_str(&text).ok()).unwrap_or_default() + } + + pub fn save(&self, home: &Home) -> anyhow::Result<()> { + home.ensure()?; + crate::home::write_private(&Self::path(home), &serde_json::to_vec_pretty(self)?) + } + + /// The installed app that defines service `id`. + pub fn owner_of(&self, id: &str) -> Option<&Package> { + self.packages.values().find(|p| p.services.iter().any(|s| s == id)) + } + + /// An app by its name, or by the id of one of its services. + pub fn find(&self, name_or_id: &str) -> Option<&Package> { + self.packages.get(name_or_id).or_else(|| self.owner_of(name_or_id)) + } +} + +pub fn sources_dir(home: &Home) -> PathBuf { + home.root.join("sources") +} + +// --- One job at a time ------------------------------------------------------------ + +/// Held while an install, update or uninstall runs, from the page or the +/// command line. Released when dropped. +pub struct Lock(#[allow(dead_code)] fs::File); + +pub fn try_lock(home: &Home) -> anyhow::Result> { + home.ensure()?; + let file = fs::OpenOptions::new().create(true).truncate(false).write(true).open(home.root.join("install.lock"))?; + // SAFETY: flock(2) on a descriptor we own; released when the file closes. + let locked = unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) } == 0; + Ok(locked.then_some(Lock(file))) +} + +pub fn lock(home: &Home) -> anyhow::Result { + try_lock(home)?.ok_or_else(|| anyhow::anyhow!("Another install, update or uninstall is running. Try again when it has finished.")) +} + +// --- Sources ---------------------------------------------------------------------- + +#[derive(Clone, Debug, PartialEq)] +pub enum Source { + Git(String), + Local(PathBuf), +} + +impl Source { + pub fn parse(text: &str) -> anyhow::Result { + let text = text.trim(); + if text.is_empty() { + bail!("Name a source: a git URL, owner/repo on GitHub, or a folder"); + } + let path = manifest::expand_tilde(text); + if path.exists() { + let path = fs::canonicalize(&path)?; + if !path.is_dir() { + bail!("{} is not a folder", path.display()); + } + return Ok(Source::Local(path)); + } + if ["https://", "http://", "ssh://", "git://", "file://", "git@"].iter().any(|scheme| text.starts_with(scheme)) { + return Ok(Source::Git(text.trim_end_matches('/').to_string())); + } + let parts: Vec<&str> = text.split('/').collect(); + let plain = |part: &str| !part.is_empty() && !part.starts_with('.') && part.chars().all(|c| c.is_ascii_alphanumeric() || "-_.".contains(c)); + if parts.len() == 2 && parts.iter().all(|part| plain(part)) && !text.starts_with('.') { + return Ok(Source::Git(format!("https://github.com/{}", text.trim_end_matches(".git")))); + } + bail!("{text} is not a folder, a git URL, or owner/repo on GitHub") + } + + pub fn text(&self) -> String { + match self { + Source::Git(url) => url.clone(), + Source::Local(path) => path.display().to_string(), + } + } + + /// A name for the app: the repository's or the folder's, made safe. + pub fn default_name(&self) -> String { + let last = match self { + Source::Git(url) => url.rsplit(['/', ':']).next().unwrap_or("").trim_end_matches(".git").to_string(), + Source::Local(path) => path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default(), + }; + clean_name(&last) + } +} + +pub fn clean_name(text: &str) -> String { + let mapped: String = text.to_ascii_lowercase().chars().map(|c| if c.is_ascii_alphanumeric() || c == '-' { c } else { '-' }).collect(); + let name = mapped.split('-').filter(|part| !part.is_empty()).collect::>().join("-"); + if name.is_empty() { "app".into() } else { name.chars().take(48).collect() } +} + +// --- Running commands ------------------------------------------------------------- + +/// The environment for git and installers: `base` (the user's login +/// environment), the directory of this `lx` first on PATH, and LAUNCHX_HOME. +pub fn environment(home: &Home, base: Vec<(String, String)>, action: &str) -> Vec<(String, String)> { + let mut env: Vec<(String, String)> = base.into_iter().filter(|(key, _)| !key.starts_with("LAUNCHX")).collect(); + let mut path = platform::lookup(&env, "PATH").unwrap_or("/usr/bin:/bin").to_string(); + if let Some(dir) = std::env::current_exe().ok().and_then(|exe| exe.parent().map(Path::to_path_buf)) { + path = format!("{}:{path}", dir.display()); + } + env.retain(|(key, _)| key != "PATH"); + env.push(("PATH".into(), path)); + let root = std::path::absolute(&home.root).unwrap_or_else(|_| home.root.clone()); + env.push(("LAUNCHX_HOME".into(), root.display().to_string())); + env.push(("LAUNCHX_INSTALL".into(), action.into())); + // Never wait for a password nobody can type. + env.push(("GIT_TERMINAL_PROMPT".into(), "0".into())); + if platform::lookup(&env, "GIT_SSH_COMMAND").is_none() { + env.push(("GIT_SSH_COMMAND".into(), "ssh -o BatchMode=yes".into())); + } + env +} + +/// Run a command in `cwd` with stdin closed, sending each line of its output +/// (stdout and stderr together) to `out`. +pub fn run(args: &[String], cwd: &Path, env: &[(String, String)], out: &Out) -> anyhow::Result<()> { + let mut child = Command::new("/bin/sh") + .args(["-c", "exec 2>&1; exec \"$@\"", "sh"]) + .args(args) + .current_dir(cwd) + .env_clear() + .envs(env.iter().map(|(k, v)| (k.as_str(), v.as_str()))) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::null()) + .spawn() + .with_context(|| format!("could not run {}", args.join(" ")))?; + let mut reader = BufReader::new(child.stdout.take().expect("piped stdout")); + let mut bytes = Vec::new(); + loop { + bytes.clear(); + if reader.read_until(b'\n', &mut bytes)? == 0 { + break; + } + let text = String::from_utf8_lossy(&bytes); + // A progress bar rewrites its line with \r; keep what it ended on. + let line = text.trim_end_matches(['\n', '\r']).rsplit('\r').next().unwrap_or(""); + out(line); + } + let status = child.wait()?; + if !status.success() { + bail!("`{}` failed ({})", args.join(" "), crate::supervisor::describe(status)); + } + Ok(()) +} + +/// A git command's output, quietly. +fn git(dir: &Path, env: &[(String, String)], args: &[&str]) -> anyhow::Result { + let output = Command::new("git") + .args(args) + .current_dir(dir) + .env_clear() + .envs(env.iter().map(|(k, v)| (k.as_str(), v.as_str()))) + .stdin(Stdio::null()) + .output() + .context("could not run git; is it installed?")?; + if !output.status.success() { + bail!("git {} failed: {}", args.join(" "), String::from_utf8_lossy(&output.stderr).trim()); + } + Ok(String::from_utf8_lossy(&output.stdout).trim().to_string()) +} + +fn git_loud(dir: &Path, env: &[(String, String)], args: &[&str], out: &Out) -> anyhow::Result<()> { + out(&format!("$ git {}", args.join(" "))); + run(&std::iter::once("git").chain(args.iter().copied()).map(String::from).collect::>(), dir, env, out) +} + +fn head(dir: &Path, env: &[(String, String)]) -> Option { + git(dir, env, &["rev-parse", "HEAD"]).ok().filter(|commit| !commit.is_empty()) +} + +fn same_remote(a: &str, b: &str) -> bool { + let norm = |url: &str| url.trim_end_matches('/').trim_end_matches(".git").to_ascii_lowercase(); + norm(a) == norm(b) +} + +// --- Plans ------------------------------------------------------------------------ + +/// What installing would do, after the source has been fetched. +#[derive(Clone, Debug)] +pub struct Plan { + pub name: String, + pub source: Source, + pub dir: PathBuf, + pub manifest: PathBuf, + pub services: Vec, + pub install: InstallDef, + /// The app as installed now, when this is an update. + pub existing: Option, +} + +impl Plan { + pub fn ids(&self) -> Vec { + self.services.iter().map(|s| s.id.clone()).collect() + } + + pub fn to_json(&self) -> Value { + json!({ + "name": self.name, + "source": self.source.text(), + "dir": self.dir, + "cloned": matches!(self.source, Source::Git(_)), + "services": self.services.iter().map(|s| json!({ + "id": s.id, "name": s.def.name, "description": s.def.description, "level": s.def.level, + })).collect::>(), + "run": self.install.run, + "uninstall": self.install.uninstall, + "needs": self.install.needs, + "data": self.install.data, + "installed": self.existing.is_some(), + }) + } + + /// The plan, as the command line shows it before asking. + pub fn describe(&self) -> Vec { + let mut lines = vec![format!("{} {}", if self.existing.is_some() { "Reinstall" } else { "Install" }, self.name)]; + lines.push(format!(" from {}", self.source.text())); + if matches!(self.source, Source::Git(_)) { + lines.push(format!(" checkout {}", self.dir.display())); + } + for service in &self.services { + lines.push(format!(" service {} ({}), level {}", service.id, service.def.name, service.def.level)); + } + if self.install.run.is_empty() { + lines.push(" installer none: LaunchX registers the manifest and runs the app from the checkout".into()); + } else { + lines.push(format!(" runs {}", self.install.run.join(" "))); + } + if !self.install.needs.is_empty() { + lines.push(format!(" needs {}", self.install.needs.join(", "))); + } + for data in &self.install.data { + lines.push(format!(" data {data}")); + } + lines + } +} + +/// Read what a checkout would install. +fn plan_from(home: &Home, name: &str, source: Source, dir: &Path) -> anyhow::Result { + let file = dir.join("launchx.toml"); + if !file.is_file() { + bail!("{} has no launchx.toml, so it is not a LaunchX app", dir.display()); + } + let (services, install) = manifest::parse_with_install(&file, &file)?; + let existing = Installs::load(home).packages.get(name).cloned(); + Ok(Plan { name: name.into(), source, dir: dir.to_path_buf(), manifest: file, services, install: install.unwrap_or_default(), existing }) +} + +/// Fetch a source and read what installing it would do. A git source is +/// cloned into `sources/`, or pulled when it already is. +pub fn prepare(home: &Home, env: &[(String, String)], text: &str, name: Option<&str>, out: &Out) -> anyhow::Result { + let source = Source::parse(text)?; + let installs = Installs::load(home); + // Installing what is installed updates it, under the name it has. + let known = installs.packages.values().find(|p| match &source { + Source::Git(url) => p.cloned && same_remote(&p.source, url), + Source::Local(path) => !p.cloned && p.dir == *path, + }); + let name = match (name, known) { + (Some(given), _) => clean_name(given), + (None, Some(package)) => package.name.clone(), + (None, None) => source.default_name(), + }; + if let Some(other) = installs.packages.get(&name) { + if known.map(|p| &p.name) != Some(&other.name) { + bail!("An app named '{name}' is already installed from {}. Choose another name with --name.", other.source); + } + } + let dir = match &source { + Source::Local(path) => path.clone(), + Source::Git(url) => { + let dir = sources_dir(home).join(&name); + if dir.join(".git").exists() { + let origin = git(&dir, env, &["remote", "get-url", "origin"]).unwrap_or_default(); + if !same_remote(&origin, url) { + bail!("{} is a checkout of {origin}, not {url}. Choose another name with --name.", dir.display()); + } + git_loud(&dir, env, &["pull", "--ff-only"], out)?; + } else { + if dir.exists() { + bail!("{} exists and is not a git checkout; move it away first", dir.display()); + } + fs::create_dir_all(sources_dir(home))?; + git_loud(&sources_dir(home), env, &["clone", url, &name], out)?; + } + dir + } + }; + plan_from(home, &name, source, &dir) +} + +// --- Install, update, uninstall: the file work ------------------------------------- + +/// Run the installer, register the app and record it. +pub fn install_files(home: &Home, env: &[(String, String)], plan: &Plan, out: &Out) -> anyhow::Result { + let path = platform::lookup(env, "PATH").unwrap_or("/usr/bin:/bin").to_string(); + let missing: Vec<&String> = plan.install.needs.iter().filter(|need| platform::which(need, &path).is_none()).collect(); + if !missing.is_empty() { + bail!("{} needs {} on PATH; install that first", plan.name, missing.iter().map(|m| format!("`{m}`")).collect::>().join(", ")); + } + let ids = plan.ids(); + let (registered, _) = manifest::load_registry(home); + if plan.install.run.is_empty() { + // The checkout's manifest is what runs, so a service of the same id + // registered from elsewhere would run instead of it. + for service in registered.iter().filter(|s| ids.contains(&s.id)) { + if service.manifest != plan.manifest { + bail!("'{}' is already registered from {}. Remove it first: lx rm {}", service.id, service.manifest.display(), service.id); + } + } + } else { + out(&format!("$ {}", plan.install.run.join(" "))); + run(&plan.install.run, &plan.dir, env, out)?; + } + let (registered, _) = manifest::load_registry(home); + let present: Vec<&String> = ids.iter().filter(|id| registered.iter().any(|s| &&s.id == id)).collect(); + if present.is_empty() { + manifest::register(home, &plan.manifest)?; + out(&format!("Registered {} from {}", ids.join(", "), plan.manifest.display())); + } else if present.len() < ids.len() { + out(&format!("The installer registered {} but not the rest of {}", present.iter().map(|s| s.as_str()).collect::>().join(", "), ids.join(", "))); + } + let now = crate::util::now_rfc3339(); + let package = Package { + name: plan.name.clone(), + source: plan.source.text(), + dir: plan.dir.clone(), + cloned: matches!(plan.source, Source::Git(_)), + services: ids, + commit: head(&plan.dir, env), + data: plan.install.data.clone(), + installed_at: plan.existing.as_ref().map(|p| p.installed_at.clone()).unwrap_or_else(|| now.clone()), + updated_at: plan.existing.as_ref().map(|_| now.clone()), + behind: plan.existing.as_ref().and(Some(0)), + checked_at: plan.existing.as_ref().and_then(|p| p.checked_at.clone()), + }; + let mut installs = Installs::load(home); + installs.packages.insert(package.name.clone(), package.clone()); + installs.save(home)?; + Ok(package) +} + +/// Pull a cloned app. Returns the plan to install from, or None when there is +/// nothing new and `force` is not set. An app from a folder always reinstalls. +pub fn prepare_update(home: &Home, env: &[(String, String)], package: &Package, force: bool, out: &Out) -> anyhow::Result> { + let source = if package.cloned { Source::Git(package.source.clone()) } else { Source::Local(package.dir.clone()) }; + if !package.dir.is_dir() { + bail!("{} is gone. Install it again with: lx install {}", package.dir.display(), package.source); + } + if package.cloned { + let before = head(&package.dir, env); + git_loud(&package.dir, env, &["pull", "--ff-only"], out)?; + let after = head(&package.dir, env); + if before == after && package.commit == after && !force { + let mut installs = Installs::load(home); + if let Some(record) = installs.packages.get_mut(&package.name) { + record.behind = Some(0); + record.checked_at = Some(crate::util::now_rfc3339()); + installs.save(home)?; + } + return Ok(None); + } + } + plan_from(home, &package.name, source, &package.dir).map(Some) +} + +/// Run the app's uninstall command, unregister it, delete the checkout if +/// LaunchX cloned it, and forget it. The app's data stays. +pub fn uninstall_files(home: &Home, env: &[(String, String)], package: &Package, out: &Out) -> anyhow::Result<()> { + let uninstall = manifest::parse_with_install(&package.dir.join("launchx.toml"), &package.dir.join("launchx.toml")) + .ok() + .and_then(|(_, install)| install) + .map(|install| install.uninstall) + .unwrap_or_default(); + if !uninstall.is_empty() { + out(&format!("$ {}", uninstall.join(" "))); + run(&uninstall, &package.dir, env, out)?; + } + let (registered, _) = manifest::load_registry(home); + for id in &package.services { + if registered.iter().any(|s| &s.id == id) { + match manifest::unregister(home, id) { + Ok(removed) => out(&format!("Unregistered {}", removed.join(", "))), + // A sibling's removal already took it. + Err(_) if !manifest::load_registry(home).0.iter().any(|s| &s.id == id) => {} + Err(error) => return Err(error), + } + } + } + if package.cloned && package.dir.starts_with(sources_dir(home)) && package.dir.is_dir() { + fs::remove_dir_all(&package.dir).with_context(|| format!("removing {}", package.dir.display()))?; + out(&format!("Deleted the checkout {}", package.dir.display())); + } + let mut installs = Installs::load(home); + installs.packages.remove(&package.name); + installs.save(home)?; + Ok(()) +} + +/// Fetch a cloned app and count the commits it is behind. Records the answer. +pub fn check(home: &Home, env: &[(String, String)], name: &str) -> anyhow::Result> { + let Some(package) = Installs::load(home).packages.get(name).cloned() else { bail!("Nothing named '{name}' is installed") }; + if !package.cloned { + return Ok(None); + } + git(&package.dir, env, &["fetch", "--quiet"])?; + let behind: u32 = git(&package.dir, env, &["rev-list", "--count", "HEAD..@{upstream}"])?.parse().unwrap_or(0); + let mut installs = Installs::load(home); + if let Some(record) = installs.packages.get_mut(name) { + record.behind = Some(behind); + record.checked_at = Some(crate::util::now_rfc3339()); + installs.save(home)?; + } + Ok(Some(behind)) +} + +// --- Around the file work: stopping and starting services --------------------------- + +/// How the caller stops and starts services: through the daemon's API, or +/// the daemon itself. Each is a no-op when no launcher runs. +pub trait Services: Sync { + /// Stop these services and what requires them; return what was running. + fn stop(&self, ids: &[String]) -> impl Future> + Send; + /// Re-read the registered manifests, then start these services. + fn reload_and_start(&self, ids: &[String]) -> impl Future + Send; +} + +async fn blocking(work: impl FnOnce() -> anyhow::Result + Send + 'static) -> anyhow::Result { + tokio::task::spawn_blocking(work).await.map_err(|error| anyhow::anyhow!("install task failed: {error}"))? +} + +/// Install from a plan: stop the app's running services, run the file work, +/// then start again what was stopped. +pub async fn install(home: &Home, env: &[(String, String)], plan: Plan, services: &S, out: &Out) -> anyhow::Result { + let stopped = services.stop(&plan.ids()).await; + if !stopped.is_empty() { + out(&format!("Stopped {} while installing", stopped.join(", "))); + } + let (home2, env2, out2) = (home.clone(), env.to_vec(), out.clone()); + let result = blocking(move || install_files(&home2, &env2, &plan, &out2)).await; + services.reload_and_start(&stopped).await; + if !stopped.is_empty() { + out(&format!("Started {} again", stopped.join(", "))); + } + result +} + +/// What one update did. +pub enum Updated { + UpToDate, + Installed(Box), +} + +pub async fn update(home: &Home, env: &[(String, String)], package: &Package, force: bool, services: &S, out: &Out) -> anyhow::Result { + let (home2, env2, out2, package2) = (home.clone(), env.to_vec(), out.clone(), package.clone()); + match blocking(move || prepare_update(&home2, &env2, &package2, force, &out2)).await? { + None => Ok(Updated::UpToDate), + Some(plan) => install(home, env, plan, services, out).await.map(|package| Updated::Installed(Box::new(package))), + } +} + +pub async fn uninstall(home: &Home, env: &[(String, String)], package: &Package, services: &S, out: &Out) -> anyhow::Result<()> { + let stopped = services.stop(&package.services).await; + if !stopped.is_empty() { + out(&format!("Stopped {}", stopped.join(", "))); + } + let (home2, env2, out2, package2) = (home.clone(), env.to_vec(), out.clone(), package.clone()); + let result = blocking(move || uninstall_files(&home2, &env2, &package2, &out2)).await; + // Whatever was stopped and is still registered (a dependent) comes back. + services.reload_and_start(&stopped.into_iter().filter(|id| !package.services.contains(id)).collect::>()).await; + result +} + +/// Text without terminal colour codes, for the page's log. +pub fn plain(line: &str) -> String { + let mut text = String::with_capacity(line.len()); + let mut chars = line.chars().peekable(); + while let Some(c) = chars.next() { + if c == '\u{1b}' { + if chars.peek() == Some(&'[') { + chars.next(); + for next in chars.by_ref() { + if next.is_ascii_alphabetic() { + break; + } + } + } + continue; + } + text.push(c); + } + text +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn sources_and_names() { + assert_eq!(Source::parse("AutoPhd-org/launchx").unwrap(), Source::Git("https://github.com/AutoPhd-org/launchx".into())); + assert_eq!(Source::parse("https://github.com/a/b.git/").unwrap(), Source::Git("https://github.com/a/b.git".into())); + assert_eq!(Source::parse("git@github.com:AutoPhd-org/your-paper-reading-agent.git").unwrap().default_name(), "your-paper-reading-agent"); + assert_eq!(Source::parse("https://github.com/AutoPhd-org/Schedule_Everything").unwrap().default_name(), "schedule-everything"); + assert!(Source::parse("not a source").is_err()); + assert!(Source::parse("./missing/dir").is_err()); + let here = std::env::temp_dir(); + assert!(matches!(Source::parse(&here.display().to_string()).unwrap(), Source::Local(_))); + assert_eq!(clean_name("--Odd Name!!"), "odd-name"); + assert!(same_remote("https://github.com/A/b.git", "https://github.com/a/b/")); + } + + #[test] + fn colour_codes_are_stripped() { + assert_eq!(plain("\u{1b}[0;32m[SUCCESS]\u{1b}[0m done"), "[SUCCESS] done"); + } +} diff --git a/src/jobs.rs b/src/jobs.rs new file mode 100644 index 0000000..3a764e4 --- /dev/null +++ b/src/jobs.rs @@ -0,0 +1,225 @@ +//! Installs, updates and uninstalls the launcher's page starts (contract §10.8). +//! Each runs as a job in the background; its output goes to `logs/install.log` +//! and, line by line, to the event stream as `log` events with the id `.install`. + +use std::fs; +use std::future::Future; +use std::io::Write; +use std::sync::Arc; +use std::time::Duration; + +use serde::Serialize; +use serde_json::{Value, json}; + +use crate::install::{self, Installs, Out}; +use crate::supervisor::{Event, Launcher, Status}; +use crate::util; + +/// The id log events of a job carry. +pub const LOG_ID: &str = ".install"; + +#[derive(Clone, Debug, Serialize)] +pub struct Job { + /// `install`, `update` or `uninstall`. + pub action: String, + /// The app, or for an install, the source until its name is known. + pub name: String, + /// `running`, `done` or `failed`. + pub status: String, + pub message: Option, + pub started_at: String, + pub finished_at: Option, +} + +impl install::Services for Arc { + async fn stop(&self, ids: &[String]) -> Vec { + let mut stopped: Vec = Vec::new(); + for id in ids { + let running = self.runtime(id).is_some_and(|rt| !matches!(rt.status, Status::Stopped | Status::Failed)); + if running { + for other in Launcher::stop(self, id).await.unwrap_or_default() { + if !stopped.contains(&other) { + stopped.push(other); + } + } + } + } + stopped + } + + async fn reload_and_start(&self, ids: &[String]) { + self.reload().await; + for id in ids { + let _ = self.start(id); + } + } +} + +fn log_path(launcher: &Launcher) -> std::path::PathBuf { + launcher.home.logs_dir().join("install.log") +} + +fn changed(launcher: &Launcher) { + let _ = launcher.events.send(Event::Change(String::new())); +} + +/// Output of the current job: appended to its log and sent to the page. +fn job_out(launcher: &Arc) -> Out { + let launcher = launcher.clone(); + Arc::new(move |line: &str| { + let line = install::plain(line); + if let Ok(mut file) = fs::OpenOptions::new().create(true).append(true).open(log_path(&launcher)) { + let _ = writeln!(file, "{line}"); + } + let _ = launcher.events.send(Event::Log { id: LOG_ID.into(), line }); + }) +} + +pub fn log_tail(launcher: &Launcher, lines: usize) -> Vec { + crate::supervisor::tail(&log_path(launcher), lines) +} + +/// Start a job, unless one is running here or on the command line. +pub fn begin(launcher: &Arc, action: &str, name: &str, work: F) -> Result +where + F: FnOnce(Arc, Out) -> Fut + Send + 'static, + Fut: Future> + Send + 'static, +{ + let busy = "Another install, update or uninstall is running. Try again when it has finished.".to_string(); + let lock = install::try_lock(&launcher.home).map_err(|e| e.to_string())?.ok_or_else(|| busy.clone())?; + let job = { + let mut slot = launcher.job.lock().unwrap_or_else(|p| p.into_inner()); + if slot.as_ref().is_some_and(|job| job.status == "running") { + return Err(busy); + } + let job = Job { + action: action.into(), + name: name.into(), + status: "running".into(), + message: None, + started_at: util::now_rfc3339(), + finished_at: None, + }; + *slot = Some(job.clone()); + job + }; + let _ = fs::create_dir_all(launcher.home.logs_dir()); + let _ = fs::write(log_path(launcher), ""); + changed(launcher); + let out = job_out(launcher); + out(&format!("[launchx] {action} {name} — {}", job.started_at)); + let launcher = launcher.clone(); + tokio::spawn(async move { + let _lock = lock; + let result = work(launcher.clone(), out.clone()).await; + let (status, message) = match result { + Ok(message) => ("done", message), + Err(error) => ("failed", format!("{error:#}")), + }; + out(&format!("[launchx] {message}")); + if let Some(job) = launcher.job.lock().unwrap_or_else(|p| p.into_inner()).as_mut() { + job.status = status.into(); + job.message = Some(message); + job.finished_at = Some(util::now_rfc3339()); + } + changed(&launcher); + }); + Ok(job) +} + +fn env(launcher: &Launcher, action: &str) -> Vec<(String, String)> { + install::environment(&launcher.home, launcher.env(), action) +} + +/// Fetch a source and say what installing it would do, without installing. +pub async fn plan(launcher: &Arc, source: String, name: Option) -> Result { + let launcher = launcher.clone(); + tokio::task::spawn_blocking(move || { + let _lock = install::try_lock(&launcher.home) + .map_err(|e| e.to_string())? + .ok_or("Another install, update or uninstall is running. Try again when it has finished.")?; + let quiet: Out = Arc::new(|_: &str| {}); + let plan = install::prepare(&launcher.home, &env(&launcher, "install"), &source, name.as_deref(), &quiet).map_err(|e| format!("{e:#}"))?; + Ok(plan.to_json()) + }) + .await + .map_err(|e| e.to_string())? +} + +pub fn start_install(launcher: &Arc, source: String, name: Option) -> Result { + let label = name.clone().unwrap_or_else(|| source.clone()); + begin(launcher, "install", &label, move |launcher, out| async move { + let home = launcher.home.clone(); + let plan = { + let (home, env, out) = (home.clone(), env(&launcher, "install"), out.clone()); + tokio::task::spawn_blocking(move || install::prepare(&home, &env, &source, name.as_deref(), &out)).await?? + }; + let action = if plan.existing.is_some() { "update" } else { "install" }; + let app = plan.name.clone(); + if let Some(job) = launcher.job.lock().unwrap_or_else(|p| p.into_inner()).as_mut() { + job.name = app.clone(); + } + let package = install::install(&home, &env(&launcher, action), plan, &launcher, &out).await?; + Ok(format!("Installed {app} ({})", short(&package.commit))) + }) +} + +pub fn start_update(launcher: &Arc, name: &str, force: bool) -> Result { + let package = Installs::load(&launcher.home).find(name).cloned().ok_or_else(|| format!("Nothing named '{name}' is installed"))?; + begin(launcher, "update", &package.name.clone(), move |launcher, out| async move { + let env = env(&launcher, "update"); + match install::update(&launcher.home, &env, &package, force, &launcher, &out).await? { + install::Updated::UpToDate => Ok(format!("{} is up to date ({})", package.name, short(&package.commit))), + install::Updated::Installed(new) => Ok(format!("Updated {} {} → {}", package.name, short(&package.commit), short(&new.commit))), + } + }) +} + +pub fn start_uninstall(launcher: &Arc, name: &str) -> Result { + let package = Installs::load(&launcher.home).find(name).cloned().ok_or_else(|| format!("Nothing named '{name}' is installed"))?; + begin(launcher, "uninstall", &package.name.clone(), move |launcher, out| async move { + let env = env(&launcher, "uninstall"); + install::uninstall(&launcher.home, &env, &package, &launcher, &out).await?; + let kept = if package.data.is_empty() { String::new() } else { format!("; your data is still in {}", package.data.join(", ")) }; + Ok(format!("Uninstalled {}{kept}", package.name)) + }) +} + +/// Look for updates to every cloned app, without pulling. Skipped while a job runs. +pub async fn check_updates(launcher: &Arc) -> Value { + let worker = launcher.clone(); + let problems = tokio::task::spawn_blocking(move || { + let Ok(Some(_lock)) = install::try_lock(&worker.home) else { return vec!["An install is running; check again later".to_string()] }; + let env = env(&worker, "update"); + let mut problems = Vec::new(); + for package in Installs::load(&worker.home).packages.values().filter(|p| p.cloned) { + if let Err(error) = install::check(&worker.home, &env, &package.name) { + problems.push(format!("{}: {error:#}", package.name)); + } + } + problems + }) + .await + .unwrap_or_default(); + changed(launcher); + let installs = Installs::load(&launcher.home); + json!({ "packages": installs.packages.values().map(install::Package::to_json).collect::>(), "errors": problems }) +} + +/// Check for updates a minute after start, then every twelve hours. +pub fn watch_updates(launcher: &Arc) { + let launcher = launcher.clone(); + tokio::spawn(async move { + tokio::time::sleep(Duration::from_secs(60)).await; + loop { + if !Installs::load(&launcher.home).packages.is_empty() { + check_updates(&launcher).await; + } + tokio::time::sleep(Duration::from_secs(12 * 3600)).await; + } + }); +} + +fn short(commit: &Option) -> String { + commit.as_deref().map(|c| c.chars().take(7).collect()).unwrap_or_else(|| "—".into()) +} diff --git a/src/main.rs b/src/main.rs index 0d29c03..78a9e6d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,8 @@ mod check; mod client; mod gateway; mod home; +mod install; +mod jobs; mod manifest; mod memory; mod platform; @@ -72,6 +74,36 @@ enum Command { Add { path: PathBuf }, /// Unregister the manifest that defines a service Rm { id: String }, + /// Install an app from a git URL, owner/repo on GitHub, or a folder; with no source, list installed apps + Install { + source: Option, + /// The name to install it under (default: the repository's) + #[arg(long)] + name: Option, + /// Do not ask before running the app's installer + #[arg(short, long)] + yes: bool, + #[arg(long)] + json: bool, + }, + /// Update installed apps: pull, run the installer again, restart what was running + Update { + /// Apps (or service ids) to update; all of them when left out + names: Vec, + /// Only fetch and report what has updates + #[arg(long)] + check: bool, + /// Run the installer even when there is nothing new + #[arg(long)] + force: bool, + }, + /// Remove an installed app: run its uninstaller, unregister it, delete the checkout LaunchX cloned. Its data stays + Uninstall { + /// The app's name, or one of its service ids + name: String, + #[arg(short, long)] + yes: bool, + }, /// Start a service on a spare port and test it against its declared level Check { id: String, @@ -247,6 +279,10 @@ async fn run(command: Command) -> anyhow::Result { Command::Logs { id, follow, lines } => logs(home, &id, follow, lines).await, Command::Add { path } => add(home, &path).await, Command::Rm { id } => rm(home, &id).await, + Command::Install { source: None, json, .. } => list_installs(home, json), + Command::Install { source: Some(source), name, yes, .. } => install_app(home, &source, name.as_deref(), yes).await, + Command::Update { names, check, force } => update_apps(home, names, check, force).await, + Command::Uninstall { name, yes } => uninstall_app(home, &name, yes).await, Command::Check { id, json } => { let config = home.config()?; let ok = check::check(&home, &config, &id, json).await?; @@ -294,6 +330,7 @@ async fn daemon(home: Home) -> anyhow::Result { let app = gateway::App::new(launcher.clone(), shutdown_tx.clone()); let server = tokio::spawn(gateway::serve(app, listener)); launcher.autostart(); + jobs::watch_updates(&launcher); println!("LaunchX {} listening at {}", supervisor::VERSION, config.launcher_url()); let mut terminate = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())?; @@ -1020,31 +1057,11 @@ async fn logs(home: Home, id: &str, follow: bool, lines: usize) -> anyhow::Resul } async fn add(home: Home, path: &std::path::Path) -> anyhow::Result { - home.ensure()?; let file = manifest::locate(path)?; - let services = manifest::parse(&file, &file)?; - let (existing, _) = manifest::load_registry(&home); - if let Some(same) = existing.iter().find(|s| s.manifest == file) { - println!("Already registered: {} ({})", same.entry.display(), file.display()); + let Some(services) = manifest::register(&home, &file)? else { + println!("Already registered: {}", file.display()); return Ok(ExitCode::SUCCESS); - } - for service in &services { - if let Some(other) = existing.iter().find(|s| s.id == service.id) { - bail!("A service named '{}' is already registered from {}. Remove it first: lx rm {}", service.id, other.manifest.display(), service.id); - } - } - let stem: String = file - .parent() - .and_then(|p| p.file_name()) - .map(|n| n.to_string_lossy().chars().map(|c| if c.is_ascii_alphanumeric() || c == '-' || c == '_' { c } else { '-' }).collect()) - .unwrap_or_else(|| services[0].id.clone()); - let mut link = home.services_dir().join(format!("{stem}.toml")); - let mut n = 2; - while link.symlink_metadata().is_ok() { - link = home.services_dir().join(format!("{stem}-{n}.toml")); - n += 1; - } - std::os::unix::fs::symlink(&file, &link).with_context(|| format!("linking {}", link.display()))?; + }; for service in &services { println!("Registered {} ({}), level {}, from {}", service.id, service.def.name, service.def.level, file.display()); } @@ -1063,8 +1080,7 @@ async fn add(home: Home, path: &std::path::Path) -> anyhow::Result { async fn rm(home: Home, id: &str) -> anyhow::Result { let (services, _) = manifest::load_registry(&home); let Some(service) = services.iter().find(|s| s.id == id) else { bail!("Unknown service '{id}'") }; - let entry = service.entry.clone(); - let siblings: Vec<&str> = services.iter().filter(|s| s.entry == entry).map(|s| s.id.as_str()).collect(); + let siblings: Vec = services.iter().filter(|s| s.entry == service.entry).map(|s| s.id.clone()).collect(); if siblings.len() > 1 { println!("{} also defines {}; unregistering all of them.", service.manifest.display(), siblings.join(", ")); } @@ -1074,13 +1090,218 @@ async fn rm(home: Home, id: &str) -> anyhow::Result { daemon.post(&format!("/api/services/{sibling}/stop"), json!({})).await?; } } - if !entry.starts_with(home.services_dir()) { - bail!("refusing to remove {}, which is outside {}", entry.display(), home.services_dir().display()); - } - fs::remove_file(&entry).with_context(|| format!("removing {}", entry.display()))?; + let removed = manifest::unregister(&home, id)?; if let Some(daemon) = &daemon { daemon.post("/api/reload", json!({})).await?; } - println!("Unregistered {} (the manifest itself is untouched).", siblings.join(", ")); + println!("Unregistered {} (the manifest itself is untouched).", removed.join(", ")); + if let Some(package) = install::Installs::load(&home).owner_of(id) { + println!("It was installed with lx install; to remove the app itself: lx uninstall {}", package.name); + } + Ok(ExitCode::SUCCESS) +} + +// --- Installing apps (§10.8) ---------------------------------------------------- + +/// Stopping and starting services through a running launcher, if there is one. +struct ViaDaemon(Option); + +impl install::Services for ViaDaemon { + async fn stop(&self, ids: &[String]) -> Vec { + let Some(daemon) = &self.0 else { return Vec::new() }; + let Ok(state) = daemon.get("/api/state").await else { return Vec::new() }; + let mut stopped = Vec::new(); + for id in ids { + let running = state["services"] + .as_array() + .into_iter() + .flatten() + .any(|s| s["id"] == id.as_str() && !matches!(s["status"].as_str(), Some("stopped" | "failed"))); + if !running { + continue; + } + if let Ok(reply) = daemon.post(&format!("/api/services/{id}/stop"), json!({})).await { + for other in reply["stopped"].as_array().into_iter().flatten().filter_map(Value::as_str) { + if !stopped.iter().any(|s| s == other) { + stopped.push(other.to_string()); + } + } + } + } + stopped + } + + async fn reload_and_start(&self, ids: &[String]) { + let Some(daemon) = &self.0 else { return }; + let _ = daemon.post("/api/reload", json!({})).await; + for id in ids { + if let Err(error) = daemon.post(&format!("/api/services/{id}/start"), json!({})).await { + eprintln!("! could not start {id} again: {error:#}"); + } + } + } +} + +fn terminal_out() -> install::Out { + std::sync::Arc::new(|line: &str| println!(" {line}")) +} + +fn install_env(home: &Home, action: &str) -> Vec<(String, String)> { + install::environment(home, std::env::vars().collect(), action) +} + +/// Ask a yes/no question on the terminal; without one, refuse unless --yes. +fn confirm(question: &str, yes: bool) -> anyhow::Result { + use std::io::{IsTerminal, Write}; + if yes { + return Ok(true); + } + if !std::io::stdin().is_terminal() { + bail!("Not asking without a terminal; pass --yes to go ahead"); + } + print!("{question} [y/N] "); + std::io::stdout().flush()?; + let mut answer = String::new(); + std::io::stdin().read_line(&mut answer)?; + Ok(matches!(answer.trim().to_ascii_lowercase().as_str(), "y" | "yes")) +} + +fn short(commit: &Option) -> String { + commit.as_deref().map(|c| c.chars().take(7).collect()).unwrap_or_else(|| "—".into()) +} + +fn list_installs(home: Home, as_json: bool) -> anyhow::Result { + let installs = install::Installs::load(&home); + if as_json { + return print_json(&json!({ "packages": installs.packages.values().map(install::Package::to_json).collect::>() })); + } + if installs.packages.is_empty() { + println!("Nothing is installed with lx install yet. Install an app with: lx install "); + return Ok(ExitCode::SUCCESS); + } + let mut rows = vec![vec!["NAME".into(), "SERVICES".into(), "COMMIT".into(), "UPDATED".into(), "SOURCE".into()]]; + for package in installs.packages.values() { + let mut commit = short(&package.commit); + if let Some(behind) = package.behind.filter(|n| *n > 0) { + commit.push_str(&format!(" ({behind} new)")); + } + let when = package.updated_at.as_deref().unwrap_or(&package.installed_at); + let source = if package.cloned { package.source.clone() } else { format!("{} (folder)", tilde(&package.dir)) }; + rows.push(vec![package.name.clone(), package.services.join(", "), commit, local(&json!(when), "%Y-%m-%d"), source]); + } + table(rows); + Ok(ExitCode::SUCCESS) +} + +async fn install_app(home: Home, source: &str, name: Option<&str>, yes: bool) -> anyhow::Result { + let _lock = install::lock(&home)?; + let out = terminal_out(); + let env = install_env(&home, "install"); + let plan = { + let (home, env, out, source, name) = (home.clone(), env.clone(), out.clone(), source.to_string(), name.map(str::to_string)); + tokio::task::spawn_blocking(move || install::prepare(&home, &env, &source, name.as_deref(), &out)).await?? + }; + for line in plan.describe() { + println!("{line}"); + } + if !plan.install.run.is_empty() && !confirm("Run the installer?", yes)? { + println!("Nothing installed."); + return Ok(ExitCode::SUCCESS); + } + let env = if plan.existing.is_some() { install_env(&home, "update") } else { env }; + let name = plan.name.clone(); + let ids = plan.ids(); + let package = install::install(&home, &env, plan, &ViaDaemon(Daemon::find(&home)), &out).await?; + println!("✓ Installed {name} ({})", short(&package.commit)); + if let Some(first) = ids.first() { + if Daemon::find(&home).is_some() { + println!("Start it with: lx start {first}"); + } else { + println!("Start LaunchX with `lx up`, then: lx start {first}"); + } + } + Ok(ExitCode::SUCCESS) +} + +/// The installed apps these names or service ids mean; all of them for none. +fn chosen(installs: &install::Installs, names: &[String]) -> anyhow::Result> { + if names.is_empty() { + return Ok(installs.packages.values().cloned().collect()); + } + names + .iter() + .map(|name| installs.find(name).cloned().ok_or_else(|| anyhow!("Nothing named '{name}' is installed (see lx install)"))) + .collect() +} + +async fn update_apps(home: Home, names: Vec, check: bool, force: bool) -> anyhow::Result { + let _lock = install::lock(&home)?; + let packages = chosen(&install::Installs::load(&home), &names)?; + if packages.is_empty() { + println!("Nothing is installed with lx install yet."); + return Ok(ExitCode::SUCCESS); + } + let env = install_env(&home, "update"); + let mut failed = false; + if check { + for package in packages { + let (h, e, n) = (home.clone(), env.clone(), package.name.clone()); + match tokio::task::spawn_blocking(move || install::check(&h, &e, &n)).await? { + Ok(Some(0)) => println!("{}: up to date", package.name), + Ok(Some(behind)) => println!("{}: {behind} new commit{} — lx update {}", package.name, if behind == 1 { "" } else { "s" }, package.name), + Ok(None) => println!("{}: installed from the folder {}; it updates when you change it", package.name, tilde(&package.dir)), + Err(error) => { + failed = true; + println!("{}: could not check: {error:#}", package.name); + } + } + } + return Ok(if failed { ExitCode::FAILURE } else { ExitCode::SUCCESS }); + } + let services = ViaDaemon(Daemon::find(&home)); + let out = terminal_out(); + for package in packages { + println!("Updating {}…", package.name); + match install::update(&home, &env, &package, force, &services, &out).await { + Ok(install::Updated::UpToDate) => println!("✓ {} is up to date ({})", package.name, short(&package.commit)), + Ok(install::Updated::Installed(new)) => println!("✓ Updated {} {} → {}", package.name, short(&package.commit), short(&new.commit)), + Err(error) => { + failed = true; + println!("✗ {}: {error:#}", package.name); + } + } + } + Ok(if failed { ExitCode::FAILURE } else { ExitCode::SUCCESS }) +} + +async fn uninstall_app(home: Home, name: &str, yes: bool) -> anyhow::Result { + let _lock = install::lock(&home)?; + let Some(package) = install::Installs::load(&home).find(name).cloned() else { + let (registered, _) = manifest::load_registry(&home); + if registered.iter().any(|s| s.id == name) { + bail!("{name} was not installed with lx install. To stop LaunchX running it: lx rm {name}"); + } + bail!("Nothing named '{name}' is installed (see lx install)"); + }; + println!("Uninstall {}", package.name); + println!(" services {}", package.services.join(", ")); + if package.cloned { + println!(" deletes {}", tilde(&package.dir)); + } else { + println!(" keeps {} (LaunchX did not clone it)", tilde(&package.dir)); + } + for data in &package.data { + println!(" keeps {data} (your data)"); + } + if !confirm("Uninstall?", yes)? { + println!("Nothing removed."); + return Ok(ExitCode::SUCCESS); + } + let env = install_env(&home, "uninstall"); + install::uninstall(&home, &env, &package, &ViaDaemon(Daemon::find(&home)), &terminal_out()).await?; + println!("✓ Uninstalled {}", package.name); + if !package.data.is_empty() { + println!("Your data is still in {}. Delete it yourself if you no longer want it.", package.data.join(", ")); + } Ok(ExitCode::SUCCESS) } diff --git a/src/manifest.rs b/src/manifest.rs index 471d086..6ef48cd 100644 --- a/src/manifest.rs +++ b/src/manifest.rs @@ -17,6 +17,23 @@ struct ManifestFile { launchx: String, #[serde(default)] service: BTreeMap, + #[serde(default)] + install: Option, +} + +/// `[install]`: how to build and install the project from a checkout of its +/// repository (contract §4.9). Read only from a checkout's own manifest. +#[derive(Clone, Debug, Default, Deserialize, PartialEq)] +#[serde(deny_unknown_fields)] +pub struct InstallDef { + #[serde(default)] + pub run: Vec, + #[serde(default)] + pub uninstall: Vec, + #[serde(default)] + pub needs: Vec, + #[serde(default)] + pub data: Vec, } #[derive(Clone, Debug, Deserialize, PartialEq)] @@ -173,6 +190,11 @@ pub fn locate(path: &Path) -> anyhow::Result { /// Parse and check one manifest file. pub fn parse(file: &Path, entry: &Path) -> anyhow::Result> { + parse_with_install(file, entry).map(|(services, _)| services) +} + +/// Parse a manifest with its `[install]` table (contract §4.9). +pub fn parse_with_install(file: &Path, entry: &Path) -> anyhow::Result<(Vec, Option)> { let text = fs::read_to_string(file).with_context(|| format!("reading {}", file.display()))?; let manifest: ManifestFile = toml::from_str(&text).map_err(|err| anyhow::anyhow!("{}: {}", file.display(), err.message()))?; if manifest.launchx != CONTRACT_VERSION { @@ -181,6 +203,11 @@ pub fn parse(file: &Path, entry: &Path) -> anyhow::Result> { if manifest.service.is_empty() { bail!("{}: defines no [service.] table", file.display()); } + if let Some(install) = &manifest.install { + if [&install.run, &install.uninstall].into_iter().any(|cmd| cmd.first().is_some_and(|c| c.trim().is_empty())) { + bail!("{}: install commands must name a program", file.display()); + } + } let dir = file.parent().map(Path::to_path_buf).unwrap_or_default(); let mut services = Vec::new(); for (id, def) in manifest.service { @@ -220,7 +247,50 @@ pub fn parse(file: &Path, entry: &Path) -> anyhow::Result> { } services.push(Service { id, def, dir: dir.clone(), manifest: file.to_path_buf(), entry: entry.to_path_buf(), error: None }); } - Ok(services) + Ok((services, manifest.install)) +} + +/// Register a manifest file by linking it into `$LAUNCHX_HOME/services` +/// (contract §4.5). Returns its services, or None when it already is. +pub fn register(home: &Home, file: &Path) -> anyhow::Result>> { + home.ensure()?; + let services = parse(file, file)?; + let (existing, _) = load_registry(home); + if existing.iter().any(|s| s.manifest == file) { + return Ok(None); + } + for service in &services { + if let Some(other) = existing.iter().find(|s| s.id == service.id) { + bail!("A service named '{}' is already registered from {}. Remove it first: lx rm {}", service.id, other.manifest.display(), service.id); + } + } + let stem: String = file + .parent() + .and_then(|p| p.file_name()) + .map(|n| n.to_string_lossy().chars().map(|c| if c.is_ascii_alphanumeric() || c == '-' || c == '_' { c } else { '-' }).collect()) + .unwrap_or_else(|| services[0].id.clone()); + let mut link = home.services_dir().join(format!("{stem}.toml")); + let mut n = 2; + while link.symlink_metadata().is_ok() { + link = home.services_dir().join(format!("{stem}-{n}.toml")); + n += 1; + } + std::os::unix::fs::symlink(file, &link).with_context(|| format!("linking {}", link.display()))?; + Ok(Some(services)) +} + +/// Remove the registration entry that defines `id`. Returns every service that +/// entry defined, since they all go with it. The manifest itself is untouched. +pub fn unregister(home: &Home, id: &str) -> anyhow::Result> { + let (services, _) = load_registry(home); + let Some(service) = services.iter().find(|s| s.id == id) else { bail!("Unknown service '{id}'") }; + let entry = service.entry.clone(); + if !entry.starts_with(home.services_dir()) { + bail!("refusing to remove {}, which is outside {}", entry.display(), home.services_dir().display()); + } + let siblings: Vec = services.iter().filter(|s| s.entry == entry).map(|s| s.id.clone()).collect(); + fs::remove_file(&entry).with_context(|| format!("removing {}", entry.display()))?; + Ok(siblings) } /// Everything registered in `$LAUNCHX_HOME/services`. Manifest-level problems diff --git a/src/supervisor.rs b/src/supervisor.rs index b12da3f..8180315 100644 --- a/src/supervisor.rs +++ b/src/supervisor.rs @@ -122,6 +122,8 @@ pub struct Launcher { publishing: Mutex<()>, http: HttpClient, env: Vec<(String, String)>, + /// The install, update or uninstall the page started last (contract §10.8). + pub job: Mutex>, } /// Everything needed to spawn one service. @@ -177,6 +179,7 @@ impl Launcher { publishing: Mutex::new(()), http: http_client(), env: platform::environment(), + job: Mutex::new(None), }); launcher.publish_agents(); Ok(launcher) @@ -213,6 +216,11 @@ impl Launcher { Some((assigned.port, assigned.token.clone())) } + /// The user's login environment, as services get it. + pub fn env(&self) -> Vec<(String, String)> { + self.env.clone() + } + pub fn runtime(&self, id: &str) -> Option { self.lock().entries.get(id).map(|e| e.rt.clone()) } @@ -596,6 +604,8 @@ impl Launcher { } pub fn snapshot(&self) -> Value { + let installs = crate::install::Installs::load(&self.home); + let job = self.job.lock().unwrap_or_else(|p| p.into_inner()).clone(); let inner = self.lock(); let defs = Self::defs(&inner); let services: Vec = inner @@ -630,6 +640,7 @@ impl Launcher { "cli": def.cli, "homepage": def.homepage, "manifest": entry.service.manifest, + "package": installs.owner_of(id).map(|p| p.name.clone()), "agents": { "discoverable": inner.persisted.agents.contains(id), "skills": inner.agent_skills.get(id).cloned().unwrap_or_default(), @@ -652,6 +663,8 @@ impl Launcher { }, "errors": inner.errors.iter().chain(&inner.agent_problems).collect::>(), "services": services, + "packages": installs.packages.values().map(crate::install::Package::to_json).collect::>(), + "job": job, "memory": { "agents": inner.persisted.memory_agents, "pages": self.memory.count(), diff --git a/tests/install.rs b/tests/install.rs new file mode 100644 index 0000000..ab2fd5f --- /dev/null +++ b/tests/install.rs @@ -0,0 +1,272 @@ +//! `lx install`, `lx update` and `lx uninstall` against a real daemon and a +//! local git repository (contract §4.9, §10.8), from the command line and the API. + +use std::fs; +use std::io::{Read, Write}; +use std::net::TcpStream; +use std::path::{Path, PathBuf}; +use std::process::{Child, Command, Output, Stdio}; +use std::time::{Duration, Instant}; + +const LX: &str = env!("CARGO_BIN_EXE_lx"); + +struct Env { + root: PathBuf, + home: PathBuf, + daemon: Option, +} + +impl Drop for Env { + fn drop(&mut self) { + if let Some(mut daemon) = self.daemon.take() { + let _ = Command::new("kill").arg(daemon.id().to_string()).status(); + let _ = daemon.wait(); + } + let _ = fs::remove_dir_all(&self.root); + } +} + +impl Env { + fn lx(&self, args: &[&str]) -> Output { + Command::new(LX) + .args(args) + .env("LAUNCHX_HOME", &self.home) + .env("LAUNCHX_INHERIT_ENV", "1") + .env("HOME", &self.root) + .stdin(Stdio::null()) + .output() + .unwrap() + } + + fn lx_text(&self, args: &[&str]) -> (bool, String) { + let output = self.lx(args); + (output.status.success(), format!("{}{}", String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr))) + } + + fn lx_ok(&self, args: &[&str]) -> String { + let (ok, text) = self.lx_text(args); + assert!(ok, "lx {args:?} failed:\n{text}"); + text + } + + fn state(&self) -> serde_json::Value { + serde_json::from_str(&self.lx_ok(&["state"])).unwrap() + } + + fn service(&self, id: &str) -> Option { + self.state()["services"].as_array().unwrap().iter().find(|s| s["id"] == id).cloned() + } + + fn installs(&self) -> serde_json::Value { + fs::read_to_string(self.home.join("installs.json")).ok().and_then(|t| serde_json::from_str(&t).ok()).unwrap_or_default() + } + + fn history(&self) -> String { + fs::read_to_string(self.root.join("install-history")).unwrap_or_default() + } + + /// Call the launcher's API with its bearer token. + fn api(&self, method: &str, path: &str, body: &str) -> (u16, serde_json::Value) { + let run: serde_json::Value = serde_json::from_str(&fs::read_to_string(self.home.join("run/launchx.json")).unwrap()).unwrap(); + let port: u16 = run["url"].as_str().unwrap().rsplit(':').next().unwrap().parse().unwrap(); + let mut stream = TcpStream::connect(("127.0.0.1", port)).unwrap(); + stream.set_read_timeout(Some(Duration::from_secs(60))).unwrap(); + let text = format!( + "{method} {path} HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\nAuthorization: Bearer {}\r\nContent-Type: application/json\r\nConnection: close\r\nContent-Length: {}\r\n\r\n{body}", + run["token"].as_str().unwrap(), + body.len() + ); + stream.write_all(text.as_bytes()).unwrap(); + let mut raw = String::new(); + let _ = stream.read_to_string(&mut raw); + let (head, body) = raw.split_once("\r\n\r\n").unwrap_or((&raw, "")); + let status = head.split_whitespace().nth(1).and_then(|s| s.parse().ok()).unwrap_or(0); + (status, serde_json::from_str(body).unwrap_or_default()) + } +} + +fn wait_for(what: &str, timeout: Duration, mut done: impl FnMut() -> bool) { + let deadline = Instant::now() + timeout; + while Instant::now() < deadline { + if done() { + return; + } + std::thread::sleep(Duration::from_millis(100)); + } + panic!("timed out waiting for {what}"); +} + +fn git(dir: &Path, args: &[&str]) { + let status = Command::new("git") + .args(["-c", "user.name=Test", "-c", "user.email=test@example.invalid", "-c", "commit.gpgsign=false", "-c", "init.defaultBranch=main"]) + .args(args) + .current_dir(dir) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .unwrap(); + assert!(status.success(), "git {args:?}"); +} + +fn free_port() -> u16 { + std::net::TcpListener::bind(("127.0.0.1", 0)).unwrap().local_addr().unwrap().port() +} + +fn executable(path: &Path, text: &str) { + use std::os::unix::fs::PermissionsExt; + fs::write(path, text).unwrap(); + fs::set_permissions(path, fs::Permissions::from_mode(0o755)).unwrap(); +} + +/// A repository holding an app: the fixture service, an installer that +/// records each run (and fails if it gets a terminal), and an uninstaller. +fn setup() -> Env { + let root = std::env::temp_dir().join(format!("lx-install-{}-{}", std::process::id(), free_port())); + let home = root.join("home"); + fs::create_dir_all(&home).unwrap(); + fs::write(home.join("config.toml"), format!("gateway_port = {}\nbackoff_unit_ms = 40\n", free_port())).unwrap(); + let fixture = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/svc.py"); + executable(&fixture, &fs::read_to_string(&fixture).unwrap()); + + let repo = root.join("echo-app"); + fs::create_dir_all(&repo).unwrap(); + fs::write( + repo.join("launchx.toml"), + format!( + r#"launchx = "0.1" + +[service.echo] +name = "Echo" +level = 1 +run = ["{}"] + +[service.echo.http] +ui = "/" + +[install] +run = ["./install.sh"] +uninstall = ["./uninstall.sh"] +needs = ["git"] +data = ["~/echo-data"] +"#, + fixture.display() + ), + ) + .unwrap(); + executable( + &repo.join("install.sh"), + "#!/bin/sh\nset -e\n[ -t 0 ] && exit 9\nmkdir -p \"$HOME/echo-data\"\necho \"$LAUNCHX_INSTALL $(cat version)\" >> \"$HOME/install-history\"\necho installing version $(cat version)\n", + ); + executable(&repo.join("uninstall.sh"), "#!/bin/sh\necho uninstall >> \"$HOME/install-history\"\n"); + fs::write(repo.join("version"), "1\n").unwrap(); + git(&repo, &["init", "-q"]); + git(&repo, &["add", "."]); + git(&repo, &["commit", "-q", "-m", "one"]); + + let mut env = Env { root, home, daemon: None }; + let daemon = Command::new(LX) + .arg("daemon") + .env("LAUNCHX_HOME", &env.home) + .env("LAUNCHX_INHERIT_ENV", "1") + .env("HOME", &env.root) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn() + .unwrap(); + env.daemon = Some(daemon); + wait_for("the daemon", Duration::from_secs(10), || env.home.join("run/launchx.json").exists()); + env +} + +#[test] +fn install_update_uninstall() { + let env = setup(); + let repo = env.root.join("echo-app"); + let url = format!("file://{}", repo.display()); + + // Nothing yet; asking without a terminal is refused. + assert!(env.lx_ok(&["install"]).contains("Nothing is installed")); + let (ok, text) = env.lx_text(&["install", &url]); + assert!(!ok && text.contains("--yes"), "{text}"); + assert!(env.service("echo").is_none(), "nothing may be registered before the user says yes"); + + // Install from a URL: cloned, installer run without a terminal, registered, recorded. + let text = env.lx_ok(&["install", &url, "--yes"]); + assert!(text.contains("runs ./install.sh") && text.contains("installing version 1") && text.contains("✓ Installed echo-app"), "{text}"); + assert!(env.home.join("sources/echo-app/.git").is_dir()); + assert_eq!(env.history(), "install 1\n"); + let package = &env.installs()["packages"]["echo-app"]; + assert_eq!(package["cloned"], true); + assert_eq!(package["services"], serde_json::json!(["echo"])); + assert_eq!(package["data"], serde_json::json!(["~/echo-data"])); + let service = env.service("echo").expect("echo is registered"); + assert_eq!(service["package"], "echo-app"); + assert_eq!(env.state()["packages"][0]["name"], "echo-app"); + assert!(env.lx_ok(&["install"]).contains("echo-app")); + + // Up to date, then a new commit upstream. + env.lx_ok(&["start", "echo"]); + assert!(env.lx_ok(&["update", "--check"]).contains("echo-app: up to date")); + assert!(env.lx_ok(&["update"]).contains("is up to date")); + fs::write(repo.join("version"), "2\n").unwrap(); + git(&repo, &["commit", "-q", "-am", "two"]); + assert!(env.lx_ok(&["update", "--check"]).contains("1 new commit")); + assert_eq!(env.installs()["packages"]["echo-app"]["behind"], 1); + let pid = env.service("echo").unwrap()["pid"].clone(); + let text = env.lx_ok(&["update", "echo"]); + assert!(text.contains("installing version 2") && text.contains("✓ Updated echo-app"), "{text}"); + assert!(text.contains("Stopped echo while installing") && text.contains("Started echo again"), "{text}"); + assert_eq!(env.history(), "install 1\nupdate 2\n"); + wait_for("echo running again", Duration::from_secs(10), || env.service("echo").unwrap()["status"] == "running"); + assert_ne!(env.service("echo").unwrap()["pid"], pid, "the update restarts what was running"); + assert_eq!(env.installs()["packages"]["echo-app"]["behind"], 0); + + // The same through the API, as the page does it: a forced update runs as a job. + let (status, reply) = env.api("POST", "/api/packages/echo-app/update", r#"{"force": true}"#); + assert_eq!(status, 202, "{reply}"); + let (status, _) = env.api("POST", "/api/packages/echo-app/update", "{}"); + assert_eq!(status, 409, "one job at a time"); + wait_for("the update job", Duration::from_secs(30), || env.state()["job"]["status"] == "done"); + assert_eq!(env.history(), "install 1\nupdate 2\nupdate 2\n"); + let (_, log) = env.api("GET", "/api/install/log", ""); + assert!(log["lines"].as_array().unwrap().iter().any(|l| l == "installing version 2"), "{log}"); + let (status, plan) = env.api("POST", "/api/install/plan", &format!(r#"{{"source": "{url}"}}"#)); + assert_eq!(status, 200, "{plan}"); + assert_eq!(plan["installed"], true); + assert_eq!(plan["run"], serde_json::json!(["./install.sh"])); + let (status, _) = env.api("POST", "/api/install/plan", r#"{"source": "not a source"}"#); + assert_eq!(status, 422); + + // lx rm points at uninstall; uninstall without a terminal needs --yes. + let (ok, text) = env.lx_text(&["uninstall", "echo-app"]); + assert!(!ok && text.contains("--yes"), "{text}"); + let text = env.lx_ok(&["uninstall", "echo", "--yes"]); + assert!(text.contains("✓ Uninstalled echo-app") && text.contains("still in ~/echo-data"), "{text}"); + assert!(env.history().ends_with("uninstall\n")); + assert!(!env.home.join("sources/echo-app").exists(), "the checkout LaunchX cloned is deleted"); + assert!(env.root.join("echo-data").is_dir(), "the app's data stays"); + assert!(env.service("echo").is_none()); + assert!(env.installs()["packages"].as_object().unwrap().is_empty()); + + // From a folder: installed in place, never deleted. + let text = env.lx_ok(&["install", repo.to_str().unwrap(), "--yes", "--name", "Local Echo"]); + assert!(text.contains("✓ Installed local-echo"), "{text}"); + assert_eq!(env.installs()["packages"]["local-echo"]["cloned"], false); + let (ok, text) = env.lx_text(&["install", &url, "--yes", "--name", "local-echo"]); + assert!(!ok && text.contains("already installed from"), "{text}"); + assert!(env.lx_ok(&["update", "--check"]).contains("installed from the folder")); + env.lx_ok(&["uninstall", "local-echo", "--yes"]); + assert!(repo.join("launchx.toml").is_file(), "a folder LaunchX did not clone is left alone"); + + // Through the API, as the page's Install button does it. + let (status, reply) = env.api("POST", "/api/install", &format!(r#"{{"source": "{url}"}}"#)); + assert_eq!(status, 202, "{reply}"); + wait_for("the install job", Duration::from_secs(30), || env.state()["job"]["status"] != "running"); + assert_eq!(env.state()["job"]["status"], "done", "{}", env.state()["job"]); + assert_eq!(env.state()["job"]["name"], "echo-app"); + let (status, _) = env.api("POST", "/api/packages/echo-app/uninstall", ""); + assert_eq!(status, 202); + wait_for("the uninstall job", Duration::from_secs(30), || env.state()["job"]["status"] != "running"); + assert_eq!(env.state()["job"]["status"], "done", "{}", env.state()["job"]); + assert!(env.service("echo").is_none()); +} diff --git a/ui/app.js b/ui/app.js index fb533af..f9d7484 100644 --- a/ui/app.js +++ b/ui/app.js @@ -57,6 +57,155 @@ function asks(memory) { return `

Asks to ${what} your memory${memory.reason ? `: ${esc(memory.reason)}` : ""}

`; } +// --- Installed apps (§10.8) ------------------------------------------------------ + +const JOB_LOG = ".install"; +const VERBS = { install: "Installing", update: "Updating", uninstall: "Uninstalling" }; +let dismissedJob = null; +let planned = null; + +function jobRunning() { + return state?.job?.status === "running"; +} + +function shortSource(pkg) { + return pkg.cloned ? pkg.source.replace(/^https:\/\/github\.com\//, "").replace(/\.git$/, "") : `the folder ${pkg.dir}`; +} + +function packageLine(pkg) { + const commit = pkg.commit ? ` · ${pkg.commit.slice(0, 7)}` : ""; + const fresh = pkg.behind > 0 ? `${pkg.behind} new commit${pkg.behind === 1 ? "" : "s"}` : ""; + const off = jobRunning() ? "disabled" : ""; + return `
+ From ${esc(shortSource(pkg))}${esc(commit)}${fresh} + + +
`; +} + +function renderJob() { + const job = state.job; + const box = $("#job"); + const running = jobRunning(); + $("#install-open").disabled = running; + $("#check-updates").disabled = running; + $("#check-updates").hidden = !(state.packages || []).some((p) => p.cloned); + if (!job || dismissedJob === job.started_at) { box.hidden = true; return; } + const markup = running + ? `${VERBS[job.action] || esc(job.action)} ${esc(job.name)}…` + : `${esc(job.message)}`; + if (box._markup !== markup) { box.innerHTML = markup; box._markup = markup; } + box.className = `job ${job.status}`; + box.hidden = false; +} + +async function startJob(url, body) { + try { + const response = await fetch(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) }); + const result = await response.json().catch(() => ({})); + if (!response.ok) throw new Error(result.error || `HTTP ${response.status}`); + dismissedJob = null; + openLogs(JOB_LOG); + refresh(); + return true; + } catch (error) { + toast(error.message); + return false; + } +} + +async function packageAction(action, name) { + const pkg = (state.packages || []).find((p) => p.name === name); + if (!pkg) return; + if (action === "uninstall") { + const lines = [`Uninstall ${name}?`, "", `Stops and removes ${pkg.services.join(", ")}.`]; + lines.push(pkg.cloned ? `Deletes the checkout ${pkg.dir}.` : `Leaves the folder ${pkg.dir} as it is.`); + if (pkg.data.length) lines.push(`Keeps your data in ${pkg.data.join(", ")}.`); + if (!confirm(lines.join("\n"))) return; + } + await startJob(`/api/packages/${encodeURIComponent(name)}/${action}`, {}); +} + +function resetInstall() { + planned = null; + $("#install-plan").innerHTML = ""; + $("#install-plan").className = "plan"; + $("#install-go").hidden = true; + $("#install-look").hidden = false; +} + +function renderPlan(plan) { + const row = (label, value) => value ? `
${label}
${value}
` : ""; + const services = plan.services.map((s) => `${esc(s.id)} ${esc(s.name)}`).join("
"); + const installer = plan.run.length ? `${esc(plan.run.join(" "))}` : "None: LaunchX registers the app and runs it from the checkout"; + $("#install-plan").innerHTML = `
+ ${row("App", esc(plan.name))} + ${row("From", esc(plan.source))} + ${plan.cloned ? row("Checkout", `${esc(plan.dir)}`) : ""} + ${row("Services", services)} + ${row("Installer", installer)} + ${row("Needs", plan.needs.map(esc).join(", "))} + ${row("Your data", plan.data.map((d) => `${esc(d)}`).join(" "))} +
+ ${plan.installed ? '

Already installed. This runs its installer again.

' : ""} + ${plan.run.length ? '

The installer runs as you, with access to your files. Install only from sources you trust.

' : ""}`; + $("#install-go").textContent = plan.installed ? "Reinstall" : "Install"; +} + +async function lookUp() { + const source = $("#install-source").value.trim(); + if (!source) return; + if (planned && planned.source === source) { install(); return; } + const look = $("#install-look"); + look.disabled = true; + $("#install-plan").className = "plan"; + $("#install-plan").innerHTML = ' Fetching…'; + try { + const response = await fetch("/api/install/plan", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ source }) }); + const result = await response.json().catch(() => ({})); + if (!response.ok) throw new Error(result.error || `HTTP ${response.status}`); + planned = { source, plan: result }; + renderPlan(result); + look.hidden = true; + $("#install-go").hidden = false; + $("#install-go").focus(); + } catch (error) { + $("#install-plan").className = "plan error"; + $("#install-plan").textContent = error.message; + } finally { + look.disabled = false; + } +} + +async function install() { + if (!planned) return; + const go = $("#install-go"); + go.disabled = true; + const ok = await startJob("/api/install", { source: planned.source }); + go.disabled = false; + if (ok) $("#install-dialog").close(); +} + +async function checkUpdates() { + const button = $("#check-updates"); + button.disabled = true; + button.textContent = "Checking…"; + try { + const response = await fetch("/api/packages/check", { method: "POST" }); + const result = await response.json(); + if (!response.ok) throw new Error(result.error || `HTTP ${response.status}`); + const fresh = result.packages.filter((p) => p.behind > 0); + if (result.errors?.length) toast(result.errors.join("; ")); + else toast(fresh.length ? `Updates for ${fresh.map((p) => p.name).join(", ")}` : "Everything is up to date"); + } catch (error) { + toast(`Could not check: ${error.message}`); + } finally { + button.textContent = "Check for updates"; + button.disabled = false; + refresh(); + } +} + const LABELS = { running: "Running", starting: "Starting", waiting: "Waiting", stopping: "Stopping", stopped: "Stopped", failed: "Failed" }; function healthDot(service) { @@ -79,6 +228,7 @@ function card(service) { : ``; const restart = service.status === "running" ? `` : ""; + const pkg = service.package && (state.packages || []).find((p) => p.name === service.package); const agents = service.agents || { discoverable: false, skills: [] }; const offers = [service.cli ? "CLI" : "", agents.skills.length ? `${agents.skills.length} skill${agents.skills.length === 1 ? "" : "s"}` : ""].filter(Boolean).join(" · "); const agentsHelp = agents.discoverable @@ -110,6 +260,7 @@ function card(service) { Discoverable by agents ${offers ? `${esc(offers)}` : ""} + ${pkg ? packageLine(pkg) : ""}
${open}${toggle}${restart}
`; } @@ -137,6 +288,7 @@ function render() { const errors = state.errors || []; $("#errors").hidden = errors.length === 0; $("#errors").innerHTML = errors.map((e) => `

${esc(e)}

`).join(""); + renderJob(); $("#footer").textContent = `LaunchX ${state.launcher.version} · ${state.launcher.home}`; } @@ -166,6 +318,7 @@ function setConnection(ok) { async function act(action, id) { if (action === "logs") { openLogs(id); return; } + if (action === "update" || action === "uninstall") { packageAction(action, id); return; } const service = state.services.find((s) => s.id === id); if (action === "stop") { const dependents = service.required_by.filter((other) => { @@ -652,11 +805,12 @@ function appendLog(line) { async function openLogs(id) { logsFor = id; const service = state.services.find((s) => s.id === id); - $("#logs-title").textContent = `${service?.name || id} · logs`; + $("#logs-title").textContent = id === JOB_LOG ? "Install output" : `${service?.name || id} · logs`; $("#logs-body").textContent = ""; $("#logs").hidden = false; try { - const response = await fetch(`/api/services/${encodeURIComponent(id)}/logs?tail=400`); + const url = id === JOB_LOG ? "/api/install/log?tail=400" : `/api/services/${encodeURIComponent(id)}/logs?tail=400`; + const response = await fetch(url); const result = await response.json(); if (logsFor !== id) return; if (!result.lines.length) $("#logs-body").textContent = "No output yet.\n"; @@ -699,6 +853,18 @@ $("#services").addEventListener("change", (event) => { if (box) setAgents(box.dataset.id, box.checked); }); $("#logs-close").addEventListener("click", closeLogs); +$("#job").addEventListener("click", (event) => { + const button = event.target.closest("button[data-job]"); + if (!button) return; + if (button.dataset.job === "output") openLogs(JOB_LOG); + else { dismissedJob = state.job?.started_at; render(); } +}); +$("#install-open").addEventListener("click", () => { resetInstall(); $("#install-dialog").showModal(); $("#install-source").select(); }); +$("#install-cancel").addEventListener("click", () => $("#install-dialog").close()); +$("#install-source").addEventListener("input", () => { if (planned) resetInstall(); }); +$("#install-form").addEventListener("submit", (event) => { event.preventDefault(); lookUp(); }); +$("#install-go").addEventListener("click", install); +$("#check-updates").addEventListener("click", checkUpdates); $("#memory-view").addEventListener("click", memoryClick); $("#memory-view").addEventListener("input", memoryInput); $("#memory-view").addEventListener("submit", (event) => { event.preventDefault(); saveMemoryPage(); }); diff --git a/ui/index.html b/ui/index.html index bcde61b..b79651b 100644 --- a/ui/index.html +++ b/ui/index.html @@ -23,12 +23,17 @@
+
+ + + +