From 910498f049e5469735b9c2a1a07711b788c4ca00 Mon Sep 17 00:00:00 2001 From: Yann Autissier Date: Sat, 19 Nov 2022 01:03:37 +0000 Subject: [PATCH] docker attach --- .env.dist | 2 +- make/apps/ansible/def.ansible.mk | 10 +-- make/apps/common.mk | 11 +++ make/apps/docker.mk | 2 +- make/apps/myos/def.ufw.mk | 4 +- make/apps/myos/ssh.mk | 2 +- make/apps/openstack/def.openstack.mk | 2 +- make/apps/packer/def.packer.mk | 4 +- make/def.app.mk | 168 ++++++++++++----------------------- make/def.docker.mk | 80 +++++++++++++++-- make/def.mk | 10 +-- 11 files changed, 160 insertions(+), 135 deletions(-) diff --git a/.env.dist b/.env.dist index e18f042..afa5b16 100644 --- a/.env.dist +++ b/.env.dist @@ -1,3 +1,3 @@ +APP_LOAD=myos APP_NAME=myos -APP_TYPE=myos DOMAIN=localhost diff --git a/make/apps/ansible/def.ansible.mk b/make/apps/ansible/def.ansible.mk index 37dcad0..3cf677a 100644 --- a/make/apps/ansible/def.ansible.mk +++ b/make/apps/ansible/def.ansible.mk @@ -1,5 +1,5 @@ +ANSIBLE_APP_LOAD ?= myos ANSIBLE_APP_NAME ?= myos -ANSIBLE_APP_TYPE ?= myos ANSIBLE_ARGS ?= $(if $(filter-out 0,$(UID)),$(if $(shell sudo -l 2>/dev/null |grep 'NOPASSWD: ALL'),,--ask-become-pass))$(if $(DOCKER_RUN),$(if $(shell ssh-add -l >/dev/null 2>&1 || echo false), --ask-pass)) ANSIBLE_AWS_ACCESS_KEY_ID ?= $(AWS_ACCESS_KEY_ID) ANSIBLE_AWS_DEFAULT_OUTPUT ?= $(AWS_DEFAULT_OUTPUT) @@ -36,22 +36,22 @@ ANSIBLE_USERNAME ?= $(USER) ANSIBLE_VERBOSE ?= $(if $(DEBUG),-vvvv,$(if $(VERBOSE),-v)) CMDARGS += ansible ansible-playbook DOCKER_RUN_OPTIONS_ANSIBLE ?= -it $(if $(DOCKER_INTERNAL_DOCKER_HOST),--add-host=host.docker.internal:$(DOCKER_INTERNAL_DOCKER_HOST)) -ENV_VARS += ANSIBLE_APP_NAME ANSIBLE_APP_TYPE ANSIBLE_AWS_ACCESS_KEY_ID ANSIBLE_AWS_DEFAULT_OUTPUT ANSIBLE_AWS_DEFAULT_REGION ANSIBLE_AWS_SECRET_ACCESS_KEY ANSIBLE_CONFIG ANSIBLE_DISKS_NFS_DISK ANSIBLE_DISKS_NFS_OPTIONS ANSIBLE_DISKS_NFS_PATH ANSIBLE_DOCKER_IMAGE_TAG ANSIBLE_DOCKER_REGISTRY ANSIBLE_EXTRA_VARS ANSIBLE_GIT_DIRECTORY ANSIBLE_GIT_KEY_FILE ANSIBLE_GIT_REPOSITORY ANSIBLE_GIT_VERSION ANSIBLE_INVENTORY ANSIBLE_MYOS ANSIBLE_PLAYBOOK ANSIBLE_SSH_AUTHORIZED_KEYS ANSIBLE_SSH_BASTION_HOSTNAME ANSIBLE_SSH_BASTION_USERNAME ANSIBLE_SSH_PRIVATE_IP_RANGE ANSIBLE_SSH_PRIVATE_KEYS ANSIBLE_SSH_PUBLIC_HOSTS ANSIBLE_SSH_USERNAME ANSIBLE_USERNAME ANSIBLE_VERBOSE +ENV_VARS += ANSIBLE_APP_LOAD ANSIBLE_APP_NAME ANSIBLE_AWS_ACCESS_KEY_ID ANSIBLE_AWS_DEFAULT_OUTPUT ANSIBLE_AWS_DEFAULT_REGION ANSIBLE_AWS_SECRET_ACCESS_KEY ANSIBLE_CONFIG ANSIBLE_DISKS_NFS_DISK ANSIBLE_DISKS_NFS_OPTIONS ANSIBLE_DISKS_NFS_PATH ANSIBLE_DOCKER_IMAGE_TAG ANSIBLE_DOCKER_REGISTRY ANSIBLE_EXTRA_VARS ANSIBLE_GIT_DIRECTORY ANSIBLE_GIT_KEY_FILE ANSIBLE_GIT_REPOSITORY ANSIBLE_GIT_VERSION ANSIBLE_INVENTORY ANSIBLE_MYOS ANSIBLE_PLAYBOOK ANSIBLE_SSH_AUTHORIZED_KEYS ANSIBLE_SSH_BASTION_HOSTNAME ANSIBLE_SSH_BASTION_USERNAME ANSIBLE_SSH_PRIVATE_IP_RANGE ANSIBLE_SSH_PRIVATE_KEYS ANSIBLE_SSH_PUBLIC_HOSTS ANSIBLE_SSH_USERNAME ANSIBLE_USERNAME ANSIBLE_VERBOSE # function ansible: Call run ansible ANSIBLE_ARGS with arg 1 define ansible $(call INFO,ansible,$(1)) - $(RUN) $(call $(if $(ANSIBLE_DOCKER_RUN),run,env-run),$(if $(ANSIBLE_DOCKER_RUN),,$(RUN) )ansible $(ANSIBLE_ARGS) $(ANSIBLE_VERBOSE) $(if $(ANSIBLE_DOCKER_RUN),-i $(ANSIBLE_INVENTORY)/.host.docker.internal) $(1),$(DOCKER_RUN_OPTIONS_ANSIBLE) $(DOCKER_REPOSITORY)/) + $(call $(if $(ANSIBLE_DOCKER_RUN),run,env-exec),$(if $(ANSIBLE_DOCKER_RUN),,$(RUN) )ansible $(ANSIBLE_ARGS) $(ANSIBLE_VERBOSE) $(if $(ANSIBLE_DOCKER_RUN),-i $(ANSIBLE_INVENTORY)/.host.docker.internal) $(1),$(DOCKER_RUN_OPTIONS_ANSIBLE) $(DOCKER_REPOSITORY)/) endef # function ansible-playbook: Call run ansible-playbook ANSIBLE_ARGS with arg 1 define ansible-playbook $(call INFO,ansible-playbook,$(1)) - $(RUN) $(call $(if $(ANSIBLE_DOCKER_RUN),run,env-run),$(if $(ANSIBLE_DOCKER_RUN),,$(RUN) )ansible$(if $(ANSIBLE_DOCKER_RUN),,-playbook) $(ANSIBLE_ARGS) $(ANSIBLE_VERBOSE) $(if $(ANSIBLE_DOCKER_RUN),-i $(ANSIBLE_INVENTORY)/.host.docker.internal) $(1),$(DOCKER_RUN_OPTIONS_ANSIBLE) --entrypoint=ansible-playbook $(DOCKER_REPOSITORY)/) + $(call $(if $(ANSIBLE_DOCKER_RUN),run,env-exec),$(if $(ANSIBLE_DOCKER_RUN),,$(RUN) )ansible$(if $(ANSIBLE_DOCKER_RUN),,-playbook) $(ANSIBLE_ARGS) $(ANSIBLE_VERBOSE) $(if $(ANSIBLE_DOCKER_RUN),-i $(ANSIBLE_INVENTORY)/.host.docker.internal) $(1),$(DOCKER_RUN_OPTIONS_ANSIBLE) --entrypoint=ansible-playbook $(DOCKER_REPOSITORY)/) endef # function ansible-pull: Call run ansible-pull ANSIBLE_ARGS with arg 1 define ansible-pull $(call INFO,ansible-pull,$(1)) - $(call $(if $(ANSIBLE_DOCKER_RUN),run,env-run),$(if $(ANSIBLE_DOCKER_RUN),,$(RUN) )ansible-pull $(ANSIBLE_ARGS) $(ANSIBLE_VERBOSE) $(1)) + $(call $(if $(ANSIBLE_DOCKER_RUN),run,env-exec),$(if $(ANSIBLE_DOCKER_RUN),,$(RUN) )ansible-pull $(ANSIBLE_ARGS) $(ANSIBLE_VERBOSE) $(1)) endef # function ansible-user-add-groups: Call ansible to add user 1 in groups 2 define ansible-user-add-groups diff --git a/make/apps/common.mk b/make/apps/common.mk index a872074..8841f8e 100644 --- a/make/apps/common.mk +++ b/make/apps/common.mk @@ -1,6 +1,17 @@ ## # COMMON +# target attach: Exec ARGS in docker SERVICE +# on local host +.PHONY: attach +attach: SERVICE ?= $(DOCKER_SERVICE) +attach: ## Attach to docker SERVICE + $(eval attach := $(COMPOSE_PROJECT_NAME)-$(SERVICE)) + $(if $(call docker-running,^$(attach)-1$), \ + $(call docker-attach,$(attach)-1) \ + , $(call docker-attach,$(attach)) \ + ) + # target bootstrap: Configure system # on local host .PHONY: bootstrap app-bootstrap diff --git a/make/apps/docker.mk b/make/apps/docker.mk index b63504d..6346cbc 100644 --- a/make/apps/docker.mk +++ b/make/apps/docker.mk @@ -226,7 +226,7 @@ docker-run-%: docker-build-% $(eval path := $(patsubst %/,%,$*)) $(eval image := $(DOCKER_REPOSITORY)/$(lastword $(subst /, ,$(path)))$(if $(findstring :,$*),,:$(DOCKER_IMAGE_TAG))) $(eval image_id := $(shell docker images -q $(image) 2>/dev/null)) - $(call docker-run,$(RUN) $(command),$(if $(image_id),$(image),$(path))) + $(call docker-run,$(command),$(if $(image_id),$(image),$(path))) # target docker-tag: Call docker-tag for each SERVICES .PHONY: docker-tag diff --git a/make/apps/myos/def.ufw.mk b/make/apps/myos/def.ufw.mk index 615382b..6f5292b 100644 --- a/make/apps/myos/def.ufw.mk +++ b/make/apps/myos/def.ufw.mk @@ -7,14 +7,14 @@ ifeq ($(SETUP_UFW),true) define ufw $(call INFO,ufw,$(1)$(comma)) $(call app-bootstrap,ufw-docker) - $(call app-exec,,ufw $(1)) + $(call app-exec,,$(if $(DOCKER_RUN),,$(SUDO)) ufw $(1)) endef # function ufw-docker: Exec command ufw-docker with args 1 define ufw-docker $(call INFO,ufw-docker,$(1)$(comma)) $(call app-bootstrap,ufw-docker) - $(call app-exec,,ufw-docker $(1)) + $(call app-exec,,$(if $(DOCKER_RUN),,$(SUDO)) ufw-docker $(1)) endef endif diff --git a/make/apps/myos/ssh.mk b/make/apps/myos/ssh.mk index db9ab3c..4de7d99 100644 --- a/make/apps/myos/ssh.mk +++ b/make/apps/myos/ssh.mk @@ -39,7 +39,7 @@ ssh-key: ifneq (,$(filter true,$(DRONE))) $(call exec,sh -c '[ ! -d $(SSH_DIR) ] && mkdir -p $(SSH_DIR) && chown $(UID) $(SSH_DIR) && chmod 0700 $(SSH_DIR) ||:') endif - $(if $(SSH_KEY),$(eval export SSH_KEY ?= $(SSH_KEY)) $(call env-run,sh -c 'printf "$$SSH_KEY\n" > $(SSH_DIR)/$(COMPOSE_PROJECT_NAME)_id_rsa && chmod 0600 $(SSH_DIR)/$(COMPOSE_PROJECT_NAME)_id_rsa && chown $(UID) $(SSH_DIR)/$(COMPOSE_PROJECT_NAME)_id_rsa ||:')) + $(if $(SSH_KEY),$(eval export SSH_KEY ?= $(SSH_KEY)) $(call env-exec,sh -c 'printf "$$SSH_KEY\n" > $(SSH_DIR)/$(COMPOSE_PROJECT_NAME)_id_rsa && chmod 0600 $(SSH_DIR)/$(COMPOSE_PROJECT_NAME)_id_rsa && chown $(UID) $(SSH_DIR)/$(COMPOSE_PROJECT_NAME)_id_rsa ||:')) # target ssh-run: Call ssh-run make run SERVICE ARGS .PHONY: ssh-run diff --git a/make/apps/openstack/def.openstack.mk b/make/apps/openstack/def.openstack.mk index 6fbc4ca..d8706e5 100644 --- a/make/apps/openstack/def.openstack.mk +++ b/make/apps/openstack/def.openstack.mk @@ -10,5 +10,5 @@ endif # function openstack: Call run openstack with arg 1 define openstack $(call INFO,openstack,$(1)) - $(RUN) $(call run,openstack $(1),$(DOCKER_REPOSITORY)/) + $(call run,openstack $(1),$(DOCKER_REPOSITORY)/) endef diff --git a/make/apps/packer/def.packer.mk b/make/apps/packer/def.packer.mk index 7d52326..83a66e3 100644 --- a/make/apps/packer/def.packer.mk +++ b/make/apps/packer/def.packer.mk @@ -86,12 +86,12 @@ endif ## it needs SSH_PRIVATE_KEYS to get access without password to GIT_REPOSITORY ## it needs AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY when deploying to AWS define packer - $(RUN) $(call run,packer $(1),$(DOCKER_RUN_OPTIONS_PACKER) $(DOCKER_REPOSITORY)/) + $(call run,packer $(1),$(DOCKER_RUN_OPTIONS_PACKER) $(DOCKER_REPOSITORY)/) endef # function packer-qemu: Call run qemu-system-% for PACKER_QEMU_ARCH define packer-qemu echo Running $(1) - $(RUN) $(call run,$(if $(DOCKER_RUN),packer,qemu-system-$(PACKER_QEMU_ARCH)) $(PACKER_QEMU_ARGS) -m 512m -drive file=$(1)$(comma)format=raw -net nic$(comma)model=virtio -net user$(comma)hostfwd=tcp:$(PACKER_SSH_ADDRESS):$(PACKER_SSH_PORT)-:22 -vnc $(PACKER_VNC_ADDRESS):$(subst 590,,$(PACKER_VNC_PORT)),$(DOCKER_RUN_OPTIONS_PACKER) --entrypoint=qemu-system-$(PACKER_QEMU_ARCH) $(DOCKER_REPOSITORY)/) + $(call run,$(if $(DOCKER_RUN),packer,qemu-system-$(PACKER_QEMU_ARCH)) $(PACKER_QEMU_ARGS) -m 512m -drive file=$(1)$(comma)format=raw -net nic$(comma)model=virtio -net user$(comma)hostfwd=tcp:$(PACKER_SSH_ADDRESS):$(PACKER_SSH_PORT)-:22 -vnc $(PACKER_VNC_ADDRESS):$(subst 590,,$(PACKER_VNC_PORT)),$(DOCKER_RUN_OPTIONS_PACKER) --entrypoint=qemu-system-$(PACKER_QEMU_ARCH) $(DOCKER_REPOSITORY)/) endef # function packer-build: Call packer build with arg 1, Add build infos to file PACKER_ISO_INFO diff --git a/make/def.app.mk b/make/def.app.mk index efc696c..dbb8a72 100644 --- a/make/def.app.mk +++ b/make/def.app.mk @@ -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 diff --git a/make/def.docker.mk b/make/def.docker.mk index 2a33be4..68b2d6f 100644 --- a/make/def.docker.mk +++ b/make/def.docker.mk @@ -66,16 +66,21 @@ define exec $(call run,$(DOCKER_IMAGE) $(or $(1),$(SHELL))) endef else -# function exec: Exec arg 1 in docker DOCKER_NAME +# function exec: call docker-exec define exec $(call INFO,exec,$(1)) - $(RUN) docker exec $(DOCKER_ENV_ARGS) $(DOCKER_EXEC_OPTIONS) $(DOCKER_RUN_WORKDIR) $(DOCKER_NAME) $(or $(1),$(SHELL)) + $(call docker-exec) endef endif # function run: Run docker run with arg 1 and docker repository 2 ## attention: arg 2 should end with slash or space define run $(call INFO,run,$(1)$(comma) $(2)) + $(if $(DOCKER_RUN_NAME), + $(if $(call docker-running,^$(DOCKER_RUN_NAME)$$), + $(call ERROR,Found already running docker,$(DOCKER_RUN_NAME)) + ) + ) $(RUN) docker run $(DOCKER_ENV_ARGS) $(DOCKER_RUN_LABELS) $(DOCKER_RUN_OPTIONS) $(DOCKER_RUN_VOLUME) $(DOCKER_RUN_WORKDIR) $(DOCKER_SSH_AUTH) $(DOCKER_RUN_NAME) $(2)$(1) endef @@ -85,21 +90,86 @@ SHELL := /bin/bash # function docker-run DOCKER=false: Run docker image 2 with arg 1 define docker-run $(call INFO,docker-run,$(1)$(comma) $(2)) + $(if $(DOCKER_RUN_NAME), + $(if $(call docker-running,^$(DOCKER_RUN_NAME)$$), + $(call ERROR,Found already running docker,$(DOCKER_RUN_NAME)) + ) + ) $(RUN) docker run $(DOCKER_ENV_ARGS) $(DOCKER_RUN_LABELS) $(DOCKER_RUN_OPTIONS) $(DOCKER_RUN_VOLUME) $(DOCKER_RUN_WORKDIR) $(DOCKER_RUN_NAME) $(or $(2),$(DOCKER_IMAGE)) $(1) endef # function exec DOCKER=false: Call env-exec with arg 1 or SHELL define exec $(call INFO,exec,$(1)) - $(call env-exec,$(or $(1),$(SHELL))) + $(call env-exec,$(RUN) $(or $(1),$(SHELL))) endef -# function run DOCKER=false: Call env-run with arg 1 +# function run DOCKER=false: Call exec with arg 1 define run $(call INFO,run,$(1)) - $(call env-run,$(1)) + $(call exec,$(1)) endef endif +# function docker-attach: Attach docker 1 or DOCKER_NAME +define docker-attach + $(call INFO,docker-attach,$(1)$(comma)) + $(eval attach := $(or $(1),$(DOCKER_NAME))) + $(if $(call docker-running,^$(attach)$), + $(RUN) docker attach $(attach) + , $(call ERROR,Unable to find docker,$(attach)) + ) +endef + +# function docker-connect: Call docker-exec +define docker-connect + $(call INFO,docker-connect,$(1)$(comma)) + $(call docker-exec,$(DOCKER_SHELL)) +endef + +# function docker-exec: Exec arg 1 in docker DOCKER_NAME +define docker-exec + $(call INFO,docker-exec,$(1)) + $(if $(call docker-running,^$(DOCKER_NAME)$), + $(RUN) docker exec $(DOCKER_ENV_ARGS) $(DOCKER_EXEC_OPTIONS) $(DOCKER_RUN_WORKDIR) $(DOCKER_NAME) $(or $(1),$(SHELL)) + , $(call ERROR,Unable to find docker,$(DOCKER_NAME)) + ) +endef + +# function docker-logs: Print logs of docker 1 or DOCKER_NAME +define docker-logs + $(call INFO,docker-logs,$(1)) + $(eval logs := $(or $(1),$(DOCKER_NAME))) + $(if $(call docker-running,^$(logs)$), + $(RUN) docker logs --follow --tail=100 $(logs) + , $(call ERROR,Unable to find docker,$(logs)) + ) +endef + +# function docker-file: eval DOCKER_FILE in dir 1 or APP_DIR +define docker-file + $(call INFO,docker-file,$(1)$(comma)) + $(eval dir := $(or $(1),$(APP_DIR))) + $(eval DOCKER_FILE := $(wildcard $(dir)/docker/*/Dockerfile $(dir)/*/Dockerfile $(dir)/Dockerfile)) + $(if $(DOCKER_FILE), + , $(call ERROR,Unable to find a,Dockerfile,in dir,$(dir)) + ) +endef + +# function docker-running: Print running dockers matching DOCKER_NAME +define docker-running +$(shell docker ps -q $(patsubst %,-f name=%,$(or $(1), ^$(DOCKER_NAME)$$, ^$)) 2>/dev/null) +endef + +# function docker-rm: Remove docker 1 +define docker-rm + $(call INFO,docker-rm,$(1)$(comma)) + $(eval rm := $(or $(1),$(DOCKER_NAME))) + $(if $(call docker-running,^$(rm)$), + $(call WARNING,Removing running docker,$(rm)) + ) + $(RUN) docker rm -f $(rm) +endef + # function docker-volume-copy: Copy files from a docker volume to another define docker-volume-copy $(call INFO,docker-volume-copy,$(1)$(comma) $(2)) diff --git a/make/def.mk b/make/def.mk index 2d55f71..3025d75 100644 --- a/make/def.mk +++ b/make/def.mk @@ -7,8 +7,8 @@ quote ?= ' lbracket ?= ( rbracket ?= ) APP ?= $(if $(wildcard .git),$(notdir $(CURDIR))) +APP_LOAD ?= $(if $(SUBREPO),subrepo) $(if $(filter .,$(MYOS)),myos) APP_NAME ?= $(subst _,,$(subst -,,$(subst .,,$(call LOWERCASE,$(APP))))) -APP_TYPE ?= $(if $(SUBREPO),subrepo) $(if $(filter .,$(MYOS)),myos) APPS ?= $(if $(MONOREPO),$(sort $(patsubst $(MONOREPO_DIR)/%/.git,%,$(wildcard $(MONOREPO_DIR)/*/.git)))) APPS_NAME ?= $(foreach app,$(APPS),$(or $(shell awk -F '=' '$$1 == "APP" {print $$2}' $(or $(wildcard $(MONOREPO_DIR)/$(app)/.env),$(wildcard $(MONOREPO_DIR)/$(app)/.env.$(ENV)),$(MONOREPO_DIR)/$(app)/.env.dist) 2>/dev/null),$(app))) BRANCH ?= $(GIT_BRANCH) @@ -238,18 +238,12 @@ define conf done < "$(file)" endef -# function env-exec: Exec arg 1 with custom env +# function env-exec: Exec arg 1 with env define env-exec $(call INFO,env-exec,$(1)) IFS=$$'\n'; env $(env_reset) $(env_args) $(1) endef -# function env-run: Call env-exec with arg 1 -define env-run - $(call INFO,env-run,$(1)) - $(call env-exec,$(or $(1),$(SHELL))) -endef - # function make: Call make with predefined options and variables # 1st arg: make command line (targets and arguments) # 2nd arg: directory to call make from