22import shutil
33
44import click
5+ import yaml
56
67from ..cli_constants import BUILD_DIR , IMAGE_TAG_TO_REPLACE
7- from ..cli_utils import echo_info
8+ from ..cli_utils import echo_error , echo_info , echo_warning
89from ..config_generation import (
910 copy_config_dir_to_build_dir ,
1011 copy_dag_dir_to_build_dir ,
1112 generate_profiles_yml ,
1213)
1314from ..data_structures import DockerArgs
14- from ..dbt_utils import run_dbt_command
15- from ..errors import DockerNotInstalledError
15+ from ..dbt_utils import read_dbt_vars_from_configs , run_dbt_command
16+ from ..docker_response_reader import DockerResponseReader
17+ from ..errors import (
18+ DataPipelinesError ,
19+ DockerErrorResponseError ,
20+ DockerNotInstalledError ,
21+ )
1622from ..io_utils import replace
17-
18-
19- def _replace_image_tag (k8s_config : pathlib .Path , docker_args : DockerArgs ) -> None :
20- echo_info (
21- f"Replacing { IMAGE_TAG_TO_REPLACE } with commit SHA = { docker_args .commit_sha } "
22- )
23- replace (k8s_config , IMAGE_TAG_TO_REPLACE , docker_args .commit_sha )
23+ from ..jinja import replace_vars_with_values
2424
2525
2626def _docker_build (docker_args : DockerArgs ) -> None :
@@ -38,14 +38,15 @@ def _docker_build(docker_args: DockerArgs) -> None:
3838 docker_client = docker .from_env ()
3939 docker_tag = docker_args .docker_build_tag ()
4040 _ , logs_generator = docker_client .images .build (path = "." , tag = docker_tag )
41- click .echo (
42- "" .join (
43- map (
44- lambda log : log ["stream" ],
45- filter (lambda log : "stream" in log , logs_generator ),
46- )
41+ try :
42+ DockerResponseReader (logs_generator ).click_echo_ok_responses ()
43+ except DockerErrorResponseError as err :
44+ echo_error (err .message )
45+ raise DataPipelinesError (
46+ "Error raised when pushing Docker image. Ensure that "
47+ "Docker image you try to push exists. Maybe try running "
48+ "'dp compile' first?"
4749 )
48- )
4950
5051
5152def _dbt_compile (env : str ) -> None :
@@ -66,16 +67,40 @@ def _copy_dbt_manifest() -> None:
6667
6768
6869def _replace_k8s_settings (docker_args : DockerArgs ) -> None :
69- k8s_config : pathlib .Path = BUILD_DIR .joinpath ("dag" , "config" , "base" , "k8s.yml" )
70- _replace_image_tag (k8s_config , docker_args )
70+ k8s_config = BUILD_DIR .joinpath ("dag" , "config" , "base" , "k8s.yml" )
71+ echo_info (
72+ f"Replacing { IMAGE_TAG_TO_REPLACE } with commit SHA = { docker_args .commit_sha } "
73+ )
74+ replace (k8s_config , IMAGE_TAG_TO_REPLACE , docker_args .commit_sha )
75+
76+
77+ def _replace_datahub_with_jinja_vars (env : str ) -> None :
78+ datahub_config_path : pathlib .Path = BUILD_DIR .joinpath (
79+ "dag" , "config" , "base" , "datahub.yml"
80+ )
81+
82+ if not datahub_config_path .exists ():
83+ echo_warning (
84+ f"File config/base/datahub.yml does not exist in { BUILD_DIR } . "
85+ "Content will not be replaced."
86+ )
87+ return
88+
89+ echo_info (f"Replacing Jinja variables in { datahub_config_path } ." )
90+ with open (datahub_config_path , "r" ) as datahub_config_file :
91+ updated_config = replace_vars_with_values (
92+ yaml .safe_load (datahub_config_file ), read_dbt_vars_from_configs (env )
93+ )
94+ with open (datahub_config_path , "w" ) as datahub_config_file :
95+ yaml .dump (updated_config , datahub_config_file )
7196
7297
7398def compile_project (
7499 env : str ,
75100 docker_build : bool = False ,
76101) -> None :
77102 """
78- Create local working directories and build artifacts
103+ Create local working directories and build artifacts.
79104
80105 :param env: Name of the environment
81106 :type env: str
@@ -88,6 +113,7 @@ def compile_project(
88113
89114 docker_args = DockerArgs (env )
90115 _replace_k8s_settings (docker_args )
116+ _replace_datahub_with_jinja_vars (env )
91117
92118 _dbt_compile (env )
93119 _copy_dbt_manifest ()
0 commit comments