-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GitOrigin-RevId: 95740a87083c703968ce3da45b15113851ef09f7
- Loading branch information
Showing
8 changed files
with
665 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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,19 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
AIRFLOW_BRANCH = "main" | ||
DEFAULT_AIRFLOW_CONSTRAINTS_BRANCH = "constraints-main" |
This file contains 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 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,79 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import sys | ||
from pathlib import Path | ||
from typing import Any, List, Optional, Tuple | ||
|
||
from airflow_breeze import global_constants | ||
from airflow_breeze.breeze import get_airflow_sources_root | ||
from airflow_breeze.console import console | ||
|
||
BUILD_CACHE_DIR = Path(get_airflow_sources_root(), '.build') | ||
|
||
|
||
def check_if_cache_exists(param_name: str) -> bool: | ||
return (Path(BUILD_CACHE_DIR) / f".{param_name}").exists() | ||
|
||
|
||
def read_from_cache_file(param_name: str) -> Optional[str]: | ||
cache_exists = check_if_cache_exists(param_name) | ||
if cache_exists: | ||
return (Path(BUILD_CACHE_DIR) / f".{param_name}").read_text().strip() | ||
else: | ||
return None | ||
|
||
|
||
def write_to_cache_file(param_name: str, param_value: str, check_allowed_values: bool = True) -> None: | ||
allowed = False | ||
if check_allowed_values: | ||
allowed, allowed_values = check_if_values_allowed(param_name, param_value) | ||
if allowed or not check_allowed_values: | ||
Path(BUILD_CACHE_DIR, f".{param_name}").write_text(param_value) | ||
else: | ||
console.print(f'[cyan]You have sent the {param_value} for {param_name}') | ||
console.print(f'[cyan]Allowed value for the {param_name} are {allowed_values}') | ||
console.print('[cyan]Provide one of the supported params. Write to cache dir failed') | ||
sys.exit() | ||
|
||
|
||
def check_cache_and_write_if_not_cached( | ||
param_name: str, default_param_value: str | ||
) -> Tuple[bool, Optional[str]]: | ||
is_cached = False | ||
allowed = False | ||
cached_value = read_from_cache_file(param_name) | ||
if cached_value is None: | ||
write_to_cache_file(param_name, default_param_value) | ||
cached_value = default_param_value | ||
else: | ||
allowed, allowed_values = check_if_values_allowed(param_name, cached_value) | ||
if allowed: | ||
is_cached = True | ||
else: | ||
write_to_cache_file(param_name, default_param_value) | ||
cached_value = default_param_value | ||
return is_cached, cached_value | ||
|
||
|
||
def check_if_values_allowed(param_name: str, param_value: str) -> Tuple[bool, List[Any]]: | ||
allowed = False | ||
allowed_values: List[Any] = [] | ||
allowed_values = getattr(global_constants, f'ALLOWED_{param_name.upper()}') | ||
if param_value in allowed_values: | ||
allowed = True | ||
return allowed, allowed_values |
This file contains 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,94 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# from airflow_breeze.utils import run_command | ||
from pathlib import Path | ||
from typing import List | ||
|
||
from airflow_breeze.breeze import get_airflow_sources_root | ||
from airflow_breeze.cache import check_cache_and_write_if_not_cached | ||
from airflow_breeze.ci.build_params import BuildParams | ||
from airflow_breeze.console import console | ||
from airflow_breeze.utils import filter_out_none, run_command | ||
|
||
PARAMS_CI_IMAGE = [ | ||
"python_base_image", | ||
"airflow_version", | ||
"airflow_branch", | ||
"airflow_extras", | ||
"airflow_pre_cached_pip_packages", | ||
"additional_airflow_extras", | ||
"additional_python_deps", | ||
"additional_dev_apt_command", | ||
"additional_dev_apt_deps", | ||
"additional_dev_apt_env", | ||
"additional_runtime_apt_command", | ||
"additional_runtime_apt_deps", | ||
"additional_runtime_apt_env", | ||
"upgrade_to_newer_dependencies", | ||
"constraints_github_repository", | ||
"airflow_constraints_reference", | ||
"airflow_constraints", | ||
"airflow_image_repository", | ||
"airflow_image_date_created", | ||
"build_id", | ||
"commit_sha", | ||
] | ||
|
||
PARAMS_TO_VERIFY_CI_IMAGE = [ | ||
"dev_apt_command", | ||
"dev_apt_deps", | ||
"runtime_apt_command", | ||
"runtime_apt_deps", | ||
] | ||
|
||
|
||
def construct_arguments_docker_command(ci_image: BuildParams) -> List[str]: | ||
args_command = [] | ||
for param in PARAMS_CI_IMAGE: | ||
args_command.append("--build-arg") | ||
args_command.append(param.upper() + "=" + str(getattr(ci_image, param))) | ||
for verify_param in PARAMS_TO_VERIFY_CI_IMAGE: | ||
param_value = str(getattr(ci_image, verify_param)) | ||
if len(param_value) > 0: | ||
args_command.append("--build-arg") | ||
args_command.append(verify_param.upper() + "=" + param_value) | ||
docker_cache = ci_image.docker_cache_ci_directive | ||
if len(docker_cache) > 0: | ||
args_command.extend(ci_image.docker_cache_ci_directive) | ||
return args_command | ||
|
||
|
||
def construct_docker_command(ci_image: BuildParams) -> List[str]: | ||
arguments = construct_arguments_docker_command(ci_image) | ||
final_command = [] | ||
final_command.extend(["docker", "build"]) | ||
final_command.extend(arguments) | ||
final_command.extend(["-t", ci_image.airflow_ci_image_name, "--target", "main", "."]) | ||
final_command.extend(["-f", str(Path(get_airflow_sources_root(), 'Dockerfile.ci').resolve())]) | ||
return final_command | ||
|
||
|
||
def build_image(verbose, **kwargs): | ||
ci_image_params = BuildParams(filter_out_none(**kwargs)) | ||
is_cached, value = check_cache_and_write_if_not_cached( | ||
"PYTHON_MAJOR_MINOR_VERSION", ci_image_params.python_version | ||
) | ||
if is_cached: | ||
ci_image_params.python_version = value | ||
cmd = construct_docker_command(ci_image_params) | ||
output = run_command(cmd, verbose=verbose, text=True) | ||
console.print(f"[blue]{output}") |
Oops, something went wrong.