-
Notifications
You must be signed in to change notification settings - Fork 1.8k
ENG-11433 Move reflex deploy out of the framework into the Cloud CLI
#6924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FarhanAliRaza
wants to merge
3
commits into
reflex-dev:main
Choose a base branch
from
FarhanAliRaza:farhan/eng-11433-move-reflex-deploy-command-out-of-the-reflex-package-into
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
2f236eb
ENG-11433 refactor(cli): move the `reflex deploy` command into the ho…
FarhanAliRaza 03a451c
ENG-11433 feat(cli): keep `reflex deploy` working from the hosting CLI
FarhanAliRaza f6945ca
ENG-11433 docs(cli): drop the stale entry-point wording from deploy.py
FarhanAliRaza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| The `reflex deploy` command implementation moved out of the `reflex` package into `reflex-hosting-cli`, so cloud code is no longer shipped inside the framework. Flags and behavior are unchanged, and `reflex-hosting-cli` remains a dependency of `reflex`, so `reflex deploy` and `reflex cloud` stay available out of the box. If the package is not installed, these commands now report which package to install instead of failing with a missing-command error. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| The shared click options for the reflex CLIs (`--loglevel`, `--json`) moved here as `reflex_base.utils.cli_options`, so CLI packages that do not depend on `reflex` can use them. `reflex.utils.cli_options` re-exports them. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| """Shared click options for the reflex CLIs.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| import click | ||
|
|
||
| from reflex_base import constants | ||
| from reflex_base.utils import console, log | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Callable | ||
|
|
||
|
|
||
| def set_loglevel(ctx: click.Context, self: click.Parameter, value: str | None): | ||
| """Set the log level. | ||
|
|
||
| Args: | ||
| ctx: The click context. | ||
| self: The click command. | ||
| value: The log level to set. | ||
| """ | ||
| if value is not None: | ||
| loglevel = constants.LogLevel.from_string(value) | ||
| console.set_log_level(loglevel) | ||
|
|
||
|
|
||
| loglevel_option = click.option( | ||
| "--loglevel", | ||
| "--log-level", | ||
| "loglevel", | ||
| type=click.Choice( | ||
| [loglevel.value for loglevel in constants.LogLevel], | ||
| case_sensitive=False, | ||
| ), | ||
| is_eager=True, | ||
| callback=set_loglevel, | ||
| expose_value=False, | ||
| help="The log level to use.", | ||
| ) | ||
|
|
||
|
|
||
| def set_log_json(ctx: click.Context, self: click.Parameter, value: bool): | ||
| """Enable machine-readable JSON log output. | ||
|
|
||
| Args: | ||
| ctx: The click context. | ||
| self: The click command. | ||
| value: Whether --json was passed. | ||
| """ | ||
| if value: | ||
| log.set_json_mode(True) | ||
|
|
||
|
|
||
| json_option = click.option( | ||
| "--json", | ||
| "log_json", | ||
| is_flag=True, | ||
| is_eager=True, | ||
| callback=set_log_json, | ||
| expose_value=False, | ||
| help="Output logs as machine-readable JSON records.", | ||
| ) | ||
|
|
||
|
|
||
| def log_options(func: Callable) -> Callable: | ||
| """Apply the shared logging CLI options (--loglevel, --json). | ||
|
|
||
| Args: | ||
| func: The click command callback. | ||
|
|
||
| Returns: | ||
| The decorated callback. | ||
| """ | ||
| return loglevel_option(json_option(func)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| The `reflex deploy` command implementation now lives here, in `reflex_cli.v2.deploy`. The package no longer imports the `reflex` framework at module scope, so it stays importable on its own. |
235 changes: 235 additions & 0 deletions
235
packages/reflex-hosting-cli/src/reflex_cli/v2/deploy.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,235 @@ | ||
| """The `reflex deploy` command. | ||
|
|
||
| This module hosts the managed-platform deploy command. The `reflex` CLI imports | ||
| it and registers it as `reflex deploy`. | ||
|
|
||
| The command body needs the reflex framework to compile and export the app, but | ||
| `reflex` is deliberately not a dependency of reflex-hosting-cli. Those imports | ||
| therefore stay inside the command body, which only ever runs under the reflex | ||
| CLI. Nothing at module scope may import `reflex`, so that this package stays | ||
| importable on its own. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| import click | ||
| from reflex_base import constants | ||
| from reflex_base.config import get_config | ||
| from reflex_base.environment import environment | ||
| from reflex_base.utils.cli_options import log_options | ||
|
|
||
|
|
||
| @click.command(name="deploy") | ||
| @log_options | ||
| @click.option( | ||
| "--app-name", | ||
| help="The name of the app to deploy.", | ||
| ) | ||
| @click.option( | ||
| "--app-id", | ||
| help="The ID of the app to deploy.", | ||
| ) | ||
| @click.option( | ||
| "-r", | ||
| "--region", | ||
| multiple=True, | ||
| help="The regions to deploy to. `reflex cloud regions` For multiple envs, repeat this option, e.g. --region sjc --region iad", | ||
| ) | ||
| @click.option( | ||
| "--env", | ||
| multiple=True, | ||
| help="The environment variables to set: <key>=<value>. For multiple envs, repeat this option, e.g. --env k1=v2 --env k2=v2.", | ||
| ) | ||
| @click.option( | ||
| "--vmtype", | ||
| help="Vm type id. Run `reflex cloud vmtypes` to get options.", | ||
| ) | ||
| @click.option( | ||
| "--min-instances", | ||
| type=int, | ||
| help="The minimum number of instances to keep running. Left unchanged when " | ||
| "omitted. Only supported on apps deployed to Google Cloud.", | ||
| ) | ||
| @click.option( | ||
| "--max-instances", | ||
| type=int, | ||
| help="The maximum number of instances to scale out to. Left unchanged when " | ||
| "omitted. Only supported on apps deployed to Google Cloud.", | ||
| ) | ||
| @click.option( | ||
| "--hostname", | ||
| help="The hostname of the frontend.", | ||
| ) | ||
| @click.option( | ||
| "--provider", | ||
| help="The hosting provider to deploy to: 'reflex-cloud' (default) or 'gcp' " | ||
| "(a GCP account connected to your org, Enterprise tier). When omitted and " | ||
| "GCP is connected, you'll be prompted in interactive mode. Deploys through " | ||
| "Reflex Cloud either way; for an unmanaged deploy run under your own " | ||
| "gcloud credentials, see `reflex cloud gcp-standalone`.", | ||
| ) | ||
| @click.option( | ||
| "--gcp-connection", | ||
| help="Which of your organization's GCP connections to deploy through, by " | ||
| "name. Run `reflex cloud providers connections` to list them. Only valid " | ||
| "with --provider gcp; omitted keeps the app on the connection it already " | ||
| "has, or your organization's default the first time it deploys to GCP.", | ||
| ) | ||
| @click.option( | ||
| "--full-deploy/--no-full-deploy", | ||
| "full_deploy", | ||
| default=None, | ||
| help="Serve the frontend from the provider's own container, on the same " | ||
| "origin as the backend, instead of Reflex's CDN. GCP only, Enterprise " | ||
| "tier. Omitted leaves the app's hosting mode unchanged; changing it stops " | ||
| "a running app so this deploy brings it back up in the new mode.", | ||
| ) | ||
| @click.option( | ||
| "--strategy", | ||
| type=click.Choice(["immediate", "rolling", "bluegreen", "canary"]), | ||
| help="How the new version rolls out. Defaults to the app's last strategy, " | ||
| "or 'immediate'.", | ||
| ) | ||
| @click.option( | ||
| "--description", | ||
| help="An optional note recorded on this deployment and shown in " | ||
| "`reflex cloud apps history`.", | ||
| ) | ||
| @click.option( | ||
| "--interactive/--no-interactive", | ||
| is_flag=True, | ||
| default=True, | ||
| help="Whether to list configuration options and ask for confirmation.", | ||
| ) | ||
| @click.option( | ||
| "--envfile", | ||
| help="The path to an env file to use. Will override any envs set manually.", | ||
| ) | ||
| @click.option( | ||
| "--project", | ||
| help="project id to deploy to", | ||
| ) | ||
| @click.option( | ||
| "--project-name", | ||
| help="The name of the project to deploy to.", | ||
| ) | ||
| @click.option( | ||
| "--token", | ||
| help="token to use for auth", | ||
| ) | ||
| @click.option( | ||
| "--config-path", | ||
| "--config", | ||
| help="path to the config file", | ||
| ) | ||
| @click.option( | ||
| "--exclude-from-backend", | ||
| "backend_excluded_dirs", | ||
| multiple=True, | ||
| type=click.Path(exists=True, path_type=Path, resolve_path=True), | ||
| help="Files or directories to exclude from the backend zip. Can be used multiple times.", | ||
| ) | ||
| @click.option( | ||
| "--server-side-rendering/--no-server-side-rendering", | ||
| "--ssr/--no-ssr", | ||
| "ssr", | ||
| default=True, | ||
| is_flag=True, | ||
| help="Whether to enable server side rendering for the frontend.", | ||
| ) | ||
| def deploy( | ||
| app_name: str | None, | ||
| app_id: str | None, | ||
| region: tuple[str, ...], | ||
| env: tuple[str], | ||
| vmtype: str | None, | ||
| min_instances: int | None, | ||
| max_instances: int | None, | ||
| hostname: str | None, | ||
| provider: str | None, | ||
| gcp_connection: str | None, | ||
| full_deploy: bool | None, | ||
| strategy: str | None, | ||
| description: str | None, | ||
| interactive: bool, | ||
| envfile: str | None, | ||
| project: str | None, | ||
| project_name: str | None, | ||
| token: str | None, | ||
| config_path: str | None, | ||
| backend_excluded_dirs: tuple[Path, ...] = (), | ||
| ssr: bool = True, | ||
| ): | ||
| """Deploy the app to the Reflex hosting service.""" | ||
| from reflex.reflex import _init | ||
| from reflex.utils import export as export_utils | ||
| from reflex.utils import prerequisites | ||
| from reflex_cli.utils import dependency | ||
| from reflex_cli.v2 import cli as hosting_cli | ||
| from reflex_cli.v2.deployments import check_version | ||
|
|
||
| config = get_config() | ||
|
|
||
| app_name = app_name or config.app_name | ||
|
|
||
| check_version() | ||
|
|
||
| environment.REFLEX_COMPILE_CONTEXT.set(constants.CompileContext.DEPLOY) | ||
|
|
||
| if not environment.REFLEX_SSR.is_set(): | ||
| environment.REFLEX_SSR.set(ssr) | ||
| elif environment.REFLEX_SSR.get() != ssr: | ||
| ssr = environment.REFLEX_SSR.get() | ||
|
|
||
| # Only check requirements if interactive. | ||
| # There is user interaction for requirements update. | ||
| if interactive: | ||
| dependency.check_requirements() | ||
|
|
||
| prerequisites.assert_in_reflex_dir() | ||
|
|
||
| # Check if we are set up. | ||
| if prerequisites.needs_reinit(): | ||
| _init(name=config.app_name) | ||
| prerequisites.check_latest_package_version(constants.ReflexHostingCLI.MODULE_NAME) | ||
|
|
||
| hosting_cli.deploy( | ||
| app_name=app_name, | ||
| app_id=app_id, | ||
| export_fn=( | ||
| lambda zip_dest_dir, api_url, deploy_url, frontend, backend, upload_db, zipping: ( | ||
| export_utils.export( | ||
| zip_dest_dir=zip_dest_dir, | ||
| api_url=api_url, | ||
| deploy_url=deploy_url, | ||
| frontend=frontend, | ||
| backend=backend, | ||
| zipping=zipping, | ||
| loglevel=config.loglevel.subprocess_level(), | ||
| upload_db_file=upload_db, | ||
| backend_excluded_dirs=backend_excluded_dirs, | ||
| prerender_routes=ssr, | ||
| ) | ||
| ) | ||
| ), | ||
| regions=list(region), | ||
| envs=list(env), | ||
| vmtype=vmtype, | ||
| min_instances=min_instances, | ||
| max_instances=max_instances, | ||
| envfile=envfile, | ||
| hostname=hostname, | ||
| interactive=interactive, | ||
| loglevel=config.loglevel, | ||
| token=token, | ||
| project=project, | ||
| project_name=project_name, | ||
| provider=provider, | ||
| gcp_connection=gcp_connection, | ||
| full_deploy=full_deploy, | ||
| strategy=strategy, | ||
| deployment_description=description, | ||
| **({"config_path": config_path} if config_path is not None else {}), | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.