seed-recipe-repos.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env bash
  2. # Seed a git recipe repo (recipes-production / recipes-playground) from
  3. # the current launcher repo state (specs/tech-architecture/
  4. # ha-addon-recipe-repos.md, step 6).
  5. #
  6. # The operator creates the bare repo on git.kilork.org FIRST; this script
  7. # builds the layout, commits it, and prints the push command:
  8. #
  9. # scripts/seed-recipe-repos.sh <name> [git-url]
  10. #
  11. # <name> the repo name (recipes-production | recipes-playground)
  12. # git-url the git URL of the repo the operator created (printed in the
  13. # push instructions; a local path also works for a dry run)
  14. #
  15. # Layout (a subset of the launcher repo, per the spec):
  16. # recipes/<name>/… # tracked recipe dirs (machine state like .lock is
  17. # # gitignored → excluded by `git archive` for free)
  18. # docker-images/<dir>/ # build contexts (source of truth; optional when
  19. # # the image is prebuilt in the registry)
  20. #
  21. # NOTE: no config.toml is copied. A recipe repo's config.toml is
  22. # operator-curated and must be CREDENTIAL-FREE (the repo may be shared
  23. # with other users): clusters/nccl only. Secrets (ssh user/key, HA
  24. # url/token, mikrotik password, registry base) come from the add-on
  25. # options, never from the repo — see the curated example in
  26. # git.kilork.org/sm121/recipes.
  27. set -euo pipefail
  28. cd "$(dirname "$0")/.."
  29. NAME="${1:-}"
  30. URL="${2:-}"
  31. if [ -z "$NAME" ]; then
  32. echo "usage: scripts/seed-recipe-repos.sh <name> [git-url]" >&2
  33. echo " e.g. scripts/seed-recipe-repos.sh recipes-production https://git.kilork.org/sm121/recipes-production.git" >&2
  34. exit 1
  35. fi
  36. if ! git rev-parse --git-dir >/dev/null 2>&1; then
  37. echo "error: run this from the sm121-launcher repo (it reads the tracked state via git archive)" >&2
  38. exit 1
  39. fi
  40. PARENT="$(dirname "$(pwd)")"
  41. OUT="$PARENT/$NAME"
  42. if [ -e "$OUT" ]; then
  43. echo "error: $OUT already exists — refusing to clobber" >&2
  44. exit 1
  45. fi
  46. TMP="$(mktemp -d)"
  47. trap 'rm -rf "$TMP"' EXIT
  48. # The tracked state of the launcher repo (gitignored machine state —
  49. # .lock ledgers, target/, scratch — is excluded for free).
  50. git archive --prefix=repo/ HEAD | tar -x -C "$TMP"
  51. mkdir -p "$OUT"
  52. # NOTE: no config.toml is seeded — it is operator-curated and must stay
  53. # credential-free (the repo may be shared with other users). The launcher
  54. # repo's config.toml carries secrets (HA token, mikrotik password, ssh
  55. # key path) and must never be copied into a shared repo.
  56. [ -d "$TMP/repo/recipes" ] && cp -R "$TMP/repo/recipes" "$OUT/recipes"
  57. [ -d "$TMP/repo/docker-images" ] && cp -R "$TMP/repo/docker-images" "$OUT/docker-images"
  58. # A minimal README so the repo documents its own layout.
  59. cat > "$OUT/README.md" <<EOF
  60. # sm121 $NAME
  61. A sm121 recipe repo (specs/tech-architecture/ha-addon-recipe-repos.md in
  62. the launcher repo). Layout:
  63. - `recipes/<name>/recipe.toml` — the recipes
  64. - `docker-images/<dir>/` — image build contexts (source of truth;
  65. optional when the image is prebuilt in the registry)
  66. - `config.toml` (operator-curated, NOT seeded by the script) — the shared
  67. cluster/config source of truth. CREDENTIAL-FREE by contract (this repo
  68. may be shared with other users): clusters/nccl only; the secrets (ssh
  69. user/key, HA url/token, mikrotik password, registry base) come from the
  70. add-on options (sm121_ssh_user, sm121_ssh_key_b64, sm121_ha_url,
  71. sm121_ha_token, sm121_registry_base, sm121_config_extra)
  72. Seeded from the sm121-launcher repo on $(date -u +%Y-%m-%d).
  73. EOF
  74. git -C "$OUT" init -q
  75. git -C "$OUT" add -A
  76. git -C "$OUT" commit -qm "seed $NAME from sm121-launcher @ $(git rev-parse --short HEAD)"
  77. N_RECIPES=$(find "$OUT/recipes" -name recipe.toml 2>/dev/null | wc -l | tr -d ' ')
  78. N_IMAGES=$(find "$OUT/docker-images" -maxdepth 1 -mindepth 1 -type d 2>/dev/null | wc -l | tr -d ' ')
  79. echo "seeded $OUT:"
  80. echo " recipes: $N_RECIPES"
  81. echo " docker-images: $N_IMAGES dirs"
  82. echo
  83. echo "push it (the operator creates the repo first):"
  84. if [ -n "$URL" ]; then
  85. echo " git -C $OUT remote add origin $URL"
  86. echo " git -C $OUT push -u origin $(git -C "$OUT" symbolic-ref --short HEAD)"
  87. else
  88. echo " git -C $OUT remote add origin <the git.kilork.org URL>"
  89. echo " git -C $OUT push -u origin main"
  90. fi
  91. echo
  92. echo "then add it to the launcher config (order = priority, first wins):"
  93. echo " [[recipe_repos]]"
  94. echo " url = \"${URL:-<git-url>}\""