provision.sh 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env bash
  2. #
  3. # provision.sh — install the 3x-ui panel into a golden image (Packer).
  4. #
  5. # Self-contained: mirrors install.sh's download/extract logic but DELIBERATELY
  6. # does NOT run config_after_install and does NOT create a database. The image
  7. # must ship without /etc/x-ui/x-ui.db so that deploy/firstboot generates unique
  8. # per-instance credentials on first boot. Both x-ui.service and
  9. # x-ui-firstboot.service are enabled but NOT started here.
  10. #
  11. # Inputs (from Packer environment_vars):
  12. # XUI_VERSION release tag (e.g. v3.3.1) or 'latest'
  13. # XUI_ARCH amd64 (default) or arm64
  14. set -euo pipefail
  15. XUI_VERSION="${XUI_VERSION:-latest}"
  16. XUI_ARCH="${XUI_ARCH:-amd64}"
  17. XUI_DIR="/usr/local/x-ui"
  18. REPO="MHSanaei/3x-ui"
  19. export DEBIAN_FRONTEND=noninteractive
  20. echo "[provision] installing base packages..."
  21. apt-get update
  22. apt-get install -y --no-install-recommends \
  23. ca-certificates curl tar tzdata socat openssl cron jq
  24. echo "[provision] resolving 3x-ui version..."
  25. if [ "$XUI_VERSION" = "latest" ]; then
  26. XUI_VERSION=$(curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" | jq -r '.tag_name')
  27. fi
  28. if [ -z "$XUI_VERSION" ] || [ "$XUI_VERSION" = "null" ]; then
  29. echo "[provision] ERROR: could not resolve 3x-ui release tag" >&2
  30. exit 1
  31. fi
  32. echo "[provision] installing 3x-ui ${XUI_VERSION} (${XUI_ARCH})"
  33. tarball="x-ui-linux-${XUI_ARCH}.tar.gz"
  34. url="https://github.com/${REPO}/releases/download/${XUI_VERSION}/${tarball}"
  35. tmp="$(mktemp -d)"
  36. trap 'rm -rf "$tmp"' EXIT
  37. # Download the RELEASED binary tarball (no Go build inside the image).
  38. curl -fL4 --retry 3 -o "${tmp}/${tarball}" "$url"
  39. # Extract into /usr/local/ (the tarball contains an x-ui/ directory).
  40. systemctl stop x-ui > /dev/null 2>&1 || true
  41. rm -rf "$XUI_DIR"
  42. tar -xzf "${tmp}/${tarball}" -C /usr/local/
  43. chmod +x "${XUI_DIR}/x-ui" "${XUI_DIR}/x-ui.sh"
  44. chmod +x "${XUI_DIR}"/bin/* 2> /dev/null || true
  45. # Install the x-ui management CLI.
  46. if [ -f "${XUI_DIR}/x-ui.sh" ]; then
  47. cp -f "${XUI_DIR}/x-ui.sh" /usr/bin/x-ui
  48. else
  49. curl -fL4 -o /usr/bin/x-ui "https://raw.githubusercontent.com/${REPO}/main/x-ui.sh"
  50. fi
  51. chmod +x /usr/bin/x-ui
  52. mkdir -p /var/log/x-ui
  53. # Panel systemd unit (Ubuntu base => debian variant).
  54. install -m 644 "${XUI_DIR}/x-ui.service.debian" /etc/systemd/system/x-ui.service
  55. # First-boot per-instance credential unit + script (uploaded to /tmp/firstboot).
  56. install -m 755 /tmp/firstboot/x-ui-firstboot.sh "${XUI_DIR}/x-ui-firstboot.sh"
  57. install -m 644 /tmp/firstboot/x-ui-firstboot.service /etc/systemd/system/x-ui-firstboot.service
  58. systemctl daemon-reload
  59. # Enable (start on next boot) but do NOT start now — there is no DB yet.
  60. systemctl enable x-ui-firstboot.service
  61. systemctl enable x-ui.service
  62. # Belt-and-braces: ensure no DB / sentinel was created during provisioning.
  63. rm -f /etc/x-ui/x-ui.db /etc/x-ui/x-ui.db-* /etc/x-ui/.firstboot-done 2> /dev/null || true
  64. echo "[provision] done — panel installed, services enabled, NO database initialized."