deploy.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. import contextlib
  2. import json
  3. import logging
  4. import subprocess
  5. import tarfile
  6. import tempfile
  7. import time
  8. import uuid
  9. from enum import Enum
  10. from itertools import cycle
  11. from pathlib import Path
  12. from typing import Any, Dict, Generator, List, Optional, Union
  13. import rignore
  14. import typer
  15. from httpx import Client
  16. from pydantic import BaseModel, EmailStr, TypeAdapter, ValidationError
  17. from rich.text import Text
  18. from rich_toolkit import RichToolkit
  19. from rich_toolkit.menu import Option
  20. from typing_extensions import Annotated
  21. from fastapi_cloud_cli.commands.login import login
  22. from fastapi_cloud_cli.utils.api import APIClient
  23. from fastapi_cloud_cli.utils.apps import AppConfig, get_app_config, write_app_config
  24. from fastapi_cloud_cli.utils.auth import is_logged_in
  25. from fastapi_cloud_cli.utils.cli import get_rich_toolkit, handle_http_errors
  26. logger = logging.getLogger(__name__)
  27. def _get_app_name(path: Path) -> str:
  28. # TODO: use pyproject.toml to get the app name
  29. return path.name
  30. def _should_exclude_entry(path: Path) -> bool:
  31. parts_to_exclude = [".venv", "__pycache__", ".mypy_cache", ".pytest_cache"]
  32. if any(part in path.parts for part in parts_to_exclude):
  33. return True
  34. if path.suffix == ".pyc":
  35. return True
  36. return False
  37. def archive(path: Path) -> Path:
  38. logger.debug("Starting archive creation for path: %s", path)
  39. files = rignore.walk(
  40. path,
  41. should_exclude_entry=_should_exclude_entry,
  42. additional_ignore_paths=[".fastapicloudignore"],
  43. )
  44. temp_dir = tempfile.mkdtemp()
  45. logger.debug("Created temp directory: %s", temp_dir)
  46. name = f"fastapi-cloud-deploy-{uuid.uuid4()}"
  47. tar_path = Path(temp_dir) / f"{name}.tar"
  48. logger.debug("Archive will be created at: %s", tar_path)
  49. file_count = 0
  50. with tarfile.open(tar_path, "w") as tar:
  51. for filename in files:
  52. if filename.is_dir():
  53. continue
  54. logger.debug("Adding %s to archive", filename.relative_to(path))
  55. tar.add(filename, arcname=filename.relative_to(path))
  56. file_count += 1
  57. logger.debug("Archive created successfully with %s files", file_count)
  58. return tar_path
  59. class Team(BaseModel):
  60. id: str
  61. slug: str
  62. name: str
  63. def _get_teams() -> List[Team]:
  64. with APIClient() as client:
  65. response = client.get("/teams/")
  66. response.raise_for_status()
  67. data = response.json()["data"]
  68. return [Team.model_validate(team) for team in data]
  69. class AppResponse(BaseModel):
  70. id: str
  71. slug: str
  72. def _create_app(team_id: str, app_name: str) -> AppResponse:
  73. with APIClient() as client:
  74. response = client.post(
  75. "/apps/",
  76. json={"name": app_name, "team_id": team_id},
  77. )
  78. response.raise_for_status()
  79. return AppResponse.model_validate(response.json())
  80. class DeploymentStatus(str, Enum):
  81. waiting_upload = "waiting_upload"
  82. ready_for_build = "ready_for_build"
  83. building = "building"
  84. extracting = "extracting"
  85. extracting_failed = "extracting_failed"
  86. building_image = "building_image"
  87. building_image_failed = "building_image_failed"
  88. deploying = "deploying"
  89. deploying_failed = "deploying_failed"
  90. verifying = "verifying"
  91. verifying_failed = "verifying_failed"
  92. success = "success"
  93. failed = "failed"
  94. @classmethod
  95. def to_human_readable(cls, status: "DeploymentStatus") -> str:
  96. return {
  97. cls.waiting_upload: "Waiting for upload",
  98. cls.ready_for_build: "Ready for build",
  99. cls.building: "Building",
  100. cls.extracting: "Extracting",
  101. cls.extracting_failed: "Extracting failed",
  102. cls.building_image: "Building image",
  103. cls.building_image_failed: "Build failed",
  104. cls.deploying: "Deploying",
  105. cls.deploying_failed: "Deploying failed",
  106. cls.verifying: "Verifying",
  107. cls.verifying_failed: "Verifying failed",
  108. cls.success: "Success",
  109. cls.failed: "Failed",
  110. }[status]
  111. class CreateDeploymentResponse(BaseModel):
  112. id: str
  113. app_id: str
  114. slug: str
  115. status: DeploymentStatus
  116. dashboard_url: str
  117. url: str
  118. def _create_deployment(app_id: str) -> CreateDeploymentResponse:
  119. with APIClient() as client:
  120. response = client.post(f"/apps/{app_id}/deployments/")
  121. response.raise_for_status()
  122. return CreateDeploymentResponse.model_validate(response.json())
  123. class RequestUploadResponse(BaseModel):
  124. url: str
  125. fields: Dict[str, str]
  126. def _upload_deployment(deployment_id: str, archive_path: Path) -> None:
  127. logger.debug(
  128. "Starting deployment upload for deployment: %s",
  129. deployment_id,
  130. )
  131. logger.debug(
  132. "Archive path: %s, size: %s bytes",
  133. archive_path,
  134. archive_path.stat().st_size,
  135. )
  136. with APIClient() as fastapi_client, Client() as client:
  137. # Get the upload URL
  138. logger.debug("Requesting upload URL from API")
  139. response = fastapi_client.post(f"/deployments/{deployment_id}/upload")
  140. response.raise_for_status()
  141. upload_data = RequestUploadResponse.model_validate(response.json())
  142. logger.debug("Received upload URL: %s", upload_data.url)
  143. # Upload the archive
  144. logger.debug("Starting file upload to S3")
  145. upload_response = client.post(
  146. upload_data.url,
  147. data=upload_data.fields,
  148. files={"file": archive_path.open("rb")},
  149. )
  150. upload_response.raise_for_status()
  151. logger.debug("File upload completed successfully")
  152. # Notify the server that the upload is complete
  153. logger.debug("Notifying API that upload is complete")
  154. notify_response = fastapi_client.post(
  155. f"/deployments/{deployment_id}/upload-complete"
  156. )
  157. notify_response.raise_for_status()
  158. logger.debug("Upload notification sent successfully")
  159. def _get_app(app_slug: str) -> Optional[AppResponse]:
  160. with APIClient() as client:
  161. response = client.get(f"/apps/{app_slug}")
  162. if response.status_code == 404:
  163. return None
  164. response.raise_for_status()
  165. data = response.json()
  166. return AppResponse.model_validate(data)
  167. def _get_apps(team_id: str) -> List[AppResponse]:
  168. with APIClient() as client:
  169. response = client.get("/apps/", params={"team_id": team_id})
  170. response.raise_for_status()
  171. data = response.json()["data"]
  172. return [AppResponse.model_validate(app) for app in data]
  173. def _stream_build_logs(deployment_id: str) -> Generator[str, None, None]:
  174. with APIClient() as client:
  175. with client.stream(
  176. "GET", f"/deployments/{deployment_id}/build-logs", timeout=60
  177. ) as response:
  178. response.raise_for_status()
  179. yield from response.iter_lines()
  180. WAITING_MESSAGES = [
  181. "🚀 Preparing for liftoff! Almost there...",
  182. "👹 Sneaking past the dependency gremlins... Don't wake them up!",
  183. "🤏 Squishing code into a tiny digital sandwich. Nom nom nom.",
  184. "📉 Server space running low. Time to delete those cat videos?",
  185. "🐢 Uploading at blazing speeds of 1 byte per hour. Patience, young padawan.",
  186. "🔌 Connecting to server... Please stand by while we argue with the firewall.",
  187. "💥 Oops! We've angered the Python God. Sacrificing a rubber duck to appease it.",
  188. "🧙 Sprinkling magic deployment dust. Abracadabra!",
  189. "👀 Hoping that @tiangolo doesn't find out about this deployment.",
  190. "🍪 Cookie monster detected on server. Deploying anti-cookie shields.",
  191. ]
  192. LONG_WAIT_MESSAGES = [
  193. "😅 Well, that's embarrassing. We're still waiting for the deployment to finish...",
  194. "🤔 Maybe we should have brought snacks for this wait...",
  195. "🥱 Yawn... Still waiting...",
  196. "🤯 Time is relative... Especially when you're waiting for a deployment...",
  197. ]
  198. def _configure_app(toolkit: RichToolkit, path_to_deploy: Path) -> AppConfig:
  199. if not toolkit.confirm(f"Setup and deploy [blue]{path_to_deploy}[/]?", tag="dir"):
  200. raise typer.Exit(0)
  201. toolkit.print_line()
  202. with toolkit.progress("Fetching teams...") as progress:
  203. with handle_http_errors(
  204. progress, message="Error fetching teams. Please try again later."
  205. ):
  206. teams = _get_teams()
  207. toolkit.print_line()
  208. team = toolkit.ask(
  209. "Select the team you want to deploy to:",
  210. tag="team",
  211. options=[Option({"name": team.name, "value": team}) for team in teams],
  212. )
  213. toolkit.print_line()
  214. create_new_app = toolkit.confirm(
  215. "Do you want to create a new app?", tag="app", default=True
  216. )
  217. toolkit.print_line()
  218. if not create_new_app:
  219. with toolkit.progress("Fetching apps...") as progress:
  220. with handle_http_errors(
  221. progress, message="Error fetching apps. Please try again later."
  222. ):
  223. apps = _get_apps(team.id)
  224. toolkit.print_line()
  225. if not apps:
  226. toolkit.print(
  227. "No apps found in this team. You can create a new app instead.",
  228. )
  229. raise typer.Exit(1)
  230. app = toolkit.ask(
  231. "Select the app you want to deploy to:",
  232. options=[Option({"name": app.slug, "value": app}) for app in apps],
  233. )
  234. else:
  235. app_name = toolkit.input(
  236. title="What's your app name?",
  237. default=_get_app_name(path_to_deploy),
  238. )
  239. toolkit.print_line()
  240. with toolkit.progress(title="Creating app...") as progress:
  241. with handle_http_errors(progress):
  242. app = _create_app(team.id, app_name)
  243. progress.log(f"App created successfully! App slug: {app.slug}")
  244. app_config = AppConfig(app_id=app.id, team_id=team.id)
  245. write_app_config(path_to_deploy, app_config)
  246. return app_config
  247. def _wait_for_deployment(
  248. toolkit: RichToolkit, app_id: str, deployment: CreateDeploymentResponse
  249. ) -> None:
  250. messages = cycle(WAITING_MESSAGES)
  251. toolkit.print(
  252. "Checking the status of your deployment 👀",
  253. tag="cloud",
  254. )
  255. toolkit.print_line()
  256. toolkit.print(
  257. f"You can also check the status at [link={deployment.dashboard_url}]{deployment.dashboard_url}[/link]",
  258. )
  259. toolkit.print_line()
  260. time_elapsed = 0.0
  261. started_at = time.monotonic()
  262. last_message_changed_at = time.monotonic()
  263. with toolkit.progress(
  264. next(messages), inline_logs=True, lines_to_show=20
  265. ) as progress:
  266. with handle_http_errors(progress=progress):
  267. for line in _stream_build_logs(deployment.id):
  268. time_elapsed = time.monotonic() - started_at
  269. data = json.loads(line)
  270. if "message" in data:
  271. progress.log(Text.from_ansi(data["message"].rstrip()))
  272. if data.get("type") == "complete":
  273. progress.log("")
  274. progress.log(
  275. f"🐔 Ready the chicken! Your app is ready at [link={deployment.url}]{deployment.url}[/link]"
  276. )
  277. progress.log("")
  278. progress.log(
  279. f"You can also check the app logs at [link={deployment.dashboard_url}]{deployment.dashboard_url}[/link]"
  280. )
  281. break
  282. if data.get("type") == "failed":
  283. progress.log("")
  284. progress.log(
  285. f"😔 Oh no! Something went wrong. Check out the logs at [link={deployment.dashboard_url}]{deployment.dashboard_url}[/link]"
  286. )
  287. raise typer.Exit(1)
  288. if time_elapsed > 30:
  289. messages = cycle(LONG_WAIT_MESSAGES) # pragma: no cover
  290. if (time.monotonic() - last_message_changed_at) > 2:
  291. progress.title = next(messages) # pragma: no cover
  292. last_message_changed_at = time.monotonic() # pragma: no cover
  293. class SignupToWaitingList(BaseModel):
  294. email: EmailStr
  295. name: Optional[str] = None
  296. organization: Optional[str] = None
  297. role: Optional[str] = None
  298. team_size: Optional[str] = None
  299. location: Optional[str] = None
  300. use_case: Optional[str] = None
  301. secret_code: Optional[str] = None
  302. def _send_waitlist_form(
  303. result: SignupToWaitingList,
  304. toolkit: RichToolkit,
  305. ) -> None:
  306. with toolkit.progress("Sending your request...") as progress:
  307. with APIClient() as client:
  308. with handle_http_errors(progress):
  309. response = client.post(
  310. "/users/waiting-list", json=result.model_dump(mode="json")
  311. )
  312. response.raise_for_status()
  313. progress.log("Let's go! Thanks for your interest in FastAPI Cloud! 🚀")
  314. def _waitlist_form(toolkit: RichToolkit) -> None:
  315. from rich_toolkit.form import Form
  316. toolkit.print(
  317. "We're currently in private beta. If you want to be notified when we launch, please fill out the form below.",
  318. tag="waitlist",
  319. )
  320. toolkit.print_line()
  321. email = toolkit.input(
  322. "Enter your email:",
  323. required=True,
  324. validator=TypeAdapter(EmailStr),
  325. )
  326. toolkit.print_line()
  327. result = SignupToWaitingList(email=email)
  328. if toolkit.confirm(
  329. "Do you want to get access faster by giving us more information?",
  330. tag="waitlist",
  331. ):
  332. toolkit.print_line()
  333. form = Form("Waitlist form", style=toolkit.style)
  334. form.add_input("name", label="Name", placeholder="John Doe")
  335. form.add_input("organization", label="Organization", placeholder="Acme Inc.")
  336. form.add_input("team", label="Team", placeholder="Team A")
  337. form.add_input("role", label="Role", placeholder="Developer")
  338. form.add_input("location", label="Location", placeholder="San Francisco")
  339. form.add_input(
  340. "use_case",
  341. label="How do you plan to use FastAPI Cloud?",
  342. placeholder="I'm building a web app",
  343. )
  344. form.add_input("secret_code", label="Secret code", placeholder="123456")
  345. result = form.run() # type: ignore
  346. try:
  347. result = SignupToWaitingList.model_validate(
  348. {
  349. "email": email,
  350. **result, # type: ignore
  351. }
  352. )
  353. except ValidationError:
  354. toolkit.print(
  355. "[error]Invalid form data. Please try again.[/]",
  356. )
  357. return
  358. toolkit.print_line()
  359. if toolkit.confirm(
  360. (
  361. "Do you agree to\n"
  362. "- Terms of Service: [link=https://fastapicloud.com/legal/terms]https://fastapicloud.com/legal/terms[/link]\n"
  363. "- Privacy Policy: [link=https://fastapicloud.com/legal/privacy-policy]https://fastapicloud.com/legal/privacy-policy[/link]\n"
  364. ),
  365. tag="terms",
  366. ):
  367. toolkit.print_line()
  368. _send_waitlist_form(
  369. result,
  370. toolkit,
  371. )
  372. with contextlib.suppress(Exception):
  373. subprocess.run(
  374. ["open", "raycast://confetti"],
  375. stdout=subprocess.DEVNULL,
  376. stderr=subprocess.DEVNULL,
  377. check=False,
  378. )
  379. def deploy(
  380. path: Annotated[
  381. Union[Path, None],
  382. typer.Argument(
  383. help="A path to the folder containing the app you want to deploy"
  384. ),
  385. ] = None,
  386. skip_wait: Annotated[
  387. bool, typer.Option("--no-wait", help="Skip waiting for deployment status")
  388. ] = False,
  389. ) -> Any:
  390. """
  391. Deploy a [bold]FastAPI[/bold] app to FastAPI Cloud. 🚀
  392. """
  393. logger.debug("Deploy command started")
  394. logger.debug("Deploy path: %s, skip_wait: %s", path, skip_wait)
  395. with get_rich_toolkit() as toolkit:
  396. if not is_logged_in():
  397. logger.debug("User not logged in, prompting for login or waitlist")
  398. toolkit.print_title("Welcome to FastAPI Cloud!", tag="FastAPI")
  399. toolkit.print_line()
  400. toolkit.print(
  401. "You need to be logged in to deploy to FastAPI Cloud.",
  402. tag="info",
  403. )
  404. toolkit.print_line()
  405. choice = toolkit.ask(
  406. "What would you like to do?",
  407. tag="auth",
  408. options=[
  409. Option({"name": "Login to my existing account", "value": "login"}),
  410. Option({"name": "Join the waiting list", "value": "waitlist"}),
  411. ],
  412. )
  413. toolkit.print_line()
  414. if choice == "login":
  415. login()
  416. else:
  417. _waitlist_form(toolkit)
  418. raise typer.Exit(1)
  419. toolkit.print_title("Starting deployment", tag="FastAPI")
  420. toolkit.print_line()
  421. path_to_deploy = path or Path.cwd()
  422. logger.debug("Deploying from path: %s", path_to_deploy)
  423. app_config = get_app_config(path_to_deploy)
  424. if not app_config:
  425. logger.debug("No app config found, configuring new app")
  426. app_config = _configure_app(toolkit, path_to_deploy=path_to_deploy)
  427. toolkit.print_line()
  428. else:
  429. logger.debug("Existing app config found, proceeding with deployment")
  430. toolkit.print("Deploying app...")
  431. toolkit.print_line()
  432. with toolkit.progress("Checking app...", transient=True) as progress:
  433. with handle_http_errors(progress):
  434. logger.debug("Checking app with ID: %s", app_config.app_id)
  435. app = _get_app(app_config.app_id)
  436. if not app:
  437. logger.debug("App not found in API")
  438. progress.set_error(
  439. "App not found. Make sure you're logged in the correct account."
  440. )
  441. if not app:
  442. toolkit.print_line()
  443. toolkit.print(
  444. "If you deleted this app, you can run [bold]fastapi unlink[/] to unlink the local configuration.",
  445. tag="tip",
  446. )
  447. raise typer.Exit(1)
  448. logger.debug("Creating archive for deployment")
  449. archive_path = archive(path or Path.cwd()) # noqa: F841
  450. with toolkit.progress(title="Creating deployment") as progress:
  451. with handle_http_errors(progress):
  452. logger.debug("Creating deployment for app: %s", app.id)
  453. deployment = _create_deployment(app.id)
  454. progress.log(
  455. f"Deployment created successfully! Deployment slug: {deployment.slug}"
  456. )
  457. progress.log("Uploading deployment...")
  458. _upload_deployment(deployment.id, archive_path)
  459. progress.log("Deployment uploaded successfully!")
  460. toolkit.print_line()
  461. if not skip_wait:
  462. logger.debug("Waiting for deployment to complete")
  463. _wait_for_deployment(toolkit, app.id, deployment=deployment)
  464. else:
  465. logger.debug("Skipping deployment wait as requested")
  466. toolkit.print(
  467. f"Check the status of your deployment at [link={deployment.dashboard_url}]{deployment.dashboard_url}[/link]"
  468. )