|
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) |
2 | 4 | 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. |
4 | 10 | export PACKAGES_PREFER_HOST ?= false |
5 | 11 |
|
6 | 12 | ## Delete packages |
7 | 13 | 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 |
9 | 25 |
|
10 | 26 | ## Reinstall packages |
11 | 27 | packages/reinstall: packages/delete packages/install |
12 | 28 | @exit 0 |
13 | 29 |
|
| 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) |
14 | 32 | ## Install packages |
15 | | -packages/install: |
16 | | - @if [ ! -d $(PACKAGES_PATH) ]; then \ |
| 33 | +packages/install: packages/delete |
| 34 | + @if [ ! -d "$(PACKAGES_PATH)" ]; then \ |
17 | 35 | 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; \ |
21 | 38 | fi |
22 | 39 |
|
23 | 40 | ## Install package (e.g. helm, helmfile, kubectl) |
24 | 41 | packages/install/%: |
25 | 42 | @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 \ |
27 | 52 | echo "* Package $$binary already installed"; \ |
28 | | - elif [ "$(PACKAGES_PREFER_HOST)" = "true" ] && installed=$$(command -v $* 2>/dev/null); then \ |
29 | | - echo Using "$*" from "$$installed" ; \ |
30 | 53 | else \ |
31 | 54 | $(MAKE) packages/install && \ |
32 | 55 | echo "* Installing $* to $(INSTALL_PATH)" && \ |
|
0 commit comments