1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-22 12:53:00 +00:00
kiwi-scp/Makefile

257 lines
5.8 KiB
Makefile
Raw Normal View History

2019-10-15 11:28:43 +00:00
##########
# COMMANDS
DOCKER:=docker
DOCKER_COMPOSE:=docker-compose
docker_bash=$(1)
2019-10-15 11:28:43 +00:00
# Check if needs root privileges?
PRIVGROUP:=docker
ifneq ($(findstring $(PRIVGROUP),$(shell groups)),$(PRIVGROUP))
DOCKER:=sudo $(DOCKER)
docker_bash=sudo bash -c '$(1)'
2019-10-15 11:28:43 +00:00
endif
2018-09-14 08:51:40 +00:00
#########
# CONFIGS
2018-09-13 23:00:30 +00:00
CONF_WILDC:=$(wildcard $(PWD)/*.conf)
2019-10-15 11:28:43 +00:00
# apply source to *all* configs!
2018-09-13 23:00:30 +00:00
CONF_SOURCE:=$(patsubst %,. %;,$(CONF_WILDC))
# extraction of env variables from *.conf files
confvalue=$(shell $(CONF_SOURCE) echo -n $${$(1)})
# docker network name
CONF_KIWI_HUB_NAME:=$(call confvalue,KIWI_HUB_NAME)
ifeq ($(CONF_KIWI_HUB_NAME),)
$(error KIWI_HUB_NAME not set in $(CONF_WILDC))
2018-09-13 23:00:30 +00:00
endif
2019-10-15 11:28:43 +00:00
# docker network CIDR
CONF_KIWI_HUB_CIDR:=$(call confvalue,KIWI_HUB_CIDR)
ifeq ($(CONF_KIWI_HUB_CIDR),)
$(error KIWI_HUB_CIDR not set in $(CONF_WILDC))
2018-12-06 13:43:26 +00:00
endif
2018-09-13 23:00:30 +00:00
# persistent data directory
CONF_TARGET_ROOT:=$(call confvalue,TARGET_ROOT)
ifeq ($(CONF_TARGET_ROOT),)
$(error TARGET_ROOT not set in $(CONF_WILDC))
2018-09-13 23:00:30 +00:00
endif
2018-09-14 08:51:40 +00:00
# suffix for project directories
PROJ_SUFFX:=$(call confvalue,SUFFIX_PROJECT)
ifeq ($(PROJ_SUFFX),)
$(error SUFFIX_PROJECT not set in $(CONF_WILDC))
endif
# suffix for disabled project directories
DOWN_SUFFX:=$(call confvalue,SUFFIX_DOWN)
2018-09-14 09:04:59 +00:00
ifeq ($(DOWN_SUFFX),)
2018-09-14 08:51:40 +00:00
$(error SUFFIX_DOWN not set in $(CONF_WILDC))
endif
#########
# CONSTANTS
2018-09-13 23:00:30 +00:00
# file to store docker network cidr
KIWI_HUB_FILE:=$(CONF_TARGET_ROOT)/up-$(CONF_KIWI_HUB_NAME)
2018-09-13 23:00:30 +00:00
# remove any suffix $2 from $1
rmsuffix=$(patsubst %$2,%,$1)
# remove project suffix from $1
projname=$(call rmsuffix,$1,$(PROJ_SUFFX))
2018-09-13 23:00:30 +00:00
# project directory handling
PROJ_WILDC:=$(wildcard *$(PROJ_SUFFX))
PROJ_NAMES:=$(call projname,$(PROJ_WILDC))
2018-09-13 23:00:30 +00:00
2018-09-14 08:51:40 +00:00
#########
# FUNCTIONS
2019-10-15 11:28:43 +00:00
# run DOCKER_COMPOSE:
# - in project directory
# - with sourced *.conf files
# - with COMPOSE_PROJECT_NAME, CONFDIR and TARGETDIR set
kiwicompose=$(call docker_bash,\
cd "$<"; \
2019-10-15 11:28:43 +00:00
$(CONF_SOURCE) \
COMPOSE_PROJECT_NAME="$(call projname,$<)" \
CONFDIR="$(CONF_TARGET_ROOT)/conf" \
TARGETDIR="$(CONF_TARGET_ROOT)/$<" \
2019-10-15 11:28:43 +00:00
$(DOCKER_COMPOSE) $(1))
2018-09-13 23:00:30 +00:00
#########
# TARGETS
# default target
.PHONY: all
all: purge-conf up
#########
# manage the docker network (container name local DNS)
$(KIWI_HUB_FILE):
2019-10-15 11:28:43 +00:00
-$(DOCKER) network create \
--driver bridge \
--internal \
--subnet "$(CONF_KIWI_HUB_CIDR)" \
"$(CONF_KIWI_HUB_NAME)"
@echo "Creating canary $(KIWI_HUB_FILE) ..."
2019-10-15 11:28:43 +00:00
@$(DOCKER) run --rm \
-v "/:/mnt" -u root alpine:latest \
ash -c '\
mkdir -p "$(addprefix /mnt, $(CONF_TARGET_ROOT))"; \
echo "$(CONF_KIWI_HUB_CIDR)" > "$(addprefix /mnt, $(KIWI_HUB_FILE))"; \
2019-10-15 11:28:43 +00:00
'
2018-09-13 23:00:30 +00:00
.PHONY: net-up
net-up: $(KIWI_HUB_FILE)
2018-09-13 23:00:30 +00:00
.PHONY: net-down
net-down: down
$(DOCKER) network rm "$(CONF_KIWI_HUB_NAME)"
@echo "Removing canary $(KIWI_HUB_FILE) ..."
2019-10-15 11:28:43 +00:00
@$(DOCKER) run --rm \
-v "/:/mnt" -u root alpine:latest \
ash -c '\
rm -f "$(addprefix /mnt, $(KIWI_HUB_FILE))"; \
2019-10-15 11:28:43 +00:00
'
2018-09-13 23:00:30 +00:00
#########
# sync project config directory to variable folder
2019-10-15 11:28:43 +00:00
# Dockerfile for running rsync as root
2019-10-15 11:28:43 +00:00
define DOCKERFILE_RSYNC
FROM alpine:latest
RUN apk --no-cache add rsync
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_TARGET_ROOT) ..."
2019-10-15 11:28:43 +00:00
$(eval sources:=$(realpath $(sources)))
$(eval sources:=$(addprefix /mnt, $(sources)))
$(eval sources:=$(patsubst %,'%',$(sources)))
$(eval dest:='$(addprefix /mnt, $(CONF_TARGET_ROOT))')
2019-10-15 11:28:43 +00:00
@$(DOCKER) run --rm \
-v "/:/mnt" -u root ldericher/kiwi-config:rsync \
ash -c '\
rsync -r $(sources) $(dest); \
'
endif
2018-09-13 23:00:30 +00:00
.PHONY: purge-conf
purge-conf:
@echo "Emptying $(CONF_TARGET_ROOT)/conf ..."
2019-10-15 11:28:43 +00:00
@$(DOCKER) run --rm \
-v "/:/mnt" -u root alpine:latest \
ash -c '\
rm -rf "$(addprefix /mnt, $(CONF_TARGET_ROOT)/conf)"; \
2019-10-15 11:28:43 +00:00
'
2018-09-13 23:00:30 +00:00
#########
# manage all projects
.PHONY: up down update
2019-10-15 11:28:43 +00:00
up: net-up copy-conf $(patsubst %,%-up,$(PROJ_NAMES))
2018-09-13 23:00:30 +00:00
down: $(patsubst %,%-down,$(PROJ_NAMES))
update: $(patsubst %,%-update,$(PROJ_NAMES))
#########
# manage single project
.PHONY: %-up
2019-10-15 11:28:43 +00:00
%-up: %$(PROJ_SUFFX) net-up
$(call kiwicompose,up -d $(x))
2018-09-13 23:00:30 +00:00
.PHONY: %-down
ifeq ($(x),)
%-down: %$(PROJ_SUFFX)
2019-10-15 11:28:43 +00:00
$(call kiwicompose,down)
2018-09-13 23:00:30 +00:00
else
%-down: %$(PROJ_SUFFX)
2019-10-15 11:28:43 +00:00
$(call kiwicompose,stop $(x))
$(call kiwicompose,rm -f $(x))
2018-09-13 23:00:30 +00:00
endif
.PHONY: %-pull
%-pull: %$(PROJ_SUFFX)
2019-10-15 11:28:43 +00:00
$(call kiwicompose,pull --ignore-pull-failures $(x))
2018-09-13 23:00:30 +00:00
.PHONY: %-build
%-build: %$(PROJ_SUFFX)
2019-10-15 11:28:43 +00:00
$(call kiwicompose,build --pull $(x))
2018-09-13 23:00:30 +00:00
.PHONY: %-logs
%-logs: %$(PROJ_SUFFX)
2019-10-15 11:28:43 +00:00
$(call kiwicompose,logs -t $(x)) 2>/dev/null | less -R +G
2018-09-13 23:00:30 +00:00
.PHONY: %-logf
%-logf: %$(PROJ_SUFFX)
2019-10-15 11:28:43 +00:00
$(call kiwicompose,logs -tf --tail=10 $(x)) ||:
2018-09-13 23:00:30 +00:00
2019-10-15 11:28:43 +00:00
ifneq ($(x),)
2018-09-13 23:00:30 +00:00
s?=bash
.PHONY: %-sh
%-sh: %$(PROJ_SUFFX)
2019-10-15 11:28:43 +00:00
$(call kiwicompose,exec $(x) /bin/sh -c "[ -e /bin/$(s) ] && /bin/$(s) || /bin/sh")
endif
2018-09-13 23:00:30 +00:00
# enabling and disabling
.PHONY: %-enable %-disable
%-enable: %$(PROJ_SUFFX)$(DOWN_SUFFX)
mv "$<" "$(call projname,$(call rmsuffix,$<,$(DOWN_SUFFX)))$(PROJ_SUFFX)"
%-disable: %$(PROJ_SUFFX)
mv "$<" "$<$(DOWN_SUFFX)"
2018-09-13 23:00:30 +00:00
# Combinations
.PHONY: %-update
%-update: %$(PROJ_SUFFX) %-build %-pull copy-conf
$(MAKE) $(call projname,$<)-down
$(MAKE) $(call projname,$<)-up
2018-09-13 23:00:30 +00:00
# Arbitrary compose command
.PHONY: %-cmd
%-cmd: %$(PROJ_SUFFX)
2019-10-15 11:28:43 +00:00
$(call kiwicompose,$(x))
2018-09-13 23:00:30 +00:00
#########
# project creation
.PHONY: %-new
%-new:
$(eval proj_dir:=$(patsubst %-new,%$(PROJ_SUFFX)$(DOWN_SUFFX),$@))
mkdir $(proj_dir)
$(eval export COMPOSEFILE)
2019-10-15 11:28:43 +00:00
echo -e "$${COMPOSEFILE}" > $(proj_dir)/docker-compose.yml
# default compose file
define COMPOSEFILE
2018-12-06 13:43:26 +00:00
version: "2"
networks:
2018-12-06 14:01:37 +00:00
# reachable from outside
default:
driver: bridge
# interconnects projects
2020-08-10 15:38:02 +00:00
kiwihub:
external:
name: $$KIWI_HUB_NAME
services:
something:
image: maintainer/repo:tag
restart: unless-stopped
2018-12-06 13:43:26 +00:00
networks:
- default
2020-08-10 15:38:02 +00:00
- kiwihub
[...]
endef