wip: external app

This commit is contained in:
Yann Autissier 2022-11-02 17:42:39 +01:00
parent 5d4d2de910
commit 9bdc761d47
6 changed files with 122 additions and 60 deletions

View File

@ -60,7 +60,7 @@ endif
# function docker-compose: Run docker-compose with arg 1
define docker-compose
$(call INFO,docker-compose,$(1))
$(if $(DOCKER_RUN),$(call docker-build,$(MYOS)/docker/compose,docker/compose:$(COMPOSE_VERSION)))
$(if $(DOCKER_RUN),$(call docker-build,$(MYOS)/docker/compose,docker/compose:$(COMPOSE_VERSION)))
$(if $(COMPOSE_FILE),$(call run,$(DOCKER_COMPOSE) $(patsubst %,-f %,$(COMPOSE_FILE)) -p $(if $(filter node,$(firstword $(subst /, ,$(STACK)))),$(NODE_COMPOSE_PROJECT_NAME),$(if $(filter User,$(firstword $(subst /, ,$(STACK)))),$(USER_COMPOSE_PROJECT_NAME),$(COMPOSE_PROJECT_NAME))) $(1)))
endef
# function docker-compose-exec-sh: Run docker-compose-exec sh -c 'arg 2' in service 1
@ -78,7 +78,7 @@ define docker-build
$(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 false,$(DOCKER_BUILD_CACHE)),$(if $(image_id),,true)))
$(if $(build_image),$(RUN) docker build $(DOCKER_BUILD_ARGS) --build-arg DOCKER_BUILD_DIR="$(path)" $(DOCKER_BUILD_LABEL) --tag $(tag) $(if $(target),--target $(target)) -f $(path)/Dockerfile .,$(call INFO,docker image $(tag) has id $(image_id)))
$(if $(build_image),$(RUN) docker build $(DOCKER_BUILD_ARGS) --build-arg DOCKER_BUILD_DIR="$(path)" $(DOCKER_BUILD_LABEL) --tag $(tag) $(if $(target),--target $(target)) -f $(path)/Dockerfile $(or $(DOCKER_BUILD_DIR),.),$(call INFO,docker image $(tag) has id $(image_id)))
endef
# function docker-commit: Commit docker image
define docker-commit

View File

@ -1,35 +1,70 @@
CMDS += apps-install install-app
##
# COMMON
# target $(APP): Call update-app
# target $(APP): Call app-update
.PHONY: $(APP)
$(APP): APP_DIR := $(RELATIVE)$(APP)
$(APP): myos-user
$(call update-app)
$(call app-update)
# target install-app install-apps: Call install-app for each ARGS
.PHONY: install-app install-apps
install-app install-apps: myos-user install-app-required
$(foreach url,$(ARGS),$(call install-app,$(url)))
# target app-%: Call app-$(command) for APP in APP_DIR
## it splits % on dashes and extracts app from the beginning and command from the last part of %
## ex: app-foo-build will call app-build for app foo in ../foo
.PHONY: app-%
app-%:
$(eval app := $(subst -$(lastword $(subst -, ,$*)),,$*))
$(eval command := $(lastword $(subst -, ,$*)))
$(if $(findstring -,$*), \
$(if $(filter app-$(command),$(.VARIABLES)), \
$(eval APP := $(app)) \
$(eval APP_DIR := $(RELATIVE)$(app)) \
$(eval COMPOSE_PROJECT_NAME := $(USER)-$(app)-$(ENV)$(addprefix -,$(subst /,,$(subst -,,$(APP_PATH))))) \
$(eval DOCKER_REPOSITORY := $(subst -,/,$(subst _,/,$(COMPOSE_PROJECT_NAME)))) \
$(eval DOCKER_IMAGE_TAG := $(if $(filter $(ENV),$(ENV_DEPLOY)),$(VERSION),$(if $(DRONE_BUILD_NUMBER),$(DRONE_BUILD_NUMBER),latest))) \
$(eval DOCKER_IMAGE := $(DOCKER_REPOSITORY)/$(app):$(DOCKER_IMAGE_TAG)) \
$(call app-$(command)) \
) \
)
# target install-app-required: Call install-app for each APP_REQUIRED
.PHONY: install-app-required
install-app-required: myos-user
$(foreach url,$(APP_REQUIRED),$(call install-app,$(url)))
# target app-required-install: Call app-install for each APP_REQUIRED
.PHONY: app-required-install
app-required-install: myos-user
$(foreach url,$(APP_REQUIRED),$(call app-install,$(url)))
# target apps-build: Call app-build for each APPS
.PHONY: apps-build
apps-build: myos-user
$(foreach app,$(APPS),$(call app-build,$(RELATIVE)$(app)))
# target apps-install install-app: Call app-install for each ARGS
.PHONY: apps-install install-app
apps-install install-app: myos-user app-required-install
$(foreach url,$(ARGS),$(call app-install,$(url)))
# target apps-update: Call app-update target for each APPS
.PHONY: apps-update
apps-update:
$(foreach app,$(APPS),$(call make,update-app APP_NAME=$(app)))
# target debug: Print more informations
.PHONY: debug
debug:
$(MAKE) help profile DEBUG=true
# target debug-%: Print value of %
.PHONY: debug-%
debug-%: $(if $(DEBUG),context-%) ;
# target install-bin-%; Call ansible-run-localhost when bin % is not available
.PHONY: install-bin-%
install-bin-%:;
$(if $(shell type $* 2>/dev/null),,$(call make,ansible-run-localhost))
# target $(SHARED): Create SHARED folder
$(SHARED):
$(RUN) mkdir -p $(SHARED)
# target update-apps: Call update-app target for each APPS
.PHONY: update-apps
update-apps:
$(foreach app,$(APPS),$(call make,update-app APP_NAME=$(app)))
# target profile: Print timing informations
.PHONY: profile
profile: context-ELAPSED_TIME
# target update-app: Fire update-app-% for APP_NAME
.PHONY: update-app
@ -42,7 +77,7 @@ update-app-%: % ;
# target update-config: Update config files
.PHONY: update-config
update-config: myos-user
$(call update-app,$(CONFIG_REPOSITORY),$(CONFIG))
$(call app-update,$(CONFIG_REPOSITORY),$(CONFIG))
# target update-hosts: Update /etc/hosts
# on local host
@ -80,3 +115,7 @@ update-upstream: myos-user .git/refs/remotes/upstream/master
# target shared: Fire SHARED
.PHONY: update-shared
update-shared: $(SHARED)
# target $(SHARED): Create SHARED folder
$(SHARED):
$(RUN) mkdir -p $(SHARED)

