feat: 仓库接入通用化——批量预检 + 多仓 ssh 别名配给
This commit is contained in:
1 parent
0787496427
commit
50ba8d9297
3 files changed
+77
-3
No files matched your search
+25
-1
@@ -38,6 +38,7 @@ __all__ = [
|
||||
"checkout_dir",
|
||||
"resolve_repo",
|
||||
"check_repo_access",
|
||||
"check_repo_access_many",
|
||||
]
|
||||
|
||||
|
||||
@@ -203,10 +204,33 @@ def check_repo_access(repo_url, runner=None, timeout=30):
|
||||
return (True, head[0] if head else "")
|
||||
|
||||
|
||||
def check_repo_access_many(repo_urls, runner=None, timeout=30):
|
||||
"""批量预检:逐个调 check_repo_access,返回 {url: (ok, detail)}。
|
||||
|
||||
顺序与输入一致;单个失败不中断其余(批量预检的意义就是一次
|
||||
看清哪些仓通、哪些不通)。空列表返回空 dict。
|
||||
"""
|
||||
out = {}
|
||||
for url in repo_urls or []:
|
||||
out[url] = check_repo_access(url, runner=runner, timeout=timeout)
|
||||
return out
|
||||
|
||||
|
||||
def _main(argv):
|
||||
if len(argv) >= 3 and argv[1] == "check-many":
|
||||
results = check_repo_access_many(argv[2:])
|
||||
failed = 0
|
||||
for url, (ok, detail) in results.items():
|
||||
if ok:
|
||||
print("ok %s HEAD=%s" % (url, detail))
|
||||
else:
|
||||
failed += 1
|
||||
print("fail %s: %s" % (url, detail))
|
||||
return 1 if failed else 0
|
||||
if len(argv) != 3 or argv[1] != "check":
|
||||
sys.stderr.write(
|
||||
"用法: python3 -m cloud.repos check <repo_url>\n")
|
||||
"用法: python3 -m cloud.repos check <repo_url>\n"
|
||||
" python3 -m cloud.repos check-many <repo_url>...\n")
|
||||
return 2
|
||||
ok, detail = check_repo_access(argv[2])
|
||||
if ok:
|
||||
|
||||
@@ -146,6 +146,26 @@ class CheckRepoAccessTestCase(unittest.TestCase):
|
||||
self.assertFalse(ok)
|
||||
self.assertIn("timed out", detail)
|
||||
|
||||
def test_many_continues_after_failure(self):
|
||||
seen = []
|
||||
|
||||
def runner(cmd, timeout=None):
|
||||
seen.append(cmd[2])
|
||||
if "good" in cmd[2]:
|
||||
return SimpleNamespace(returncode=0,
|
||||
stdout="abc123\tHEAD\n", stderr="")
|
||||
return SimpleNamespace(returncode=128, stdout="",
|
||||
stderr="fatal: denied")
|
||||
from cloud.repos import check_repo_access_many
|
||||
out = check_repo_access_many(
|
||||
["git@github.com:o/good.git", "git@github.com:o/bad.git"],
|
||||
runner=runner)
|
||||
self.assertEqual(seen, ["git@github.com:o/good.git",
|
||||
"git@github.com:o/bad.git"])
|
||||
self.assertTrue(out["git@github.com:o/good.git"][0])
|
||||
self.assertFalse(out["git@github.com:o/bad.git"][0])
|
||||
self.assertEqual(check_repo_access_many([]), {})
|
||||
|
||||
|
||||
class CheckoutDirTestCase(unittest.TestCase):
|
||||
def test_sanitizes_and_stays_inside(self):
|
||||
|
||||
@@ -4,13 +4,20 @@
|
||||
# 用法(主机上以 root 跑):
|
||||
# ./provision_codexsvc_key.sh git@github.com:ORG/REPO.git [key-name]
|
||||
# ./provision_codexsvc_key.sh --check git@github.com:ORG/REPO.git [key-name]
|
||||
# ./provision_codexsvc_key.sh --list
|
||||
#
|
||||
# 流程:
|
||||
# 1. 在 /home/codexsvc/.ssh/ 生成 ed25519 key(已存在则复用,绝不覆盖);
|
||||
# 2. 打印公钥(只打印公钥,私钥绝不打印),请把公钥以只读 deploy key
|
||||
# 加到 GitHub 仓库 Settings -> Deploy keys;
|
||||
# 3. --check 时以 codexsvc 身份跑 git ls-remote,通了才算配好。
|
||||
# 4. 另有现成的 key 要复用时,把 key 文件拷到上述目录并 chown
|
||||
# 3. 在 /home/codexsvc/.ssh/config 里幂等地写一个 Host 别名
|
||||
# github-<key-name>(HostName github.com、该 key、IdentitiesOnly):
|
||||
# 多仓各用各的别名,项目 repo_url 写 git@github-<key-name>:ORG/REPO.git
|
||||
# 形,git 按别名自动选 key,不用在 worker 里拼 GIT_SSH_COMMAND,
|
||||
# 也不用担心 ssh 试遍全部 key 撞 GitHub 的尝试上限;
|
||||
# 别名块带 # codexsvc-<key-name> 标记,已存在时不重复写。
|
||||
# 4. --check 时以 codexsvc 身份跑 git ls-remote,通了才算配好。
|
||||
# 5. 另有现成的 key 要复用时,把 key 文件拷到上述目录并 chown
|
||||
# codexsvc、chmod 600,再用 --check 验,不要改本脚本去读 root 的 key。
|
||||
#
|
||||
# 安全边界:本脚本不往控制面、环境文件或任何日志里写私钥;公钥
|
||||
@@ -20,6 +27,11 @@ CHECK=0
|
||||
if [ "${1:-}" = "--check" ]; then
|
||||
CHECK=1; shift
|
||||
fi
|
||||
if [ "${1:-}" = "--list" ]; then
|
||||
ls -1 /home/codexsvc/.ssh/id_*.pub 2>/dev/null || true
|
||||
grep -h "^Host github-" /home/codexsvc/.ssh/config 2>/dev/null || true
|
||||
exit 0
|
||||
fi
|
||||
URL="${1:-}"
|
||||
NAME="${2:-codex-github}"
|
||||
if [ -z "$URL" ]; then
|
||||
@@ -45,6 +57,24 @@ if [ ! -f "$KEY" ]; then
|
||||
fi
|
||||
chmod 600 "$KEY" 2>/dev/null || true
|
||||
chown codexsvc:codexsvc "$KEY" "$KEY.pub" 2>/dev/null || true
|
||||
CONFIG="$SSH_DIR/config"
|
||||
touch "$CONFIG"
|
||||
chown codexsvc:codexsvc "$CONFIG"
|
||||
chmod 600 "$CONFIG"
|
||||
if ! grep -q "^# codexsvc-$NAME$" "$CONFIG" 2>/dev/null; then
|
||||
cat >> "$CONFIG" <<EOF
|
||||
# codexsvc-$NAME
|
||||
Host github-$NAME
|
||||
HostName github.com
|
||||
User git
|
||||
IdentityFile $KEY
|
||||
IdentitiesOnly yes
|
||||
StrictHostKeyChecking accept-new
|
||||
EOF
|
||||
echo "已写 ssh 别名:git@github-$NAME:ORG/REPO.git(项目 repo_url 用此形)"
|
||||
else
|
||||
echo "ssh 别名 github-$NAME 已存在,不重复写"
|
||||
fi
|
||||
echo "公钥(加到 GitHub 仓库的只读 Deploy keys):"
|
||||
cat "$KEY.pub"
|
||||
if [ "$CHECK" = "1" ]; then
|
||||
|
||||
Reference in new issue
Block a user