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

View File

@ -1,3 +1,3 @@
APP_LOAD=myos
APP_NAME=myos APP_NAME=myos
APP_TYPE=myos
DOMAIN=localhost DOMAIN=localhost

View File

@ -1,5 +1,5 @@
ANSIBLE_APP_LOAD ?= myos
ANSIBLE_APP_NAME ?= 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_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_ACCESS_KEY_ID ?= $(AWS_ACCESS_KEY_ID)
ANSIBLE_AWS_DEFAULT_OUTPUT ?= $(AWS_DEFAULT_OUTPUT) ANSIBLE_AWS_DEFAULT_OUTPUT ?= $(AWS_DEFAULT_OUTPUT)
@ -36,22 +36,22 @@ ANSIBLE_USERNAME ?= $(USER)
ANSIBLE_VERBOSE ?= $(if $(DEBUG),-vvvv,$(if $(VERBOSE),-v)) ANSIBLE_VERBOSE ?= $(if $(DEBUG),-vvvv,$(if $(VERBOSE),-v))
CMDARGS += ansible ansible-playbook CMDARGS += ansible ansible-playbook
DOCKER_RUN_OPTIONS_ANSIBLE ?= -it $(if $(DOCKER_INTERNAL_DOCKER_HOST),--add-host=host.docker.internal:$(DOCKER_INTERNAL_DOCKER_HOST)) 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 # function ansible: Call run ansible ANSIBLE_ARGS with arg 1
define ansible define ansible
$(call INFO,ansible,$(1)) $(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 endef
# function ansible-playbook: Call run ansible-playbook ANSIBLE_ARGS with arg 1 # function ansible-playbook: Call run ansible-playbook ANSIBLE_ARGS with arg 1
define ansible-playbook define ansible-playbook
$(call INFO,ansible-playbook,$(1)) $(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 endef
# function ansible-pull: Call run ansible-pull ANSIBLE_ARGS with arg 1 # function ansible-pull: Call run ansible-pull ANSIBLE_ARGS with arg 1
define ansible-pull define ansible-pull
$(call INFO,ansible-pull,$(1)) $(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 endef
# function ansible-user-add-groups: Call ansible to add user 1 in groups 2 # function ansible-user-add-groups: Call ansible to add user 1 in groups 2
define ansible-user-add-groups define ansible-user-add-groups

View File

@ -1,6 +1,17 @@
## ##
# COMMON # 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 # target bootstrap: Configure system
# on local host # on local host
.PHONY: bootstrap app-bootstrap .PHONY: bootstrap app-bootstrap

View File

@ -226,7 +226,7 @@ docker-run-%: docker-build-%
$(eval path := $(patsubst %/,%,$*)) $(eval path := $(patsubst %/,%,$*))
$(eval image := $(DOCKER_REPOSITORY)/$(lastword $(subst /, ,$(path)))$(if $(findstring :,$*),,:$(DOCKER_IMAGE_TAG))) $(eval image := $(DOCKER_REPOSITORY)/$(lastword $(subst /, ,$(path)))$(if $(findstring :,$*),,:$(DOCKER_IMAGE_TAG)))
$(eval image_id := $(shell docker images -q $(image) 2>/dev/null)) $(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 # target docker-tag: Call docker-tag for each SERVICES
.PHONY: docker-tag .PHONY: docker-tag

View File

@ -7,14 +7,14 @@ ifeq ($(SETUP_UFW),true)
define ufw define ufw
$(call INFO,ufw,$(1)$(comma)) $(call INFO,ufw,$(1)$(comma))
$(call app-bootstrap,ufw-docker) $(call app-bootstrap,ufw-docker)
$(call app-exec,,ufw $(1)) $(call app-exec,,$(if $(DOCKER_RUN),,$(SUDO)) ufw $(1))
endef endef
# function ufw-docker: Exec command ufw-docker with args 1 # function ufw-docker: Exec command ufw-docker with args 1
define ufw-docker define ufw-docker
$(call INFO,ufw-docker,$(1)$(comma)) $(call INFO,ufw-docker,$(1)$(comma))
$(call app-bootstrap,ufw-docker) $(call app-bootstrap,ufw-docker)
$(call app-exec,,ufw-docker $(1)) $(call app-exec,,$(if $(DOCKER_RUN),,$(SUDO)) ufw-docker $(1))
endef endef
endif endif

View File

@ -39,7 +39,7 @@ ssh-key:
ifneq (,$(filter true,$(DRONE))) ifneq (,$(filter true,$(DRONE)))
$(call exec,sh -c '[ ! -d $(SSH_DIR) ] && mkdir -p $(SSH_DIR) && chown $(UID) $(SSH_DIR) && chmod 0700 $(SSH_DIR) ||:') $(call exec,sh -c '[ ! -d $(SSH_DIR) ] && mkdir -p $(SSH_DIR) && chown $(UID) $(SSH_DIR) && chmod 0700 $(SSH_DIR) ||:')
endif 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 # target ssh-run: Call ssh-run make run SERVICE ARGS
.PHONY: ssh-run .PHONY: ssh-run

View File

@ -10,5 +10,5 @@ endif
# function openstack: Call run openstack with arg 1 # function openstack: Call run openstack with arg 1
define openstack define openstack
$(call INFO,openstack,$(1)) $(call INFO,openstack,$(1))
$(RUN) $(call run,openstack $(1),$(DOCKER_REPOSITORY)/) $(call run,openstack $(1),$(DOCKER_REPOSITORY)/)
endef endef

View File

@ -86,12 +86,12 @@ endif
## it needs SSH_PRIVATE_KEYS to get access without password to GIT_REPOSITORY ## 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 ## it needs AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY when deploying to AWS
define packer define packer
$(RUN) $(call run,packer $(1),$(DOCKER_RUN_OPTIONS_PACKER) $(DOCKER_REPOSITORY)/) $(call run,packer $(1),$(DOCKER_RUN_OPTIONS_PACKER) $(DOCKER_REPOSITORY)/)
endef endef
# function packer-qemu: Call run qemu-system-% for PACKER_QEMU_ARCH # function packer-qemu: Call run qemu-system-% for PACKER_QEMU_ARCH
define packer-qemu define packer-qemu
echo Running $(1) 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 endef
# function packer-build: Call packer build with arg 1, Add build infos to file PACKER_ISO_INFO # function packer-build: Call packer build with arg 1, Add build infos to file PACKER_ISO_INFO

View File

@ -1,35 +1,45 @@
CMDARGS += app-%-exec app-%-run 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 # function app-bootstrap: Define custom variables for app 1 in dir 2 with name 3 and type 4
define app-bootstrap define app-bootstrap
$(call INFO,app-bootstrap,$(1)$(comma) $(2$(comma) $(3))$(comma) $(4)) $(call INFO,app-bootstrap,$(1)$(comma) $(2$(comma) $(3))$(comma) $(4))
$(eval APP := $(or $(1), $(APP))) $(eval APP := $(or $(1), $(APP)))
$(eval APP_DIR := $(or $(2), $(RELATIVE)$(APP))) $(eval APP_DIR := $(or $(2), $(RELATIVE)$(APP)))
$(eval APP_NAME := $(or $(3),$(subst -,,$(subst .,,$(call LOWERCASE,$(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)))) $(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), $(eval DOCKER_BUILD_DIR := $(APP_DIR))
$(call .env,$(APP_DIR)/.env,$(APP_DIR)/.env.sample) $(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))
$(call .env,$(APP_DIR)/.env) $(if $(wildcard $(APP_DIR)/.env.sample), $(call .env,$(APP_DIR)/.env,$(APP_DIR)/.env.sample))
)
endef endef
# function app-build: Call docker-build for each Dockerfile in dir 1 # function app-build: Call docker-build for each Dockerfile in dir 1
define app-build define app-build
$(call INFO,app-build,$(1)$(comma)) $(call INFO,app-build,$(1)$(comma))
$(if $(filter-out $(APP_DIR),$(1)), $(call docker-file,$(1))
$(eval DOCKER_FILE := $(wildcard $(1)/docker/*/Dockerfile $(1)/*/Dockerfile $(1)/Dockerfile)) $(foreach dockerfile,$(DOCKER_FILE),
$(call app-docker,$(dockerfile))
$(call docker-build, $(dir $(dockerfile)), $(DOCKER_IMAGE), "" )
) )
$(if $(DOCKER_FILE), \ endef
$(foreach dockerfile,$(DOCKER_FILE),
$(call app-docker,$(dockerfile)) # function app-connect: Call docker exec $(DOCKER_SHELL) for each Dockerfile in dir 1
$(call docker-build, $(dir $(dockerfile)), $(DOCKER_IMAGE), "" ) define app-connect
), $(call INFO,app-connect,$(1)$(comma))
$(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-connect)
) )
endef endef
@ -45,66 +55,28 @@ define app-docker
$(eval DOCKER_LABELS := SERVICE_NAME=$(docker) SERVICE_TAGS=urlprefix-$(service).$(APP_DOMAIN)/$(APP_PATH)) $(eval DOCKER_LABELS := SERVICE_NAME=$(docker) SERVICE_TAGS=urlprefix-$(service).$(APP_DOMAIN)/$(APP_PATH))
$(eval DOCKER_NAME := $(docker)) $(eval DOCKER_NAME := $(docker))
$(eval DOCKER_RUN_NAME := --name $(DOCKER_NAME)) $(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 endef
# function app-down: Call docker rm for each Dockerfile in dir 1 # function app-down: Call docker rm for each Dockerfile in dir 1
define app-down define app-down
$(call INFO,app-down,$(1)$(comma)) $(call INFO,app-down,$(1)$(comma))
$(if $(filter-out $(APP_DIR),$(1)), $(call docker-file,$(1))
$(eval DOCKER_FILE := $(wildcard $(1)/docker/*/Dockerfile $(1)/*/Dockerfile $(1)/Dockerfile)) $(foreach dockerfile,$(DOCKER_FILE),
) $(call app-docker,$(dockerfile))
$(if $(DOCKER_FILE), $(call docker-rm)
$(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)))
) )
endef endef
# function app-exec: Call docker exec $(ARGS) for each Dockerfile in dir 1 # function app-exec: Call docker exec $(ARGS) for each Dockerfile in dir 1
define app-exec define app-exec
$(call INFO,app-exec,$(1)$(comma) $(2)) $(call INFO,app-exec,$(1)$(comma) $(2))
$(if $(filter-out $(APP_DIR),$(1)), $(call docker-file,$(1))
$(eval DOCKER_FILE := $(wildcard $(1)/docker/*/Dockerfile $(1)/*/Dockerfile $(1)/Dockerfile))
)
$(eval args := $(or $(2), $(ARGS))) $(eval args := $(or $(2), $(ARGS)))
$(if $(DOCKER_FILE), $(foreach dockerfile,$(DOCKER_FILE),
$(foreach dockerfile,$(DOCKER_FILE), $(call app-docker,$(dockerfile))
$(call app-docker,$(dockerfile)) $(call exec,$(args))
$(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 endef
@ -114,45 +86,30 @@ define app-install
$(eval url := $(or $(1), $(APP_REPOSITORY_URL))) $(eval url := $(or $(1), $(APP_REPOSITORY_URL)))
$(eval dir := $(or $(2), $(RELATIVE)$(lastword $(subst /, ,$(url))))) $(eval dir := $(or $(2), $(RELATIVE)$(lastword $(subst /, ,$(url)))))
$(if $(wildcard $(dir)/.git), $(if $(wildcard $(dir)/.git),
$(call INFO,app $(url) already installed in dir $(dir)), $(call INFO,app $(url) already installed in dir $(dir))
$(RUN) git clone $(QUIET) $(url) $(dir) , $(RUN) git clone $(QUIET) $(url) $(dir)
) )
endef endef
# function app-logs: Call docker logs $(ARGS) for each Dockerfile in dir 1 # function app-logs: Call docker logs $(ARGS) for each Dockerfile in dir 1
define app-logs define app-logs
$(call INFO,app-logs,$(1)$(comma) $(2)) $(call INFO,app-logs,$(1)$(comma) $(2))
$(if $(filter-out $(APP_DIR),$(1)), $(call docker-file,$(1))
$(eval DOCKER_FILE := $(wildcard $(1)/docker/*/Dockerfile $(1)/*/Dockerfile $(1)/Dockerfile)) $(foreach dockerfile,$(DOCKER_FILE),
) $(call app-docker,$(dockerfile))
$(if $(DOCKER_FILE), $(call docker-logs)
$(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 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 define app-ps
$(call INFO,app-ps,$(1)$(comma) $(2)) $(call INFO,app-ps,$(1)$(comma))
$(if $(filter-out $(APP_DIR),$(1)), $(call docker-file,$(1))
$(eval DOCKER_FILE := $(wildcard $(1)/docker/*/Dockerfile $(1)/*/Dockerfile $(1)/Dockerfile)) $(foreach dockerfile,$(DOCKER_FILE),
) $(call app-docker,$(dockerfile))
$(if $(DOCKER_FILE), $(eval DOCKERS += $(DOCKER_NAME))
$(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)))
) )
$(RUN) docker ps $(patsubst %,-f name=%,$(DOCKERS)) 2>/dev/null
endef endef
# function app-rebuild: Call app-build with DOCKER_BUILD_CACHE=false # 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 # function app-run: Call docker-run for each Dockerfile in dir 1 with args 2
define app-run define app-run
$(call INFO,app-run,$(1)$(comma) $(2)) $(call INFO,app-run,$(1)$(comma) $(2))
$(if $(filter-out $(APP_DIR),$(1)), $(call docker-file,$(1))
$(eval DOCKER_FILE := $(wildcard $(1)/docker/*/Dockerfile $(1)/*/Dockerfile $(1)/Dockerfile))
)
$(eval args := $(or $(2), $(ARGS))) $(eval args := $(or $(2), $(ARGS)))
$(eval DOCKER_RUN_OPTIONS += -it) $(eval DOCKER_RUN_OPTIONS += -it)
$(if $(DOCKER_FILE), \ $(foreach dockerfile,$(DOCKER_FILE),
$(foreach dockerfile,$(DOCKER_FILE), $(call app-docker,$(dockerfile))
$(call app-docker,$(dockerfile)) $(if $(shell docker images -q $(DOCKER_IMAGE) 2>/dev/null),
$(if $(shell docker images -q $(DOCKER_IMAGE) 2>/dev/null), $(call docker-run,$(args))
$(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 a,Dockerfile in dir,$(or $(1),$(APP_DIR)))
) )
endef endef
@ -189,8 +140,7 @@ define app-up
$(eval DOCKER_RUN_OPTIONS += -d) $(eval DOCKER_RUN_OPTIONS += -d)
$(if $(shell docker ps -q -f name=$(DOCKER_NAME) 2>/dev/null), $(if $(shell docker ps -q -f name=$(DOCKER_NAME) 2>/dev/null),
$(call INFO,docker $(DOCKER_NAME) already running) $(call INFO,docker $(DOCKER_NAME) already running)
, , $(call app-run,$(1))
$(call app-run,$(1))
) )
endef endef
@ -200,7 +150,7 @@ define app-update
$(eval url := $(or $(1), $(APP_REPOSITORY_URL))) $(eval url := $(or $(1), $(APP_REPOSITORY_URL)))
$(eval dir := $(or $(2), $(APP_DIR))) $(eval dir := $(or $(2), $(APP_DIR)))
$(if $(wildcard $(dir)/.git), $(if $(wildcard $(dir)/.git),
$(RUN) sh -c 'cd $(dir) && git pull $(QUIET)', $(RUN) sh -c 'cd $(dir) && git pull $(QUIET)'
$(call app-install,$(url),$(dir)) , $(call app-install,$(url),$(dir))
) )
endef endef

View File

@ -66,16 +66,21 @@ define exec
$(call run,$(DOCKER_IMAGE) $(or $(1),$(SHELL))) $(call run,$(DOCKER_IMAGE) $(or $(1),$(SHELL)))
endef endef
else else
# function exec: Exec arg 1 in docker DOCKER_NAME # function exec: call docker-exec
define exec define exec
$(call INFO,exec,$(1)) $(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 endef
endif endif
# function run: Run docker run with arg 1 and docker repository 2 # function run: Run docker run with arg 1 and docker repository 2
## attention: arg 2 should end with slash or space ## attention: arg 2 should end with slash or space
define run define run
$(call INFO,run,$(1)$(comma) $(2)) $(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) $(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 endef
@ -85,21 +90,86 @@ SHELL := /bin/bash
# function docker-run DOCKER=false: Run docker image 2 with arg 1 # function docker-run DOCKER=false: Run docker image 2 with arg 1
define docker-run define docker-run
$(call INFO,docker-run,$(1)$(comma) $(2)) $(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) $(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 endef
# function exec DOCKER=false: Call env-exec with arg 1 or SHELL # function exec DOCKER=false: Call env-exec with arg 1 or SHELL
define exec define exec
$(call INFO,exec,$(1)) $(call INFO,exec,$(1))
$(call env-exec,$(or $(1),$(SHELL))) $(call env-exec,$(RUN) $(or $(1),$(SHELL)))
endef endef
# function run DOCKER=false: Call env-run with arg 1 # function run DOCKER=false: Call exec with arg 1
define run define run
$(call INFO,run,$(1)) $(call INFO,run,$(1))
$(call env-run,$(1)) $(call exec,$(1))
endef endef
endif 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 # function docker-volume-copy: Copy files from a docker volume to another
define docker-volume-copy define docker-volume-copy
$(call INFO,docker-volume-copy,$(1)$(comma) $(2)) $(call INFO,docker-volume-copy,$(1)$(comma) $(2))

View File

@ -7,8 +7,8 @@ quote ?= '
lbracket ?= ( lbracket ?= (
rbracket ?= ) rbracket ?= )
APP ?= $(if $(wildcard .git),$(notdir $(CURDIR))) APP ?= $(if $(wildcard .git),$(notdir $(CURDIR)))
APP_LOAD ?= $(if $(SUBREPO),subrepo) $(if $(filter .,$(MYOS)),myos)
APP_NAME ?= $(subst _,,$(subst -,,$(subst .,,$(call LOWERCASE,$(APP))))) 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 ?= $(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))) 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) BRANCH ?= $(GIT_BRANCH)
@ -238,18 +238,12 @@ define conf
done < "$(file)" done < "$(file)"
endef endef
# function env-exec: Exec arg 1 with custom env # function env-exec: Exec arg 1 with env
define env-exec define env-exec
$(call INFO,env-exec,$(1)) $(call INFO,env-exec,$(1))
IFS=$$'\n'; env $(env_reset) $(env_args) $(1) IFS=$$'\n'; env $(env_reset) $(env_args) $(1)
endef 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 # function make: Call make with predefined options and variables
# 1st arg: make command line (targets and arguments) # 1st arg: make command line (targets and arguments)
# 2nd arg: directory to call make from # 2nd arg: directory to call make from