|
38 | 38 | from stack.repos.repo_util import host_and_path_for_repo, image_registry_for_repo, fs_path_for_repo, process_repo, get_repo_current_hash, is_repo_dirty, get_container_tag_for_repo |
39 | 39 | from stack.util import include_exclude_check, stack_is_external, error_exit, get_yaml |
40 | 40 | from stack.util import run_shell_command |
| 41 | +from stack import constants |
41 | 42 |
|
42 | 43 | docker = DockerClient() |
43 | 44 |
|
@@ -78,83 +79,98 @@ def make_container_build_env(dev_root_path: str, default_container_base_dir: str |
78 | 79 |
|
79 | 80 |
|
80 | 81 | def process_container(build_context: BuildContext) -> bool: |
81 | | - log_info(f"Building: {build_context.container.name}:stack") |
82 | 82 |
|
83 | | - default_container_tag = f"{build_context.container.name}:stack" |
84 | | - build_context.container_build_env.update({"STACK_FULL_CONTAINER_IMAGE_TAG": default_container_tag}) |
85 | | - build_context.container_build_env.update({"STACK_DEFAULT_CONTAINER_IMAGE_TAG": default_container_tag}) |
| 83 | + building_container = build_context.container |
| 84 | + build_envs = build_context.container_build_env |
| 85 | + |
| 86 | + default_container_tag = f"{building_container.name}:stack" |
| 87 | + log_info(f"Processing build of container: {default_container_tag}") |
| 88 | + |
| 89 | + build_envs.update({"STACK_FULL_CONTAINER_IMAGE_TAG": default_container_tag}) |
| 90 | + build_envs.update({"STACK_DEFAULT_CONTAINER_IMAGE_TAG": default_container_tag}) |
86 | 91 |
|
87 | 92 | build_dir = None |
88 | 93 | build_script_filename = None |
89 | 94 |
|
90 | 95 | # Check if this is in an external stack |
91 | 96 | if stack_is_external(build_context.stack): |
92 | | - if build_context.container.build: |
93 | | - build_script_filename = Path(build_context.container.file_path).parent.joinpath(build_context.container.build) |
| 97 | + log_debug(f"Determined stack: {build_context.stack.name} is external") |
| 98 | + # DBDB What is this code below doing? |
| 99 | + # "build" is pulled from the container description yaml |
| 100 | + # Presumably it means "the relative name of the build file" |
| 101 | + if building_container.build: |
| 102 | + # If the build script filename was provided, we use that |
| 103 | + build_script_filename = Path(building_container.file_path).parent.joinpath(building_container.build) |
94 | 104 | build_dir = build_script_filename.parent |
95 | | - build_context.container_build_env["STACK_BUILD_DIR"] = build_dir |
| 105 | + build_envs["STACK_BUILD_DIR"] = build_dir |
96 | 106 | else: |
97 | | - container_build_script_dir = Path(build_context.stack.name).parent.parent.joinpath("container-build") |
| 107 | + # If the build script filename is not explicitly provided, we try to infer it |
| 108 | + # DBDB this code seems not to work because we use the bare stack name rather than a directory |
| 109 | + # We go looking for a "containers" directory in the root of the container's repo. |
| 110 | + container_build_script_dir = fs_path_for_repo(building_container.ref).joinpath(constants.stack_files_directory_name).joinpath(constants.containers_directory_name) |
| 111 | + log_debug(f"Looking for build script in this directory: {container_build_script_dir}") |
98 | 112 | if os.path.exists(container_build_script_dir): |
99 | | - temp_build_dir = container_build_script_dir.joinpath(build_context.container.name.replace("/", "-")) |
| 113 | + temp_build_dir = container_build_script_dir.joinpath(building_container.name.replace("/", "-")) |
100 | 114 | temp_build_script_filename = temp_build_dir.joinpath("build.sh") |
101 | 115 | # Now check if the container exists in the external stack. |
| 116 | + log_debug(f"Looking for build script at: {temp_build_script_filename}") |
102 | 117 | if not temp_build_script_filename.exists(): |
103 | 118 | # If not, revert to building an internal container |
| 119 | + # DBDB Why? |
104 | 120 | container_build_script_dir = build_context.default_container_base_dir |
105 | | - build_dir = container_build_script_dir.joinpath(build_context.container.name.replace("/", "-")) |
| 121 | + build_dir = container_build_script_dir.joinpath(building_container.name.replace("/", "-")) |
106 | 122 | build_script_filename = build_dir.joinpath("build.sh") |
107 | | - build_context.container_build_env["STACK_BUILD_DIR"] = build_dir |
| 123 | + build_envs["STACK_BUILD_DIR"] = build_dir |
| 124 | + |
108 | 125 | if not build_dir: |
109 | | - build_dir = build_context.default_container_base_dir.joinpath(build_context.container.name.replace("/", "-")) |
| 126 | + build_dir = build_context.default_container_base_dir.joinpath(building_container.name.replace("/", "-")) |
110 | 127 | build_script_filename = build_dir.joinpath("build.sh") |
111 | 128 |
|
112 | 129 | log_debug(f"Build script filename: {build_script_filename}") |
| 130 | + log_debug(f"Build script filename: {build_dir}") |
113 | 131 |
|
114 | 132 | if os.path.exists(build_script_filename): |
115 | 133 | build_command = build_script_filename.as_posix() |
116 | 134 | else: |
117 | 135 | log_debug(f"No script file found: {build_script_filename}, using default build script") |
118 | | - if build_context.container.ref: |
119 | | - repo_full_path = fs_path_for_repo(build_context.container.ref) |
| 136 | + if building_container.ref: |
| 137 | + repo_full_path = fs_path_for_repo(building_container.ref) |
120 | 138 | else: |
121 | 139 | repo_full_path = build_context.stack.repo_path |
122 | 140 |
|
123 | | - if build_context.container.path: |
124 | | - repo_full_path = repo_full_path.joinpath(build_context.container.path) |
| 141 | + if building_container.path: |
| 142 | + repo_full_path = repo_full_path.joinpath(building_container.path) |
125 | 143 | repo_dir_or_build_dir = repo_full_path if repo_full_path and repo_full_path.exists() else build_dir |
126 | 144 | build_command = ( |
127 | 145 | os.path.join(build_context.default_container_base_dir, "default-build.sh") |
128 | 146 | + f" {default_container_tag} {repo_dir_or_build_dir}" |
129 | 147 | ) |
130 | | - build_context.container_build_env["STACK_BUILD_DIR"] = repo_dir_or_build_dir |
| 148 | + build_envs["STACK_BUILD_DIR"] = repo_dir_or_build_dir |
131 | 149 |
|
| 150 | + build_envs["STACK_IMAGE_NAME"] = building_container.name |
132 | 151 |
|
133 | | - build_context.container_build_env["STACK_IMAGE_NAME"] = build_context.container.name |
134 | | - |
135 | | - build_context.container_build_env["STACK_REPO_STACK_DIR"] = str(build_context.stack.repo_path) if build_context.stack.repo_path else "" |
136 | | - build_context.container_build_env["STACK_REPO_CONTAINER_DIR"] = str(build_context.container.repo_path) if build_context.container.repo_path else build_context.container_build_env["STACK_REPO_STACK_DIR"] |
137 | | - build_context.container_build_env["STACK_REPO_SOURCE_DIR"] = str(fs_path_for_repo(build_context.container.ref)) if build_context.container.ref else build_context.container_build_env["STACK_REPO_CONTAINER_DIR"] |
| 152 | + build_envs["STACK_REPO_STACK_DIR"] = str(build_context.stack.repo_path) if build_context.stack.repo_path else "" |
| 153 | + build_envs["STACK_REPO_CONTAINER_DIR"] = str(build_context.container.repo_path) if building_container.repo_path else build_envs["STACK_REPO_STACK_DIR"] |
| 154 | + build_envs["STACK_REPO_SOURCE_DIR"] = str(fs_path_for_repo(building_container.ref)) if building_container.ref else build_envs["STACK_REPO_CONTAINER_DIR"] |
138 | 155 |
|
139 | 156 | if not opts.o.dry_run: |
140 | 157 | # No PATH at all causes failures with podman. |
141 | | - if "PATH" not in build_context.container_build_env: |
142 | | - build_context.container_build_env["PATH"] = os.environ["PATH"] |
143 | | - log_debug(f"Executing: {build_command} with environment: {build_context.container_build_env}") |
| 158 | + if "PATH" not in build_envs: |
| 159 | + build_envs["PATH"] = os.environ["PATH"] |
| 160 | + log_debug(f"Executing: {build_command} with environment: {build_envs}") |
144 | 161 |
|
145 | | - build_result = run_shell_command(build_command, env=build_context.container_build_env, quiet=opts.o.quiet) |
| 162 | + build_result = run_shell_command(build_command, env=build_envs, quiet=opts.o.quiet) |
146 | 163 |
|
147 | | - log_debug(f"Return code is: {build_result}") |
| 164 | + log_debug(f"Build command return code is: {build_result}") |
148 | 165 | if build_result != 0: |
149 | 166 | return False |
150 | 167 | else: |
151 | 168 | return True |
152 | 169 | else: |
153 | | - log_info("Skipped") |
| 170 | + log_info("Skipped for dry run") |
154 | 171 | return True |
155 | 172 |
|
156 | 173 |
|
157 | | - |
158 | 174 | def build_containers(parent_stack, |
159 | 175 | build_policy=get_config_setting("build-policy", BUILD_POLICIES[0]), |
160 | 176 | image_registry=get_config_setting("image-registry"), |
@@ -337,6 +353,7 @@ def build_containers(parent_stack, |
337 | 353 | log_info(f"Container {container_tag} needs to be built.") |
338 | 354 | container_needs_pulled = False |
339 | 355 | container_needs_built = True |
| 356 | + # DBDB add comment explaining what this code below is doing. |
340 | 357 | if not os.path.exists(target_fs_repo_path) or (git_pull and target_fs_repo_path not in dont_pull_repo_fs_paths): |
341 | 358 | reconstructed_ref = f"{container_spec.ref.split('@')[0]}@{target_hash}" |
342 | 359 | process_repo(git_pull, False, git_ssh, dev_root_path, [], reconstructed_ref) |
@@ -373,7 +390,6 @@ def build_containers(parent_stack, |
373 | 390 | except: |
374 | 391 | pass |
375 | 392 |
|
376 | | - log_info(f"Building {container_spec.name}") |
377 | 393 | result = process_container(build_context) |
378 | 394 | if result: |
379 | 395 | container_was_built = True |
|
0 commit comments