General QoL

This commit is contained in:
Jörn-Michael Miehe 2019-10-15 13:28:43 +02:00
parent 7f6b8952cb
commit d3434557fc

131
Makefile
View file

@ -1,7 +1,22 @@
##########
# COMMANDS
DOCKER:=docker
DOCKER_COMPOSE:=docker-compose
docker_bash=bash -c '$(1)'
# Check if needs root privileges?
PRIVGROUP:=docker
ifneq ($(findstring $(PRIVGROUP),$(shell groups)),$(PRIVGROUP))
DOCKER:=sudo $(DOCKER)
docker_bash=sudo $(docker_bash)
endif
######### #########
# CONFIGS # CONFIGS
CONF_WILDC:=$(wildcard $(PWD)/*.conf) CONF_WILDC:=$(wildcard $(PWD)/*.conf)
# apply source to *all* configs!
CONF_SOURCE:=$(patsubst %,. %;,$(CONF_WILDC)) CONF_SOURCE:=$(patsubst %,. %;,$(CONF_WILDC))
# extraction of env variables from *.conf files # extraction of env variables from *.conf files
@ -13,6 +28,7 @@ ifeq ($(CONF_DOCKERNET),)
$(error DOCKERNET not set in $(CONF_WILDC)) $(error DOCKERNET not set in $(CONF_WILDC))
endif endif
# docker network CIDR
CONF_DOCKERCIDR:=$(call confvalue,DOCKERCIDR) CONF_DOCKERCIDR:=$(call confvalue,DOCKERCIDR)
ifeq ($(CONF_DOCKERNET),) ifeq ($(CONF_DOCKERNET),)
$(error DOCKERCIDR not set in $(CONF_WILDC)) $(error DOCKERCIDR not set in $(CONF_WILDC))
@ -49,13 +65,17 @@ PROJ_NAMES:=$(basename $(PROJ_WILDC))
######### #########
# FUNCTIONS # FUNCTIONS
# different complexities of commands with root privileges # run DOCKER_COMPOSE:
# - in project directory # - in project directory
projsudo=cd "$<"; sudo bash -c "$(1)" # - with sourced *.conf files
# - additionally with sourced *.conf files # - with COMPOSE_PROJECT_NAME, CONFDIR and TARGETDIR set
confprojsudo=$(call projsudo,$(CONF_SOURCE) $(1)) kiwicompose=$(call docker_bash,\
# - only for compose: additionally with COMPOSE_PROJECT_NAME, CONFDIR and TARGETDIR set cd "$(<)"; \
sudocompose=$(call confprojsudo,COMPOSE_PROJECT_NAME="$(basename $<)" CONFDIR="$(CONF_TARGETROOT)/conf" TARGETDIR="$(CONF_TARGETROOT)/$<" docker-compose $(1)) $(CONF_SOURCE) \
COMPOSE_PROJECT_NAME="$(patsubst %$(PROJ_SUFFX),%,$<)" \
CONFDIR="$(CONF_TARGETROOT)/conf" \
TARGETDIR="$(CONF_TARGETROOT)/$<" \
$(DOCKER_COMPOSE) $(1))
######### #########
# TARGETS # TARGETS
@ -67,75 +87,118 @@ all: purge-conf up
######### #########
# manage the docker network (container name local DNS) # manage the docker network (container name local DNS)
$(FILE_DOCKERNET): $(FILE_DOCKERNET):
sudo docker network create --driver bridge --internal --subnet "$(CONF_DOCKERCIDR)" "$(CONF_DOCKERNET)" ||: -$(DOCKER) network create \
sudo mkdir -p "$(CONF_TARGETROOT)" --driver bridge \
sudo chmod 700 "$(CONF_TARGETROOT)" --internal \
sudo echo "$(CONF_DOCKERCIDR)" | sudo tee "$@" --subnet "$(CONF_DOCKERCIDR)" \
"$(CONF_DOCKERNET)"
@echo "Creating canary $(FILE_DOCKERNET) ..."
@$(DOCKER) run --rm \
-v "/:/mnt" -u root alpine:latest \
ash -c '\
mkdir -p "$(addprefix /mnt, $(CONF_TARGETROOT))"; \
echo "$(CONF_DOCKERCIDR)" > "$(addprefix /mnt, $(FILE_DOCKERNET))"; \
'
.PHONY: net-up .PHONY: net-up
net-up: $(FILE_DOCKERNET) net-up: $(FILE_DOCKERNET)
.PHONY: net-down .PHONY: net-down
net-down: down net-down: down
sudo docker network rm "$(CONF_DOCKERNET)" $(DOCKER) network rm "$(CONF_DOCKERNET)"
sudo rm "$(FILE_DOCKERNET)" @echo "Removing canary $(FILE_DOCKERNET) ..."
@$(DOCKER) run --rm \
-v "/:/mnt" -u root alpine:latest \
ash -c '\
rm -f "$(addprefix /mnt, $(FILE_DOCKERNET))"; \
'
######### #########
# sync project config directory to variable folder # sync project config directory to variable folder
.PHONY: %-copyconf
%-copyconf: %$(PROJ_SUFFX) # Dockerfile for
@if [ -d "$</conf" ]; then \ define DOCKERFILE_RSYNC
sudo rsync -r "$</conf" "$(CONF_TARGETROOT)"; \ FROM alpine:latest
echo "Synced '$</conf' to '$(CONF_TARGETROOT)'"; \ RUN apk --no-cache add rsync
fi endef
.PHONY: copy-conf
copy-conf:
ifneq ($(wildcard *${PROJ_SUFFX}/conf),)
$(eval export DOCKERFILE_RSYNC)
@echo "Building auxiliary image ldericher/kiwi-config:rsync ..."
@echo -e "$${DOCKERFILE_RSYNC}" | \
$(DOCKER) build -t ldericher/kiwi-config:rsync . -f- &> /dev/null
$(eval sources:=$(wildcard *${PROJ_SUFFX}/conf))
@echo "Syncing $(sources) to $(CONF_TARGETROOT) ..."
$(eval sources:=$(realpath $(sources)))
$(eval sources:=$(addprefix /mnt, $(sources)))
$(eval sources:=$(patsubst %,'%',$(sources)))
$(eval dest:='$(addprefix /mnt, $(CONF_TARGETROOT))')
@$(DOCKER) run --rm \
-v "/:/mnt" -u root ldericher/kiwi-config:rsync \
ash -c '\
rsync -r $(sources) $(dest); \
'
endif
.PHONY: purge-conf .PHONY: purge-conf
purge-conf: purge-conf:
sudo rm -rf "$(CONF_TARGETROOT)/conf" @echo "Emptying $(CONF_TARGETROOT)/conf ..."
@$(DOCKER) run --rm \
-v "/:/mnt" -u root alpine:latest \
ash -c '\
rm -rf "$(addprefix /mnt, $(CONF_TARGETROOT)/conf)"; \
'
######### #########
# manage all projects # manage all projects
.PHONY: up down update .PHONY: up down update
up: net-up $(patsubst %,%-copyconf,$(PROJ_NAMES)) $(patsubst %,%-up,$(PROJ_NAMES)) up: net-up copy-conf $(patsubst %,%-up,$(PROJ_NAMES))
down: $(patsubst %,%-down,$(PROJ_NAMES)) down: $(patsubst %,%-down,$(PROJ_NAMES))
update: $(patsubst %,%-update,$(PROJ_NAMES)) update: $(patsubst %,%-update,$(PROJ_NAMES))
######### #########
# manage single project # manage single project
.PHONY: %-up .PHONY: %-up
%-up: %$(PROJ_SUFFX) %-up: %$(PROJ_SUFFX) net-up
$(call sudocompose,up -d $(x)) $(call kiwicompose,up -d $(x))
.PHONY: %-down .PHONY: %-down
ifeq ($(x),) ifeq ($(x),)
%-down: %$(PROJ_SUFFX) %-down: %$(PROJ_SUFFX)
$(call sudocompose,down) $(call kiwicompose,down)
else else
%-down: %$(PROJ_SUFFX) %-down: %$(PROJ_SUFFX)
$(call sudocompose,stop $(x)) $(call kiwicompose,stop $(x))
$(call sudocompose,rm -f $(x)) $(call kiwicompose,rm -f $(x))
endif endif
.PHONY: %-pull .PHONY: %-pull
%-pull: %$(PROJ_SUFFX) %-pull: %$(PROJ_SUFFX)
$(call sudocompose,pull $(x)) $(call kiwicompose,pull --ignore-pull-failures $(x))
.PHONY: %-build .PHONY: %-build
%-build: %$(PROJ_SUFFX) %-build: %$(PROJ_SUFFX)
$(call sudocompose,build --pull $(x)) $(call kiwicompose,build --pull $(x))
.PHONY: %-logs .PHONY: %-logs
%-logs: %$(PROJ_SUFFX) %-logs: %$(PROJ_SUFFX)
$(call sudocompose,logs -t $(x)) 2>/dev/null | less -R +G $(call kiwicompose,logs -t $(x)) 2>/dev/null | less -R +G
.PHONY: %-logf .PHONY: %-logf
%-logf: %$(PROJ_SUFFX) %-logf: %$(PROJ_SUFFX)
$(call sudocompose,logs -tf --tail=10 $(x)) ||: $(call kiwicompose,logs -tf --tail=10 $(x)) ||:
ifneq ($(x),)
s?=bash s?=bash
.PHONY: %-sh .PHONY: %-sh
%-sh: %$(PROJ_SUFFX) %-sh: %$(PROJ_SUFFX)
$(call sudocompose,exec $(x) $(s)) ||: $(call kiwicompose,exec $(x) /bin/sh -c "[ -e /bin/$(s) ] && /bin/$(s) || /bin/sh")
endif
# enabling and disabling # enabling and disabling
.PHONY: %-enable %-disable .PHONY: %-enable %-disable
@ -152,7 +215,7 @@ s?=bash
# Arbitrary compose command # Arbitrary compose command
.PHONY: %-cmd .PHONY: %-cmd
%-cmd: %$(PROJ_SUFFX) %-cmd: %$(PROJ_SUFFX)
$(call sudocompose,$(x)) $(call kiwicompose,$(x))
######### #########
# project creation # project creation
@ -161,7 +224,7 @@ s?=bash
$(eval proj_dir:=$(patsubst %-new,%$(PROJ_SUFFX)$(DOWN_SUFFX),$@)) $(eval proj_dir:=$(patsubst %-new,%$(PROJ_SUFFX)$(DOWN_SUFFX),$@))
mkdir $(proj_dir) mkdir $(proj_dir)
$(eval export COMPOSEFILE) $(eval export COMPOSEFILE)
echo -e "$$COMPOSEFILE" > $(proj_dir)/docker-compose.yml echo -e "$${COMPOSEFILE}" > $(proj_dir)/docker-compose.yml
# default compose file # default compose file
define COMPOSEFILE define COMPOSEFILE
@ -172,7 +235,7 @@ networks:
default: default:
driver: bridge driver: bridge
# interconnects projects # interconnects projects
gassi: hubnet:
external: external:
name: $$DOCKERNET name: $$DOCKERNET
@ -182,6 +245,6 @@ services:
restart: unless-stopped restart: unless-stopped
networks: networks:
- default - default
- gassi - hubnet
[...] [...]
endef endef