hosting.py 40 KB

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