feat(rust): port OpenCode provisioning with verified checksums
Add phoebus-executor::opencode (manifest parsing, architecture detection, fetch through Exec, SHA-256 verification, fail-closed placeholders, extraction, symlink, config writer). Add --bootstrap-opencode to the CLI so the Phase 0 CLI-coverage gate passes.
This commit is contained in:
parent
fb8bdfd633
commit
4071c7d1f5
5 changed files with 571 additions and 1 deletions
2
installer-rs/Cargo.lock
generated
2
installer-rs/Cargo.lock
generated
|
|
@ -510,6 +510,8 @@ dependencies = [
|
||||||
"phoebus-ai",
|
"phoebus-ai",
|
||||||
"phoebus-core",
|
"phoebus-core",
|
||||||
"serde",
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
"sha2",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"toml",
|
"toml",
|
||||||
|
|
|
||||||
|
|
@ -9,10 +9,12 @@ rust-version.workspace = true
|
||||||
phoebus-core.workspace = true
|
phoebus-core.workspace = true
|
||||||
phoebus-ai.workspace = true
|
phoebus-ai.workspace = true
|
||||||
serde.workspace = true
|
serde.workspace = true
|
||||||
|
serde_json.workspace = true
|
||||||
thiserror.workspace = true
|
thiserror.workspace = true
|
||||||
zeroize.workspace = true
|
zeroize.workspace = true
|
||||||
toml.workspace = true
|
toml.workspace = true
|
||||||
tempfile.workspace = true
|
tempfile.workspace = true
|
||||||
|
sha2 = "0.10"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tempfile.workspace = true
|
tempfile.workspace = true
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ pub mod error;
|
||||||
pub mod exec;
|
pub mod exec;
|
||||||
pub mod kernel;
|
pub mod kernel;
|
||||||
pub mod log;
|
pub mod log;
|
||||||
|
pub mod opencode;
|
||||||
pub mod portage;
|
pub mod portage;
|
||||||
pub mod probe;
|
pub mod probe;
|
||||||
pub mod services;
|
pub mod services;
|
||||||
|
|
|
||||||
539
installer-rs/crates/phoebus-executor/src/opencode.rs
Normal file
539
installer-rs/crates/phoebus-executor/src/opencode.rs
Normal file
|
|
@ -0,0 +1,539 @@
|
||||||
|
//! Reproducible OpenCode provisioning.
|
||||||
|
//!
|
||||||
|
//! The vendor `curl | sh` installer is never used. A pinned release tarball is
|
||||||
|
//! fetched through [`Exec`], its SHA-256 is verified against the local manifest,
|
||||||
|
//! and only then is it extracted. Verification fails closed: a missing or
|
||||||
|
//! placeholder (`^0+$`) checksum is refused before anything is downloaded, and
|
||||||
|
//! a mismatch discards the download.
|
||||||
|
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
use phoebus_core::InstallConfig;
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
|
|
||||||
|
use crate::command::CommandSpec;
|
||||||
|
use crate::error::{ExecError, Result};
|
||||||
|
use crate::exec::Exec;
|
||||||
|
|
||||||
|
/// One architecture's pinned artifact.
|
||||||
|
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||||
|
pub struct Entry {
|
||||||
|
pub url: String,
|
||||||
|
pub sha256: String,
|
||||||
|
pub binary: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A parsed `opencode.manifest`.
|
||||||
|
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||||
|
pub struct Manifest {
|
||||||
|
version: String,
|
||||||
|
entries: BTreeMap<String, Entry>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Manifest {
|
||||||
|
/// Parse a `KEY=value` manifest. Unknown keys are ignored; a missing
|
||||||
|
/// `VERSION` or entry is handled by the caller via [`Manifest::entry`].
|
||||||
|
pub fn parse(text: &str) -> Self {
|
||||||
|
let mut version = String::new();
|
||||||
|
let mut entries: BTreeMap<String, Entry> = BTreeMap::new();
|
||||||
|
for raw in text.lines() {
|
||||||
|
let line = raw.trim();
|
||||||
|
if line.is_empty() || line.starts_with('#') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let Some((key, value)) = line.split_once('=') else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let key = key.trim();
|
||||||
|
let value = value.trim();
|
||||||
|
if key == "VERSION" {
|
||||||
|
version = value.to_string();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let Some((arch, field)) = key.split_once('.') else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let entry = entries.entry(arch.to_string()).or_default();
|
||||||
|
match field {
|
||||||
|
"url" => entry.url = value.to_string(),
|
||||||
|
"sha256" => entry.sha256 = value.to_string(),
|
||||||
|
"binary" => entry.binary = value.to_string(),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Self { version, entries }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn version(&self) -> &str {
|
||||||
|
&self.version
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The entry for `arch`, provided it has a URL.
|
||||||
|
pub fn entry(&self, arch: &str) -> Option<&Entry> {
|
||||||
|
self.entries.get(arch).filter(|e| !e.url.is_empty())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The manifest key for the architecture this binary was built for.
|
||||||
|
pub fn host_arch() -> Option<&'static str> {
|
||||||
|
match std::env::consts::ARCH {
|
||||||
|
"x86_64" => Some("x86_64"),
|
||||||
|
"aarch64" => Some("aarch64"),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// True for an empty or all-zero (placeholder) checksum.
|
||||||
|
fn is_placeholder(checksum: &str) -> bool {
|
||||||
|
checksum.is_empty() || checksum.chars().all(|c| c == '0')
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Lowercase hex SHA-256 of `bytes`.
|
||||||
|
pub fn sha256_hex(bytes: &[u8]) -> String {
|
||||||
|
let mut hasher = Sha256::new();
|
||||||
|
hasher.update(bytes);
|
||||||
|
format!("{:x}", hasher.finalize())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// SHA-256 of a file, fail-closed on read errors.
|
||||||
|
pub fn sha256_file(path: &Path) -> Result<String> {
|
||||||
|
let bytes = std::fs::read(path).map_err(|e| ExecError::io(path.to_path_buf(), e))?;
|
||||||
|
Ok(sha256_hex(&bytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Verify a downloaded file against the expected digest. Empty and placeholder
|
||||||
|
/// digests are refused before the file is read.
|
||||||
|
pub fn checksum_ok(path: &Path, expected: &str) -> Result<bool> {
|
||||||
|
if is_placeholder(expected) {
|
||||||
|
return Ok(false);
|
||||||
|
}
|
||||||
|
Ok(sha256_file(path)? == expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Where the verified binary lives: an explicit override, then `/opt` when it
|
||||||
|
/// is writable (root on the live installer), otherwise the state directory.
|
||||||
|
pub fn bin_dir(state_dir: &Path) -> PathBuf {
|
||||||
|
if let Ok(dir) = std::env::var("PHOEBUS_OPENCODE_DIR") {
|
||||||
|
if !dir.is_empty() {
|
||||||
|
return PathBuf::from(dir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let opt = Path::new("/opt");
|
||||||
|
if dir_writable(opt) {
|
||||||
|
return opt.join("phoebus/opencode");
|
||||||
|
}
|
||||||
|
state_dir.join("opencode")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn dir_writable(dir: &Path) -> bool {
|
||||||
|
if !dir.is_dir() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
let probe = dir.join(".phoebus-write-probe");
|
||||||
|
match std::fs::OpenOptions::new()
|
||||||
|
.write(true)
|
||||||
|
.create_new(true)
|
||||||
|
.open(&probe)
|
||||||
|
{
|
||||||
|
Ok(_) => {
|
||||||
|
let _ = std::fs::remove_file(&probe);
|
||||||
|
true
|
||||||
|
}
|
||||||
|
Err(_) => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_executable(path: &Path) -> bool {
|
||||||
|
use std::os::unix::fs::PermissionsExt;
|
||||||
|
match std::fs::metadata(path) {
|
||||||
|
Ok(meta) => meta.is_file() && meta.permissions().mode() & 0o111 != 0,
|
||||||
|
Err(_) => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Locate a usable `opencode` binary (PATH, then the install directory).
|
||||||
|
pub fn detect(bin_dir: &Path) -> Option<PathBuf> {
|
||||||
|
if let Some(found) = crate::exec::find_in_path("opencode") {
|
||||||
|
return Some(found);
|
||||||
|
}
|
||||||
|
[
|
||||||
|
bin_dir.join("opencode"),
|
||||||
|
PathBuf::from("/usr/local/bin/opencode"),
|
||||||
|
]
|
||||||
|
.into_iter()
|
||||||
|
.find(|candidate| is_executable(candidate))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Download, verify and extract `arch`'s pinned release into `dest`.
|
||||||
|
///
|
||||||
|
/// Returns the path to the installed binary. In dry-run nothing is downloaded
|
||||||
|
/// or extracted; the checksum policy is still enforced.
|
||||||
|
pub fn bootstrap(exec: &Exec, manifest: &Manifest, arch: &str, dest: &Path) -> Result<PathBuf> {
|
||||||
|
let entry = manifest.entry(arch).ok_or_else(|| {
|
||||||
|
ExecError::operation(
|
||||||
|
"opencode",
|
||||||
|
"bootstrap",
|
||||||
|
format!("no pinned manifest entry for {arch}"),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
if is_placeholder(&entry.sha256) {
|
||||||
|
return Err(ExecError::operation(
|
||||||
|
"opencode",
|
||||||
|
"bootstrap",
|
||||||
|
format!("manifest has a placeholder checksum for {arch}; refusing"),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let binary = if entry.binary.is_empty() {
|
||||||
|
"opencode".to_string()
|
||||||
|
} else {
|
||||||
|
entry.binary.clone()
|
||||||
|
};
|
||||||
|
let installed = dest.join(&binary);
|
||||||
|
|
||||||
|
if exec.dry_run {
|
||||||
|
exec.logger
|
||||||
|
.info(format!("[dry-run] would fetch and verify {}", entry.url));
|
||||||
|
return Ok(installed);
|
||||||
|
}
|
||||||
|
|
||||||
|
exec.require_tools(&["curl", "tar"])?;
|
||||||
|
std::fs::create_dir_all(dest).map_err(|e| ExecError::io(dest.to_path_buf(), e))?;
|
||||||
|
|
||||||
|
// The staging directory owns the download; dropping it removes the file on
|
||||||
|
// every path, including verification failure.
|
||||||
|
let staging = tempfile::Builder::new()
|
||||||
|
.prefix("opencode.")
|
||||||
|
.tempdir()
|
||||||
|
.map_err(|e| ExecError::io(std::env::temp_dir(), e))?;
|
||||||
|
let tarball = staging.path().join("opencode.tar.gz");
|
||||||
|
|
||||||
|
exec.logger
|
||||||
|
.info(format!("opencode: fetching {}", entry.url));
|
||||||
|
exec.run(
|
||||||
|
CommandSpec::new("opencode", "download OpenCode", "curl")
|
||||||
|
.args(["-fL", "--retry", "3", "-o"])
|
||||||
|
.arg(tarball.to_string_lossy().to_string())
|
||||||
|
.arg(&entry.url),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
if !checksum_ok(&tarball, &entry.sha256)? {
|
||||||
|
return Err(ExecError::operation(
|
||||||
|
"opencode",
|
||||||
|
"verify OpenCode",
|
||||||
|
"SHA-256 verification FAILED; discarding download",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
exec.logger.info("opencode: checksum verified");
|
||||||
|
|
||||||
|
exec.run(
|
||||||
|
CommandSpec::new("opencode", "extract OpenCode", "tar")
|
||||||
|
.arg("-xzf")
|
||||||
|
.arg(tarball.to_string_lossy().to_string())
|
||||||
|
.args(["-C"])
|
||||||
|
.arg(dest.to_string_lossy().to_string()),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
if !installed.is_file() {
|
||||||
|
return Err(ExecError::operation(
|
||||||
|
"opencode",
|
||||||
|
"extract OpenCode",
|
||||||
|
format!("expected binary '{}' not found", binary),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
use std::os::unix::fs::PermissionsExt;
|
||||||
|
let _ = std::fs::set_permissions(&installed, std::fs::Permissions::from_mode(0o755));
|
||||||
|
|
||||||
|
let link = dest.join("opencode");
|
||||||
|
if link != installed {
|
||||||
|
let _ = std::fs::remove_file(&link);
|
||||||
|
let _ = std::os::unix::fs::symlink(&installed, &link);
|
||||||
|
}
|
||||||
|
|
||||||
|
exec.logger.info(format!(
|
||||||
|
"opencode: installed at {} ({})",
|
||||||
|
installed.display(),
|
||||||
|
manifest.version()
|
||||||
|
));
|
||||||
|
Ok(installed)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ensure a binary is present, bootstrapping the pinned release if needed.
|
||||||
|
pub fn ensure(exec: &Exec, manifest: &Manifest, bin_dir: &Path) -> Result<PathBuf> {
|
||||||
|
if let Some(found) = detect(bin_dir) {
|
||||||
|
return Ok(found);
|
||||||
|
}
|
||||||
|
let arch = host_arch().ok_or_else(|| {
|
||||||
|
ExecError::operation(
|
||||||
|
"opencode",
|
||||||
|
"detect architecture",
|
||||||
|
format!("unsupported architecture {}", std::env::consts::ARCH),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
exec.logger
|
||||||
|
.info("opencode: not found; bootstrapping the pinned release");
|
||||||
|
bootstrap(exec, manifest, arch, bin_dir)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Path of the provider configuration (never contains credentials).
|
||||||
|
pub fn ai_config_path(state_dir: &Path) -> PathBuf {
|
||||||
|
state_dir.join("ai/opencode.json")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Write provider/model/endpoint configuration with mode 0600. Credentials are
|
||||||
|
/// never written here.
|
||||||
|
pub fn write_config(exec: &Exec, config: &InstallConfig) -> Result<PathBuf> {
|
||||||
|
let path = ai_config_path(&exec.state_dir);
|
||||||
|
if let Some(parent) = path.parent() {
|
||||||
|
std::fs::create_dir_all(parent).map_err(|e| ExecError::io(parent.to_path_buf(), e))?;
|
||||||
|
}
|
||||||
|
let mut map = serde_json::Map::new();
|
||||||
|
map.insert(
|
||||||
|
"$schema".into(),
|
||||||
|
serde_json::Value::String("https://opencode.ai/config.json".into()),
|
||||||
|
);
|
||||||
|
for (key, value) in [
|
||||||
|
("provider", config.ai.provider.as_str()),
|
||||||
|
("model", config.ai.model.as_str()),
|
||||||
|
("baseURL", config.ai.endpoint.as_str()),
|
||||||
|
] {
|
||||||
|
if !value.is_empty() {
|
||||||
|
map.insert(key.into(), serde_json::Value::String(value.to_string()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let text = serde_json::to_string_pretty(&serde_json::Value::Object(map))
|
||||||
|
.map_err(|e| ExecError::operation("opencode", "serialize config", e.to_string()))?;
|
||||||
|
std::fs::write(&path, format!("{text}\n")).map_err(|e| ExecError::io(path.clone(), e))?;
|
||||||
|
use std::os::unix::fs::PermissionsExt;
|
||||||
|
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600))
|
||||||
|
.map_err(|e| ExecError::io(path.clone(), e))?;
|
||||||
|
exec.logger.info(format!(
|
||||||
|
"wrote OpenCode config to {} (credentials are never stored here)",
|
||||||
|
path.display()
|
||||||
|
));
|
||||||
|
Ok(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use phoebus_core::Redactor;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use crate::command::{CommandOutput, ProcessRunner};
|
||||||
|
use crate::log::Logger;
|
||||||
|
|
||||||
|
/// A runner that simulates download and extraction without the network.
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct FixtureRunner {
|
||||||
|
payload: Vec<u8>,
|
||||||
|
extract_binary: Option<String>,
|
||||||
|
fail_download: bool,
|
||||||
|
recorded: Arc<Mutex<Vec<CommandSpec>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FixtureRunner {
|
||||||
|
fn new(payload: impl Into<Vec<u8>>) -> Self {
|
||||||
|
Self {
|
||||||
|
payload: payload.into(),
|
||||||
|
extract_binary: None,
|
||||||
|
fail_download: false,
|
||||||
|
recorded: Arc::new(Mutex::new(Vec::new())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extract_binary(mut self, name: &str) -> Self {
|
||||||
|
self.extract_binary = Some(name.to_string());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
fn recorded(&self) -> Vec<CommandSpec> {
|
||||||
|
self.recorded.lock().unwrap().clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn programs(&self) -> Vec<String> {
|
||||||
|
self.recorded().iter().map(|s| s.program.clone()).collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ok() -> CommandOutput {
|
||||||
|
CommandOutput {
|
||||||
|
status: 0,
|
||||||
|
success: true,
|
||||||
|
stdout: Vec::new(),
|
||||||
|
stderr: Vec::new(),
|
||||||
|
duration: Duration::from_millis(1),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn failed(program: &str) -> CommandOutput {
|
||||||
|
CommandOutput {
|
||||||
|
status: 1,
|
||||||
|
success: false,
|
||||||
|
stdout: Vec::new(),
|
||||||
|
stderr: format!("{program} failed").into_bytes(),
|
||||||
|
duration: Duration::from_millis(1),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ProcessRunner for FixtureRunner {
|
||||||
|
fn execute(&self, spec: &CommandSpec) -> Result<CommandOutput> {
|
||||||
|
self.recorded.lock().unwrap().push(spec.clone());
|
||||||
|
match spec.program.as_str() {
|
||||||
|
"curl" => {
|
||||||
|
if self.fail_download {
|
||||||
|
return Ok(failed("curl"));
|
||||||
|
}
|
||||||
|
if let Some(pos) = spec.args.iter().position(|a| a == "-o") {
|
||||||
|
if let Some(path) = spec.args.get(pos + 1) {
|
||||||
|
std::fs::write(path, &self.payload)
|
||||||
|
.map_err(|e| ExecError::io(path.clone(), e))?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(ok())
|
||||||
|
}
|
||||||
|
"tar" => {
|
||||||
|
if let Some(name) = &self.extract_binary {
|
||||||
|
if let Some(pos) = spec.args.iter().position(|a| a == "-C") {
|
||||||
|
if let Some(dest) = spec.args.get(pos + 1) {
|
||||||
|
let path = Path::new(dest).join(name);
|
||||||
|
std::fs::write(&path, &self.payload)
|
||||||
|
.map_err(|e| ExecError::io(path, e))?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(ok())
|
||||||
|
}
|
||||||
|
_ => Ok(ok()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn make_exec(runner: FixtureRunner) -> Exec {
|
||||||
|
let path = tempfile::tempdir().unwrap().keep();
|
||||||
|
let logger = Logger::open(path.join("l.log"), Redactor::new(), false);
|
||||||
|
Exec::new(Box::new(runner), logger, false, path.join("root"), path)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn manifest_with(sha: &str) -> Manifest {
|
||||||
|
Manifest::parse(&format!(
|
||||||
|
"VERSION=v1.2.3\n\
|
||||||
|
x86_64.url=https://example.invalid/opencode-linux-x64.tar.gz\n\
|
||||||
|
x86_64.sha256={sha}\n\
|
||||||
|
x86_64.binary=opencode\n"
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
const GOOD_PAYLOAD: &[u8] = b"fixture opencode payload";
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn manifest_parses_version_and_entries() {
|
||||||
|
let manifest = manifest_with(&sha256_hex(GOOD_PAYLOAD));
|
||||||
|
assert_eq!(manifest.version(), "v1.2.3");
|
||||||
|
let entry = manifest.entry("x86_64").expect("x86_64 entry");
|
||||||
|
assert!(entry.url.ends_with("opencode-linux-x64.tar.gz"));
|
||||||
|
assert_eq!(entry.binary, "opencode");
|
||||||
|
assert!(manifest.entry("sparc").is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn checksum_accepts_and_rejects() {
|
||||||
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
|
let file = tmp.path().join("artifact");
|
||||||
|
std::fs::write(&file, GOOD_PAYLOAD).unwrap();
|
||||||
|
let good = sha256_hex(GOOD_PAYLOAD);
|
||||||
|
assert!(checksum_ok(&file, &good).unwrap());
|
||||||
|
assert!(!checksum_ok(&file, "deadbeef").unwrap());
|
||||||
|
assert!(!checksum_ok(&file, &"0".repeat(64)).unwrap());
|
||||||
|
assert!(!checksum_ok(&file, "").unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn good_checksum_extracts() {
|
||||||
|
let runner = FixtureRunner::new(GOOD_PAYLOAD).extract_binary("opencode");
|
||||||
|
let exec = make_exec(runner.clone());
|
||||||
|
let dest = tempfile::tempdir().unwrap();
|
||||||
|
let manifest = manifest_with(&sha256_hex(GOOD_PAYLOAD));
|
||||||
|
|
||||||
|
let installed = bootstrap(&exec, &manifest, "x86_64", dest.path()).unwrap();
|
||||||
|
assert_eq!(installed, dest.path().join("opencode"));
|
||||||
|
assert!(installed.is_file());
|
||||||
|
assert_eq!(runner.programs(), vec!["curl", "tar"]);
|
||||||
|
assert!(dest.path().join("opencode").exists());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn placeholder_checksum_is_refused_before_download() {
|
||||||
|
let runner = FixtureRunner::new(GOOD_PAYLOAD).extract_binary("opencode");
|
||||||
|
let exec = make_exec(runner.clone());
|
||||||
|
let dest = tempfile::tempdir().unwrap();
|
||||||
|
let manifest = manifest_with(&"0".repeat(64));
|
||||||
|
|
||||||
|
let err = bootstrap(&exec, &manifest, "x86_64", dest.path()).unwrap_err();
|
||||||
|
assert!(err.to_string().contains("placeholder"), "{err}");
|
||||||
|
assert!(runner.programs().is_empty(), "no command may run");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn mismatch_is_refused_and_download_discarded() {
|
||||||
|
let runner = FixtureRunner::new(GOOD_PAYLOAD).extract_binary("opencode");
|
||||||
|
let exec = make_exec(runner.clone());
|
||||||
|
let dest = tempfile::tempdir().unwrap();
|
||||||
|
let manifest = manifest_with(&sha256_hex(b"something else"));
|
||||||
|
|
||||||
|
let err = bootstrap(&exec, &manifest, "x86_64", dest.path()).unwrap_err();
|
||||||
|
assert!(err.to_string().contains("SHA-256"), "{err}");
|
||||||
|
|
||||||
|
// The staged download is gone and no binary was extracted.
|
||||||
|
let tarball = runner
|
||||||
|
.recorded()
|
||||||
|
.iter()
|
||||||
|
.find(|s| s.program == "curl")
|
||||||
|
.and_then(|s| {
|
||||||
|
let pos = s.args.iter().position(|a| a == "-o")?;
|
||||||
|
s.args.get(pos + 1).cloned()
|
||||||
|
})
|
||||||
|
.expect("curl -o path recorded");
|
||||||
|
assert!(!Path::new(&tarball).exists(), "download was not discarded");
|
||||||
|
assert!(!dest.path().join("opencode").exists());
|
||||||
|
assert!(!runner.programs().contains(&"tar".to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unsupported_arch_is_refused() {
|
||||||
|
let runner = FixtureRunner::new(GOOD_PAYLOAD);
|
||||||
|
let exec = make_exec(runner.clone());
|
||||||
|
let dest = tempfile::tempdir().unwrap();
|
||||||
|
let manifest = manifest_with(&sha256_hex(GOOD_PAYLOAD));
|
||||||
|
|
||||||
|
let err = bootstrap(&exec, &manifest, "sparc", dest.path()).unwrap_err();
|
||||||
|
assert!(
|
||||||
|
err.to_string().contains("no pinned manifest entry"),
|
||||||
|
"{err}"
|
||||||
|
);
|
||||||
|
assert!(runner.programs().is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn config_writer_omits_empty_fields_and_is_private() {
|
||||||
|
use phoebus_core::InstallConfig;
|
||||||
|
use std::os::unix::fs::PermissionsExt;
|
||||||
|
|
||||||
|
let runner = FixtureRunner::new(Vec::new());
|
||||||
|
let exec = make_exec(runner);
|
||||||
|
let mut config = InstallConfig::default();
|
||||||
|
config.ai.provider = "anthropic".into();
|
||||||
|
config.ai.model = "claude-sonnet-4".into();
|
||||||
|
|
||||||
|
let path = write_config(&exec, &config).unwrap();
|
||||||
|
let text = std::fs::read_to_string(&path).unwrap();
|
||||||
|
assert!(text.contains("\"$schema\""));
|
||||||
|
assert!(text.contains("\"provider\": \"anthropic\""));
|
||||||
|
assert!(text.contains("\"model\": \"claude-sonnet-4\""));
|
||||||
|
assert!(!text.contains("baseURL"));
|
||||||
|
let mode = std::fs::metadata(&path).unwrap().permissions().mode() & 0o777;
|
||||||
|
assert_eq!(mode, 0o600);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -17,6 +17,19 @@ use phoebus_executor::{
|
||||||
use phoebus_installer::Installer;
|
use phoebus_installer::Installer;
|
||||||
use phoebus_tui::TuiOutcome;
|
use phoebus_tui::TuiOutcome;
|
||||||
|
|
||||||
|
/// Pinned OpenCode release manifest. Embedded so the static binary needs no
|
||||||
|
/// runtime resources. See `resources/opencode.manifest`.
|
||||||
|
const OPENCODE_MANIFEST: &str = include_str!("../resources/opencode.manifest");
|
||||||
|
|
||||||
|
/// Resolve the manifest text. `PHOEBUS_OPENCODE_MANIFEST` overrides the
|
||||||
|
/// embedded copy so a fixture can be verified end to end (tests, mirrors).
|
||||||
|
fn opencode_manifest() -> std::io::Result<String> {
|
||||||
|
match std::env::var("PHOEBUS_OPENCODE_MANIFEST") {
|
||||||
|
Ok(path) if !path.is_empty() => std::fs::read_to_string(path),
|
||||||
|
_ => Ok(OPENCODE_MANIFEST.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
#[command(
|
#[command(
|
||||||
name = "phoebus-installer",
|
name = "phoebus-installer",
|
||||||
|
|
@ -59,6 +72,10 @@ struct Cli {
|
||||||
/// Write the effective configuration as TOML and exit.
|
/// Write the effective configuration as TOML and exit.
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
save_config: Option<PathBuf>,
|
save_config: Option<PathBuf>,
|
||||||
|
|
||||||
|
/// Fetch and verify the pinned OpenCode release, then exit.
|
||||||
|
#[arg(long)]
|
||||||
|
bootstrap_opencode: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct TerminalConfirm;
|
struct TerminalConfirm;
|
||||||
|
|
@ -139,7 +156,8 @@ fn run() -> std::result::Result<ExitCode, Box<dyn std::error::Error>> {
|
||||||
let mut do_install = cli.install;
|
let mut do_install = cli.install;
|
||||||
|
|
||||||
// TUI when no explicit mode and a terminal is present.
|
// TUI when no explicit mode and a terminal is present.
|
||||||
if !cli.plan && !cli.install && !cli.dry_run && phoebus_tui::is_tty() {
|
if !cli.plan && !cli.install && !cli.dry_run && !cli.bootstrap_opencode && phoebus_tui::is_tty()
|
||||||
|
{
|
||||||
match phoebus_tui::run(config.clone())? {
|
match phoebus_tui::run(config.clone())? {
|
||||||
TuiOutcome::Exit => return Ok(ExitCode::SUCCESS),
|
TuiOutcome::Exit => return Ok(ExitCode::SUCCESS),
|
||||||
TuiOutcome::Save(saved) => {
|
TuiOutcome::Save(saved) => {
|
||||||
|
|
@ -201,6 +219,14 @@ fn run() -> std::result::Result<ExitCode, Box<dyn std::error::Error>> {
|
||||||
exec.register_secret(secret);
|
exec.register_secret(secret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cli.bootstrap_opencode {
|
||||||
|
let manifest = phoebus_executor::opencode::Manifest::parse(&opencode_manifest()?);
|
||||||
|
let dest = phoebus_executor::opencode::bin_dir(&state_dir);
|
||||||
|
let installed = phoebus_executor::opencode::ensure(&exec, &manifest, &dest)?;
|
||||||
|
println!("OpenCode ready at {}", installed.display());
|
||||||
|
return Ok(ExitCode::SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
let mut installer = Installer::new(config, exec);
|
let mut installer = Installer::new(config, exec);
|
||||||
|
|
||||||
// Plan / default mode.
|
// Plan / default mode.
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue