Skip to content

Commit 6246b31

Browse files
authored
Merge pull request #127 from bozemanpass/dboreham/container-build-cleanup
Clean up container build code a bit
2 parents ad7d064 + e38704b commit 6246b31

File tree

2 files changed

+48
-30
lines changed

2 files changed

+48
-30
lines changed

src/stack/build/build_containers.py

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
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
3939
from stack.util import include_exclude_check, stack_is_external, error_exit, get_yaml
4040
from stack.util import run_shell_command
41+
from stack import constants
4142

4243
docker = DockerClient()
4344

@@ -78,83 +79,98 @@ def make_container_build_env(dev_root_path: str, default_container_base_dir: str
7879

7980

8081
def process_container(build_context: BuildContext) -> bool:
81-
log_info(f"Building: {build_context.container.name}:stack")
8282

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})
8691

8792
build_dir = None
8893
build_script_filename = None
8994

9095
# Check if this is in an external stack
9196
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)
94104
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
96106
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}")
98112
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("/", "-"))
100114
temp_build_script_filename = temp_build_dir.joinpath("build.sh")
101115
# Now check if the container exists in the external stack.
116+
log_debug(f"Looking for build script at: {temp_build_script_filename}")
102117
if not temp_build_script_filename.exists():
103118
# If not, revert to building an internal container
119+
# DBDB Why?
104120
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("/", "-"))
106122
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+
108125
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("/", "-"))
110127
build_script_filename = build_dir.joinpath("build.sh")
111128

112129
log_debug(f"Build script filename: {build_script_filename}")
130+
log_debug(f"Build script filename: {build_dir}")
113131

114132
if os.path.exists(build_script_filename):
115133
build_command = build_script_filename.as_posix()
116134
else:
117135
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)
120138
else:
121139
repo_full_path = build_context.stack.repo_path
122140

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)
125143
repo_dir_or_build_dir = repo_full_path if repo_full_path and repo_full_path.exists() else build_dir
126144
build_command = (
127145
os.path.join(build_context.default_container_base_dir, "default-build.sh")
128146
+ f" {default_container_tag} {repo_dir_or_build_dir}"
129147
)
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
131149

150+
build_envs["STACK_IMAGE_NAME"] = building_container.name
132151

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"]
138155

139156
if not opts.o.dry_run:
140157
# 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}")
144161

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)
146163

147-
log_debug(f"Return code is: {build_result}")
164+
log_debug(f"Build command return code is: {build_result}")
148165
if build_result != 0:
149166
return False
150167
else:
151168
return True
152169
else:
153-
log_info("Skipped")
170+
log_info("Skipped for dry run")
154171
return True
155172

156173

157-
158174
def build_containers(parent_stack,
159175
build_policy=get_config_setting("build-policy", BUILD_POLICIES[0]),
160176
image_registry=get_config_setting("image-registry"),
@@ -337,6 +353,7 @@ def build_containers(parent_stack,
337353
log_info(f"Container {container_tag} needs to be built.")
338354
container_needs_pulled = False
339355
container_needs_built = True
356+
# DBDB add comment explaining what this code below is doing.
340357
if not os.path.exists(target_fs_repo_path) or (git_pull and target_fs_repo_path not in dont_pull_repo_fs_paths):
341358
reconstructed_ref = f"{container_spec.ref.split('@')[0]}@{target_hash}"
342359
process_repo(git_pull, False, git_ssh, dev_root_path, [], reconstructed_ref)
@@ -373,7 +390,6 @@ def build_containers(parent_stack,
373390
except:
374391
pass
375392

376-
log_info(f"Building {container_spec.name}")
377393
result = process_container(build_context)
378394
if result:
379395
container_was_built = True

src/stack/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
config_key = "config"
2727
configmaps_key = "configmaps"
2828
container_lock_file_name = "container.lock"
29+
containers_directory_name = "containers"
2930
container_file_name = "container.yml"
3031
cluster_issuer_key = "cluster-issuer"
3132
deploy_to_key = "deploy-to"
@@ -56,6 +57,7 @@
5657
services_key = "services"
5758
spec_file_name = "spec.yml"
5859
stack_file_name = "stack.yml"
60+
stack_files_directory_name = "stack-files"
5961
stack_key = "stack"
6062
stacks_key = "stacks"
6163
volumes_key = "volumes"

0 commit comments

Comments
 (0)