hosting.py 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. """Hosting service related utilities."""
  2. from __future__ import annotations
  3. import contextlib
  4. import enum
  5. import json
  6. import os
  7. import re
  8. import time
  9. import uuid
  10. import webbrowser
  11. from datetime import datetime
  12. from http import HTTPStatus
  13. from typing import List, Optional
  14. import httpx
  15. import websockets
  16. from pydantic import Field, ValidationError, root_validator
  17. from reflex import constants
  18. from reflex.base import Base
  19. from reflex.config import get_config
  20. from reflex.utils import console
  21. config = get_config()
  22. # Endpoint to create or update a deployment
  23. POST_DEPLOYMENTS_ENDPOINT = f"{config.cp_backend_url}/deployments"
  24. # Endpoint to get all deployments for the user
  25. GET_DEPLOYMENTS_ENDPOINT = f"{config.cp_backend_url}/deployments"
  26. # Endpoint to fetch information from backend in preparation of a deployment
  27. POST_DEPLOYMENTS_PREPARE_ENDPOINT = f"{config.cp_backend_url}/deployments/prepare"
  28. # Endpoint to authenticate current user
  29. POST_VALIDATE_ME_ENDPOINT = f"{config.cp_backend_url}/authenticate/me"
  30. # Endpoint to fetch a login token after user completes authentication on web
  31. FETCH_TOKEN_ENDPOINT = f"{config.cp_backend_url}/authenticate"
  32. # Endpoint to delete a deployment
  33. DELETE_DEPLOYMENTS_ENDPOINT = f"{config.cp_backend_url}/deployments"
  34. # Endpoint to get deployment status
  35. GET_DEPLOYMENT_STATUS_ENDPOINT = f"{config.cp_backend_url}/deployments"
  36. # Public endpoint to get the list of supported regions in hosting service
  37. GET_REGIONS_ENDPOINT = f"{config.cp_backend_url}/deployments/regions"
  38. # Websocket endpoint to stream logs of a deployment
  39. DEPLOYMENT_LOGS_ENDPOINT = f'{config.cp_backend_url.replace("http", "ws")}/deployments'
  40. # Expected server response time to new deployment request. In seconds.
  41. DEPLOYMENT_PICKUP_DELAY = 30
  42. # End of deployment workflow message. Used to determine if it is the last message from server.
  43. END_OF_DEPLOYMENT_MESSAGES = ["deploy success", "deploy failed"]
  44. # How many iterations to try and print the deployment event messages from server during deployment.
  45. DEPLOYMENT_EVENT_MESSAGES_RETRIES = 90
  46. # Timeout limit for http requests
  47. HTTP_REQUEST_TIMEOUT = 60 # seconds
  48. def get_existing_access_token() -> tuple[str, str]:
  49. """Fetch the access token from the existing config if applicable.
  50. Returns:
  51. The access token and the invitation code.
  52. If either is not found, return empty string for it instead.
  53. """
  54. console.debug("Fetching token from existing config...")
  55. access_token = invitation_code = ""
  56. try:
  57. with open(constants.Hosting.HOSTING_JSON, "r") as config_file:
  58. hosting_config = json.load(config_file)
  59. access_token = hosting_config.get("access_token", "")
  60. invitation_code = hosting_config.get("code", "")
  61. except Exception as ex:
  62. console.debug(
  63. f"Unable to fetch token from {constants.Hosting.HOSTING_JSON} due to: {ex}"
  64. )
  65. return access_token, invitation_code
  66. def validate_token(token: str):
  67. """Validate the token with the control plane.
  68. Args:
  69. token: The access token to validate.
  70. Raises:
  71. ValueError: if access denied.
  72. Exception: if runs into timeout, failed requests, unexpected errors. These should be tried again.
  73. """
  74. try:
  75. response = httpx.post(
  76. POST_VALIDATE_ME_ENDPOINT,
  77. headers=authorization_header(token),
  78. timeout=HTTP_REQUEST_TIMEOUT,
  79. )
  80. if response.status_code == HTTPStatus.FORBIDDEN:
  81. raise ValueError
  82. response.raise_for_status()
  83. except httpx.RequestError as re:
  84. console.debug(f"Request to auth server failed due to {re}")
  85. raise Exception("request error") from re
  86. except httpx.HTTPError as ex:
  87. console.debug(f"Unable to validate the token due to: {ex}")
  88. raise Exception("server error") from ex
  89. except ValueError as ve:
  90. console.debug(f"Access denied for {token}")
  91. raise ValueError("access denied") from ve
  92. except Exception as ex:
  93. console.debug(f"Unexpected error: {ex}")
  94. raise Exception("internal errors") from ex
  95. def delete_token_from_config(include_invitation_code: bool = False):
  96. """Delete the invalid token from the config file if applicable.
  97. Args:
  98. include_invitation_code:
  99. Whether to delete the invitation code as well.
  100. When user logs out, we delete the invitation code together.
  101. """
  102. if os.path.exists(constants.Hosting.HOSTING_JSON):
  103. hosting_config = {}
  104. try:
  105. with open(constants.Hosting.HOSTING_JSON, "w") as config_file:
  106. hosting_config = json.load(config_file)
  107. del hosting_config["access_token"]
  108. if include_invitation_code:
  109. del hosting_config["code"]
  110. json.dump(hosting_config, config_file)
  111. except Exception as ex:
  112. # Best efforts removing invalid token is OK
  113. console.debug(
  114. f"Unable to delete the invalid token from config file, err: {ex}"
  115. )
  116. def save_token_to_config(token: str, code: str | None = None):
  117. """Best efforts cache the token, and optionally invitation code to the config file.
  118. Args:
  119. token: The access token to save.
  120. code: The invitation code to save if exists.
  121. """
  122. hosting_config: dict[str, str] = {"access_token": token}
  123. if code:
  124. hosting_config["code"] = code
  125. try:
  126. with open(constants.Hosting.HOSTING_JSON, "w") as config_file:
  127. json.dump(hosting_config, config_file)
  128. except Exception as ex:
  129. console.warn(
  130. f"Unable to save token to {constants.Hosting.HOSTING_JSON} due to: {ex}"
  131. )
  132. def authenticated_token() -> tuple[str, str]:
  133. """Fetch the access token from the existing config if applicable and validate it.
  134. Returns:
  135. The access token and the invitation code.
  136. If either is not found, return empty string for it instead.
  137. """
  138. # Check if the user is authenticated
  139. access_token, invitation_code = get_existing_access_token()
  140. if not access_token:
  141. console.debug("No access token found from the existing config.")
  142. access_token = ""
  143. elif not validate_token_with_retries(access_token):
  144. access_token = ""
  145. return access_token, invitation_code
  146. def authorization_header(token: str) -> dict[str, str]:
  147. """Construct an authorization header with the specified token as bearer token.
  148. Args:
  149. token: The access token to use.
  150. Returns:
  151. The authorization header in dict format.
  152. """
  153. return {"Authorization": f"Bearer {token}"}
  154. def requires_authenticated() -> str:
  155. """Check if the user is authenticated.
  156. Returns:
  157. The validated access token or empty string if not authenticated.
  158. """
  159. access_token, invitation_code = authenticated_token()
  160. if access_token:
  161. return access_token
  162. return authenticate_on_browser(invitation_code)
  163. class DeploymentPrepInfo(Base):
  164. """The params/settings returned from the prepare endpoint
  165. including the deployment key and the frontend/backend URLs once deployed.
  166. The key becomes part of both frontend and backend URLs.
  167. """
  168. # The deployment key
  169. key: str
  170. # The backend URL
  171. api_url: str
  172. # The frontend URL
  173. deploy_url: str
  174. class DeploymentPrepareResponse(Base):
  175. """The params/settings returned from the prepare endpoint,
  176. used in the CLI for the subsequent launch request.
  177. """
  178. # The app prefix, used on the server side only
  179. app_prefix: str
  180. # The reply from the server for a prepare request to deploy over a particular key
  181. # If reply is not None, it means server confirms the key is available for use.
  182. reply: Optional[DeploymentPrepInfo] = None
  183. # The list of existing deployments by the user under the same app name.
  184. # This is used to allow easy upgrade case when user attempts to deploy
  185. # in the same named app directory, user intends to upgrade the existing deployment.
  186. existing: Optional[List[DeploymentPrepInfo]] = None
  187. # The suggested key name based on the app name.
  188. # This is for a new deployment, user has not deployed this app before.
  189. # The server returns key suggestion based on the app name.
  190. suggestion: Optional[DeploymentPrepInfo] = None
  191. enabled_regions: Optional[List[str]] = None
  192. @root_validator(pre=True)
  193. def ensure_at_least_one_deploy_params(cls, values):
  194. """Ensure at least one set of param is returned for any of the cases we try to prepare.
  195. Args:
  196. values: The values passed in.
  197. Raises:
  198. ValueError: If all of the optional fields are None.
  199. Returns:
  200. The values passed in.
  201. """
  202. if (
  203. values.get("reply") is None
  204. and not values.get("existing") # existing cannot be an empty list either
  205. and values.get("suggestion") is None
  206. ):
  207. raise ValueError(
  208. "At least one set of params for deploy is required from control plane."
  209. )
  210. return values
  211. class DeploymentsPreparePostParam(Base):
  212. """Params for app API URL creation backend API."""
  213. # The app name which is found in the config
  214. app_name: str
  215. # The deployment key
  216. key: Optional[str] = None # name of the deployment
  217. # The frontend hostname to deploy to. This is used to deploy at hostname not in the regular domain.
  218. frontend_hostname: Optional[str] = None
  219. def prepare_deploy(
  220. app_name: str,
  221. key: str | None = None,
  222. frontend_hostname: str | None = None,
  223. ) -> DeploymentPrepareResponse:
  224. """Send a POST request to Control Plane to prepare a new deployment.
  225. Control Plane checks if there is conflict with the key if provided.
  226. If the key is absent, it will return existing deployments and a suggested name based on the app_name in the request.
  227. Args:
  228. key: The deployment name.
  229. app_name: The app name.
  230. frontend_hostname: The frontend hostname to deploy to. This is used to deploy at hostname not in the regular domain.
  231. Raises:
  232. Exception: If the operation fails. The exception message is the reason.
  233. Returns:
  234. The response containing the backend URLs if successful, None otherwise.
  235. """
  236. # Check if the user is authenticated
  237. if not (token := requires_authenticated()):
  238. raise Exception("not authenticated")
  239. try:
  240. response = httpx.post(
  241. POST_DEPLOYMENTS_PREPARE_ENDPOINT,
  242. headers=authorization_header(token),
  243. json=DeploymentsPreparePostParam(
  244. app_name=app_name, key=key, frontend_hostname=frontend_hostname
  245. ).dict(exclude_none=True),
  246. timeout=HTTP_REQUEST_TIMEOUT,
  247. )
  248. response_json = response.json()
  249. console.debug(f"Response from prepare endpoint: {response_json}")
  250. if response.status_code == HTTPStatus.FORBIDDEN:
  251. console.debug(f'Server responded with 403: {response_json.get("detail")}')
  252. raise ValueError(f'{response_json.get("detail", "forbidden")}')
  253. response.raise_for_status()
  254. return DeploymentPrepareResponse(
  255. app_prefix=response_json["app_prefix"],
  256. reply=response_json["reply"],
  257. suggestion=response_json["suggestion"],
  258. existing=response_json["existing"],
  259. enabled_regions=response_json.get("enabled_regions"),
  260. )
  261. except httpx.RequestError as re:
  262. console.debug(f"Unable to prepare launch due to {re}.")
  263. raise Exception("request error") from re
  264. except httpx.HTTPError as he:
  265. console.debug(f"Unable to prepare deploy due to {he}.")
  266. raise Exception(f"{he}") from he
  267. except json.JSONDecodeError as jde:
  268. console.debug(f"Server did not respond with valid json: {jde}")
  269. raise Exception("internal errors") from jde
  270. except (KeyError, ValidationError) as kve:
  271. console.debug(f"The server response format is unexpected {kve}")
  272. raise Exception("internal errors") from kve
  273. except ValueError as ve:
  274. # This is a recognized client error, currently indicates forbidden
  275. raise Exception(f"{ve}") from ve
  276. except Exception as ex:
  277. console.debug(f"Unexpected error: {ex}.")
  278. raise Exception("internal errors") from ex
  279. class DeploymentPostResponse(Base):
  280. """The URL for the deployed site."""
  281. # The frontend URL
  282. frontend_url: str = Field(..., regex=r"^https?://", min_length=8)
  283. # The backend URL
  284. backend_url: str = Field(..., regex=r"^https?://", min_length=8)
  285. class DeploymentsPostParam(Base):
  286. """Params for hosted instance deployment POST request."""
  287. # Key is the name of the deployment, it becomes part of the URL
  288. key: str = Field(..., regex=r"^[a-zA-Z0-9-]+$")
  289. # Name of the app
  290. app_name: str = Field(..., min_length=1)
  291. # json encoded list of regions to deploy to
  292. regions_json: str = Field(..., min_length=1)
  293. # The app prefix, used on the server side only
  294. app_prefix: str = Field(..., min_length=1)
  295. # The version of reflex CLI used to deploy
  296. reflex_version: str = Field(..., min_length=1)
  297. # The number of CPUs
  298. cpus: Optional[int] = None
  299. # The memory in MB
  300. memory_mb: Optional[int] = None
  301. # Whether to auto start the hosted deployment
  302. auto_start: Optional[bool] = None
  303. # Whether to auto stop the hosted deployment when idling
  304. auto_stop: Optional[bool] = None
  305. # The frontend hostname to deploy to. This is used to deploy at hostname not in the regular domain.
  306. frontend_hostname: Optional[str] = None
  307. # The description of the deployment
  308. description: Optional[str] = None
  309. # The json encoded list of environment variables
  310. envs_json: Optional[str] = None
  311. # The command line prefix for tracing
  312. reflex_cli_entrypoint: Optional[str] = None
  313. # The metrics endpoint
  314. metrics_endpoint: Optional[str] = None
  315. def deploy(
  316. frontend_file_name: str,
  317. backend_file_name: str,
  318. export_dir: str,
  319. key: str,
  320. app_name: str,
  321. regions: list[str],
  322. app_prefix: str,
  323. vm_type: str | None = None,
  324. cpus: int | None = None,
  325. memory_mb: int | None = None,
  326. auto_start: bool | None = None,
  327. auto_stop: bool | None = None,
  328. frontend_hostname: str | None = None,
  329. envs: dict[str, str] | None = None,
  330. with_tracing: str | None = None,
  331. with_metrics: str | None = None,
  332. ) -> DeploymentPostResponse:
  333. """Send a POST request to Control Plane to launch a new deployment.
  334. Args:
  335. frontend_file_name: The frontend file name.
  336. backend_file_name: The backend file name.
  337. export_dir: The directory where the frontend/backend zip files are exported.
  338. key: The deployment name.
  339. app_name: The app name.
  340. regions: The list of regions to deploy to.
  341. app_prefix: The app prefix.
  342. vm_type: The VM type.
  343. cpus: The number of CPUs.
  344. memory_mb: The memory in MB.
  345. auto_start: Whether to auto start.
  346. auto_stop: Whether to auto stop.
  347. frontend_hostname: The frontend hostname to deploy to. This is used to deploy at hostname not in the regular domain.
  348. envs: The environment variables.
  349. with_tracing: A string indicating the command line prefix for tracing.
  350. with_metrics: A string indicating the metrics endpoint.
  351. Raises:
  352. AssertionError: If the request is rejected by the hosting server.
  353. Exception: If the operation fails. The exception message is the reason.
  354. Returns:
  355. The response containing the URL of the site to be deployed if successful, None otherwise.
  356. """
  357. # Check if the user is authenticated
  358. if not (token := requires_authenticated()):
  359. raise Exception("not authenticated")
  360. try:
  361. params = DeploymentsPostParam(
  362. key=key,
  363. app_name=app_name,
  364. regions_json=json.dumps(regions),
  365. app_prefix=app_prefix,
  366. cpus=cpus,
  367. memory_mb=memory_mb,
  368. auto_start=auto_start,
  369. auto_stop=auto_stop,
  370. envs_json=json.dumps(envs) if envs else None,
  371. frontend_hostname=frontend_hostname,
  372. reflex_version=constants.Reflex.VERSION,
  373. reflex_cli_entrypoint=with_tracing,
  374. metrics_endpoint=with_metrics,
  375. )
  376. with open(
  377. os.path.join(export_dir, frontend_file_name), "rb"
  378. ) as frontend_file, open(
  379. os.path.join(export_dir, backend_file_name), "rb"
  380. ) as backend_file:
  381. # https://docs.python-requests.org/en/latest/user/advanced/#post-multiple-multipart-encoded-files
  382. files = [
  383. ("files", (frontend_file_name, frontend_file)),
  384. ("files", (backend_file_name, backend_file)),
  385. ]
  386. response = httpx.post(
  387. POST_DEPLOYMENTS_ENDPOINT,
  388. headers=authorization_header(token),
  389. data=params.dict(exclude_none=True),
  390. files=files,
  391. )
  392. # If the server explicitly states bad request,
  393. # display a different error
  394. if response.status_code == HTTPStatus.BAD_REQUEST:
  395. raise AssertionError(f"Server rejected this request: {response.text}")
  396. response.raise_for_status()
  397. response_json = response.json()
  398. return DeploymentPostResponse(
  399. frontend_url=response_json["frontend_url"],
  400. backend_url=response_json["backend_url"],
  401. )
  402. except OSError as oe:
  403. console.debug(f"Client side error related to file operation: {oe}")
  404. raise
  405. except httpx.RequestError as re:
  406. console.debug(f"Unable to deploy due to request error: {re}")
  407. raise Exception("request error") from re
  408. except httpx.HTTPError as he:
  409. console.debug(f"Unable to deploy due to {he}.")
  410. raise Exception("internal errors") from he
  411. except json.JSONDecodeError as jde:
  412. console.debug(f"Server did not respond with valid json: {jde}")
  413. raise Exception("internal errors") from jde
  414. except (KeyError, ValidationError) as kve:
  415. console.debug(f"Post params or server response format unexpected: {kve}")
  416. raise Exception("internal errors") from kve
  417. except AssertionError as ve:
  418. console.debug(f"Unable to deploy due to request error: {ve}")
  419. # re-raise the error back to the user as client side error
  420. raise
  421. except Exception as ex:
  422. console.debug(f"Unable to deploy due to internal errors: {ex}.")
  423. raise Exception("internal errors") from ex
  424. class DeploymentsGetParam(Base):
  425. """Params for hosted instance GET request."""
  426. # The app name which is found in the config
  427. app_name: Optional[str]
  428. class DeploymentGetResponse(Base):
  429. """The params/settings returned from the GET endpoint."""
  430. # The deployment key
  431. key: str
  432. # The list of regions to deploy to
  433. regions: List[str]
  434. # The app name which is found in the config
  435. app_name: str
  436. # The VM type
  437. vm_type: str
  438. # The number of CPUs
  439. cpus: int
  440. # The memory in MB
  441. memory_mb: int
  442. # The site URL
  443. url: str
  444. # The list of environment variable names (values are never shown)
  445. envs: List[str]
  446. def list_deployments(
  447. app_name: str | None = None,
  448. ) -> list[dict]:
  449. """Send a GET request to Control Plane to list deployments.
  450. Args:
  451. app_name: the app name as an optional filter when listing deployments.
  452. Raises:
  453. Exception: If the operation fails. The exception message shows the reason.
  454. Returns:
  455. The list of deployments if successful, None otherwise.
  456. """
  457. if not (token := requires_authenticated()):
  458. raise Exception("not authenticated")
  459. params = DeploymentsGetParam(app_name=app_name)
  460. try:
  461. response = httpx.get(
  462. GET_DEPLOYMENTS_ENDPOINT,
  463. headers=authorization_header(token),
  464. params=params.dict(exclude_none=True),
  465. timeout=HTTP_REQUEST_TIMEOUT,
  466. )
  467. response.raise_for_status()
  468. return [
  469. DeploymentGetResponse(
  470. key=deployment["key"],
  471. regions=deployment["regions"],
  472. app_name=deployment["app_name"],
  473. vm_type=deployment["vm_type"],
  474. cpus=deployment["cpus"],
  475. memory_mb=deployment["memory_mb"],
  476. url=deployment["url"],
  477. envs=deployment["envs"],
  478. ).dict()
  479. for deployment in response.json()
  480. ]
  481. except httpx.RequestError as re:
  482. console.debug(f"Unable to list deployments due to request error: {re}")
  483. raise Exception("request timeout") from re
  484. except httpx.HTTPError as he:
  485. console.debug(f"Unable to list deployments due to {he}.")
  486. raise Exception("internal errors") from he
  487. except (ValidationError, KeyError, json.JSONDecodeError) as vkje:
  488. console.debug(f"Server response format unexpected: {vkje}")
  489. raise Exception("internal errors") from vkje
  490. except Exception as ex:
  491. console.error(f"Unexpected error: {ex}.")
  492. raise Exception("internal errors") from ex
  493. def fetch_token(request_id: str) -> tuple[str, str]:
  494. """Fetch the access token for the request_id from Control Plane.
  495. Args:
  496. request_id: The request ID used when the user opens the browser for authentication.
  497. Returns:
  498. The access token if it exists, None otherwise.
  499. """
  500. access_token = invitation_code = ""
  501. try:
  502. resp = httpx.get(
  503. f"{FETCH_TOKEN_ENDPOINT}/{request_id}",
  504. timeout=HTTP_REQUEST_TIMEOUT,
  505. )
  506. resp.raise_for_status()
  507. access_token = (resp_json := resp.json()).get("access_token", "")
  508. invitation_code = resp_json.get("code", "")
  509. except httpx.RequestError as re:
  510. console.debug(f"Unable to fetch token due to request error: {re}")
  511. except httpx.HTTPError as he:
  512. console.debug(f"Unable to fetch token due to {he}")
  513. except json.JSONDecodeError as jde:
  514. console.debug(f"Server did not respond with valid json: {jde}")
  515. except KeyError as ke:
  516. console.debug(f"Server response format unexpected: {ke}")
  517. except Exception:
  518. console.debug("Unexpected errors: {ex}")
  519. return access_token, invitation_code
  520. def poll_backend(backend_url: str) -> bool:
  521. """Poll the backend to check if it is up.
  522. Args:
  523. backend_url: The URL of the backend to poll.
  524. Returns:
  525. True if the backend is up, False otherwise.
  526. """
  527. try:
  528. console.debug(f"Polling backend at {backend_url}")
  529. resp = httpx.get(f"{backend_url}/ping", timeout=HTTP_REQUEST_TIMEOUT)
  530. resp.raise_for_status()
  531. return True
  532. except httpx.HTTPError:
  533. return False
  534. def poll_frontend(frontend_url: str) -> bool:
  535. """Poll the frontend to check if it is up.
  536. Args:
  537. frontend_url: The URL of the frontend to poll.
  538. Returns:
  539. True if the frontend is up, False otherwise.
  540. """
  541. try:
  542. console.debug(f"Polling frontend at {frontend_url}")
  543. resp = httpx.get(f"{frontend_url}", timeout=HTTP_REQUEST_TIMEOUT)
  544. resp.raise_for_status()
  545. return True
  546. except httpx.HTTPError:
  547. return False
  548. class DeploymentDeleteParam(Base):
  549. """Params for hosted instance DELETE request."""
  550. # key is the name of the deployment, it becomes part of the site URL
  551. key: str
  552. def delete_deployment(key: str):
  553. """Send a DELETE request to Control Plane to delete a deployment.
  554. Args:
  555. key: The deployment name.
  556. Raises:
  557. ValueError: If the key is not provided.
  558. Exception: If the operation fails. The exception message is the reason.
  559. """
  560. if not (token := requires_authenticated()):
  561. raise Exception("not authenticated")
  562. if not key:
  563. raise ValueError("Valid key is required for the delete.")
  564. try:
  565. response = httpx.delete(
  566. f"{DELETE_DEPLOYMENTS_ENDPOINT}/{key}",
  567. headers=authorization_header(token),
  568. timeout=HTTP_REQUEST_TIMEOUT,
  569. )
  570. response.raise_for_status()
  571. except httpx.TimeoutException as te:
  572. console.debug("Unable to delete deployment due to request timeout.")
  573. raise Exception("request timeout") from te
  574. except httpx.HTTPError as he:
  575. console.debug(f"Unable to delete deployment due to {he}.")
  576. raise Exception("internal errors") from he
  577. except Exception as ex:
  578. console.debug(f"Unexpected errors {ex}.")
  579. raise Exception("internal errors") from ex
  580. class SiteStatus(Base):
  581. """Deployment status info."""
  582. # The frontend URL
  583. frontend_url: Optional[str] = None
  584. # The backend URL
  585. backend_url: Optional[str] = None
  586. # Whether the frontend/backend URL is reachable
  587. reachable: bool
  588. # The last updated iso formatted timestamp if site is reachable
  589. updated_at: Optional[str] = None
  590. @root_validator(pre=True)
  591. def ensure_one_of_urls(cls, values):
  592. """Ensure at least one of the frontend/backend URLs is provided.
  593. Args:
  594. values: The values passed in.
  595. Raises:
  596. ValueError: If none of the URLs is provided.
  597. Returns:
  598. The values passed in.
  599. """
  600. if values.get("frontend_url") is None and values.get("backend_url") is None:
  601. raise ValueError("At least one of the URLs is required.")
  602. return values
  603. class DeploymentStatusResponse(Base):
  604. """Response for deployment status request."""
  605. # The frontend status
  606. frontend: SiteStatus
  607. # The backend status
  608. backend: SiteStatus
  609. def get_deployment_status(key: str) -> DeploymentStatusResponse:
  610. """Get the deployment status.
  611. Args:
  612. key: The deployment name.
  613. Raises:
  614. ValueError: If the key is not provided.
  615. Exception: If the operation fails. The exception message is the reason.
  616. Returns:
  617. The deployment status response including backend and frontend.
  618. """
  619. if not key:
  620. raise ValueError(
  621. "A non empty key is required for querying the deployment status."
  622. )
  623. if not (token := requires_authenticated()):
  624. raise Exception("not authenticated")
  625. try:
  626. response = httpx.get(
  627. f"{GET_DEPLOYMENT_STATUS_ENDPOINT}/{key}/status",
  628. headers=authorization_header(token),
  629. timeout=HTTP_REQUEST_TIMEOUT,
  630. )
  631. response.raise_for_status()
  632. response_json = response.json()
  633. return DeploymentStatusResponse(
  634. frontend=SiteStatus(
  635. frontend_url=response_json["frontend"]["url"],
  636. reachable=response_json["frontend"]["reachable"],
  637. updated_at=response_json["frontend"]["updated_at"],
  638. ),
  639. backend=SiteStatus(
  640. backend_url=response_json["backend"]["url"],
  641. reachable=response_json["backend"]["reachable"],
  642. updated_at=response_json["backend"]["updated_at"],
  643. ),
  644. )
  645. except Exception as ex:
  646. console.debug(f"Unable to get deployment status due to {ex}.")
  647. raise Exception("internal errors") from ex
  648. def convert_to_local_time(iso_timestamp: str) -> str:
  649. """Convert the iso timestamp to local time.
  650. Args:
  651. iso_timestamp: The iso timestamp to convert.
  652. Returns:
  653. The converted timestamp string.
  654. """
  655. try:
  656. local_dt = datetime.fromisoformat(iso_timestamp).astimezone()
  657. return local_dt.strftime("%Y-%m-%d %H:%M:%S.%f %Z")
  658. except Exception as ex:
  659. console.debug(f"Unable to convert iso timestamp {iso_timestamp} due to {ex}.")
  660. return iso_timestamp
  661. class LogType(str, enum.Enum):
  662. """Enum for log types."""
  663. # Logs printed from the user code, the "app"
  664. APP_LOG = "app"
  665. # Build logs are the server messages while building/running user deployment
  666. BUILD_LOG = "build"
  667. # Deploy logs are specifically for the messages at deploy time
  668. # returned to the user the current stage of the deployment, such as building, uploading.
  669. DEPLOY_LOG = "deploy"
  670. # All the logs which can be printed by all above types.
  671. ALL_LOG = "all"
  672. async def get_logs(
  673. key: str,
  674. log_type: LogType = LogType.APP_LOG,
  675. from_iso_timestamp: datetime | None = None,
  676. ):
  677. """Get the deployment logs and stream on console.
  678. Args:
  679. key: The deployment name.
  680. log_type: The type of logs to query from server.
  681. See the LogType definitions for how they are used.
  682. from_iso_timestamp: An optional timestamp with timezone info to limit
  683. where the log queries should start from.
  684. Raises:
  685. ValueError: If the key is not provided.
  686. Exception: If the operation fails. The exception message is the reason.
  687. """
  688. if not (token := requires_authenticated()):
  689. raise Exception("not authenticated")
  690. if not key:
  691. raise ValueError("Valid key is required for querying logs.")
  692. try:
  693. logs_endpoint = f"{DEPLOYMENT_LOGS_ENDPOINT}/{key}/logs?access_token={token}&log_type={log_type.value}"
  694. console.debug(f"log server endpoint: {logs_endpoint}")
  695. if from_iso_timestamp is not None:
  696. logs_endpoint += (
  697. f"&from_iso_timestamp={from_iso_timestamp.astimezone().isoformat()}"
  698. )
  699. _ws = websockets.connect(logs_endpoint) # type: ignore
  700. async with _ws as ws:
  701. while True:
  702. row_json = json.loads(await ws.recv())
  703. console.debug(f"Server responded with logs: {row_json}")
  704. if row_json and isinstance(row_json, dict):
  705. row_to_print = {}
  706. for k, v in row_json.items():
  707. if v is None:
  708. row_to_print[k] = str(v)
  709. elif k == "timestamp":
  710. row_to_print[k] = convert_to_local_time(v)
  711. else:
  712. row_to_print[k] = v
  713. print(" | ".join(row_to_print.values()))
  714. else:
  715. console.debug("Server responded, no new logs, this is normal")
  716. except Exception as ex:
  717. console.debug(f"Unable to get more deployment logs due to {ex}.")
  718. console.print("Log server disconnected ...")
  719. console.print(
  720. "Note that the server has limit to only stream logs for several minutes"
  721. )
  722. def check_requirements_txt_exist() -> bool:
  723. """Check if requirements.txt exists in the top level app directory.
  724. Returns:
  725. True if requirements.txt exists, False otherwise.
  726. """
  727. return os.path.exists(constants.RequirementsTxt.FILE)
  728. def check_requirements_for_non_reflex_packages() -> bool:
  729. """Check the requirements.txt file for packages other than reflex.
  730. Returns:
  731. True if packages other than reflex are found, False otherwise.
  732. """
  733. if not check_requirements_txt_exist():
  734. return False
  735. try:
  736. with open(constants.RequirementsTxt.FILE) as fp:
  737. for req_line in fp.readlines():
  738. package_name = re.search(r"^([^=<>!~]+)", req_line.lstrip())
  739. # If we find a package that is not reflex
  740. if (
  741. package_name
  742. and package_name.group(1) != constants.Reflex.MODULE_NAME
  743. ):
  744. return True
  745. except Exception as ex:
  746. console.warn(f"Unable to scan requirements.txt for dependencies due to {ex}")
  747. return False
  748. def authenticate_on_browser(
  749. invitation_code: str,
  750. ) -> str:
  751. """Open the browser to authenticate the user.
  752. Args:
  753. invitation_code: The invitation code if it exists.
  754. Returns:
  755. The access token if valid, empty otherwise.
  756. """
  757. console.print(f"Opening {config.cp_web_url} ...")
  758. request_id = uuid.uuid4().hex
  759. auth_url = f"{config.cp_web_url}?request-id={request_id}&code={invitation_code}"
  760. if not webbrowser.open(auth_url):
  761. console.warn(
  762. f"Unable to automatically open the browser. Please go to {auth_url} to authenticate."
  763. )
  764. access_token = invitation_code = ""
  765. with console.status("Waiting for access token ..."):
  766. for _ in range(constants.Hosting.WEB_AUTH_RETRIES):
  767. access_token, invitation_code = fetch_token(request_id)
  768. if access_token:
  769. break
  770. else:
  771. time.sleep(constants.Hosting.WEB_AUTH_SLEEP_DURATION)
  772. if access_token and validate_token_with_retries(access_token):
  773. save_token_to_config(access_token, invitation_code)
  774. else:
  775. access_token = ""
  776. return access_token
  777. def validate_token_with_retries(access_token: str) -> bool:
  778. """Validate the access token with retries.
  779. Args:
  780. access_token: The access token to validate.
  781. Returns:
  782. True if the token is valid,
  783. False if invalid or unable to validate.
  784. """
  785. with console.status("Validating access token ..."):
  786. for _ in range(constants.Hosting.WEB_AUTH_RETRIES):
  787. try:
  788. validate_token(access_token)
  789. return True
  790. except ValueError:
  791. console.error(f"Access denied")
  792. delete_token_from_config()
  793. break
  794. except Exception as ex:
  795. console.debug(f"Unable to validate token due to: {ex}, trying again")
  796. time.sleep(constants.Hosting.WEB_AUTH_SLEEP_DURATION)
  797. return False
  798. def interactive_get_deployment_key_from_user_input(
  799. pre_deploy_response: DeploymentPrepareResponse,
  800. app_name: str,
  801. frontend_hostname: str | None = None,
  802. ) -> tuple[str, str, str]:
  803. """Interactive get the deployment key from user input.
  804. Args:
  805. pre_deploy_response: The response from the initial prepare call to server.
  806. app_name: The app name.
  807. frontend_hostname: The frontend hostname to deploy to. This is used to deploy at hostname not in the regular domain.
  808. Returns:
  809. The deployment key, backend URL, frontend URL.
  810. """
  811. key_candidate = api_url = deploy_url = ""
  812. if reply := pre_deploy_response.reply:
  813. api_url = reply.api_url
  814. deploy_url = reply.deploy_url
  815. key_candidate = reply.key
  816. elif pre_deploy_response.existing:
  817. # validator already checks existing field is not empty list
  818. # Note: keeping this simple as we only allow one deployment per app
  819. existing = pre_deploy_response.existing[0]
  820. console.print(f"Overwrite deployment [ {existing.key} ] ...")
  821. key_candidate = existing.key
  822. api_url = existing.api_url
  823. deploy_url = existing.deploy_url
  824. elif suggestion := pre_deploy_response.suggestion:
  825. key_candidate = suggestion.key
  826. api_url = suggestion.api_url
  827. deploy_url = suggestion.deploy_url
  828. # If user takes the suggestion, we will use the suggested key and proceed
  829. while key_input := console.ask(
  830. f"Choose a name for your deployed app. Enter to use default.",
  831. default=key_candidate,
  832. ):
  833. try:
  834. pre_deploy_response = prepare_deploy(
  835. app_name,
  836. key=key_input,
  837. frontend_hostname=frontend_hostname,
  838. )
  839. if (
  840. pre_deploy_response.reply is None
  841. or key_input != pre_deploy_response.reply.key
  842. ):
  843. # Rejected by server, try again
  844. continue
  845. key_candidate = pre_deploy_response.reply.key
  846. api_url = pre_deploy_response.reply.api_url
  847. deploy_url = pre_deploy_response.reply.deploy_url
  848. # we get the confirmation, so break from the loop
  849. break
  850. except Exception:
  851. console.error(
  852. "Cannot deploy at this name, try picking a different name"
  853. )
  854. return key_candidate, api_url, deploy_url
  855. def process_envs(envs: list[str]) -> dict[str, str]:
  856. """Process the environment variables.
  857. Args:
  858. envs: The environment variables expected in key=value format.
  859. Raises:
  860. SystemExit: If the envs are not in valid format.
  861. Returns:
  862. The processed environment variables in a dict.
  863. """
  864. processed_envs = {}
  865. for env in envs:
  866. kv = env.split("=", maxsplit=1)
  867. if len(kv) != 2:
  868. raise SystemExit("Invalid env format: should be <key>=<value>.")
  869. if not re.match(r"^[a-zA-Z_][a-zA-Z0-9_]*$", kv[0]):
  870. raise SystemExit(
  871. "Invalid env name: should start with a letter or underscore, followed by letters, digits, or underscores."
  872. )
  873. processed_envs[kv[0]] = kv[1]
  874. return processed_envs
  875. def log_out_on_browser():
  876. """Open the browser to authenticate the user."""
  877. # Fetching existing invitation code so user sees the log out page without having to enter it
  878. invitation_code = None
  879. with contextlib.suppress(Exception):
  880. _, invitation_code = get_existing_access_token()
  881. console.debug("Found existing invitation code in config")
  882. delete_token_from_config()
  883. console.print(f"Opening {config.cp_web_url} ...")
  884. if not webbrowser.open(f"{config.cp_web_url}?code={invitation_code}"):
  885. console.warn(
  886. f"Unable to open the browser automatically. Please go to {config.cp_web_url} to log out."
  887. )
  888. async def display_deploy_milestones(key: str, from_iso_timestamp: datetime):
  889. """Display the deploy milestone messages reported back from the hosting server.
  890. Args:
  891. key: The deployment key.
  892. from_iso_timestamp: The timestamp of the deployment request time, this helps with the milestone query.
  893. Raises:
  894. ValueError: If a non-empty key is not provided.
  895. Exception: If the user is not authenticated.
  896. """
  897. if not key:
  898. raise ValueError("Non-empty key is required for querying deploy status.")
  899. if not (token := requires_authenticated()):
  900. raise Exception("not authenticated")
  901. try:
  902. logs_endpoint = f"{DEPLOYMENT_LOGS_ENDPOINT}/{key}/logs?access_token={token}&log_type={LogType.DEPLOY_LOG.value}&from_iso_timestamp={from_iso_timestamp.astimezone().isoformat()}"
  903. console.debug(f"log server endpoint: {logs_endpoint}")
  904. _ws = websockets.connect(logs_endpoint) # type: ignore
  905. async with _ws as ws:
  906. # Stream back the deploy events reported back from the server
  907. for _ in range(DEPLOYMENT_EVENT_MESSAGES_RETRIES):
  908. row_json = json.loads(await ws.recv())
  909. console.debug(f"Server responded with: {row_json}")
  910. if row_json and isinstance(row_json, dict):
  911. # Only show the timestamp and actual message
  912. console.print(
  913. " | ".join(
  914. [
  915. convert_to_local_time(row_json["timestamp"]),
  916. row_json["message"],
  917. ]
  918. )
  919. )
  920. if any(
  921. msg in row_json["message"].lower()
  922. for msg in END_OF_DEPLOYMENT_MESSAGES
  923. ):
  924. console.debug(
  925. "Received end of deployment message, stop event message streaming"
  926. )
  927. return
  928. else:
  929. console.debug("Server responded, no new events yet, this is normal")
  930. except Exception as ex:
  931. console.debug(f"Unable to get more deployment events due to {ex}.")
  932. def wait_for_server_to_pick_up_request():
  933. """Wait for server to pick up the request. Right now is just sleep."""
  934. with console.status(
  935. f"Waiting for server to pick up request ~ {DEPLOYMENT_PICKUP_DELAY} seconds ..."
  936. ):
  937. for _ in range(DEPLOYMENT_PICKUP_DELAY):
  938. time.sleep(1)
  939. def interactive_prompt_for_envs() -> list[str]:
  940. """Interactive prompt for environment variables.
  941. Returns:
  942. The list of environment variables in key=value string format.
  943. """
  944. envs = []
  945. envs_finished = False
  946. env_count = 1
  947. env_key_prompt = f" * env-{env_count} name (enter to skip)"
  948. console.print("Environment variables for your production App ...")
  949. while not envs_finished:
  950. env_key = console.ask(env_key_prompt)
  951. if not env_key:
  952. envs_finished = True
  953. if envs:
  954. console.print("Finished adding envs.")
  955. else:
  956. console.print("No envs added. Continuing ...")
  957. break
  958. # If it possible to have empty values for env, so we do not check here
  959. env_value = console.ask(f" env-{env_count} value")
  960. envs.append(f"{env_key}={env_value}")
  961. env_count += 1
  962. env_key_prompt = f" * env-{env_count} name (enter to skip)"
  963. return envs
  964. def get_regions() -> list[dict]:
  965. """Get the supported regions from the hosting server.
  966. Returns:
  967. A list of dict representation of the region information.
  968. """
  969. try:
  970. response = httpx.get(
  971. GET_REGIONS_ENDPOINT,
  972. timeout=HTTP_REQUEST_TIMEOUT,
  973. )
  974. response.raise_for_status()
  975. response_json = response.json()
  976. if response_json is None or not isinstance(response_json, list):
  977. console.debug("Expect server to return a list ")
  978. return []
  979. if (
  980. response_json
  981. and response_json[0] is not None
  982. and not isinstance(response_json[0], dict)
  983. ):
  984. console.debug("Expect return values are dict's")
  985. return []
  986. return response_json
  987. except Exception as ex:
  988. console.debug(f"Unable to get regions due to {ex}.")
  989. return []