def run_shell_with_build_image_checks(verbose: bool, dry_run: bool, shell_params: ShellParams): """ Executes shell command built from params passed, checking if build is not needed. * checks if there are enough resources to run shell * checks if image was built at least once (if not - forces the build) * if not forces, checks if build is needed and asks the user if so * builds the image if needed * prints information about the build * constructs docker compose command to enter shell * executes it :param verbose: print commands when running :param dry_run: do not execute "write" commands - just print what would happen :param shell_params: parameters of the execution """ check_docker_resources(verbose, shell_params.airflow_image_name) build_ci_image_check_cache = Path(BUILD_CACHE_DIR, shell_params.airflow_branch, f".built_{shell_params.python}") if build_ci_image_check_cache.exists(): console.print( f'[bright_blue]{shell_params.the_image_type} image already built locally.[/]' ) else: console.print( f'[bright_yellow]{shell_params.the_image_type} image not built locally. ' f'Forcing build.[/]') shell_params.force_build = True if not shell_params.force_build: build_image_if_needed_steps(verbose, dry_run, shell_params) else: build_image( verbose, dry_run=dry_run, python=shell_params.python, upgrade_to_newer_dependencies="false", ) shell_params.print_badge_info() cmd = [ 'docker-compose', 'run', '--service-ports', "-e", "BREEZE", '--rm', 'airflow' ] cmd_added = shell_params.command_passed env_variables = construct_env_variables_docker_compose_command( shell_params) if cmd_added is not None: cmd.extend(['-c', cmd_added]) run_command(cmd, verbose=verbose, dry_run=dry_run, env=env_variables, text=True)
def enter_shell( **kwargs ) -> Union[subprocess.CompletedProcess, subprocess.CalledProcessError]: """ Executes entering shell using the parameters passed as kwargs: * checks if docker version is good * checks if docker-compose version is good * updates kwargs with cached parameters * displays ASCIIART and CHEATSHEET unless disabled * build ShellParams from the updated kwargs * executes the command to drop the user to Breeze shell """ verbose = kwargs['verbose'] dry_run = kwargs['dry_run'] if not check_docker_is_running(verbose): console.print( '[red]Docker is not running.[/]\n' '[bright_yellow]Please make sure Docker is installed and running.[/]' ) sys.exit(1) check_docker_version(verbose) check_docker_compose_version(verbose) updated_kwargs = synchronize_cached_params(kwargs) if read_from_cache_file('suppress_asciiart') is None: console.print(ASCIIART, style=ASCIIART_STYLE) if read_from_cache_file('suppress_cheatsheet') is None: console.print(CHEATSHEET, style=CHEATSHEET_STYLE) enter_shell_params = ShellParams(**filter_out_none(**updated_kwargs)) return run_shell_with_build_image_checks(verbose, dry_run, enter_shell_params)
def fix_ownership(verbose: bool, dry_run: bool): shell_params = ShellParams( verbose=verbose, mount_sources=MOUNT_ALL, python=DEFAULT_PYTHON_MAJOR_MINOR_VERSION, ) extra_docker_flags = get_extra_docker_flags(MOUNT_ALL) env = construct_env_variables_docker_compose_command(shell_params) cmd = [ "docker", "run", "-t", *extra_docker_flags, "-e", "GITHUB_ACTIONS=", "-e", "SKIP_ENVIRONMENT_INITIALIZATION=true", "--pull", "never", shell_params.airflow_image_name_with_tag, "/opt/airflow/scripts/in_container/run_fix_ownership.sh", ] run_command(cmd, verbose=verbose, dry_run=dry_run, text=True, env=env, check=False) # Always succeed sys.exit(0)
def stop(verbose: bool, dry_run: bool, preserve_volumes: bool): command_to_execute = ['docker-compose', 'down', "--remove-orphans"] if not preserve_volumes: command_to_execute.append("--volumes") shell_params = ShellParams({}) env_variables = construct_env_variables_docker_compose_command(shell_params) run_command(command_to_execute, verbose=verbose, dry_run=dry_run, env=env_variables)
def enter_shell(**kwargs): """ Executes entering shell using the parameters passed as kwargs: * checks if docker version is good * checks if docker-compose version is good * updates kwargs with cached parameters * displays ASCIIART and CHEATSHEET unless disabled * build ShellParams from the updated kwargs * executes the command to drop the user to Breeze shell """ verbose = kwargs['verbose'] dry_run = kwargs['dry_run'] check_docker_version(verbose) check_docker_compose_version(verbose) updated_kwargs = synchronize_cached_params(kwargs) if read_from_cache_file('suppress_asciiart') is None: console.print(ASCIIART, style=ASCIIART_STYLE) if read_from_cache_file('suppress_cheatsheet') is None: console.print(CHEATSHEET, style=CHEATSHEET_STYLE) enter_shell_params = ShellParams(**filter_out_none(**updated_kwargs)) run_shell_with_build_image_checks(verbose, dry_run, enter_shell_params)
def resource_check(verbose: bool, dry_run: bool): shell_params = ShellParams(verbose=verbose, python=DEFAULT_PYTHON_MAJOR_MINOR_VERSION) check_docker_resources(verbose, shell_params.airflow_image_name, dry_run=dry_run)