Add Arch mirror manager: benchmark, apply, pin, restore, status

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QKxGW1raAWaqeU8WdHsMsp
This commit is contained in:
2026-08-24 18:34:55 -04:00
co-authored by Claude Fable 5
parent 18041c7537
commit 2bcf0a20f0
7 changed files with 201 additions and 0 deletions
+77
View File
@@ -0,0 +1,77 @@
# Sourced helpers for omarchy-cn-mirror-* commands
CN_MIRRORS_JSON="$OMARCHY_PATH/cn/mirrors.json"
CN_MIRRORLIST="/etc/pacman.d/mirrorlist"
CN_PROFILE_FILE="$HOME/.config/omarchycn/mirror-profile"
cn_mirror_ids() {
jq -r '.mirrors[].id' "$CN_MIRRORS_JSON"
}
cn_mirror_url() {
jq -r --arg id "$1" '.mirrors[] | select(.id == $id) | .url' "$CN_MIRRORS_JSON"
}
cn_mirror_name() {
jq -r --arg id "$1" '.mirrors[] | select(.id == $id) | .name' "$CN_MIRRORS_JSON"
}
cn_mirror_ids_by_region() {
jq -r --arg r "$1" '.mirrors[] | select(.region == $r) | .id' "$CN_MIRRORS_JSON"
}
# Probe one mirror: prints "<speed_bytes_s> <ttfb_s> <sync_age_s|stale|unknown>"
cn_mirror_probe() {
local url="$1"
local speed ttfb lastsync age now
read -r speed ttfb < <(curl -sS -o /dev/null -m 12 --connect-timeout 5 \
-w '%{speed_download} %{time_starttransfer}\n' \
"$url/core/os/x86_64/core.db" 2>/dev/null) || true
speed="${speed%%.*}"
[[ -z $speed ]] && speed=0
age="unknown"
lastsync=$(curl -sS -m 5 --connect-timeout 5 "$url/lastsync" 2>/dev/null | tr -cd '0-9')
if [[ -n $lastsync ]]; then
now=$(date +%s)
age=$((now - lastsync))
(( age > 86400 )) && age="stale"
fi
echo "$speed ${ttfb:-0} $age"
}
# Rank ids by probe speed, excluding dead (speed 0) and stale mirrors
cn_mirror_rank() {
local id url speed ttfb age
for id in "$@"; do
url=$(cn_mirror_url "$id")
read -r speed ttfb age < <(cn_mirror_probe "$url")
(( speed > 0 )) || continue
[[ $age == "stale" ]] && continue
echo "$speed $id"
done | sort -rn | awk '{print $2}'
}
# Write mirrorlist from mirror ids (first = primary), with timestamped backup
cn_mirror_write_list() {
local profile="$1"
shift
local stamp id url content=""
stamp=$(date +%Y%m%d-%H%M%S)
content="## OmarchyCN mirrorlist (profile: $profile, generated $stamp)\n"
for id in "$@"; do
url=$(cn_mirror_url "$id")
content+="## $id: $(cn_mirror_name "$id")\nServer = $url/\$repo/os/\$arch\n"
done
sudo cp "$CN_MIRRORLIST" "$CN_MIRRORLIST.omarchycn-bak-$stamp"
printf '%b' "$content" | sudo tee "$CN_MIRRORLIST" > /dev/null
mkdir -p "${CN_PROFILE_FILE%/*}"
echo "$profile" > "$CN_PROFILE_FILE"
echo "Mirrorlist written ($CN_MIRRORLIST), backup: $CN_MIRRORLIST.omarchycn-bak-$stamp"
}
+12
View File
@@ -0,0 +1,12 @@
{
"mirrors": [
{ "id": "tuna", "name": "清华大学 TUNA", "region": "cn", "url": "https://mirrors.tuna.tsinghua.edu.cn/archlinux" },
{ "id": "ustc", "name": "中国科学技术大学", "region": "cn", "url": "https://mirrors.ustc.edu.cn/archlinux" },
{ "id": "aliyun", "name": "阿里云", "region": "cn", "url": "https://mirrors.aliyun.com/archlinux" },
{ "id": "tencent", "name": "腾讯云", "region": "cn", "url": "https://mirrors.cloud.tencent.com/archlinux" },
{ "id": "netease", "name": "网易", "region": "cn", "url": "https://mirrors.163.com/archlinux" },
{ "id": "sjtu", "name": "上海交通大学", "region": "cn", "url": "https://mirror.sjtu.edu.cn/archlinux" },
{ "id": "geo", "name": "Arch 官方 Geo Mirror", "region": "global", "url": "https://geo.mirror.pkgbuild.com" },
{ "id": "worldwide", "name": "Arch 官方 Worldwide", "region": "global", "url": "https://mirror.rackspace.com/archlinux" }
]
}