snapshot-userdata.sh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/bin/bash
  2. #
  3. # Lightsail snapshot provisioning user-data (used by build-snapshot.sh).
  4. #
  5. # Installs the 3x-ui panel into a build instance but creates NO database and
  6. # NO credentials, and enables the first-boot unit. The instance is then snapshot
  7. # so that every instance launched from the snapshot generates its own unique
  8. # credentials on first boot (see deploy/firstboot/).
  9. #
  10. # This is the Lightsail equivalent of deploy/packer/scripts/provision.sh. It is
  11. # NOT for end users — use deploy/lightsail/launch-script.sh for a direct install.
  12. set -e
  13. export DEBIAN_FRONTEND=noninteractive
  14. REPO=MHSanaei/3x-ui
  15. XUI_DIR=/usr/local/x-ui
  16. RAW="https://raw.githubusercontent.com/${REPO}/main"
  17. apt-get update
  18. apt-get install -y --no-install-recommends \
  19. ca-certificates curl tar tzdata socat openssl cron jq
  20. ARCH=$(dpkg --print-architecture) # amd64 | arm64
  21. VER=$(curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" | jq -r .tag_name)
  22. if [ -z "$VER" ] || [ "$VER" = "null" ]; then
  23. echo "failed to resolve 3x-ui version" >&2
  24. exit 1
  25. fi
  26. tmp=$(mktemp -d)
  27. curl -fL4 --retry 3 -o "${tmp}/x.tar.gz" \
  28. "https://github.com/${REPO}/releases/download/${VER}/x-ui-linux-${ARCH}.tar.gz"
  29. systemctl stop x-ui > /dev/null 2>&1 || true
  30. rm -rf "$XUI_DIR"
  31. tar -xzf "${tmp}/x.tar.gz" -C /usr/local/
  32. chmod +x "${XUI_DIR}/x-ui" "${XUI_DIR}/x-ui.sh"
  33. chmod +x "${XUI_DIR}"/bin/* 2> /dev/null || true
  34. cp -f "${XUI_DIR}/x-ui.sh" /usr/bin/x-ui
  35. chmod +x /usr/bin/x-ui
  36. mkdir -p /var/log/x-ui
  37. # Panel + first-boot systemd units.
  38. install -m 644 "${XUI_DIR}/x-ui.service.debian" /etc/systemd/system/x-ui.service
  39. curl -fL4 -o "${XUI_DIR}/x-ui-firstboot.sh" "${RAW}/deploy/firstboot/x-ui-firstboot.sh"
  40. curl -fL4 -o /etc/systemd/system/x-ui-firstboot.service "${RAW}/deploy/firstboot/x-ui-firstboot.service"
  41. chmod 755 "${XUI_DIR}/x-ui-firstboot.sh"
  42. chmod 644 /etc/systemd/system/x-ui-firstboot.service
  43. systemctl daemon-reload
  44. systemctl enable x-ui-firstboot.service
  45. systemctl enable x-ui.service
  46. # No DB, no creds in the image — first boot generates them per-instance.
  47. rm -f /etc/x-ui/x-ui.db /etc/x-ui/x-ui.db-* /etc/x-ui/.firstboot-done 2> /dev/null || true
  48. # Marker that build-snapshot.sh polls for over SSH.
  49. touch /var/lib/3xui-provision-done
  50. echo "[snapshot-userdata] provisioned 3x-ui ${VER} (${ARCH}); no DB created."