Skip to content

Commit d1ae062

Browse files
committed
Improve default build to support path/ from the stack container definition.
1 parent 1a8b01c commit d1ae062

3 files changed

Lines changed: 24 additions & 12 deletions

File tree

src/stack/build/build_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
class BuildContext:
2626
stack: str
2727
container: ContainerSpec
28-
container_build_dir: Path
28+
default_container_base_dir: Path
2929
container_build_env: Mapping[str,str]
3030
dev_root_path: str
3131

src/stack/build/build_util.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,27 @@ class ContainerSpec:
5050
name: str
5151
ref: str
5252
build: str
53+
path: str
5354
file_path: str
5455

55-
def __init__(self, name: str=None, ref=None, build=None):
56+
def __init__(self, name: str=None, ref=None, build=None, path=None):
5657
self.name = name
5758
self.ref = ref
5859
self.build = build
60+
self.path = path
5961
self.file_path = None
6062

6163
def __repr__(self):
6264
return str(self)
6365

6466
def __str__(self):
65-
ret = { "name": self.name, "ref": self.ref, "build": self.build, "file_path": self.file_path }
67+
ret = { "name": self.name, "ref": self.ref, "build": self.build, "path": self.path, "file_path": self.file_path }
6668
return json.dumps(ret)
6769

6870
def init_from_file(self, file_path: Path):
69-
self.file_path = file_path
71+
self.file_path = Path(file_path).as_posix()
72+
self.path = Path(self.file_path).parent.as_posix()
73+
7074
y = get_yaml().load(open(file_path, "r"))
7175
self.name = y["container"]["name"]
7276
self.ref = y["container"].get("ref")

src/stack/build/prepare_containers.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@
5454
# epilog="Config provided either in .env or settings.ini or env vars: BPI_REPO_BASE_DIR (defaults to ~/bpi)"
5555

5656

57-
def make_container_build_env(dev_root_path: str, container_build_dir: str, debug: bool, force_rebuild: bool, extra_build_args: str):
57+
def make_container_build_env(dev_root_path: str, default_container_base_dir: str, debug: bool, force_rebuild: bool, extra_build_args: str):
5858
container_build_env = {
5959
"BPI_NPM_REGISTRY_URL": get_npm_registry_url(),
6060
"BPI_GO_AUTH_TOKEN": config("BPI_GO_AUTH_TOKEN", default=""),
6161
"BPI_NPM_AUTH_TOKEN": config("BPI_NPM_AUTH_TOKEN", default=""),
6262
"BPI_REPO_BASE_DIR": dev_root_path,
63-
"BPI_CONTAINER_BASE_DIR": container_build_dir,
63+
"BPI_CONTAINER_BASE_DIR": default_container_base_dir,
6464
"BPI_HOST_UID": f"{os.getuid()}",
6565
"BPI_HOST_GID": f"{os.getgid()}",
6666
"BPI_IMAGE_LOCAL_TAG": "stack",
@@ -99,11 +99,11 @@ def process_container(build_context: BuildContext) -> bool:
9999
# Now check if the container exists in the external stack.
100100
if not temp_build_script_filename.exists():
101101
# If not, revert to building an internal container
102-
container_build_script_dir = build_context.container_build_dir
102+
container_build_script_dir = build_context.default_container_base_dir
103103
build_dir = container_build_script_dir.joinpath(build_context.container.name.replace("/", "-"))
104104
build_script_filename = build_dir.joinpath("build.sh")
105105
if not build_dir:
106-
build_dir = build_context.container_build_dir.joinpath(build_context.container.name.replace("/", "-"))
106+
build_dir = build_context.default_container_base_dir.joinpath(build_context.container.name.replace("/", "-"))
107107
build_script_filename = build_dir.joinpath("build.sh")
108108

109109
if opts.o.verbose:
@@ -117,9 +117,11 @@ def process_container(build_context: BuildContext) -> bool:
117117
# TODO: make this less of a hack -- should be specified in some metadata somewhere
118118
# Check if we have a repo for this container. If not, set the context dir to the container-build subdir
119119
repo_full_path = os.path.join(build_context.dev_root_path, repo_dir)
120+
if build_context.container.path:
121+
repo_full_path = os.path.join(repo_full_path, build_context.container.path)
120122
repo_dir_or_build_dir = repo_full_path if os.path.exists(repo_full_path) else build_dir
121123
build_command = (
122-
os.path.join(build_context.container_build_dir, "default-build.sh")
124+
os.path.join(build_context.default_container_base_dir, "default-build.sh")
123125
+ f" {default_container_tag} {repo_dir_or_build_dir}"
124126
)
125127

@@ -162,7 +164,7 @@ def command(ctx, stack, include, exclude, git_ssh, build_policy, extra_build_arg
162164
error_exit(f"{build_policy} is not one of {BUILD_POLICIES}")
163165

164166
# See: https://stackoverflow.com/questions/25389095/python-get-path-of-root-project-structure
165-
container_build_dir = Path(__file__).absolute().parent.parent.joinpath("data", "container-build")
167+
default_container_base_dir = Path(__file__).absolute().parent.parent.joinpath("data", "container-build")
166168

167169
dev_root_path = get_dev_root_path(ctx)
168170

@@ -180,7 +182,7 @@ def command(ctx, stack, include, exclude, git_ssh, build_policy, extra_build_arg
180182

181183

182184
container_build_env = make_container_build_env(
183-
dev_root_path, container_build_dir, opts.o.debug, "build-force" == build_policy, extra_build_args
185+
dev_root_path, default_container_base_dir, opts.o.debug, "build-force" == build_policy, extra_build_args
184186
)
185187

186188
# check if we have any repos that specify the container targets / build info
@@ -207,6 +209,11 @@ def command(ctx, stack, include, exclude, git_ssh, build_policy, extra_build_arg
207209
print(f"Error: Missing container repo for {fs_path_for_container_specs}, run fetch repositories")
208210
sys.exit(1)
209211

212+
if stack_container.path:
213+
container_spec.path = Path(os.path.join(fs_path_for_container_specs, stack_container.path)).absolute().as_posix()
214+
else:
215+
container_spec.path = fs_path_for_container_specs.absolute().as_posix()
216+
210217
image_registries_to_check = [r for r in [image_registry, image_registry_for_repo(stack_container.ref)] if r]
211218

212219
container_spec_yml_path = os.path.join(fs_path_for_container_specs, container_file_name)
@@ -310,7 +317,8 @@ def command(ctx, stack, include, exclude, git_ssh, build_policy, extra_build_arg
310317
elif container_needs_built:
311318
if build_policy in ["prebuilt", "prebuilt-local", "prebuilt-remote"]:
312319
error_exit(f"No prebuilt image available for: {container_spec.name}")
313-
build_context = BuildContext(stack, container_spec, container_build_dir, container_build_env, dev_root_path)
320+
321+
build_context = BuildContext(stack, container_spec, default_container_base_dir, container_build_env, dev_root_path)
314322

315323
for tag in [stack_legacy_tag, stack_local_tag, container_tag]:
316324
try:

0 commit comments

Comments
 (0)