-
Notifications
You must be signed in to change notification settings - Fork 44
/
Makefile.settings
112 lines (94 loc) · 5.19 KB
/
Makefile.settings
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
111
112
# Docker Compose Files
TEST_COMPOSE_FILE := docker/test/docker-compose.yml
RELEASE_COMPOSE_FILE := docker/release/docker-compose.yml
# Docker Compose Project Names
RELEASE_PROJECT := $(PROJECT_NAME)$(BUILD_ID)
TEST_PROJECT := $(RELEASE_PROJECT)test
# Output directory for test reports
REPORTS_DIR ?= reports
# Use these settings to specify a custom Docker registry
DOCKER_REGISTRY ?= docker.io
# WARNING: Set DOCKER_REGISTRY_AUTH to empty for Docker Hub
# Set DOCKER_REGISTRY_AUTH to auth endpoint for private Docker registry
DOCKER_REGISTRY_AUTH ?=
# Set shell
SHELL=/bin/bash -e -o pipefail
# App version settings
COMMIT_TIMESTAMP := $(shell echo $$(git log -1 --pretty='format:%cd' --date='format:%Y%m%d%H%M%S'))
COMMIT_HASH := $(shell echo $$(git rev-parse --short HEAD))
COMMIT_TAG := $(shell echo $$(git tag --points-at HEAD))
export APP_VERSION ?= $(COMMIT_TIMESTAMP).$(COMMIT_HASH)$(if $(BUILD_ID),.$(BUILD_ID),)
# Docker host settings
DOCKER_HOST_IP := $(shell echo $$DOCKER_HOST | awk -F/ '{printf $$3}' | awk -F: '{printf $$1}')
DOCKER_HOST_IP := $(if $(DOCKER_HOST_IP),$(DOCKER_HOST_IP),localhost)
# Cosmetics
RED := "\e[1;31m"
YELLOW := "\e[1;33m"
NC := "\e[0m"
INFO := @bash -c 'printf $(YELLOW); echo "=> $$1"; printf $(NC)' MESSAGE
WARNING := @bash -c 'printf $(RED); echo "WARNING: $$1"; printf $(NC)' MESSAGE
# Arguments
# Use /nopull to disable image pulls
# Use /verbose to enable verbose output
NOPULL_ARG = $(findstring /nopull,$(ARGS))
VERBOSE_ARG = $(findstring /verbose,$(ARGS))
RELEASE_ARGS = -p $(RELEASE_PROJECT) -f $(RELEASE_COMPOSE_FILE) $(VERBOSE_FLAG)
TEST_ARGS = -p $(TEST_PROJECT) -f $(TEST_COMPOSE_FILE) $(VERBOSE_FLAG)
VERBOSE_FLAG = $(if $(VERBOSE_ARG),--verbose,)
NOPULL_FLAG = $(if $(NOPULL_ARG),,--pull)
# Extract extra arguments
ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
# Default Tags
TAGS ?= $(if $(ARGS),$(ARGS),latest $(APP_VERSION) $(COMMIT_HASH) $(COMMIT_TAG))
# Image and Repository Tag introspection functions
# Syntax: $(call get_image_id,<docker-compose-environment>,<service-name>)
# Syntax: $(call get_repo_tags,<docker-compose-environment>,<service-name>,<fully-qualified-image-name>)
get_container_id = $$(docker-compose $(1) ps -q $(2) 2>/dev/null)
get_container_state = $$(echo $(call get_container_id,$(1),$(2)) | xargs -I ID docker inspect -f '{{ $(3) }}' ID)
get_image_id = $$(echo $(call get_container_id,$(1),$(2)) | xargs -I ARGS docker inspect -f '{{ .Image }}' ARGS)
filter_repo_tags = $(if $(findstring docker.io,$(1)),$(subst docker.io/,,$(1))[^[:space:]|\$$]*,$(1)[^[:space:]|\$$]*)
get_repo_tags = $$(echo $(call get_image_id,$(1),$(2)) | xargs -I ID docker inspect -f '{{range .RepoTags}}{{.}} {{end}}' ID | grep -oh "$(call filter_repo_tags,$(3))" | xargs)
# Port introspection functions
# Syntax: $(call get_port_mapping,<docker-compose-environment>,<service-name>,<internal-port>)
get_raw_port_mapping = $$(docker-compose $(1) ps -q $(2) 2>/dev/null | xargs -I ID docker port ID $(3))
get_port_mapping = $$(echo $$(IFS=':' read -r -a array <<< "$(call get_raw_port_mapping,$(1),$(2),$(3))" && echo "$${array[1]}"))
# Service health functions
# Syntax: $(call check_service_health,<docker-compose-environment>,<service-name>)
get_service_health = $$(echo $(call get_container_state,$(1),$(2),.State.Health.Status))
get_service_running = $$(echo $(call get_container_state,$(1),$(2),.State.Running))
check_service_health = { \
until [[ ($(call get_service_health,$(1),$(2)) != starting) \
|| ($(call get_service_running,$(1),$(2)) = false) ]]; \
do sleep 1; \
done; \
if [[ $(call get_service_health,$(1),$(2)) != healthy ]]; \
then echo $(2) failed health check; exit 1; \
fi; \
}
# Tagging and Publishing functions
# Syntax: $(call tag_image,<docker-compose-environment>,<service-name>,<fully-qualified-tag>)
# Syntax: $(call publish_image,<docker-compose-environment>,<service-name>,<fully-qualified-repository>)
tag_image = $$(echo $(call get_image_id,$(1),$(2)) | xargs -I ARG docker tag ARG $(3);)
publish_image = { \
for tag in $(call get_repo_tags,$(1),$(2),$(3)); \
do echo $$tag | xargs -I TAG docker push TAG; \
done; \
}
# AWS assume role settings
# Attempts to assume IAM role using STS
# Syntax: $(call assume_role,<role-arn>)
get_assume_session = aws sts assume-role --role-arn=$(1) --role-session-name=admin
get_assume_credential = jq --null-input '$(1)' | jq .Credentials.$(2) -r
define assume_role
$(eval AWS_SESSION = $(shell $(call get_assume_session,$(1))))
$(eval export AWS_ACCESS_KEY_ID = $(shell $(call get_assume_credential,$(AWS_SESSION),AccessKeyId)))
$(eval export AWS_SECRET_ACCESS_KEY = $(shell $(call get_assume_credential,$(AWS_SESSION),SecretAccessKey)))
$(eval export AWS_SESSION_TOKEN = $(shell $(call get_assume_credential,$(AWS_SESSION),SessionToken)))
endef
# Exit code function
# Syntax: $(call get_exit_code,<docker-compose-environment>,<service-name>)
get_exit_code = $$(docker-compose $(1) ps -q $(2) 2>/dev/null | xargs -I ID docker inspect -f "{{ .State.ExitCode }}" ID)
check_exit_code = exit $(call get_exit_code,$(1),$(2))
# Dangling image function
# Syntax: $(call clean_dangling_images,<repository>)
clean_dangling_images = docker images -q -f dangling=true -f label=application=$(1) | xargs -I ARGS docker rmi -f ARGS || true