From 5fb692cbd75ba0794c29737c5c0890112c3957c0 Mon Sep 17 00:00:00 2001 From: Bowrna Date: Mon, 25 Apr 2022 21:29:26 +0530 Subject: [PATCH] resolving conflict --- BREEZE.rst | 23 ++- .../commands/developer_commands.py | 35 ++++- .../src/airflow_breeze/shell/enter_shell.py | 42 +++++- images/breeze/output-commands.svg | 3 +- images/breeze/output-exec.svg | 133 ++++++++++++++++++ 5 files changed, 227 insertions(+), 9 deletions(-) create mode 100644 images/breeze/output-exec.svg diff --git a/BREEZE.rst b/BREEZE.rst index 37b47f29cfa24..c9f44c90c5467 100644 --- a/BREEZE.rst +++ b/BREEZE.rst @@ -472,7 +472,14 @@ Those are commands mostly used by contributors: * Initialize local virtualenv with ``./scripts/tools/initialize_virtualenv.py`` command * Run static checks with autocomplete support ``breeze static-checks`` command * Run test specified with ``./breeze-legacy tests`` command -* Join running interactive shell with ``./breeze-legacy exec`` command +* Build CI docker image with ``breeze build-image`` command +* Cleanup breeze with ``breeze cleanup`` command +* Run static checks with autocomplete support ``breeze static-checks`` command +* Run test specified with ``./breeze-legacy tests`` command + +Additional management tasks: + +* Join running interactive shell with ``breeze exec`` command * Stop running interactive environment with ``breeze stop`` command * Execute arbitrary docker-compose command with ``./breeze-legacy docker-compose`` command @@ -580,8 +587,7 @@ capability of creating multiple virtual terminals and multiplex between them. Mo found at `tmux GitHub wiki page `_ . Tmux has several useful shortcuts that allow you to split the terminals, open new tabs etc - it's pretty useful to learn it. -Here is the part of Breeze video which is relevant (note that it refers to the old ``./breeze-legacy`` -command but it is very similar to current ``breeze`` command): +Here is the part of Breeze video which is relevant: .. raw:: html @@ -595,12 +601,11 @@ command but it is very similar to current ``breeze`` command): Another way is to exec into Breeze terminal from the host's terminal. Often you can have multiple terminals in the host (Linux/MacOS/WSL2 on Windows) and you can simply use those terminals -to enter the running container. It's as easy as launching ``./breeze-legacy exec`` while you already started the +to enter the running container. It's as easy as launching ``breeze exec`` while you already started the Breeze environment. You will be dropped into bash and environment variables will be read in the same way as when you enter the environment. You can do it multiple times and open as many terminals as you need. -Here is the part of Breeze video which is relevant (note that it refers to the old ``./breeze-legacy`` -command and it is not yet available in the current ``breeze`` command): +Here is the part of Breeze video which is relevant: .. raw:: html @@ -612,6 +617,12 @@ command and it is not yet available in the current ``breeze`` command): +Those are all available flags of ``exec`` command: + +.. image:: ./images/breeze/output-exec.svg + :width: 100% + :alt: Breeze exec + Additional tools ---------------- diff --git a/dev/breeze/src/airflow_breeze/commands/developer_commands.py b/dev/breeze/src/airflow_breeze/commands/developer_commands.py index e44c360b6c90d..f2c8715f95b9d 100644 --- a/dev/breeze/src/airflow_breeze/commands/developer_commands.py +++ b/dev/breeze/src/airflow_breeze/commands/developer_commands.py @@ -50,7 +50,7 @@ get_available_packages, ) from airflow_breeze.pre_commit_ids import PRE_COMMIT_LIST -from airflow_breeze.shell.enter_shell import enter_shell +from airflow_breeze.shell.enter_shell import enter_shell, find_airflow_container from airflow_breeze.shell.shell_params import ShellParams from airflow_breeze.utils.confirm import set_forced_answer from airflow_breeze.utils.console import get_console @@ -66,6 +66,7 @@ "commands": [ "shell", "start-airflow", + "exec", "stop", "build-docs", "static-checks", @@ -144,6 +145,9 @@ ], }, ], + "breeze exec": [ + {"name": "Drops in the interactive shell of active airflow container"}, + ], "breeze stop": [ { "name": "Stop flags", @@ -478,3 +482,32 @@ def args_doc_builder(self) -> List[str]: for single_filter in self.package_filter: doc_args.extend(["--package-filter", single_filter]) return doc_args + + +@main.command(name='exec', help='Joins the interactive shell of running airflow container') +@option_verbose +@option_dry_run +@click.argument('exec_args', nargs=-1, type=click.UNPROCESSED) +def exec(verbose: bool, dry_run: bool, exec_args: Tuple): + container_running = find_airflow_container(verbose, dry_run) + if container_running: + cmd_to_run = [ + "docker", + "exec", + "-it", + container_running, + "/opt/airflow/scripts/docker/entrypoint_exec.sh", + ] + if exec_args: + cmd_to_run.extend(exec_args) + process = run_command( + cmd_to_run, + verbose=verbose, + dry_run=dry_run, + check=False, + no_output_dump_on_exception=False, + text=True, + ) + if not process: + sys.exit(1) + sys.exit(process.returncode) diff --git a/dev/breeze/src/airflow_breeze/shell/enter_shell.py b/dev/breeze/src/airflow_breeze/shell/enter_shell.py index 51294d55bb9da..6d39fb0f8bbb2 100644 --- a/dev/breeze/src/airflow_breeze/shell/enter_shell.py +++ b/dev/breeze/src/airflow_breeze/shell/enter_shell.py @@ -18,7 +18,7 @@ import subprocess import sys from pathlib import Path -from typing import Dict, Union +from typing import Dict, Optional, Union from airflow_breeze import global_constants from airflow_breeze.build_image.ci.build_ci_image import build_ci_image @@ -137,3 +137,43 @@ def run_shell_with_build_image_checks( if cmd_added is not None: cmd.extend(['-c', cmd_added]) return run_command(cmd, verbose=verbose, dry_run=dry_run, env=env_variables, text=True) + + +def stop_exec_on_error(returncode: int): + get_console().print('\n[error]ERROR in finding the airflow docker-compose process id[/]\n') + sys.exit(returncode) + + +def find_airflow_container(verbose, dry_run) -> Optional[str]: + exec_shell_params = ShellParams(verbose=verbose, dry_run=dry_run) + check_docker_resources(verbose, exec_shell_params.airflow_image_name, dry_run) + exec_shell_params.print_badge_info() + env_variables = construct_env_variables_docker_compose_command(exec_shell_params) + cmd = ['docker-compose', 'ps', '--all', '--filter', 'status=running', 'airflow'] + docker_compose_ps_command = run_command( + cmd, + verbose=verbose, + dry_run=dry_run, + text=True, + capture_output=True, + env=env_variables, + # print output if run in verbose mode to better diagnose problems. + no_output_dump_on_exception=not verbose, + ) + if dry_run: + return "CONTAINER_ID" + if docker_compose_ps_command.returncode != 0: + stop_exec_on_error(docker_compose_ps_command.returncode) + return None + + output = docker_compose_ps_command.stdout + container_info = output.strip().split('\n') + if container_info: + container_running = container_info[-1].split(' ')[0] + if container_running.startswith('-'): + # On docker-compose v1 we get '--------' as output here + stop_exec_on_error(docker_compose_ps_command.returncode) + return container_running + else: + stop_exec_on_error(1) + return None diff --git a/images/breeze/output-commands.svg b/images/breeze/output-commands.svg index e5cdec13fbcf5..23ecba434865f 100644 --- a/images/breeze/output-commands.svg +++ b/images/breeze/output-commands.svg @@ -1,4 +1,4 @@ -
╭─ Developer tools ────────────────────────────────────────────────────────────────────────────────────────────────────╮
shell Enter breeze.py environment. this is the default command use when no other is selected.
start-airflow Enter breeze.py environment and starts all Airflow components in the tmux session.
+
exec Joins the interactive shell of running airflow container
stop Stop running breeze environment.
build-docs Build documentation in the container.
static-checks Run static checks.
diff --git a/images/breeze/output-exec.svg b/images/breeze/output-exec.svg new file mode 100644 index 0000000000000..9e8900f0fb315 --- /dev/null +++ b/images/breeze/output-exec.svg @@ -0,0 +1,133 @@ + + + + +
+
+
+ + + + + +
Command: exec
+
+
+
+
Usage: breeze exec [OPTIONS] [EXEC_ARGS]...
+
+
Joins the interactive shell of running airflow container
+
+
╭─ Options ────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
+
--verbose -v Print verbose information about performed steps.
+
--dry-run -D If dry-run is set, commands are only printed, not executed.
+
--help -h Show this message and exit.
+
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
+
+
+
+
+ +
+