toolkit: --latest scans the releases list (finds pre-releases) + forward board cred to the stock helper

- fetch: resolve the latest .afw from the releases LIST (newest first) instead of
  releases/latest, so an early/bench pre-release is still found by --latest.
- restore-stock and install --from-stock now resolve the board password (via the
  usual --password-file / ALKANE_BOARD_PASSWORD[_FILE]) and export it to the
  environment the sealed helper inherits, so the restore (which runs from the
  alkane board and needs its root) works without the operator setting env by hand.

Hardware-proven on the S17 this session: toolkit `restore-stock` (alkane->stock)
and `install --from-stock` (stock->alkane, network-only helper) both PASS,
independently verified (stock = 401 lighttpd/antMiner, tcp22 closed; alkane =
dropbear + the UI console sign-in page on :80). Builds clean, tests pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BdKQBPjAXpCwmigUov5eQk
This commit is contained in:
2026-11-11 09:34:44 -06:00
co-authored by Claude Opus 4.8
parent 400edbe913
commit ae9e2c67cc
2 changed files with 58 additions and 37 deletions
+41 -36
View File
@@ -60,9 +60,11 @@ impl ReleaseSource {
}
}
fn api_latest(&self) -> String {
/// The releases list (newest first). Unlike `releases/latest`, this includes
/// pre-releases, so an early/bench channel is still found by `--latest`.
fn api_list(&self) -> String {
format!(
"{}/api/v1/repos/{}/{}/releases/latest",
"{}/api/v1/repos/{}/{}/releases?limit=50",
self.base_url.trim_end_matches('/'),
self.owner,
self.repo
@@ -70,6 +72,26 @@ impl ReleaseSource {
}
}
/// Find the first `.afw` asset for `board` across a newest-first list of
/// releases. Returns (name, url).
fn pick_asset(releases: &Value, board: &str) -> Option<(String, String)> {
let needle = format!("-{board}-");
for release in releases.as_array()? {
let Some(assets) = release.get("assets").and_then(Value::as_array) else {
continue;
};
for asset in assets {
let name = asset.get("name").and_then(Value::as_str).unwrap_or("");
if name.ends_with(".afw") && name.contains(&needle) {
if let Some(url) = asset.get("browser_download_url").and_then(Value::as_str) {
return Some((name.to_string(), url.to_string()));
}
}
}
}
None
}
/// A temp curl `--config` file carrying the auth header, removed on drop so the
/// token never lands in a process argument or a lingering file.
struct TokenConfig {
@@ -155,25 +177,11 @@ fn curl_download(src: &ReleaseSource, url: &str, dest: &Path) -> Result<(), Fetc
}
/// Resolve the latest release's `.afw` asset for `board`: returns (name, url).
/// Scans the newest-first releases list (so a pre-release counts as latest).
pub fn latest_afw_asset(src: &ReleaseSource, board: &str) -> Result<(String, String), FetchError> {
let body = curl_bytes(src, &src.api_latest())?;
let doc: Value = serde_json::from_slice(&body).map_err(|e| FetchError::Parse(e.to_string()))?;
let assets = doc
.get("assets")
.and_then(Value::as_array)
.ok_or_else(|| FetchError::Parse("no assets array in release".to_string()))?;
let needle = format!("-{board}-");
for asset in assets {
let name = asset.get("name").and_then(Value::as_str).unwrap_or("");
if name.ends_with(".afw") && name.contains(&needle) {
let url = asset
.get("browser_download_url")
.and_then(Value::as_str)
.ok_or_else(|| FetchError::Parse("asset has no download url".to_string()))?;
return Ok((name.to_string(), url.to_string()));
}
}
Err(FetchError::NoAsset(board.to_string()))
let body = curl_bytes(src, &src.api_list())?;
let releases: Value = serde_json::from_slice(&body).map_err(|e| FetchError::Parse(e.to_string()))?;
pick_asset(&releases, board).ok_or_else(|| FetchError::NoAsset(board.to_string()))
}
/// Download the latest `.afw` for `board` into `dest_dir`; returns its path.
@@ -195,27 +203,24 @@ mod tests {
let src = ReleaseSource::alkane_os();
assert_eq!(src.owner, "blueflare");
assert_eq!(src.repo, "alkane-os");
assert!(src.api_latest().ends_with("/api/v1/repos/blueflare/alkane-os/releases/latest"));
assert!(src.api_latest().starts_with("https://gitea.bluefla.re"));
assert!(src.api_list().contains("/api/v1/repos/blueflare/alkane-os/releases"));
assert!(src.api_list().starts_with("https://gitea.bluefla.re"));
}
#[test]
fn asset_selection_matches_board_and_extension() {
// The selection logic over a captured release document shape.
let doc = serde_json::json!({
"assets": [
fn pick_asset_takes_the_newest_matching_afw() {
// Newest-first list of releases; the board's .afw in the first release wins.
let releases = serde_json::json!([
{"tag_name": "v0.1.0-bench1", "assets": [
{"name": "SHA256SUMS", "browser_download_url": "u0"},
{"name": "alkane-os-0.1.0-am335x-arm.afw", "browser_download_url": "u1"},
{"name": "alkane-os-0.1.0-zynq-7010-armv7.afw", "browser_download_url": "u2"},
]
});
let assets = doc["assets"].as_array().unwrap();
let board = "zynq-7010";
let needle = format!("-{board}-");
let hit = assets.iter().find(|a| {
let n = a["name"].as_str().unwrap_or("");
n.ends_with(".afw") && n.contains(&needle)
});
assert_eq!(hit.unwrap()["browser_download_url"], "u2");
]},
{"tag_name": "v0.0.9", "assets": [
{"name": "alkane-os-0.0.9-zynq-7010-armv7.afw", "browser_download_url": "old"},
]},
]);
assert_eq!(pick_asset(&releases, "zynq-7010"), Some(("alkane-os-0.1.0-zynq-7010-armv7.afw".into(), "u2".into())));
assert_eq!(pick_asset(&releases, "cv1835"), None);
}
}
+17 -1
View File
@@ -138,6 +138,17 @@ fn resolve_password(opts: &Opts) -> Result<SecretFile, Box<dyn std::error::Error
})
}
/// Resolve the board password and publish it to the environment the sealed
/// stock helper inherits (`ALKANE_BOARD_PASSWORD_FILE`). Returns the secret
/// guard so a temp file lives until the caller is done.
fn export_board_password(opts: &Opts) -> Result<SecretFile, Box<dyn std::error::Error>> {
let secret = resolve_password(opts)?;
if let Some(path) = &secret.path {
std::env::set_var("ALKANE_BOARD_PASSWORD_FILE", path);
}
Ok(secret)
}
fn ssh_options(
opts: &Opts,
default_user: &str,
@@ -475,6 +486,8 @@ fn install_from_stock_flow(
"no stock helper configured: set ALKANE_STOCK_HELPER to the dsh-provided from-stock installer".into(),
);
}
// Forward any board credential to the helper's environment.
let _secret = export_board_password(opts)?;
println!(
"installing {} onto stock board {host} (sealed from-stock method)...",
verified.manifest.board
@@ -964,8 +977,11 @@ fn resolve_afw_source(opts: &Opts) -> Result<PathBuf, Box<dyn std::error::Error>
/// Return an alkane board to genuine stock via the sealed stock-restore method,
/// then leave the independent verification to the operator/bench.
fn run_restore_stock(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
let opts = parse_args(args, &["host", "confirm"], &["dry-run"])?;
let opts = parse_args(args, &[ssh_values(), &["confirm"]].concat(), &["dry-run"])?;
let host = opts.value("host").ok_or("--host is required")?;
// Restore runs from the alkane board, so the helper needs its root
// credential; forward it through the environment the helper inherits.
let _secret = export_board_password(&opts)?;
if !stock::available() {
return Err(
"no stock helper configured: set ALKANE_STOCK_HELPER to the dsh-provided restore helper".into(),