updates.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. """Update checking and management routes."""
  2. import asyncio
  3. import logging
  4. import os
  5. import re
  6. import shutil
  7. import sys
  8. import httpx
  9. from fastapi import APIRouter, BackgroundTasks, Depends
  10. from sqlalchemy.ext.asyncio import AsyncSession
  11. from backend.app.core.config import APP_VERSION, GITHUB_REPO, settings
  12. from backend.app.core.database import get_db
  13. logger = logging.getLogger(__name__)
  14. router = APIRouter(prefix="/updates", tags=["updates"])
  15. # Global state for update progress
  16. _update_status = {
  17. "status": "idle", # idle, checking, downloading, installing, complete, error
  18. "progress": 0,
  19. "message": "",
  20. "error": None,
  21. }
  22. def _is_docker_environment() -> bool:
  23. """Detect if running inside a Docker container."""
  24. if os.path.exists("/.dockerenv"):
  25. return True
  26. try:
  27. with open("/proc/1/cgroup") as f:
  28. if "docker" in f.read():
  29. return True
  30. except (FileNotFoundError, PermissionError):
  31. pass
  32. git_dir = settings.base_dir / ".git"
  33. return not git_dir.exists()
  34. def _find_executable(name: str) -> str | None:
  35. """Find an executable in PATH or common locations."""
  36. # Try standard PATH first
  37. path = shutil.which(name)
  38. if path:
  39. return path
  40. # Common locations for executables (useful when running as systemd service)
  41. common_paths = [
  42. f"/usr/bin/{name}",
  43. f"/usr/local/bin/{name}",
  44. f"/opt/homebrew/bin/{name}",
  45. f"/home/linuxbrew/.linuxbrew/bin/{name}",
  46. f"{os.path.expanduser('~')}/.nvm/current/bin/{name}",
  47. f"{os.path.expanduser('~')}/.local/bin/{name}",
  48. ]
  49. for p in common_paths:
  50. if os.path.isfile(p) and os.access(p, os.X_OK):
  51. return p
  52. return None
  53. def parse_version(version: str) -> tuple:
  54. """Parse version string into tuple for comparison.
  55. Returns (major, minor, patch, is_prerelease, prerelease_num)
  56. where is_prerelease is 0 for release, 1 for prerelease.
  57. This ensures releases sort higher than prereleases of same version.
  58. Examples:
  59. "0.1.5" -> (0, 1, 5, 0, 0) # release
  60. "0.1.5b7" -> (0, 1, 5, 1, 7) # beta 7
  61. "0.1.5b10" -> (0, 1, 5, 1, 10) # beta 10
  62. """
  63. # Remove 'v' prefix if present
  64. version = version.lstrip("v")
  65. # Match version pattern: major.minor.patch[b|beta|alpha|rc]N
  66. match = re.match(r"(\d+)\.(\d+)\.(\d+)(?:b|beta|alpha|rc)?(\d+)?", version)
  67. if match:
  68. major = int(match.group(1))
  69. minor = int(match.group(2))
  70. patch = int(match.group(3))
  71. prerelease_num = int(match.group(4)) if match.group(4) else 0
  72. # Check if this is a prerelease (has b/beta/alpha/rc suffix)
  73. is_prerelease = 1 if re.search(r"[a-zA-Z]", version.split(".")[-1]) else 0
  74. return (major, minor, patch, is_prerelease, prerelease_num)
  75. # Fallback: try simple split
  76. parts = []
  77. for part in version.split("."):
  78. try:
  79. parts.append(int(part))
  80. except ValueError:
  81. num = "".join(c for c in part if c.isdigit())
  82. parts.append(int(num) if num else 0)
  83. return tuple(parts) + (0, 0)
  84. def is_newer_version(latest: str, current: str) -> bool:
  85. """Check if latest version is newer than current.
  86. Properly handles prerelease versions:
  87. - 0.1.5 > 0.1.5b7 (release is newer than any beta)
  88. - 0.1.5b8 > 0.1.5b7 (later beta is newer)
  89. - 0.1.6b1 > 0.1.5 (next version beta is newer than current release)
  90. """
  91. try:
  92. latest_parsed = parse_version(latest)
  93. current_parsed = parse_version(current)
  94. # Compare (major, minor, patch) first
  95. latest_base = latest_parsed[:3]
  96. current_base = current_parsed[:3]
  97. if latest_base > current_base:
  98. return True
  99. elif latest_base < current_base:
  100. return False
  101. # Same base version - compare prerelease status
  102. # is_prerelease: 0 = release, 1 = prerelease
  103. # Release (0) should be "greater" than prerelease (1)
  104. latest_is_prerelease = latest_parsed[3] if len(latest_parsed) > 3 else 0
  105. current_is_prerelease = current_parsed[3] if len(current_parsed) > 3 else 0
  106. if latest_is_prerelease < current_is_prerelease:
  107. # latest is release, current is prerelease -> latest is newer
  108. return True
  109. elif latest_is_prerelease > current_is_prerelease:
  110. # latest is prerelease, current is release -> latest is NOT newer
  111. return False
  112. # Both are same type (both release or both prerelease)
  113. # Compare prerelease numbers
  114. latest_prerelease_num = latest_parsed[4] if len(latest_parsed) > 4 else 0
  115. current_prerelease_num = current_parsed[4] if len(current_parsed) > 4 else 0
  116. return latest_prerelease_num > current_prerelease_num
  117. except Exception:
  118. return False
  119. @router.get("/version")
  120. async def get_version():
  121. """Get current application version."""
  122. return {
  123. "version": APP_VERSION,
  124. "repo": GITHUB_REPO,
  125. }
  126. @router.get("/check")
  127. async def check_for_updates(db: AsyncSession = Depends(get_db)):
  128. """Check GitHub for available updates."""
  129. global _update_status
  130. _update_status = {
  131. "status": "checking",
  132. "progress": 0,
  133. "message": "Checking for updates...",
  134. "error": None,
  135. }
  136. try:
  137. async with httpx.AsyncClient() as client:
  138. response = await client.get(
  139. f"https://api.github.com/repos/{GITHUB_REPO}/releases/latest",
  140. headers={"Accept": "application/vnd.github.v3+json"},
  141. timeout=10.0,
  142. )
  143. if response.status_code == 404:
  144. # No releases yet
  145. _update_status = {
  146. "status": "idle",
  147. "progress": 100,
  148. "message": "No releases found",
  149. "error": None,
  150. }
  151. return {
  152. "update_available": False,
  153. "current_version": APP_VERSION,
  154. "latest_version": None,
  155. "message": "No releases found",
  156. }
  157. response.raise_for_status()
  158. release_data = response.json()
  159. latest_version = release_data.get("tag_name", "").lstrip("v")
  160. release_name = release_data.get("name", latest_version)
  161. release_notes = release_data.get("body", "")
  162. release_url = release_data.get("html_url", "")
  163. published_at = release_data.get("published_at", "")
  164. update_available = is_newer_version(latest_version, APP_VERSION)
  165. _update_status = {
  166. "status": "idle",
  167. "progress": 100,
  168. "message": "Update available" if update_available else "Up to date",
  169. "error": None,
  170. }
  171. is_docker = _is_docker_environment()
  172. return {
  173. "update_available": update_available,
  174. "current_version": APP_VERSION,
  175. "latest_version": latest_version,
  176. "release_name": release_name,
  177. "release_notes": release_notes,
  178. "release_url": release_url,
  179. "published_at": published_at,
  180. "is_docker": is_docker,
  181. "update_method": "docker" if is_docker else "git",
  182. }
  183. except httpx.HTTPError as e:
  184. logger.error(f"Failed to check for updates: {e}")
  185. _update_status = {
  186. "status": "error",
  187. "progress": 0,
  188. "message": "Failed to check for updates",
  189. "error": str(e),
  190. }
  191. return {
  192. "update_available": False,
  193. "current_version": APP_VERSION,
  194. "latest_version": None,
  195. "error": str(e),
  196. }
  197. async def _perform_update():
  198. """Perform the actual update using git fetch and reset."""
  199. global _update_status
  200. try:
  201. base_dir = settings.base_dir
  202. # Find git executable (may not be in PATH when running as systemd service)
  203. git_path = _find_executable("git")
  204. if not git_path:
  205. _update_status = {
  206. "status": "error",
  207. "progress": 0,
  208. "message": "Git not found",
  209. "error": "Could not find git executable. Please ensure git is installed.",
  210. }
  211. return
  212. logger.info(f"Using git at: {git_path}")
  213. # Git config to avoid safe.directory issues
  214. git_config = ["-c", f"safe.directory={base_dir}"]
  215. _update_status = {
  216. "status": "downloading",
  217. "progress": 10,
  218. "message": "Configuring git...",
  219. "error": None,
  220. }
  221. # Ensure remote uses HTTPS (SSH may not be available)
  222. https_url = f"https://github.com/{GITHUB_REPO}.git"
  223. process = await asyncio.create_subprocess_exec(
  224. git_path,
  225. *git_config,
  226. "remote",
  227. "set-url",
  228. "origin",
  229. https_url,
  230. cwd=str(base_dir),
  231. stdout=asyncio.subprocess.PIPE,
  232. stderr=asyncio.subprocess.PIPE,
  233. )
  234. await process.communicate()
  235. _update_status = {
  236. "status": "downloading",
  237. "progress": 20,
  238. "message": "Fetching latest changes...",
  239. "error": None,
  240. }
  241. # Fetch from origin
  242. process = await asyncio.create_subprocess_exec(
  243. git_path,
  244. *git_config,
  245. "fetch",
  246. "origin",
  247. "main",
  248. cwd=str(base_dir),
  249. stdout=asyncio.subprocess.PIPE,
  250. stderr=asyncio.subprocess.PIPE,
  251. )
  252. stdout, stderr = await process.communicate()
  253. if process.returncode != 0:
  254. error_msg = stderr.decode() if stderr else "Git fetch failed"
  255. logger.error(f"Git fetch failed: {error_msg}")
  256. _update_status = {
  257. "status": "error",
  258. "progress": 0,
  259. "message": "Failed to fetch updates",
  260. "error": error_msg,
  261. }
  262. return
  263. _update_status = {
  264. "status": "downloading",
  265. "progress": 40,
  266. "message": "Applying updates...",
  267. "error": None,
  268. }
  269. # Hard reset to origin/main (clean update, no merge conflicts)
  270. process = await asyncio.create_subprocess_exec(
  271. git_path,
  272. *git_config,
  273. "reset",
  274. "--hard",
  275. "origin/main",
  276. cwd=str(base_dir),
  277. stdout=asyncio.subprocess.PIPE,
  278. stderr=asyncio.subprocess.PIPE,
  279. )
  280. stdout, stderr = await process.communicate()
  281. if process.returncode != 0:
  282. error_msg = stderr.decode() if stderr else "Git reset failed"
  283. logger.error(f"Git reset failed: {error_msg}")
  284. _update_status = {
  285. "status": "error",
  286. "progress": 0,
  287. "message": "Failed to apply updates",
  288. "error": error_msg,
  289. }
  290. return
  291. _update_status = {
  292. "status": "installing",
  293. "progress": 50,
  294. "message": "Installing dependencies...",
  295. "error": None,
  296. }
  297. # Install Python dependencies
  298. process = await asyncio.create_subprocess_exec(
  299. sys.executable,
  300. "-m",
  301. "pip",
  302. "install",
  303. "-r",
  304. "requirements.txt",
  305. "-q",
  306. cwd=str(base_dir),
  307. stdout=asyncio.subprocess.PIPE,
  308. stderr=asyncio.subprocess.PIPE,
  309. )
  310. stdout, stderr = await process.communicate()
  311. if process.returncode != 0:
  312. logger.warning(f"pip install warning: {stderr.decode() if stderr else 'unknown'}")
  313. # Try to build frontend if npm is available (optional - static files are pre-built)
  314. npm_path = _find_executable("npm")
  315. frontend_dir = base_dir / "frontend"
  316. if npm_path and frontend_dir.exists():
  317. _update_status = {
  318. "status": "installing",
  319. "progress": 70,
  320. "message": "Building frontend...",
  321. "error": None,
  322. }
  323. # npm install
  324. process = await asyncio.create_subprocess_exec(
  325. npm_path,
  326. "install",
  327. cwd=str(frontend_dir),
  328. stdout=asyncio.subprocess.PIPE,
  329. stderr=asyncio.subprocess.PIPE,
  330. )
  331. await process.communicate()
  332. # npm run build
  333. process = await asyncio.create_subprocess_exec(
  334. npm_path,
  335. "run",
  336. "build",
  337. cwd=str(frontend_dir),
  338. stdout=asyncio.subprocess.PIPE,
  339. stderr=asyncio.subprocess.PIPE,
  340. )
  341. stdout, stderr = await process.communicate()
  342. if process.returncode != 0:
  343. logger.warning(f"Frontend build warning: {stderr.decode() if stderr else 'unknown'}")
  344. else:
  345. logger.info("npm not found or frontend dir missing - using pre-built static files")
  346. _update_status = {
  347. "status": "complete",
  348. "progress": 100,
  349. "message": "Update complete! Please restart the application.",
  350. "error": None,
  351. }
  352. logger.info("Update completed successfully")
  353. except Exception as e:
  354. logger.error(f"Update failed: {e}")
  355. _update_status = {
  356. "status": "error",
  357. "progress": 0,
  358. "message": "Update failed",
  359. "error": str(e),
  360. }
  361. @router.post("/apply")
  362. async def apply_update(background_tasks: BackgroundTasks):
  363. """Apply available update (git pull + rebuild)."""
  364. global _update_status
  365. if _update_status["status"] in ["downloading", "installing"]:
  366. return {
  367. "success": False,
  368. "message": "Update already in progress",
  369. "status": _update_status,
  370. }
  371. # Check if running in Docker
  372. if _is_docker_environment():
  373. return {
  374. "success": False,
  375. "is_docker": True,
  376. "message": (
  377. "Docker installations cannot be updated in-app. "
  378. "Please update via Docker Compose: "
  379. "git pull && docker compose build --pull && docker compose up -d"
  380. ),
  381. }
  382. # Start update in background
  383. background_tasks.add_task(_perform_update)
  384. _update_status = {
  385. "status": "downloading",
  386. "progress": 10,
  387. "message": "Starting update...",
  388. "error": None,
  389. }
  390. return {
  391. "success": True,
  392. "message": "Update started",
  393. "status": _update_status,
  394. }
  395. @router.get("/status")
  396. async def get_update_status():
  397. """Get current update status."""
  398. return _update_status