entrypoint.sh 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/bin/sh
  2. # sm121 HA add-on entrypoint (specs/tech-architecture/ha-addon-recipe-repos.md):
  3. #
  4. # 1. write the effective /etc/sm121/config.toml from the add-on options
  5. # (env vars); an optional base config at /etc/sm121/base.toml (e.g. a
  6. # recipe repo's config.toml, mounted) carries the cluster definitions
  7. # — the generated sections (recipe_repos / home_assistant / registry)
  8. # are APPENDED, so the base must not define them
  9. # 2. drop the SSH key (a mounted secret) at SM121_SSH_KEY_PATH
  10. # 3. sync the recipe repos (clone/pull)
  11. # 4. run `sm121 web` on 3100; a supervisor loop re-syncs the repos every
  12. # SM121_REPO_RESYNC_MINUTES so new recipes land without a restart
  13. #
  14. # Env (the add-on options — HA maps each `sm121_*` option to the
  15. # uppercased env var; the standalone flow sets the same names directly):
  16. # SM121_RECIPE_REPOS JSON list of {"url","token"} → [[recipe_repos]]
  17. # SM121_SSH_USER ssh_user
  18. # SM121_SSH_KEY_B64 base64 private key (HA option; written to KEY_PATH)
  19. # SM121_SSH_KEY path to a mounted private key (docker secret mount)
  20. # SM121_SSH_KEY_PATH where the key lands (default /etc/sm121/ssh_key)
  21. # SM121_HA_URL / SM121_HA_TOKEN → [home_assistant]
  22. # SM121_REGISTRY_BASE → [registry] base (for --image-source registry)
  23. # SM121_CONFIG_EXTRA raw TOML appended verbatim (per-install
  24. # credentials that must not live in the shared
  25. # repo, e.g. a [mikrotik] block with its password)
  26. # SM121_LAUNCHER_URL launcher repo (default git.kilork.org/sm121/
  27. # sm121-launcher) — sm121 is COMPILED from it on
  28. # first boot (the Pi is the build host)
  29. # SM121_LAUNCHER_REV rev to compile (default main; a commit hash
  30. # pins the binary)
  31. # SM121_BUILD_JOBS cargo -j (default 2 — a Pi 4 must not OOM)
  32. # SM121_REPO_RESYNC_MINUTES background re-sync interval (default 10)
  33. # SM121_WEB_PORT dashboard port (default 3100)
  34. # SM121_WEB_BIND dashboard bind (default 0.0.0.0)
  35. # SM121_WEB_TOKEN bearer token (REQUIRED for a non-loopback bind)
  36. set -eu
  37. CONFIG=/etc/sm121/config.toml
  38. mkdir -p /etc/sm121
  39. # --- 1. the effective config --------------------------------------------------
  40. if [ -f /etc/sm121/base.toml ]; then
  41. cp /etc/sm121/base.toml "$CONFIG"
  42. else
  43. : > "$CONFIG"
  44. fi
  45. {
  46. # (if-form, not `&&`: a false condition must not trip `set -e`)
  47. if [ -n "${SM121_SSH_USER:-}" ]; then printf 'ssh_user = "%s"\n' "$SM121_SSH_USER"; fi
  48. if [ -n "${SM121_SSH_KEY_PATH:-}" ]; then printf 'ssh_key_path = "%s"\n' "$SM121_SSH_KEY_PATH"; fi
  49. } >> "$CONFIG"
  50. # JSON list of {"url","token"} → [[recipe_repos]] blocks (tojson keeps the
  51. # values properly escaped — JSON basic strings ARE valid TOML strings).
  52. if [ -n "${SM121_RECIPE_REPOS:-}" ]; then
  53. printf '%s' "$SM121_RECIPE_REPOS" | jq -r '
  54. .[] | ("[[recipe_repos]]\nurl = " + (.url | tojson) + "\n"
  55. + (if .token then "token = " + (.token | tojson) + "\n" else "" end))
  56. ' >> "$CONFIG"
  57. fi
  58. if [ -n "${SM121_HA_URL:-}" ] && [ -n "${SM121_HA_TOKEN:-}" ]; then
  59. printf '[home_assistant]\nurl = "%s"\ntoken = "%s"\n' \
  60. "$SM121_HA_URL" "$SM121_HA_TOKEN" >> "$CONFIG"
  61. fi
  62. if [ -n "${SM121_REGISTRY_BASE:-}" ]; then
  63. printf '[registry]\nbase = "%s"\n' "$SM121_REGISTRY_BASE" >> "$CONFIG"
  64. fi
  65. # Raw per-install TOML, appended verbatim (the option is a multi-line
  66. # string). This is where credentials that can't be expressed as flat
  67. # options live — e.g. a [mikrotik] block (it carries a password, so it is
  68. # deliberately NOT in the shared recipe repo's config.toml). Never logged.
  69. if [ -n "${SM121_CONFIG_EXTRA:-}" ]; then
  70. printf '%s\n' "$SM121_CONFIG_EXTRA" >> "$CONFIG"
  71. fi
  72. # --- 2. the SSH key ------------------------------------------------------------
  73. # HA add-on options cannot mount files: the key arrives base64-encoded as
  74. # SM121_SSH_KEY_B64 (the `sm121_ssh_key_b64` option) and is written to
  75. # SM121_SSH_KEY_PATH (default /etc/sm121/ssh_key). A plain file path
  76. # (SM121_SSH_KEY, e.g. a docker secret mount) is also accepted.
  77. KEY_PATH="${SM121_SSH_KEY_PATH:-/etc/sm121/ssh_key}"
  78. if [ -n "${SM121_SSH_KEY_B64:-}" ]; then
  79. mkdir -p "$(dirname "$KEY_PATH")"
  80. printf '%s' "$SM121_SSH_KEY_B64" | base64 -d > "$KEY_PATH"
  81. chmod 600 "$KEY_PATH"
  82. elif [ -n "${SM121_SSH_KEY:-}" ] && [ -f "$SM121_SSH_KEY" ]; then
  83. cp "$SM121_SSH_KEY" "$KEY_PATH"
  84. chmod 600 "$KEY_PATH"
  85. fi
  86. # --- 2b. compile sm121 if needed (the Pi is the build host) ------------------
  87. # The launcher source comes from the launcher repo (SM121_LAUNCHER_URL @
  88. # SM121_LAUNCHER_REV). The binary + a rev stamp + the cargo target cache
  89. # live in the add-on's data folder ($HOME — HA sets it to the add-on's data
  90. # dir; falls back to /root), so a reboot with an unchanged rev is fast and
  91. # a rev change recompiles once. First boot is slow (a full release build:
  92. # minutes on a Pi 5, longer on a Pi 4) — the add-on just shows "starting".
  93. HOME="${HOME:-/root}"
  94. SM121_BIN="$HOME/.sm121/bin/sm121"
  95. LAUNCHER_URL="${SM121_LAUNCHER_URL:-https://git.kilork.org/sm121/sm121-launcher.git}"
  96. LAUNCHER_REV="${SM121_LAUNCHER_REV:-main}"
  97. STAMP="$HOME/.sm121/rev"
  98. if [ ! -x "$SM121_BIN" ] || [ "$(cat "$STAMP" 2>/dev/null)" != "$LAUNCHER_REV" ]; then
  99. echo "sm121: compiling from $LAUNCHER_URL @ $LAUNCHER_REV (first boot / rev changed)"
  100. mkdir -p "$HOME/.sm121/bin"
  101. SRC="$(mktemp -d)"
  102. case "$LAUNCHER_REV" in
  103. main|master) git clone -q --depth 1 --branch "$LAUNCHER_REV" "$LAUNCHER_URL" "$SRC" ;;
  104. *) git clone -q "$LAUNCHER_URL" "$SRC" && git -C "$SRC" checkout -q "$LAUNCHER_REV" ;;
  105. esac
  106. CARGO_TARGET_DIR="$HOME/.sm121/target" \
  107. CARGO_BUILD_JOBS="${SM121_BUILD_JOBS:-2}" \
  108. cargo build --release --manifest-path "$SRC/Cargo.toml"
  109. cp "$HOME/.sm121/target/release/sm121" "$SM121_BIN"
  110. printf '%s' "$LAUNCHER_REV" > "$STAMP"
  111. rm -rf "$SRC"
  112. echo "sm121: built ($LAUNCHER_REV) → $SM121_BIN"
  113. fi
  114. # --- 3 + 4. sync + web, with the re-sync supervisor ----------------------------
  115. SM121="$SM121_BIN --config $CONFIG"
  116. $SM121 repos sync || echo "warning: initial repo sync failed (the supervisor retries)"
  117. INTERVAL="${SM121_REPO_RESYNC_MINUTES:-10}"
  118. (
  119. while :; do
  120. sleep "$((INTERVAL * 60))"
  121. $SM121 repos sync || true
  122. done
  123. ) &
  124. # A web crash exits the container; the HA restart policy (always) re-runs
  125. # this entrypoint (re-sync + fresh dashboard). No state is lost (the run
  126. # ledger lives in the recipe repos, the cache in ~/.cache/sm121).
  127. exec $SM121 web \
  128. --port "${SM121_WEB_PORT:-3100}" \
  129. --bind "${SM121_WEB_BIND:-0.0.0.0}" \
  130. ${SM121_WEB_TOKEN:+--token "$SM121_WEB_TOKEN"}