2021-02-09 17:05:00 +01:00
|
|
|
CMDS += docker-compose-exec docker-run docker-run-%
|
|
|
|
COMPOSE_FILE ?= $(wildcard docker/docker-compose.yml $(foreach file,$(patsubst docker/docker-compose.%,%,$(basename $(wildcard docker/docker-compose.*.yml))),$(if $(filter true,$(COMPOSE_FILE_$(file)) $(COMPOSE_FILE_$(call UPPERCASE,$(file)))),docker/docker-compose.$(file).yml)))
|
|
|
|
COMPOSE_FILE_$(ENV) ?= true
|
|
|
|
COMPOSE_FILE_DEBUG ?= $(DEBUG)
|
|
|
|
COMPOSE_FILE_NFS ?= $(MOUNT_NFS)
|
|
|
|
COMPOSE_FILE_SSH ?= true
|
|
|
|
ifneq ($(SUBREPO),)
|
|
|
|
COMPOSE_FILE_SUBREPO ?= true
|
|
|
|
else
|
|
|
|
COMPOSE_FILE_APP ?= true
|
|
|
|
endif
|
|
|
|
ifneq (,$(filter $(ENV),$(ENV_DEPLOY)))
|
|
|
|
COMPOSE_FILE_TMPFS ?= false
|
|
|
|
else
|
|
|
|
COMPOSE_FILE_TMPFS ?= true
|
|
|
|
endif
|
2021-05-31 20:52:49 +02:00
|
|
|
COMPOSE_IGNORE_ORPHANS ?= false
|
2021-02-09 17:05:00 +01:00
|
|
|
COMPOSE_PROJECT_NAME ?= $(USER)_$(ENV)_$(APP)
|
|
|
|
COMPOSE_SERVICE_NAME ?= $(subst _,-,$(COMPOSE_PROJECT_NAME))
|
2021-06-12 03:13:01 +02:00
|
|
|
CONTEXT += COMPOSE_FILE DOCKER_IMAGE_TAG DOCKER_REPOSITORY
|
2021-06-09 00:44:36 +02:00
|
|
|
CONTEXT_DEBUG += DOCKER_REGISTRY
|
2021-02-09 17:05:00 +01:00
|
|
|
DOCKER_BUILD_ARGS ?= $(if $(filter $(DOCKER_BUILD_NO_CACHE),true),--pull --no-cache) $(foreach var,$(DOCKER_BUILD_VARS),$(if $($(var)),--build-arg $(var)='$($(var))'))
|
|
|
|
DOCKER_BUILD_CACHE ?= true
|
|
|
|
DOCKER_BUILD_NO_CACHE ?= false
|
|
|
|
DOCKER_BUILD_TARGET ?= $(if $(filter $(ENV),$(DOCKER_BUILD_TARGETS)),$(ENV),$(DOCKER_BUILD_TARGET_DEFAULT))
|
|
|
|
DOCKER_BUILD_TARGET_DEFAULT ?= local
|
|
|
|
DOCKER_BUILD_TARGETS ?= local debug tests preprod prod
|
|
|
|
DOCKER_BUILD_VARS ?= APP BRANCH DOCKER_GID DOCKER_REPOSITORY GID GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME TARGET UID USER VERSION
|
|
|
|
DOCKER_COMPOSE_DOWN_OPTIONS ?=
|
|
|
|
DOCKER_COMPOSE_UP_OPTIONS ?= -d
|
2021-05-17 15:28:24 +02:00
|
|
|
DOCKER_GID ?= $(call gid,docker)
|
2021-05-22 02:34:24 +02:00
|
|
|
DOCKER_IMAGE_TAG ?= $(if $(filter $(ENV),$(ENV_DEPLOY)),$(VERSION),$(if $(DRONE_BUILD_NUMBER),$(DRONE_BUILD_NUMBER),latest))
|
2021-02-09 17:05:00 +01:00
|
|
|
DOCKER_IMAGES ?= $(patsubst %/,%,$(patsubst docker/%,%,$(dir $(wildcard docker/*/Dockerfile))))
|
|
|
|
DOCKER_PLUGIN ?= rexray/s3fs:latest
|
|
|
|
DOCKER_PLUGIN_ARGS ?= $(foreach var,$(DOCKER_PLUGIN_VARS),$(if $(DOCKER_PLUGIN_$(var)),$(var)='$(DOCKER_PLUGIN_$(var))'))
|
|
|
|
DOCKER_PLUGIN_OPTIONS ?= --grant-all-permissions
|
2021-06-12 03:13:01 +02:00
|
|
|
DOCKER_PLUGIN_S3FS_ACCESSKEY ?= $(AWS_ACCESS_KEY_ID)
|
2021-02-09 17:05:00 +01:00
|
|
|
DOCKER_PLUGIN_S3FS_OPTIONS ?= allow_other,nonempty,use_path_request_style,url=https://s3-eu-west-1.amazonaws.com
|
2021-06-12 03:13:01 +02:00
|
|
|
DOCKER_PLUGIN_S3FS_SECRETKEY ?= $(AWS_SECRET_ACCESS_KEY)
|
2021-02-09 17:05:00 +01:00
|
|
|
DOCKER_PLUGIN_S3FS_REGION ?= eu-west-1
|
|
|
|
DOCKER_PLUGIN_VARS ?= S3FS_ACCESSKEY S3FS_OPTIONS S3FS_SECRETKEY S3FS_REGION
|
|
|
|
DOCKER_REGISTRY ?= registry
|
|
|
|
DOCKER_REGISTRY_USERNAME ?= $(USER)
|
|
|
|
DOCKER_REGISTRY_REPOSITORY ?= $(addsuffix /,$(DOCKER_REGISTRY))$(subst $(USER),$(DOCKER_REGISTRY_USERNAME),$(DOCKER_REPOSITORY))
|
|
|
|
DOCKER_REPOSITORY ?= $(subst _,/,$(COMPOSE_PROJECT_NAME))
|
2021-05-31 20:52:49 +02:00
|
|
|
DOCKER_SERVICE ?= $(shell $(call docker-compose,--log-level critical config --services) |tail -1)
|
2021-06-12 03:13:01 +02:00
|
|
|
DOCKER_SERVICES ?= $(eval DRYRUN_IGNORE := true) $(shell $(call docker-compose,--log-level critical config --services)) $(eval DRYRUN_IGNORE := false)
|
2021-02-09 17:05:00 +01:00
|
|
|
DOCKER_SHELL ?= $(SHELL)
|
2021-05-22 02:34:24 +02:00
|
|
|
ENV_VARS += COMPOSE_PROJECT_NAME COMPOSE_SERVICE_NAME DOCKER_BUILD_TARGET DOCKER_GID DOCKER_IMAGE_TAG DOCKER_REGISTRY DOCKER_REPOSITORY DOCKER_SHELL
|
2021-02-09 17:05:00 +01:00
|
|
|
|
|
|
|
# https://github.com/docker/libnetwork/pull/2348
|
|
|
|
ifeq ($(HOST_SYSTEM), DARWIN)
|
|
|
|
DOCKER_HOST_IFACE ?= $(shell docker run --rm -it --net=host alpine /sbin/ip -4 route list match 0/0 2>/dev/null |awk '{print $$5}' |awk '!seen[$$0]++' |head -1)
|
|
|
|
DOCKER_HOST_INET ?= $(shell docker run --rm -it --net=host alpine /sbin/ip -4 addr show $(DOCKER_HOST_IFACE) 2>/dev/null |awk '$$1 == "inet" {sub(/\/.*/,"",$$2); print $$2}')
|
|
|
|
DOCKER_INTERNAL_DOCKER_GATEWAY ?= $(shell docker run --rm -it alpine getent hosts gateway.docker.internal |awk '{print $$1}' |head -1)
|
|
|
|
DOCKER_INTERNAL_DOCKER_HOST ?= $(shell docker run --rm -it alpine getent hosts host.docker.internal |awk '{print $$1}' |head -1)
|
|
|
|
else
|
|
|
|
DOCKER_HOST_IFACE ?= $(shell /sbin/ip -4 route list match 0/0 2>/dev/null |awk '{print $$5}' |awk '!seen[$$0]++' |head -1)
|
|
|
|
DOCKER_HOST_INET ?= $(shell /sbin/ip -4 addr show $(DOCKER_HOST_IFACE) 2>/dev/null |awk '$$1 == "inet" {sub(/\/.*/,"",$$2); print $$2}')
|
|
|
|
DOCKER_INTERNAL_DOCKER_GATEWAY ?= $(shell /sbin/ip -4 route list match 0/0 2>/dev/null |awk '{print $$3}' |awk '!seen[$$0]++' |head -1)
|
|
|
|
DOCKER_INTERNAL_DOCKER_HOST ?= $(shell /sbin/ip addr show docker0 2>/dev/null |awk '$$1 == "inet" {sub(/\/.*/,"",$$2); print $$2}')
|
|
|
|
endif
|
|
|
|
|
|
|
|
ifeq ($(DRONE), true)
|
2021-05-17 15:28:24 +02:00
|
|
|
APP_PATH_PREFIX := $(DRONE_BUILD_NUMBER)
|
|
|
|
COMPOSE_PROJECT_NAME := $(USER)_$(ENV)$(APP_PATH_PREFIX)_$(APP)
|
2021-02-09 17:05:00 +01:00
|
|
|
COMPOSE_SERVICE_NAME := $(subst _,-,$(COMPOSE_PROJECT_NAME))
|
2021-05-22 02:34:24 +02:00
|
|
|
DOCKER_BUILD_CACHE := false
|
|
|
|
DOCKER_COMPOSE_DOWN_OPTIONS := --rmi all -v
|
|
|
|
DOCKER_COMPOSE_UP_OPTIONS := -d --build
|
2021-02-09 17:05:00 +01:00
|
|
|
DOCKER_REPOSITORY := $(USER)/$(ENV)/$(APP)
|
|
|
|
endif
|
|
|
|
|
|
|
|
ifeq ($(DOCKER), true)
|
|
|
|
|
2021-06-02 00:54:33 +02:00
|
|
|
# function docker-compose: Run docker-compose with arg 1
|
2021-02-09 17:05:00 +01:00
|
|
|
define docker-compose
|
2021-06-12 03:13:01 +02:00
|
|
|
$(call INFO,docker-compose,$(1))
|
2021-02-09 17:05:00 +01:00
|
|
|
$(call run,docker/compose:$(COMPOSE_VERSION) $(patsubst %,-f %,$(COMPOSE_FILE)) -p $(COMPOSE_PROJECT_NAME) $(1))
|
|
|
|
endef
|
2021-06-02 00:54:33 +02:00
|
|
|
# function docker-compose-exec: Run docker-compose-exec with arg 2 in service 1
|
2021-02-09 17:05:00 +01:00
|
|
|
define docker-compose-exec
|
2021-06-12 03:13:01 +02:00
|
|
|
$(call INFO,docker-compose-exec,$(1)$(comma) $(2))
|
2021-02-09 17:05:00 +01:00
|
|
|
$(call run,docker/compose:$(COMPOSE_VERSION) $(patsubst %,-f %,$(COMPOSE_FILE)) -p $(COMPOSE_PROJECT_NAME) exec -T $(1) sh -c '$(2)')
|
|
|
|
endef
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
SHELL := /bin/bash
|
|
|
|
define docker-compose
|
2021-06-12 03:13:01 +02:00
|
|
|
$(call INFO,docker-compose,$(1))
|
2021-02-09 17:05:00 +01:00
|
|
|
$(call run,docker-compose $(patsubst %,-f %,$(COMPOSE_FILE)) -p $(COMPOSE_PROJECT_NAME) $(1))
|
|
|
|
endef
|
|
|
|
define docker-compose-exec
|
2021-06-12 03:13:01 +02:00
|
|
|
$(call INFO,docker-compose-exec,$(1)$(comma) $(2))
|
2021-02-09 17:05:00 +01:00
|
|
|
$(call run,docker-compose $(patsubst %,-f %,$(COMPOSE_FILE)) -p $(COMPOSE_PROJECT_NAME) exec -T $(1) sh -c '$(2)')
|
|
|
|
endef
|
|
|
|
|
|
|
|
endif
|
|
|
|
|
2021-06-02 00:54:33 +02:00
|
|
|
# function docker-build: Build docker image
|
2021-02-09 17:05:00 +01:00
|
|
|
define docker-build
|
2021-06-12 03:13:01 +02:00
|
|
|
$(call INFO,docker-build,$(1)$(comma) $(2)$(comma) $(3))
|
2021-02-09 17:05:00 +01:00
|
|
|
$(eval path := $(patsubst %/,%,$(1)))
|
|
|
|
$(eval tag := $(or $(2),$(DOCKER_REPOSITORY)/$(lastword $(subst /, ,$(path))):$(DOCKER_IMAGE_TAG)))
|
|
|
|
$(eval target := $(subst ",,$(subst ',,$(or $(3),$(DOCKER_BUILD_TARGET)))))
|
|
|
|
$(eval image_id := $(shell docker images -q $(tag) 2>/dev/null))
|
|
|
|
$(eval build_image := $(or $(filter $(DOCKER_BUILD_CACHE),false),$(if $(image_id),,true)))
|
2021-06-12 03:13:01 +02:00
|
|
|
$(if $(build_image),$(RUN) docker build $(DOCKER_BUILD_ARGS) --build-arg DOCKER_BUILD_DIR="$(path)" --tag $(tag) $(if $(target),--target $(target)) -f $(path)/Dockerfile .,$(if $(VERBOSE),echo "docker image $(tag) has id $(image_id)",true))
|
2021-02-09 17:05:00 +01:00
|
|
|
endef
|
2021-06-02 00:54:33 +02:00
|
|
|
# function docker-commit: Commit docker image
|
2021-02-09 17:05:00 +01:00
|
|
|
define docker-commit
|
2021-06-12 03:13:01 +02:00
|
|
|
$(call INFO,docker-commit,$(1)$(comma) $(2)$(comma) $(3)$(comma) $(4))
|
2021-02-09 17:05:00 +01:00
|
|
|
$(eval service := $(or $(1),$(DOCKER_SERVICE)))
|
|
|
|
$(eval container := $(or $(2),$(firstword $(shell $(call docker-compose,--log-level critical ps -q $(service))))))
|
|
|
|
$(eval repository := $(or $(3),$(DOCKER_REPOSITORY)/$(service)))
|
|
|
|
$(eval tag := $(or $(4),$(DOCKER_IMAGE_TAG)))
|
2021-06-12 03:13:01 +02:00
|
|
|
$(RUN) docker commit $(container) $(repository):$(tag)
|
2021-02-09 17:05:00 +01:00
|
|
|
endef
|
2021-06-02 00:54:33 +02:00
|
|
|
# function docker-push: Push docker image
|
2021-02-09 17:05:00 +01:00
|
|
|
define docker-push
|
2021-06-12 03:13:01 +02:00
|
|
|
$(call INFO,docker-push,$(1)$(comma) $(2)$(comma) $(3))
|
2021-02-09 17:05:00 +01:00
|
|
|
$(eval service := $(or $(1),$(DOCKER_SERVICE)))
|
|
|
|
$(eval name := $(or $(2),$(DOCKER_REGISTRY_REPOSITORY)/$(service)))
|
|
|
|
$(eval tag := $(or $(3),$(DOCKER_IMAGE_TAG)))
|
2021-06-12 03:13:01 +02:00
|
|
|
$(RUN) docker push $(name):$(tag)
|
2021-02-09 17:05:00 +01:00
|
|
|
endef
|
2021-06-02 00:54:33 +02:00
|
|
|
# function docker-stack: Call itself recursively for each stack to expand stacks
|
2021-02-09 17:05:00 +01:00
|
|
|
# docker-stack: if 1st arg is a variable and can be expand to values, it calls
|
|
|
|
# itself again, once whith each value, else calls docker-stack-update function
|
|
|
|
# 1st arg: stacks, extract it from stack_names:stack_versions
|
|
|
|
# 2nd arg: versions, extract it from stack_names:stack_versions or 2nd arg
|
|
|
|
define docker-stack
|
2021-06-12 03:13:01 +02:00
|
|
|
$(call INFO,docker-stack,$(1)$(comma) $(2))
|
2021-02-09 17:05:00 +01:00
|
|
|
$(eval stacks := $(firstword $(subst :, ,$(1))))
|
|
|
|
$(eval versions := $(or $(if $(findstring :,$(1)),$(lastword $(subst :, ,$(1)))),$(2)))
|
|
|
|
$(if $($(stacks)),$(foreach substack,$($(stacks)),$(call docker-stack,$(substack),$(if $(findstring :,$(1)),$(versions)))),$(call docker-stack-update,$(stacks),$(versions)))
|
|
|
|
endef
|
2021-06-02 00:54:33 +02:00
|
|
|
# function docker-stack-update: Update COMPOSE_FILE with .yml files of the stack
|
2021-02-09 17:05:00 +01:00
|
|
|
# docker-stack-update: adds all .yml files of the stack to COMPOSE_FILE variable
|
|
|
|
# and update the .env file with the .env.dist files of the stack
|
|
|
|
# 1st arg: stack_path/stack_name:stack_version
|
|
|
|
# stack: get stack_name:stack_version from 1st arg
|
|
|
|
# name: get stack name from $(stack)
|
|
|
|
# 2nd arg: stack version, or extract it from $(stack), default to latest
|
|
|
|
# 3rd arg: stack path, or extract it from $(stack), default to stack/$(name)
|
|
|
|
# add $(path)/$(name).yml, $(path)/$(name).$(ENV).yml and $(path)/$(name).$(version).yml to COMPOSE_FILE variable
|
|
|
|
# if $(path)/.env.dist file exists, update .env file
|
|
|
|
define docker-stack-update
|
2021-06-12 03:13:01 +02:00
|
|
|
$(call INFO,docker-stack-update,$(1)$(comma) $(2)$(comma) $(3))
|
2021-02-09 17:05:00 +01:00
|
|
|
$(eval stack := $(patsubst %.yml,%,$(notdir $(1))))
|
|
|
|
$(eval name := $(firstword $(subst :, ,$(stack))))
|
|
|
|
$(eval version := $(or $(2),$(if $(findstring :,$(stack)),$(lastword $(subst :, ,$(stack))),latest)))
|
|
|
|
$(eval path := $(patsubst %/,%,$(or $(3),$(if $(findstring /,$(1)),$(if $(wildcard stack/$(1) stack/$(1).yml),stack/$(if $(findstring .yml,$(1)),$(dir $(1)),$(if $(wildcard stack/$(1).yml),$(dir $(1)),$(1))),$(dir $(1)))),stack/$(name))))
|
|
|
|
$(eval COMPOSE_FILE += $(wildcard $(path)/$(name).yml $(path)/$(name).$(ENV).yml $(path)/$(name).$(ENV).$(version).yml $(path)/$(name).$(version).yml))
|
2021-06-09 00:44:36 +02:00
|
|
|
$(if $(wildcard $(path)/.env.dist),$(call .env,,$(path)/.env.dist,$(wildcard $(CONFIG)/$(ENV)/$(APP)/.env $(path)/.env.$(ENV) .env)))
|
2021-02-09 17:05:00 +01:00
|
|
|
endef
|
2021-06-02 00:54:33 +02:00
|
|
|
# function docker-tag: Tag docker image
|
2021-02-09 17:05:00 +01:00
|
|
|
define docker-tag
|
2021-06-12 03:13:01 +02:00
|
|
|
$(call INFO,docker-tag,$(1)$(comma) $(2)$(comma) $(3)$(comma) $(4)$(comma) $(5))
|
2021-02-09 17:05:00 +01:00
|
|
|
$(eval service := $(or $(1),$(DOCKER_SERVICE)))
|
|
|
|
$(eval source := $(or $(2),$(DOCKER_REPOSITORY)/$(service)))
|
|
|
|
$(eval source_tag := $(or $(3),$(DOCKER_IMAGE_TAG)))
|
|
|
|
$(eval target := $(or $(4),$(DOCKER_REGISTRY_REPOSITORY)/$(service)))
|
|
|
|
$(eval target_tag := $(or $(5),$(source_tag)))
|
2021-06-12 03:13:01 +02:00
|
|
|
$(RUN) docker tag $(source):$(source_tag) $(target):$(target_tag)
|
2021-02-09 17:05:00 +01:00
|
|
|
endef
|