entrypoint.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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_REPO_RESYNC_MINUTES background re-sync interval (default 10)
  27. # SM121_WEB_PORT dashboard port (default 3100)
  28. # SM121_WEB_BIND dashboard bind (default 0.0.0.0)
  29. # SM121_WEB_TOKEN bearer token (REQUIRED for a non-loopback bind)
  30. set -eu
  31. CONFIG=/etc/sm121/config.toml
  32. mkdir -p /etc/sm121
  33. # --- 1. the effective config --------------------------------------------------
  34. if [ -f /etc/sm121/base.toml ]; then
  35. cp /etc/sm121/base.toml "$CONFIG"
  36. else
  37. : > "$CONFIG"
  38. fi
  39. {
  40. # (if-form, not `&&`: a false condition must not trip `set -e`)
  41. if [ -n "${SM121_SSH_USER:-}" ]; then printf 'ssh_user = "%s"\n' "$SM121_SSH_USER"; fi
  42. if [ -n "${SM121_SSH_KEY_PATH:-}" ]; then printf 'ssh_key_path = "%s"\n' "$SM121_SSH_KEY_PATH"; fi
  43. } >> "$CONFIG"
  44. # JSON list of {"url","token"} → [[recipe_repos]] blocks (tojson keeps the
  45. # values properly escaped — JSON basic strings ARE valid TOML strings).
  46. if [ -n "${SM121_RECIPE_REPOS:-}" ]; then
  47. printf '%s' "$SM121_RECIPE_REPOS" | jq -r '
  48. .[] | ("[[recipe_repos]]\nurl = " + (.url | tojson) + "\n"
  49. + (if .token then "token = " + (.token | tojson) + "\n" else "" end))
  50. ' >> "$CONFIG"
  51. fi
  52. if [ -n "${SM121_HA_URL:-}" ] && [ -n "${SM121_HA_TOKEN:-}" ]; then
  53. printf '[home_assistant]\nurl = "%s"\ntoken = "%s"\n' \
  54. "$SM121_HA_URL" "$SM121_HA_TOKEN" >> "$CONFIG"
  55. fi
  56. if [ -n "${SM121_REGISTRY_BASE:-}" ]; then
  57. printf '[registry]\nbase = "%s"\n' "$SM121_REGISTRY_BASE" >> "$CONFIG"
  58. fi
  59. # Raw per-install TOML, appended verbatim (the option is a multi-line
  60. # string). This is where credentials that can't be expressed as flat
  61. # options live — e.g. a [mikrotik] block (it carries a password, so it is
  62. # deliberately NOT in the shared recipe repo's config.toml). Never logged.
  63. if [ -n "${SM121_CONFIG_EXTRA:-}" ]; then
  64. printf '%s\n' "$SM121_CONFIG_EXTRA" >> "$CONFIG"
  65. fi
  66. # --- 2. the SSH key ------------------------------------------------------------
  67. # HA add-on options cannot mount files: the key arrives base64-encoded as
  68. # SM121_SSH_KEY_B64 (the `sm121_ssh_key_b64` option) and is written to
  69. # SM121_SSH_KEY_PATH (default /etc/sm121/ssh_key). A plain file path
  70. # (SM121_SSH_KEY, e.g. a docker secret mount) is also accepted.
  71. KEY_PATH="${SM121_SSH_KEY_PATH:-/etc/sm121/ssh_key}"
  72. if [ -n "${SM121_SSH_KEY_B64:-}" ]; then
  73. mkdir -p "$(dirname "$KEY_PATH")"
  74. printf '%s' "$SM121_SSH_KEY_B64" | base64 -d > "$KEY_PATH"
  75. chmod 600 "$KEY_PATH"
  76. elif [ -n "${SM121_SSH_KEY:-}" ] && [ -f "$SM121_SSH_KEY" ]; then
  77. cp "$SM121_SSH_KEY" "$KEY_PATH"
  78. chmod 600 "$KEY_PATH"
  79. fi
  80. # --- 3 + 4. sync + web, with the re-sync supervisor ----------------------------
  81. SM121="sm121 --config $CONFIG"
  82. $SM121 repos sync || echo "warning: initial repo sync failed (the supervisor retries)"
  83. INTERVAL="${SM121_REPO_RESYNC_MINUTES:-10}"
  84. (
  85. while :; do
  86. sleep "$((INTERVAL * 60))"
  87. $SM121 repos sync || true
  88. done
  89. ) &
  90. # A web crash exits the container; the HA restart policy (always) re-runs
  91. # this entrypoint (re-sync + fresh dashboard). No state is lost (the run
  92. # ledger lives in the recipe repos, the cache in ~/.cache/sm121).
  93. exec $SM121 web \
  94. --port "${SM121_WEB_PORT:-3100}" \
  95. --bind "${SM121_WEB_BIND:-0.0.0.0}" \
  96. ${SM121_WEB_TOKEN:+--token "$SM121_WEB_TOKEN"}