external app

This commit is contained in:
Yann Autissier
2022-11-16 08:46:00 +00:00
parent 3d79bcdad3
commit 23cd8f2779
14 changed files with 135 additions and 275 deletions
+13 -10
View File
@@ -75,12 +75,13 @@ down: docker-compose-down ## Remove application dockers
# target exec: Exec ARGS in docker SERVICE
# on local host
.PHONY: exec
exec: SERVICE ?= $(DOCKER_SERVICE)
exec: ## Exec command in docker SERVICE
ifneq (,$(filter $(ENV),$(ENV_DEPLOY)))
$(RUN) $(call exec,$(ARGS))
else
$(call make,docker-compose-exec,,ARGS)
endif
#ifneq (,$(filter $(ENV),$(ENV_DEPLOY)))
# $(RUN) $(call exec,$(ARGS))
#else
$(call docker-compose-exec-sh,$(SERVICE),$(ARGS)) || true
#endif
# target exec@%: Exec ARGS in docker SERVICE of % ENV
# on all remote hosts
@@ -144,12 +145,14 @@ restart: docker-compose-restart app-start ## Restart application
# target run: Run command ARGS in a new docker SERVICE
# on local host
.PHONY: run
run: SERVICE ?= $(DOCKER_SERVICE)
run: ## Run a command in a new docker
ifneq (,$(filter $(ENV),$(ENV_DEPLOY)))
$(call run,$(ARGS))
else
$(call make,docker-compose-run,,ARGS)
endif
#ifneq (,$(filter $(ENV),$(ENV_DEPLOY)))
# $(call run,$(ARGS))
#else
$(eval DOCKER_RUN_OPTIONS += -it)
$(call docker-compose,run $(DOCKER_COMPOSE_RUN_OPTIONS) $(SERVICE) $(ARGS))
#endif
# target run@%: Run command ARGS in a new docker SERVICE of % ENV
# on all remote hosts
+1 -1
View File
@@ -61,7 +61,7 @@ endif
# function docker-build: Build docker image
define docker-build
$(call INFO,docker-build,$(1)$(comma) $(2)$(comma) $(3))
$(eval path := $(patsubst %/,%,$(1)))
$(eval path := $(patsubst $(DOCKER_BUILD_DIR)/%,%,$(patsubst %/,%,$(1))))
$(eval service := $(subst .,,$(call LOWERCASE,$(lastword $(subst /, ,$(path))))))
$(eval tag := $(or $(2),$(DOCKER_REPOSITORY)/$(service):$(DOCKER_IMAGE_TAG)))
$(eval target := $(subst ",,$(subst ',,$(or $(3),$(DOCKER_BUILD_TARGET)))))
+2 -1
View File
@@ -1,5 +1,5 @@
APP_DIR ?= $(CURDIR)
APP_DOMAIN ?= $(addsuffix .,$(filter-out master,$(ENV)))$(USER).$(DOMAIN)
APP_DOMAIN ?= $(addsuffix .,$(filter-out $(ENV_DEPLOY),$(ENV)))$(USER).$(DOMAIN)
APP_HOST ?= $(addsuffix .,$(APP_NAME))$(APP_DOMAIN)
APP_INSTALLED ?= $(APPS)
APP_PARENT ?= $(MONOREPO)
@@ -16,6 +16,7 @@ APP_SCHEME ?= https
APP_UPSTREAM_REPOSITORY ?= $(or $(shell git config --get remote.upstream.url 2>/dev/null),$(GIT_UPSTREAM_REPOSITORY))
APP_URI ?= $(APP_HOST)/$(APP_PATH)
APP_URL ?= $(APP_SCHEME)://$(APP_URI)
CMDARGS += exec exec:% exec@% run run:% run@%
CONTEXT += APP APPS BRANCH DOMAIN VERSION RELEASE
CONTEXT_DEBUG += APP_DIR APP_URL APP_REPOSITORY APP_UPSTREAM_REPOSITORY ENV_DEPLOY
ENV_DEPLOY ?= $(shell ls .git/refs/remotes/origin/ 2>/dev/null)
+12 -4
View File
@@ -14,11 +14,19 @@ $(APP): myos-user
app-%:
$(eval app := $(subst -$(lastword $(subst -, ,$*)),,$*))
$(eval command := $(lastword $(subst -, ,$*)))
$(if $(filter app-$(command),$(.VARIABLES)), \
$(call app-bootstrap,$(app)) \
$(call app-$(command)) \
$(if $(wildcard $(RELATIVE)$(app)), \
$(if $(filter app-$(command),$(.VARIABLES)), \
$(call app-bootstrap,$(app)) \
$(call app-$(command)) \
, \
$(if $(wildcard $(RELATIVE)$*), \
$(call app-bootstrap,$*) \
, \
$(call WARNING,Unable to find app command,$(command)) \
) \
) \
, \
$(call app-bootstrap,$*) \
$(call WARNING,Unable to find app,$(app),in dir,$(RELATIVE)$(app)) \
)
# target app-required-install: Call app-install for each APP_REQUIRED
+90 -27
View File
@@ -1,3 +1,5 @@
CMDARGS += app-%-exec app-%-run
# function app-bootstrap: Define custom variables for app 1 in dir 2 with name 3 and type 4
define app-bootstrap
$(call INFO,app-bootstrap,$(1)$(comma) $(2$(comma) $(3))$(comma) $(4))
@@ -18,7 +20,7 @@ endef
# function app-build: Call docker-build for each Dockerfile in dir 1
define app-build
$(call INFO,app-build,$(1))
$(call INFO,app-build,$(1)$(comma))
$(if $(filter-out $(APP_DIR),$(1)),
$(eval DOCKER_FILE := $(wildcard $(1)/docker/*/Dockerfile $(1)/*/Dockerfile $(1)/Dockerfile))
)
@@ -27,13 +29,13 @@ define app-build
$(call app-docker,$(dockerfile))
$(call docker-build, $(dir $(dockerfile)), $(DOCKER_IMAGE), "" )
),
$(call ERROR,Unable to find any Dockerfile in dir: $(or $(1),$(APP_DIR)))
$(call ERROR,Unable to find a,Dockerfile in dir,$(or $(1),$(APP_DIR)))
)
endef
# function app-docker: Define custom variables for Dockerfile 1
define app-docker
$(call INFO,app-docker,$(1))
$(call INFO,app-docker,$(1)$(comma))
$(eval dir := $(or $(APP_DIR)))
$(eval dockerfile := $(or $(1)))
$(if $(wildcard $(dockerfile)),
@@ -44,26 +46,64 @@ define app-docker
$(eval DOCKER_NAME := $(docker))
$(eval DOCKER_RUN_NAME := --name $(DOCKER_NAME))
,
$(call ERROR,Unable to find Dockerfile: $(dockerfile))
$(call ERROR,Unable to find Dockerfile,$(dockerfile))
)
endef
# function app-connect: Call docker exec $(DOCKER_SHELL) for each Dockerfile in dir 1
define app-connect
$(call INFO,app-connect,$(1)$(comma))
$(if $(filter-out $(APP_DIR),$(1)),
$(eval DOCKER_FILE := $(wildcard $(1)/docker/*/Dockerfile $(1)/*/Dockerfile $(1)/Dockerfile))
)
$(if $(DOCKER_FILE),
$(foreach dockerfile,$(DOCKER_FILE),
$(call app-docker,$(dockerfile))
$(if $(shell docker ps -q -f name=$(DOCKER_NAME) 2>/dev/null),
$(RUN) docker exec -it $(DOCKER_NAME) $(DOCKER_SHELL)
,
$(call WARNING,Unable to find docker,$(DOCKER_NAME))
)
),
$(call ERROR,Unable to find a,Dockerfile,in dir $(or $(1),$(APP_DIR)))
)
endef
# function app-down: Call docker rm for each Dockerfile in dir 1
define app-down
$(call INFO,app-down,$(1))
$(call INFO,app-down,$(1)$(comma))
$(if $(filter-out $(APP_DIR),$(1)),
$(eval DOCKER_FILE := $(wildcard $(1)/docker/*/Dockerfile $(1)/*/Dockerfile $(1)/Dockerfile))
)
$(if $(DOCKER_FILE), \
$(if $(DOCKER_FILE),
$(foreach dockerfile,$(DOCKER_FILE),
$(call app-docker,$(dockerfile))
$(if $(shell docker ps -q -f name=$(DOCKER_NAME) 2>/dev/null),
$(shell docker rm -f $(DOCKER_NAME))
$(RUN) docker rm -f $(DOCKER_NAME)
,
$(call ERROR,Unable to find docker $(DOCKER_NAME))
$(call WARNING,Unable to find docker,$(DOCKER_NAME))
)
),
$(call ERROR,Unable to find any Dockerfile in dir: $(or $(1),$(APP_DIR)))
$(call ERROR,Unable to find a,Dockerfile,in dir $(or $(1),$(APP_DIR)))
)
endef
# function app-exec: Call docker exec $(ARGS) for each Dockerfile in dir 1
define app-exec
$(call INFO,app-exec,$(1)$(comma) $(2))
$(if $(filter-out $(APP_DIR),$(1)),
$(eval DOCKER_FILE := $(wildcard $(1)/docker/*/Dockerfile $(1)/*/Dockerfile $(1)/Dockerfile))
)
$(if $(DOCKER_FILE),
$(foreach dockerfile,$(DOCKER_FILE),
$(call app-docker,$(dockerfile))
$(if $(shell docker ps -q -f name=$(DOCKER_NAME) 2>/dev/null),
$(RUN) docker exec -it $(DOCKER_NAME) $(ARGS)
,
$(call WARNING,Unable to find docker,$(DOCKER_NAME))
)
),
$(call ERROR,Unable to find a,Dockerfile,in dir $(or $(1),$(APP_DIR)))
)
endef
@@ -78,9 +118,45 @@ define app-install
)
endef
# function app-logs: Call docker logs $(ARGS) for each Dockerfile in dir 1
define app-logs
$(call INFO,app-logs,$(1)$(comma) $(2))
$(if $(filter-out $(APP_DIR),$(1)),
$(eval DOCKER_FILE := $(wildcard $(1)/docker/*/Dockerfile $(1)/*/Dockerfile $(1)/Dockerfile))
)
$(if $(DOCKER_FILE),
$(foreach dockerfile,$(DOCKER_FILE),
$(call app-docker,$(dockerfile))
$(if $(shell docker ps -q -f name=$(DOCKER_NAME) 2>/dev/null),
$(RUN) docker logs --follow --tail=100 $(DOCKER_NAME)
,
$(call WARNING,Unable to find docker,$(DOCKER_NAME))
)
),
$(call ERROR,Unable to find a,Dockerfile,in dir $(or $(1),$(APP_DIR)))
)
endef
# function app-ps: Call docker ps $(ARGS) for each Dockerfile in dir 1
define app-ps
$(call INFO,app-ps,$(1)$(comma) $(2))
$(if $(filter-out $(APP_DIR),$(1)),
$(eval DOCKER_FILE := $(wildcard $(1)/docker/*/Dockerfile $(1)/*/Dockerfile $(1)/Dockerfile))
)
$(if $(DOCKER_FILE),
$(foreach dockerfile,$(DOCKER_FILE),
$(call app-docker,$(dockerfile))
$(eval DOCKERS += $(DOCKER_NAME))
)
$(RUN) docker ps $(patsubst %,-f name=%,$(DOCKERS)) 2>/dev/null
,
$(call ERROR,Unable to find a,Dockerfile,in dir $(or $(1),$(APP_DIR)))
)
endef
# function app-rebuild: Call app-build with DOCKER_BUILD_CACHE=false
define app-rebuild
$(call INFO,app-rebuild,$(1))
$(call INFO,app-rebuild,$(1)$(comma))
$(eval DOCKER_BUILD_CACHE := false)
$(call app-build,$(1))
endef
@@ -99,31 +175,18 @@ define app-run
$(if $(shell docker images -q $(DOCKER_IMAGE) 2>/dev/null),
$(call docker-run,$(args))
,
$(call ERROR,Unable to find docker image $(DOCKER_IMAGE))
$(call ERROR,Unable to find docker image,$(DOCKER_IMAGE))
)
),
$(call ERROR,Unable to find any Dockerfile in dir: $(or $(1),$(APP_DIR)))
$(call ERROR,Unable to find a,Dockerfile in dir,$(or $(1),$(APP_DIR)))
)
endef
# function app-up: Call docker-run (-d) for each Dockerfile in dir 1
define app-up
$(call INFO,app-up,$(1))
$(if $(filter-out $(APP_DIR),$(1)),
$(eval DOCKER_FILE := $(wildcard $(1)/docker/*/Dockerfile $(1)/*/Dockerfile $(1)/Dockerfile))
)
$(call INFO,app-up,$(1)$(comma))
$(eval DOCKER_RUN_OPTIONS += -d)
$(if $(DOCKER_FILE), \
$(foreach dockerfile,$(DOCKER_FILE),
$(call app-docker,$(dockerfile))
$(if $(shell docker images -q $(DOCKER_IMAGE) 2>/dev/null),
$(call docker-run,$(args))
,
$(call ERROR,Unable to find docker image $(DOCKER_IMAGE))
)
),
$(call ERROR,Unable to find any Dockerfile in dir: $(or $(1),$(APP_DIR)))
)
$(call app-run,$(1))
endef
# function app-update: Run 'cd dir 1 && git pull' or Call app-install
+3 -3
View File
@@ -16,7 +16,7 @@ CMD_APK_INSTALL ?= $(if $(shell type -p apk),apk --no-cache --up
CMD_APK_REMOVE ?= $(if $(shell type -p apk),apk --no-cache del)
CMD_APT_INSTALL ?= $(if $(shell type -p apt-get),apt-get update && apt-get -fy install)
CMD_APT_REMOVE ?= $(if $(shell type -p apt-get),apt-get -fy remove)
CMDARGS ?= app-%-run apps-install exec exec:% exec@% install-app run run:% run@%
CMDARGS ?= apps-install install-app
COLOR_BLUE ?= \033[01;34m
COLOR_BROWN ?= \033[33m
COLOR_CYAN ?= \033[36m
@@ -48,7 +48,7 @@ DRONE ?= false
DRYRUN ?= false
DRYRUN_RECURSIVE ?= false
ELAPSED_TIME = $(shell $(call TIME))
ENV ?= local
ENV ?= master
ENV_ARGS ?= $(env_args)
ENV_FILE ?= $(wildcard $(CONFIG)/$(ENV)/$(APP)/.env .env)
ENV_LIST ?= $(shell ls .git/refs/heads/ 2>/dev/null)
@@ -177,7 +177,7 @@ INFO_FD := 2
INFO = $(if $(VERBOSE),$(if $(filter-out true,$(IGNORE_VERBOSE)), \
printf '${COLOR_INFO}$(APP)${COLOR_RESET}[${COLOR_VALUE}$(MAKELEVEL)${COLOR_RESET}]$(if $@, ${COLOR_VALUE}$@${COLOR_RESET}):${COLOR_RESET} ' >&$(INFO_FD) \
$(if $(2), \
&& printf 'Call ${COLOR_HIGHLIGHT}$(1)${COLOR_RESET}$(lbracket)' >&$(INFO_FD) \
&& printf 'Calling ${COLOR_HIGHLIGHT}$(1)${COLOR_RESET}$(lbracket)' >&$(INFO_FD) \
&& $(or $(strip $(call PRINTF,$(2))),printf '$(2)') >&$(INFO_FD) \
&& printf '$(rbracket)' >&$(INFO_FD) \
$(if $(3),&& printf ' ${COLOR_VALUE}in${COLOR_RESET} $(3)' >&$(INFO_FD)) \