Skip to content

Commit 743c047

Browse files
authored
Use installed packages, segregate executables by platform (#375)
1 parent 5c3201c commit 743c047

File tree

5 files changed

+66
-21
lines changed

5 files changed

+66
-21
lines changed

Makefile

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export BUILD_HARNESS_EXTENSIONS_PATH ?= $(BUILD_HARNESS_PATH)/../build-harness-e
1616
export BUILD_HARNESS_OS ?= $(OS)
1717
export BUILD_HARNESS_ARCH ?= $(shell uname -m | sed 's/x86_64/amd64/g')
1818
export SELF ?= $(MAKE)
19-
export PATH := $(BUILD_HARNESS_PATH)/vendor:$(PATH)
2019

2120
# Forces auto-init off to avoid invoking the macro on recursive $(MAKE)
2221
export BUILD_HARNESS_AUTO_INIT := false
@@ -61,6 +60,29 @@ ifneq ($(wildcard $(BUILD_HARNESS_EXTENSIONS_PATH)/modules/*/Makefile*),)
6160
-include $(BUILD_HARNESS_EXTENSIONS_PATH)/modules/*/Makefile*
6261
endif
6362

63+
# Unless PACKAGES_PREFER_HOST is not "false", add the INSTALL_PATH, which
64+
# is where build-harness installs needed tools, to the PATH, but wait
65+
# until it is set, which may not be the first time through this Makefile.
66+
# There is an incredibly subtle behavior here. Changes to PATH do not
67+
# affect `make` itself, so $(shell ...) will not see the new PATH.
68+
# Even more subtle, simple recipes that do not require a subshell,
69+
# such as `kubectl version`, will NOT see the new PATH. To use binaries
70+
# installed in the INSTALL_PATH, you must use a recipe that forces a subshell,
71+
# such as by using a pipe or compound command, or if nothing else is needed,
72+
# using a no-op command such as `: && kubectl version`.
73+
# To make things even more subtle, this is inconsistent across different
74+
# versions of Gnu make, with disagreement about the correct behavior and
75+
# bugs in the implementation. The above behavior is what we have observed
76+
# with Gnu make 3.81, which is what Apple ships with macOS. Gnu make 4.4.1
77+
# updates PATH everywhere. We suspect some versions in between update the
78+
# PATH for recipes but not for $(shell ...).
79+
# See:
80+
# - https://savannah.gnu.org/bugs/?10593#comment5
81+
# - https://savannah.gnu.org/bugs/?56834
82+
ifneq ($(INSTALL_PATH),)
83+
export PATH := $(if $(subst false,,$(PACKAGES_PREFER_HOST)),$(PATH),$(INSTALL_PATH):$(PATH))
84+
endif
85+
6486
# For backwards compatibility with all of our other projects that use build-harness
6587
init::
6688
exit 0

modules/docs/Makefile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ docs/targets.md: docs/deps
1717
.PHONY : docs/terraform.md
1818
## Update `docs/terraform.md` from `terraform-docs`
1919
docs/terraform.md: docs/deps packages/install/terraform-docs
20-
@echo "<!-- markdownlint-disable -->" > $@
21-
@terraform-docs md . >> $@
22-
@echo "<!-- markdownlint-restore -->" >> $@
20+
@echo "<!-- markdownlint-disable -->" > $@ ; \
21+
terraform-docs md . >> $@ ; \
22+
echo "<!-- markdownlint-restore -->" >> $@
2323

2424
.PHONY : docs/github-action.md
2525
## Update `docs/github-action.md` from `action.yaml`
2626
docs/github-action.md: docs/deps packages/install/gomplate
27-
@echo "<!-- markdownlint-disable -->" > $@
28-
@gomplate -d action=./action.yml -f $(BUILD_HARNESS_PATH)/templates/docs-github-action.gotmpl --config $(BUILD_HARNESS_PATH)/configs/gomplate.yaml >> $@
29-
@echo "<!-- markdownlint-restore -->" >> $@
27+
@echo "<!-- markdownlint-disable -->" > $@ ; \
28+
gomplate -d action=./action.yml -f $(BUILD_HARNESS_PATH)/templates/docs-github-action.gotmpl --config $(BUILD_HARNESS_PATH)/configs/gomplate.yaml >> $@ ; \
29+
echo "<!-- markdownlint-restore -->" >> $@
3030

3131
.PHONY : docs/github-actions-reusable-workflows.md
3232
## Update `docs/github-actions-reusable-workflows.md` from `.github/workflows/*.yaml`

modules/packages/Makefile

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,55 @@
1-
export INSTALL_PATH ?= $(BUILD_HARNESS_PATH)/vendor
1+
export VENDOR_DIR ?= $(BUILD_HARNESS_PATH)/vendor
2+
export VENDOR_SUBDIR := $(shell uname -s)/$(shell uname -m)
3+
export INSTALL_PATH ?= $(VENDOR_DIR)/$(VENDOR_SUBDIR)
24
export PACKAGES_VERSION ?= master
3-
export PACKAGES_PATH ?= $(BUILD_HARNESS_PATH)/vendor/packages
5+
export PACKAGES_PATH ?= $(VENDOR_DIR)/packages
6+
# PACKAGES_PREFER_HOST is used to force the use of the host's tools
7+
# rather than the tools installed by build-harness in the git repo tree.
8+
# This is to guard against the possibility that a malicious PR could install
9+
# a compromised version of a tool that would be used by subsequent CI runs.
410
export PACKAGES_PREFER_HOST ?= false
511

612
## Delete packages
713
packages/delete:
8-
rm -rf $(PACKAGES_PATH)
14+
@# Do some checking to guard against running something like `rm -rf /` by mistake.
15+
@# Check if packages is a subdirectory of build-harness and is a valid directory before deleting it.
16+
@# Also, do not delete it if PRESERVE_PACKAGES is not empty.
17+
@# Use realpath to resolve symlinks and relative paths and compare the actual paths.
18+
@# Do not use realpath with [ -d ] because it returns an empty string if the path does not exist.
19+
@if [ -n "$(findstring $(realpath $(BUILD_HARNESS_PATH)),$(realpath $(PACKAGES_PATH)))" ] \
20+
&& [ ! "$(realpath $(BUILD_HARNESS_PATH))" = "$(realpath $(PACKAGES_PATH))" ] \
21+
&& [ -d "$(PACKAGES_PATH)" ] && [ -z "$(PRESERVE_PACKAGES)" ]; then \
22+
printf "* Removing existing packages cache under %s ...\n" "$(realpath $(PACKAGES_PATH))"; \
23+
rm -rf "$(realpath $(PACKAGES_PATH))"; \
24+
fi
925

1026
## Reinstall packages
1127
packages/reinstall: packages/delete packages/install
1228
@exit 0
1329

30+
# Set PRESERVE_PACKAGES to a non-empty value to preserve the packages cache if it is less than a day old
31+
packages/install: PRESERVE_PACKAGES ?= $(shell [ -d "$(PACKAGES_PATH)" ] && find "$(PACKAGES_PATH)" -maxdepth 0 -mtime 0)
1432
## Install packages
15-
packages/install:
16-
@if [ ! -d $(PACKAGES_PATH) ]; then \
33+
packages/install: packages/delete
34+
@if [ ! -d "$(PACKAGES_PATH)" ]; then \
1735
echo "* Installing packages $(PACKAGES_VERSION)..."; \
18-
rm -rf $(PACKAGES_PATH); \
19-
$(GIT) clone -c advice.detachedHead=false --depth=1 -b $(PACKAGES_VERSION) https://github.com/cloudposse/packages.git $(PACKAGES_PATH); \
20-
rm -rf $(PACKAGES_PATH)/.git; \
36+
$(GIT) clone -c advice.detachedHead=false --depth=1 -b $(PACKAGES_VERSION) https://github.com/cloudposse/packages.git "$(PACKAGES_PATH)"; \
37+
rm -rf "$(realpath $(PACKAGES_PATH))"/.git; \
2138
fi
2239

2340
## Install package (e.g. helm, helmfile, kubectl)
2441
packages/install/%:
2542
@binary="$*"; \
26-
if [ -x "$(INSTALL_PATH)/$$binary" ]; then \
43+
if [ "$(PACKAGES_PREFER_HOST)" = "true" ]; then \
44+
if installed=$$(command -v $* 2>/dev/null); then \
45+
echo Using "$*" from "$$installed" ; \
46+
else \
47+
echo "* Package $$binary not found on the host" >&2; \
48+
echo "* NOT Installing $* because PACKAGES_PREFER_HOST is true" >&2; \
49+
exit 1; \
50+
fi; \
51+
elif [ -x "$(INSTALL_PATH)/$$binary" ]; then \
2752
echo "* Package $$binary already installed"; \
28-
elif [ "$(PACKAGES_PREFER_HOST)" = "true" ] && installed=$$(command -v $* 2>/dev/null); then \
29-
echo Using "$*" from "$$installed" ; \
3053
else \
3154
$(MAKE) packages/install && \
3255
echo "* Installing $* to $(INSTALL_PATH)" && \

modules/readme/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ readme/lint:
6565

6666
## Create README.md by building it from README.yaml
6767
readme/build: readme/deps $(README_TEMPLATE_FILE) $(README_DEPS)
68-
@gomplate --file $(README_TEMPLATE_FILE) --out $(README_FILE) --config $(BUILD_HARNESS_PATH)/configs/gomplate.yaml
69-
@echo "Generated $(README_FILE) from $(README_TEMPLATE_FILE) using data from $(README_TEMPLATE_YAML)"
68+
@gomplate --file $(README_TEMPLATE_FILE) --out $(README_FILE) --config $(BUILD_HARNESS_PATH)/configs/gomplate.yaml && \
69+
echo "Generated $(README_FILE) from $(README_TEMPLATE_FILE) using data from $(README_TEMPLATE_YAML)"
7070

7171
readme/generate-related-references:
7272
@$(BUILD_HARNESS_PATH)/bin/generate_related_references.py

templates/Makefile.build-harness

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ precommit/terraform pr/auto-format precommit/terraform/host pr/auto-format/host:
160160
pr/readme pr/readme/host: ARGS := readme/deps readme
161161
pr/github-update pr/github-update/host: ARGS := github/update
162162
precommit/terraform pr/auto-format pr/readme pr/github-update: build-harness/runner
163-
precommit/terraform/host pr/auto-format/host pr/readme/host pr/github-update/host: git-safe-directgory
163+
precommit/terraform/host pr/auto-format/host pr/readme/host pr/github-update/host: git-safe-directory
164164
$(MAKE) $(ARGS)
165165

166166
pr/pre-commit: ARGS := pre-commit/run

0 commit comments

Comments
 (0)