hosting.py 45 KB

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