View File

@ -1,16 +0,0 @@
##
# DEBUG
# target debug: Print more informations
.PHONY: debug
debug:
$(MAKE) help profile DEBUG=true
# target debug-%: Print value of %
.PHONY: debug-%
debug-%: $(if $(DEBUG),context-%) ;
# target profile: Print timing informations
.PHONY: profile
profile: context-ELAPSED_TIME

60
make/def.app.mk Normal file
View File

@ -0,0 +1,60 @@
# function app-build: Call docker-build or docker-compose-build in app 1
define app-build
$(call INFO,app-build,$(1)$(comma) $(2))
$(eval dir := $(or $(1), $(APP_DIR)))
$(eval DOCKER_BUILD_DIR := $(dir))
$(if $(wildcard $(dir)/Dockerfile), \
$(call docker-build,$(dir),,''), \
$(call ERROR,Unable to find docker file $(dir)/Dockerfile) \
)
endef
# function app-install: Run 'git clone url 1 dir 2' or Call app-update with url 1 dir 2
define app-install
$(call INFO,app-install,$(1)$(comma) $(2))
$(eval url := $(or $(1), $(APP_REPOSITORY_URL)))
$(eval dir := $(or $(2), $(RELATIVE)$(lastword $(subst /, ,$(url)))))
$(if $(wildcard $(dir)/.git), \
$(call app-update,$(url),$(dir)), \
$(RUN) git clone $(QUIET) $(url) $(dir) \
)
endef
# function app-rebuild: Call app-build with DOCKER_BUILD_CACHE=false
define app-rebuild
$(call INFO,app-rebuild,$(1)$(comma) $(2))
$(eval DOCKER_BUILD_CACHE := false)
$(call app-build,$(1),$(2))
endef
# function app-run: Call docker-run or docker-compose-run in app 1
define app-run
$(call INFO,app-run,$(1)$(comma) $(2))
$(eval image := $(or $(1), $(DOCKER_IMAGE)))
$(eval args := $(or $(2), $(ARGS)))
$(if $(shell docker images -q $(image) 2>/dev/null), \
$(call docker-run,$(args),$(image)), \
$(call ERROR,Unable to find docker image $(image)) \
)
endef
# function app-up: Call docker-run in app 1
define app-up
$(call INFO,app-up,$(1))
$(eval image := $(or $(1), $(DOCKER_IMAGE)))
$(if $(shell docker images -q $(image) 2>/dev/null), \
$(call docker-run,$(image)), \
$(call ERROR,Unable to find docker image $(image)) \
)
endef
# function app-update: Run 'cd dir 1 && git pull' or Call app-install
define app-update
$(call INFO,app-update,$(1)$(comma) $(2))
$(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)) \
)
endef

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)
CMDS ?= exec exec:% exec@% install-app install-apps run run:% run@%
CMDS ?= app-%-run exec exec:% exec@% run run:% run@%
COLOR_BLUE ?= \033[01;34m
COLOR_BROWN ?= \033[33m
COLOR_CYAN ?= \033[36m
@ -250,17 +250,6 @@ define env-run
$(call env-exec,$(or $(1),$(SHELL)))
endef
# function install-app: Run 'git clone url 1 dir 2' or Call update-app with url 1 dir 2
define install-app
$(call INFO,install-app,$(1)$(comma) $(2))
$(eval url := $(or $(1), $(APP_REPOSITORY_URL)))
$(eval dir := $(or $(2), $(RELATIVE)$(lastword $(subst /, ,$(url)))))
$(if $(wildcard $(dir)/.git), \
$(call update-app,$(url),$(dir)), \
$(RUN) git clone $(QUIET) $(url) $(dir) \
)
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
@ -287,17 +276,6 @@ define make
$(if $(filter true,$(DRYRUN_RECURSIVE)),$(MAKE) $(MAKE_DIR) $(patsubst %,-o %,$(MAKE_OLDFILE)) MAKE_OLDFILE="$(MAKE_OLDFILE)" DRYRUN=$(DRYRUN) RECURSIVE=$(RECURSIVE) $(MAKE_ARGS) $(cmd))
endef
# function update-app: Run 'cd dir 1 && git pull' or Call install-app
define update-app
$(call INFO,update-app,$(1)$(comma) $(2))
$(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 install-app,$(url),$(dir)) \
)
endef
# function TARGET:ENV: Create a new target ending with :env
## it sets ENV, ENV_FILE and calls original target
define TARGET:ENV

View File

@ -69,6 +69,7 @@ endif
ifeq ($(SYSTEM),Darwin)
ifneq ($(DOCKER), true)
PACKER_QEMU_ACCELERATOR := hvf
# PACKER_QEMU_ARGS += -cpu Penryn,vendor=GenuineIntel
PACKER_QEMU_ARGS += -cpu host
else
PACKER_QEMU_ACCELERATOR := tcg