forked from docker-production-aws/microtrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile.settings
106 lines (88 loc) · 4.77 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
#### STANDARD SETTINGS ####
# Filenames
TEST_COMPOSE_FILE := docker/test/docker-compose.yml
REL_COMPOSE_FILE := docker/release/docker-compose.yml
# Docker Compose Project Names
REL_PROJECT := $(PROJECT_NAME)$(BUILD_ID)
TEST_PROJECT := $(REL_PROJECT)test
# 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 ?=
# Expression to login to Docker registry
DOCKER_LOGIN_EXPRESSION ?= docker login -u $$DOCKER_USER -p $$DOCKER_PASSWORD $(DOCKER_REGISTRY_AUTH)
# Arguments
NOPULL_ARG = $(findstring :nopull,$(ARGS))
VERBOSE_ARG = $(findstring :verbose,$(ARGS))
RELEASE_ARGS = -p $(REL_PROJECT) -f $(REL_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)
# 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' 2>/dev/null))
COMMIT_ID := $(shell echo $$(git rev-parse --short HEAD 2>/dev/null))
COMMIT_TAG := $(shell echo $$(git tag --points-at HEAD 2>/dev/null))
export APP_VERSION ?= $(COMMIT_TIMESTAMP).$(COMMIT_ID)$(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
# Extract extra arguments
ifeq ($(firstword $(MAKECMDGOALS)),$(filter $(firstword $(MAKECMDGOALS)),release test clean compose dcompose))
ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
endif
# Extract tag arguments
ifeq ($(firstword $(MAKECMDGOALS)),$(filter $(firstword $(MAKECMDGOALS)),tag))
TAG_ARGS_RAW := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
ifeq ($(TAG_ARGS_RAW),)
$(error You must specify a tag)
endif
TAG_ARGS += $(foreach tag,$(TAG_ARGS_RAW),$(shell echo $(tag) | sed -e 's/[^a-zA-Z0-9\.\-]/_/g'))
endif
# 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))
get_image_id = $$(echo $(call get_container_id,$(1),$(2)) | xargs -I ARGS docker inspect -f '{{ .Image }}' ARGS)
get_container_state = $$(echo $(call get_container_id,$(1),$(2)) | xargs -I ID docker inspect -f '$(3)' ID)
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) | 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),{{if .State.Running}}{{ .State.Health.Status }}{{end}}))
check_service_health = { \
until [[ $(call get_service_health,$(1),$(2)) != starting ]]; \
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; \
}
# Exit code function
# Syntax: $(call get_exit_code,<docker-compose-environment>,<service-name>)
get_exit_code = $$(echo $(call get_container_state,$(1),$(2),{{ .State.ExitCode }}))
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