2026-04-12 08:09:14 -04:00
|
|
|
//! Dynamic container registry configuration.
|
|
|
|
|
//!
|
|
|
|
|
//! Manages a list of container registries that the node uses to pull app images.
|
|
|
|
|
//! Registries are tried in order — if the first fails, the next is attempted.
|
|
|
|
|
//! Configuration is persisted to disk and editable via RPC.
|
|
|
|
|
|
|
|
|
|
use anyhow::{Context, Result};
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
use std::path::Path;
|
|
|
|
|
use tokio::fs;
|
|
|
|
|
|
|
|
|
|
const REGISTRY_FILE: &str = "config/registries.json";
|
2026-05-01 05:02:39 -04:00
|
|
|
const OVH_REGISTRY_URL: &str = "146.59.87.168:3000/lfg2025";
|
2026-07-10 18:55:32 +01:00
|
|
|
/// Retired registry host (release server retired 2026-06-13; the registry
|
|
|
|
|
/// frontend was fully dead by 2026-07-10 — 500 on every /v2 manifest read).
|
|
|
|
|
/// Never a default, never force-enabled; stripped from saved configs on
|
|
|
|
|
/// load. The literal exists ONLY so the strip can match — nothing may pull
|
|
|
|
|
/// through this host.
|
|
|
|
|
const RETIRED_TX1138_HOST: &str = "git.tx1138.com";
|
2026-04-12 08:09:14 -04:00
|
|
|
|
|
|
|
|
/// A single container registry.
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
|
pub struct Registry {
|
2026-07-10 18:55:32 +01:00
|
|
|
/// Registry URL (e.g., "146.59.87.168:3000/lfg2025").
|
2026-04-12 08:09:14 -04:00
|
|
|
pub url: String,
|
|
|
|
|
/// Human-readable name.
|
|
|
|
|
pub name: String,
|
|
|
|
|
/// Whether TLS verification is required (false for HTTP registries).
|
|
|
|
|
pub tls_verify: bool,
|
|
|
|
|
/// Whether this registry is enabled.
|
|
|
|
|
#[serde(default = "default_true")]
|
|
|
|
|
pub enabled: bool,
|
|
|
|
|
/// Priority (lower = tried first).
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
pub priority: u32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn default_true() -> bool {
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Registry configuration.
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
|
pub struct RegistryConfig {
|
|
|
|
|
pub registries: Vec<Registry>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for RegistryConfig {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self {
|
2026-07-10 18:55:32 +01:00
|
|
|
registries: vec![Registry {
|
|
|
|
|
url: OVH_REGISTRY_URL.to_string(),
|
|
|
|
|
name: "Server 1 (OVH)".to_string(),
|
|
|
|
|
tls_verify: false,
|
|
|
|
|
enabled: true,
|
|
|
|
|
priority: 0,
|
|
|
|
|
}],
|
2026-04-12 08:09:14 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl RegistryConfig {
|
|
|
|
|
/// Get enabled registries sorted by priority.
|
|
|
|
|
pub fn active_registries(&self) -> Vec<&Registry> {
|
|
|
|
|
let mut regs: Vec<&Registry> = self.registries.iter().filter(|r| r.enabled).collect();
|
|
|
|
|
regs.sort_by_key(|r| r.priority);
|
|
|
|
|
regs
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Rewrite an image reference to use a specific registry.
|
2026-07-10 18:55:32 +01:00
|
|
|
/// E.g., "docker.io/lfg2025/bitcoin-knots:latest" with registry "146.59.87.168:3000/lfg2025"
|
2026-04-23 08:22:32 -04:00
|
|
|
/// becomes "146.59.87.168:3000/lfg2025/bitcoin-knots:latest".
|
2026-04-12 08:09:14 -04:00
|
|
|
pub fn rewrite_image(&self, image: &str, registry: &Registry) -> String {
|
|
|
|
|
// Extract the image name (last component after the org/namespace)
|
|
|
|
|
// Handles: "registry/org/image:tag" -> "image:tag"
|
|
|
|
|
let image_name = extract_image_name(image);
|
|
|
|
|
format!("{}/{}", registry.url, image_name)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Extract the image name from a full image reference.
|
2026-07-10 18:55:32 +01:00
|
|
|
/// "146.59.87.168:3000/lfg2025/bitcoin-knots:latest" -> "bitcoin-knots:latest"
|
2026-04-12 08:09:14 -04:00
|
|
|
/// "docker.io/gitea/gitea:1.23" -> "gitea:1.23"
|
|
|
|
|
fn extract_image_name(image: &str) -> &str {
|
|
|
|
|
// Split by '/' and take the last segment (image:tag)
|
|
|
|
|
image.rsplit('/').next().unwrap_or(image)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-22 03:26:09 -04:00
|
|
|
/// Load registry config from disk, merging in any default registries
|
|
|
|
|
/// that the operator hasn't explicitly removed. This lets us roll out
|
|
|
|
|
/// new default mirrors (e.g. a new Server 3) to existing nodes without
|
|
|
|
|
/// them having to edit their saved config. Explicit removals stick —
|
|
|
|
|
/// if the URL is absent from disk AND absent from current defaults, it
|
|
|
|
|
/// stays gone.
|
2026-04-12 08:09:14 -04:00
|
|
|
pub async fn load_registries(data_dir: &Path) -> Result<RegistryConfig> {
|
|
|
|
|
let path = data_dir.join(REGISTRY_FILE);
|
|
|
|
|
if !path.exists() {
|
|
|
|
|
return Ok(RegistryConfig::default());
|
|
|
|
|
}
|
|
|
|
|
let content = fs::read_to_string(&path)
|
|
|
|
|
.await
|
|
|
|
|
.context("Failed to read registry config")?;
|
2026-04-22 03:26:09 -04:00
|
|
|
let mut config: RegistryConfig =
|
2026-04-12 08:09:14 -04:00
|
|
|
serde_json::from_str(&content).unwrap_or_else(|_| RegistryConfig::default());
|
2026-04-22 03:26:09 -04:00
|
|
|
|
2026-04-23 08:51:26 -04:00
|
|
|
// One-time migration: the Hetzner VPS at 23.182.128.160 was
|
|
|
|
|
// decommissioned 2026-04-23. Existing nodes have it baked into
|
|
|
|
|
// their saved registry list (was the original Server 1). Strip it
|
|
|
|
|
// on load so every container pull doesn't pay a connection-refused
|
|
|
|
|
// timeout against a dead host. Exception to the usual "explicit
|
|
|
|
|
// removals stick" rule: the user never chose to add this — it
|
|
|
|
|
// was a default.
|
|
|
|
|
let before = config.registries.len();
|
2026-04-28 15:00:58 -04:00
|
|
|
config
|
|
|
|
|
.registries
|
|
|
|
|
.retain(|r| !r.url.contains("23.182.128.160"));
|
2026-07-10 18:55:32 +01:00
|
|
|
// Same treatment for the retired tx1138 registry (was Server 2 in older
|
|
|
|
|
// defaults): strip it on load so nothing ever pulls through the dead
|
|
|
|
|
// host again.
|
|
|
|
|
config
|
|
|
|
|
.registries
|
|
|
|
|
.retain(|r| !r.url.contains(RETIRED_TX1138_HOST));
|
2026-04-23 08:51:26 -04:00
|
|
|
let mut changed = config.registries.len() != before;
|
|
|
|
|
|
2026-04-22 03:26:09 -04:00
|
|
|
// Migrate: any default registry URL that isn't already in the
|
|
|
|
|
// saved list gets appended at the end (so existing priority order
|
|
|
|
|
// is preserved for anything the operator already configured).
|
|
|
|
|
let defaults = RegistryConfig::default();
|
|
|
|
|
let known: std::collections::HashSet<String> =
|
|
|
|
|
config.registries.iter().map(|r| r.url.clone()).collect();
|
|
|
|
|
let max_priority = config
|
|
|
|
|
.registries
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|r| r.priority)
|
|
|
|
|
.max()
|
|
|
|
|
.unwrap_or(0);
|
|
|
|
|
for (i, def) in defaults.registries.iter().enumerate() {
|
|
|
|
|
if !known.contains(&def.url) {
|
|
|
|
|
let mut cloned = def.clone();
|
|
|
|
|
cloned.priority = max_priority.saturating_add(10 + i as u32);
|
|
|
|
|
config.registries.push(cloned);
|
2026-04-23 08:51:26 -04:00
|
|
|
changed = true;
|
2026-04-22 03:26:09 -04:00
|
|
|
}
|
|
|
|
|
}
|
2026-05-01 05:02:39 -04:00
|
|
|
let before_order: Vec<(String, bool, u32)> = config
|
|
|
|
|
.registries
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|r| (r.url.clone(), r.enabled, r.priority))
|
|
|
|
|
.collect();
|
|
|
|
|
force_ovh_registry_primary(&mut config);
|
|
|
|
|
changed = changed
|
|
|
|
|
|| before_order
|
|
|
|
|
!= config
|
|
|
|
|
.registries
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|r| (r.url.clone(), r.enabled, r.priority))
|
|
|
|
|
.collect::<Vec<_>>();
|
2026-04-23 08:51:26 -04:00
|
|
|
if changed {
|
2026-04-22 03:26:09 -04:00
|
|
|
// Persist so the next load doesn't have to re-merge.
|
2026-07-02 21:02:54 -04:00
|
|
|
if let Err(e) = save_registries(data_dir, &config).await {
|
|
|
|
|
tracing::warn!("Failed to persist migrated registry config: {e:#}");
|
|
|
|
|
}
|
2026-04-22 03:26:09 -04:00
|
|
|
}
|
2026-04-12 08:09:14 -04:00
|
|
|
Ok(config)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-01 05:02:39 -04:00
|
|
|
fn force_ovh_registry_primary(config: &mut RegistryConfig) {
|
|
|
|
|
let defaults = RegistryConfig::default();
|
|
|
|
|
for def in defaults.registries {
|
|
|
|
|
if !config.registries.iter().any(|r| r.url == def.url) {
|
|
|
|
|
config.registries.push(def);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for registry in config.registries.iter_mut() {
|
|
|
|
|
match registry.url.as_str() {
|
|
|
|
|
OVH_REGISTRY_URL => {
|
|
|
|
|
registry.name = "Server 1 (OVH)".to_string();
|
|
|
|
|
registry.tls_verify = false;
|
|
|
|
|
registry.enabled = true;
|
|
|
|
|
registry.priority = 0;
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
if registry.priority <= 10 {
|
|
|
|
|
registry.priority = registry.priority.saturating_add(20);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-12 08:09:14 -04:00
|
|
|
/// Save registry config to disk.
|
|
|
|
|
pub async fn save_registries(data_dir: &Path, config: &RegistryConfig) -> Result<()> {
|
|
|
|
|
let dir = data_dir.join("config");
|
|
|
|
|
fs::create_dir_all(&dir)
|
|
|
|
|
.await
|
|
|
|
|
.context("Failed to create config dir")?;
|
|
|
|
|
let path = data_dir.join(REGISTRY_FILE);
|
|
|
|
|
let content =
|
|
|
|
|
serde_json::to_string_pretty(config).context("Failed to serialize registry config")?;
|
|
|
|
|
fs::write(&path, content)
|
|
|
|
|
.await
|
|
|
|
|
.context("Failed to write registry config")?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
use tempfile::TempDir;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_extract_image_name() {
|
|
|
|
|
assert_eq!(
|
2026-07-10 18:55:32 +01:00
|
|
|
extract_image_name("146.59.87.168:3000/lfg2025/bitcoin-knots:latest"),
|
2026-04-12 08:09:14 -04:00
|
|
|
"bitcoin-knots:latest"
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
extract_image_name("docker.io/gitea/gitea:1.23"),
|
|
|
|
|
"gitea:1.23"
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(extract_image_name("localhost/myimage:v1"), "myimage:v1");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_rewrite_image() {
|
|
|
|
|
let config = RegistryConfig::default();
|
2026-07-10 18:55:32 +01:00
|
|
|
// An image hardcoded to some other registry rewrites to OVH when
|
|
|
|
|
// asked for the primary mirror.
|
2026-04-21 15:54:07 -04:00
|
|
|
let primary = &config.registries[0];
|
2026-04-12 08:09:14 -04:00
|
|
|
assert_eq!(
|
2026-07-10 18:55:32 +01:00
|
|
|
config.rewrite_image("docker.io/lfg2025/bitcoin-knots:latest", primary),
|
2026-04-23 08:22:32 -04:00
|
|
|
"146.59.87.168:3000/lfg2025/bitcoin-knots:latest"
|
2026-04-12 08:09:14 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_active_registries_sorted() {
|
|
|
|
|
let config = RegistryConfig::default();
|
|
|
|
|
let active = config.active_registries();
|
2026-07-10 18:55:32 +01:00
|
|
|
assert_eq!(active.len(), 1);
|
|
|
|
|
assert_eq!(active[0].url, OVH_REGISTRY_URL);
|
2026-04-12 08:09:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn test_load_default() {
|
|
|
|
|
let tmp = TempDir::new().unwrap();
|
|
|
|
|
let config = load_registries(tmp.path()).await.unwrap();
|
2026-07-10 18:55:32 +01:00
|
|
|
assert_eq!(config.registries.len(), 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn test_load_strips_retired_tx1138_registry() {
|
|
|
|
|
// Nodes provisioned before the retirement have the tx1138 registry
|
|
|
|
|
// baked into their saved config (was Server 2). It must be stripped
|
|
|
|
|
// on load and never re-added by the defaults merge.
|
|
|
|
|
let tmp = TempDir::new().unwrap();
|
|
|
|
|
let config = RegistryConfig {
|
|
|
|
|
registries: vec![
|
|
|
|
|
Registry {
|
|
|
|
|
url: format!("{RETIRED_TX1138_HOST}/lfg2025"),
|
|
|
|
|
name: "Server 2 (tx1138)".into(),
|
|
|
|
|
tls_verify: true,
|
|
|
|
|
enabled: true,
|
|
|
|
|
priority: 10,
|
|
|
|
|
},
|
|
|
|
|
Registry {
|
|
|
|
|
url: OVH_REGISTRY_URL.into(),
|
|
|
|
|
name: "Server 1 (OVH)".into(),
|
|
|
|
|
tls_verify: false,
|
|
|
|
|
enabled: true,
|
|
|
|
|
priority: 0,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
save_registries(tmp.path(), &config).await.unwrap();
|
|
|
|
|
let loaded = load_registries(tmp.path()).await.unwrap();
|
|
|
|
|
assert!(
|
|
|
|
|
!loaded
|
|
|
|
|
.registries
|
|
|
|
|
.iter()
|
|
|
|
|
.any(|r| r.url.contains(RETIRED_TX1138_HOST)),
|
|
|
|
|
"retired tx1138 registry must be stripped on load; got {:?}",
|
|
|
|
|
loaded.registries
|
|
|
|
|
);
|
2026-04-12 08:09:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn test_save_load_roundtrip() {
|
|
|
|
|
let tmp = TempDir::new().unwrap();
|
|
|
|
|
let mut config = RegistryConfig::default();
|
|
|
|
|
config.registries.push(Registry {
|
|
|
|
|
url: "myregistry.com/apps".into(),
|
|
|
|
|
name: "Custom".into(),
|
|
|
|
|
tls_verify: true,
|
|
|
|
|
enabled: true,
|
|
|
|
|
priority: 5,
|
|
|
|
|
});
|
|
|
|
|
save_registries(tmp.path(), &config).await.unwrap();
|
|
|
|
|
let loaded = load_registries(tmp.path()).await.unwrap();
|
2026-07-10 18:55:32 +01:00
|
|
|
assert_eq!(loaded.registries.len(), 2);
|
2026-04-12 08:09:14 -04:00
|
|
|
}
|
|
|
|
|
}
|