docker attach

This commit is contained in:
Yann Autissier
2022-11-19 01:03:37 +00:00
parent 96567c54dc
commit 910498f049
11 changed files with 160 additions and 135 deletions
+59 -109
View File
@@ -1,35 +1,45 @@
CMDARGS += app-%-exec app-%-run
# function app-attach: Call docker-attach for each Dockerfile in dir 1
define app-attach
$(call INFO,app-attach,$(1)$(comma))
$(call docker-file,$(1))
$(foreach dockerfile,$(DOCKER_FILE),
$(call app-docker,$(dockerfile))
$(call docker-attach)
)
endef
# 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))
$(eval APP := $(or $(1), $(APP)))
$(eval APP_DIR := $(or $(2), $(RELATIVE)$(APP)))
$(eval APP_NAME := $(or $(3),$(subst -,,$(subst .,,$(call LOWERCASE,$(APP))))))
$(eval APP_TYPE := $(or $(4), git))
$(eval DOCKER_BUILD_DIR := $(APP_DIR))
$(eval DOCKER_FILE := $(wildcard $(APP_DIR)/docker/*/Dockerfile $(APP_DIR)/*/Dockerfile $(APP_DIR)/Dockerfile))
$(eval COMPOSE_FILE := $(wildcard $(APP_DIR)/docker-compose.yml $(APP_DIR)/docker-compose.$(ENV).yml $(APP_DIR)/docker/docker-compose.yml $(foreach file,$(patsubst $(APP_DIR)/docker/docker-compose.%,%,$(basename $(wildcard $(APP_DIR)/docker/docker-compose.*.yml))),$(if $(filter true,$(COMPOSE_FILE_$(file)) $(COMPOSE_FILE_$(call UPPERCASE,$(file)))),$(APP_DIR)/docker/docker-compose.$(file).yml))))
$(if $(wildcard $(APP_DIR)/.env.sample),
$(call .env,$(APP_DIR)/.env,$(APP_DIR)/.env.sample)
,
$(call .env,$(APP_DIR)/.env)
)
$(eval DOCKER_BUILD_DIR := $(APP_DIR))
$(if $(wildcard $(APP_DIR)/.env.dist), $(call .env,$(APP_DIR)/.env,$(APP_DIR)/.env.dist))
$(if $(wildcard $(APP_DIR)/.env.example), $(call .env,$(APP_DIR)/.env,$(APP_DIR)/.env.example))
$(if $(wildcard $(APP_DIR)/.env.sample), $(call .env,$(APP_DIR)/.env,$(APP_DIR)/.env.sample))
endef
# function app-build: Call docker-build for each Dockerfile in dir 1
define app-build
$(call INFO,app-build,$(1)$(comma))
$(if $(filter-out $(APP_DIR),$(1)),
$(eval DOCKER_FILE := $(wildcard $(1)/docker/*/Dockerfile $(1)/*/Dockerfile $(1)/Dockerfile))
$(call docker-file,$(1))
$(foreach dockerfile,$(DOCKER_FILE),
$(call app-docker,$(dockerfile))
$(call docker-build, $(dir $(dockerfile)), $(DOCKER_IMAGE), "" )
)
$(if $(DOCKER_FILE), \
$(foreach dockerfile,$(DOCKER_FILE),
$(call app-docker,$(dockerfile))
$(call docker-build, $(dir $(dockerfile)), $(DOCKER_IMAGE), "" )
),
$(call ERROR,Unable to find a,Dockerfile in dir,$(or $(1),$(APP_DIR)))
endef
# function app-connect: Call docker exec $(DOCKER_SHELL) for each Dockerfile in dir 1
define app-connect
$(call INFO,app-connect,$(1)$(comma))
$(call docker-file,$(1))
$(foreach dockerfile,$(DOCKER_FILE),
$(call app-docker,$(dockerfile))
$(call docker-connect)
)
endef
@@ -45,66 +55,28 @@ define app-docker
$(eval DOCKER_LABELS := SERVICE_NAME=$(docker) SERVICE_TAGS=urlprefix-$(service).$(APP_DOMAIN)/$(APP_PATH))
$(eval DOCKER_NAME := $(docker))
$(eval DOCKER_RUN_NAME := --name $(DOCKER_NAME))
,
$(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)))
, $(call ERROR,Unable to find Dockerfile,$(dockerfile))
)
endef
# function app-down: Call docker rm for each Dockerfile in dir 1
define app-down
$(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),
$(foreach dockerfile,$(DOCKER_FILE),
$(call app-docker,$(dockerfile))
$(if $(shell docker ps -q -f name=$(DOCKER_NAME) 2>/dev/null),
$(RUN) docker rm -f $(DOCKER_NAME)
,
$(call WARNING,Unable to find docker,$(DOCKER_NAME))
)
),
$(call ERROR,Unable to find a,Dockerfile,in dir $(or $(1),$(APP_DIR)))
$(call docker-file,$(1))
$(foreach dockerfile,$(DOCKER_FILE),
$(call app-docker,$(dockerfile))
$(call docker-rm)
)
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))
)
$(call docker-file,$(1))
$(eval args := $(or $(2), $(ARGS)))
$(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)))
$(foreach dockerfile,$(DOCKER_FILE),
$(call app-docker,$(dockerfile))
$(call exec,$(args))
)
endef
@@ -114,45 +86,30 @@ define app-install
$(eval url := $(or $(1), $(APP_REPOSITORY_URL)))
$(eval dir := $(or $(2), $(RELATIVE)$(lastword $(subst /, ,$(url)))))
$(if $(wildcard $(dir)/.git),
$(call INFO,app $(url) already installed in dir $(dir)),
$(RUN) git clone $(QUIET) $(url) $(dir)
$(call INFO,app $(url) already installed in dir $(dir))
, $(RUN) git clone $(QUIET) $(url) $(dir)
)
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)))
$(call docker-file,$(1))
$(foreach dockerfile,$(DOCKER_FILE),
$(call app-docker,$(dockerfile))
$(call docker-logs)
)
endef
# function app-ps: Call docker ps $(ARGS) for each Dockerfile in dir 1
# function app-ps: Call docker ps 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)))
$(call INFO,app-ps,$(1)$(comma))
$(call docker-file,$(1))
$(foreach dockerfile,$(DOCKER_FILE),
$(call app-docker,$(dockerfile))
$(eval DOCKERS += $(DOCKER_NAME))
)
$(RUN) docker ps $(patsubst %,-f name=%,$(DOCKERS)) 2>/dev/null
endef
# function app-rebuild: Call app-build with DOCKER_BUILD_CACHE=false
@@ -165,21 +122,15 @@ endef
# function app-run: Call docker-run for each Dockerfile in dir 1 with args 2
define app-run
$(call INFO,app-run,$(1)$(comma) $(2))
$(if $(filter-out $(APP_DIR),$(1)),
$(eval DOCKER_FILE := $(wildcard $(1)/docker/*/Dockerfile $(1)/*/Dockerfile $(1)/Dockerfile))
)
$(call docker-file,$(1))
$(eval args := $(or $(2), $(ARGS)))
$(eval DOCKER_RUN_OPTIONS += -it)
$(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 a,Dockerfile in dir,$(or $(1),$(APP_DIR)))
$(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))
)
)
endef
@@ -189,8 +140,7 @@ define app-up
$(eval DOCKER_RUN_OPTIONS += -d)
$(if $(shell docker ps -q -f name=$(DOCKER_NAME) 2>/dev/null),
$(call INFO,docker $(DOCKER_NAME) already running)
,
$(call app-run,$(1))
, $(call app-run,$(1))
)
endef
@@ -200,7 +150,7 @@ define app-update
$(eval url := $(or $(1), $(APP_REPOSITORY_URL)))
$(eval dir := $(or $(2), $(APP_DIR)))
$(if $(wildcard $(dir)/.git),
$(RUN) sh -c 'cd $(dir) && git pull $(QUIET)',
$(call app-install,$(url),$(dir))
$(RUN) sh -c 'cd $(dir) && git pull $(QUIET)'
, $(call app-install,$(url),$(dir))
)
endef