-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
110 lines (97 loc) · 3.48 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
PROVIDER ?= cloudformation
DOCKER_CMD := $(shell docker buildx version >/dev/null 2>&1 && echo "buildx" || echo "build")
GIT_REMOTE := $(shell git remote get-url origin | sed -e 's/.*[\/:]\([^/]*\/[^/]*\)\.git/\1/')
DOCKER_REGISTRY ?= ghcr.io
IMAGE_REPO ?= $(DOCKER_REGISTRY)/$(GIT_REMOTE)
GIT_SHA := $(shell git rev-parse --short HEAD)
IMAGE_TAG ?= $(GIT_SHA)
IMAGE_NAME ?= $(IMAGE_REPO):$(IMAGE_TAG)
CONTAINER_BASE_NAME ?= test-kitchen
CONTAINER_NAME ?= $(CONTAINER_BASE_NAME)-$(PROVIDER)
UID := $(shell id -u)
GID := $(shell id -g)
DOCKER_COMMAND ?= /bin/bash
OBSERVE_CUSTOMER := $(OBSERVE_CUSTOMER)
OBSERVE_TOKEN := $(OBSERVE_TOKEN)
OBSERVE_DOMAIN ?= observe-eng.com
AWS_ACCESS_KEY_ID := $(AWS_ACCESS_KEY_ID)
AWS_SECRET_ACCESS_KEY := $(AWS_SECRET_ACCESS_KEY)
AWS_SESSION_TOKEN := $(AWS_SESSION_TOKEN)
AWS_REGION ?= us-east-1
debug:
@echo "${GIT_REMOTE}"
.PHONY: docker/clean
docker/clean: docker/test/clean
docker kill $(CONTAINER_NAME) || true
docker rm $(CONTAINER_NAME) || true
docker rmi -f $(IMAGE_NAME) || true
.PHONY: docker/build
docker/build:
@if [ -z "$(shell docker images -q $(IMAGE_NAME))" ]; then \
echo "Image does not exist. Building..."; \
if [ "$(DOCKER_CMD)" = "buildx" ]; then \
git_branch=$$(git rev-parse --abbrev-ref HEAD | sed 's/\//-/g') && \
docker buildx build --load \
--cache-from=$(IMAGE_REPO):latest \
--cache-from=$(IMAGE_REPO):main \
--cache-from=$(IMAGE_REPO):$$git_branch \
--build-arg UID=$(UID) \
--build-arg GID=$(GID) \
-t $(IMAGE_NAME) .; \
else \
docker build --build-arg UID=$(UID) --build-arg GID=$(GID) -t $(IMAGE_NAME) .; \
fi; \
else \
echo "Image already exists. Skipping build."; \
fi
.PHONY: docker/%
docker/%:
$(MAKE) docker/run DOCKER_COMMAND="make $*"
.PHONY: docker/run
docker/run: docker/build
@if [ -z "`docker ps -q -f name=$(CONTAINER_NAME)`" ]; then \
echo "Container is not running. Starting a new one."; \
if [ "$$CI" = "true" ]; then \
DOCKER_FLAGS="--rm"; \
else \
DOCKER_FLAGS="-it --rm"; \
fi; \
docker run $$DOCKER_FLAGS \
--name $(CONTAINER_NAME) \
-e PROVIDER=$(PROVIDER) \
-e AWS_REGION=$(AWS_REGION) \
-e USER=$(USER) \
-e OBSERVE_CUSTOMER=$(OBSERVE_CUSTOMER) \
-e OBSERVE_TOKEN=$(OBSERVE_TOKEN) \
-e OBSERVE_DOMAIN=$(OBSERVE_DOMAIN) \
-e AWS_ACCESS_KEY_ID=$(AWS_ACCESS_KEY_ID) \
-e AWS_SECRET_ACCESS_KEY=$(AWS_SECRET_ACCESS_KEY) \
$(if $(AWS_SESSION_TOKEN),-e AWS_SESSION_TOKEN=$(AWS_SESSION_TOKEN),) \
-v $(PWD):/workdir \
-u $(UID):$(GID) \
$(IMAGE_NAME) $(DOCKER_COMMAND); \
else \
echo "Container is already running. Executing command inside the container."; \
docker exec $(CONTAINER_NAME) $(DOCKER_COMMAND); \
fi
.PHONY: test
test: test/create test/verify
.PHONY: test/prerequisites
test/prerequisites:
./validate_deps.sh
@if [ -z "$(USER)" ] ; then echo "USER is not set."; exit 1; fi
@if [ -z "$(OBSERVE_CUSTOMER)" ] ; then echo "OBSERVE_CUSTOMER is not set."; exit 1; fi
@if [ -z "$(OBSERVE_TOKEN)" ] ; then echo "OBSERVE_TOKEN is not set."; exit 1; fi
@if [ -z "$(AWS_ACCESS_KEY_ID)" ] ; then echo "AWS_ACCESS_KEY_ID is not set."; exit 1; fi
@if [ -z "$(AWS_SECRET_ACCESS_KEY)" ] ; then echo "AWS_SECRET_ACCESS_KEY is not set."; exit 1; fi
.PHONY: test/create
test/create: test/prerequisites
kitchen create
.PHONY: test/verify
test/verify: test/create
kitchen verify
.PHONY: test/clean
test/clean: test/prerequisites
kitchen destroy || true
rm -rf .kitchen/* || true
aws logs delete-log-group --log-group-name /aws/lambda/spec-test-$(PROVIDER)-$(USER) || true