1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-12-26 02:43:02 +00:00
kiwi-scp/Makefile

176 lines
4 KiB
Makefile
Raw Normal View History

2018-09-14 08:51:40 +00:00
#########
# CONFIGS
2018-09-13 23:00:30 +00:00
CONF_WILDC:=$(wildcard $(PWD)/*.conf)
CONF_SOURCE:=$(patsubst %,. %;,$(CONF_WILDC))
# extraction of env variables from *.conf files
confvalue=$(shell $(CONF_SOURCE) echo -n $${$(1)})
# docker network name
CONF_DOCKERNET:=$(call confvalue,DOCKERNET)
ifeq ($(CONF_DOCKERNET),)
$(error DOCKERNET not set in $(CONF_WILDC))
endif
# persistent data directory
CONF_TARGETROOT:=$(call confvalue,TARGETROOT)
ifeq ($(CONF_TARGETROOT),)
$(error TARGETROOT not set in $(CONF_WILDC))
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
FILE_DOCKERNET:=$(CONF_TARGETROOT)/up-$(CONF_DOCKERNET)
# project directory handling
PROJ_WILDC:=$(wildcard *$(PROJ_SUFFX))
2018-09-13 23:00:30 +00:00
PROJ_NAMES:=$(basename $(PROJ_WILDC))
2018-09-14 08:51:40 +00:00
#########
# FUNCTIONS
2018-09-13 23:00:30 +00:00
# different complexities of commands with root privileges
# - in project directory
projsudo=cd "$<"; sudo bash -c "$(1)"
# - additionally with sourced *.conf files
confprojsudo=$(call projsudo,$(CONF_SOURCE) $(1))
# - only for compose: additionally with COMPOSE_PROJECT_NAME, CONFDIR and TARGETDIR set
sudocompose=$(call confprojsudo,COMPOSE_PROJECT_NAME="$(basename $<)" CONFDIR="$(CONF_TARGETROOT)/conf" TARGETDIR="$(CONF_TARGETROOT)/$<" docker-compose $(1))
#########
# TARGETS
# default target
.PHONY: all
all: purge-conf up
#########
# manage the docker network (container name local DNS)
$(FILE_DOCKERNET):
sudo docker network create "$(CONF_DOCKERNET)" ||:
sudo mkdir -p "$(CONF_TARGETROOT)"
sudo chmod 700 "$(CONF_TARGETROOT)"
sudo docker network inspect -f '{{(index .IPAM.Config 0).Subnet}}' "$(CONF_DOCKERNET)" | sudo tee "$@"
.PHONY: net-up
net-up: $(FILE_DOCKERNET)
.PHONY: net-down
net-down: down
sudo docker network rm $(CONF_DOCKERNET)
sudo rm $(FILE_DOCKERNET)
#########
# sync project config directory to variable folder
.PHONY: %-copyconf
%-copyconf: %$(PROJ_SUFFX)
2018-09-13 23:00:30 +00:00
@if [ -d "$</conf" ]; then \
sudo rsync -r "$</conf" "$(CONF_TARGETROOT)"; \
echo "Synced '$</conf' to '$(CONF_TARGETROOT)'"; \
fi
.PHONY: purge-conf
purge-conf:
sudo rm -rf "$(CONF_TARGETROOT)/conf"
#########
# manage all projects
.PHONY: up down update
2018-09-25 01:40:02 +00:00
up: net-up $(patsubst %,%-copyconf,$(PROJ_NAMES)) $(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
2018-09-25 01:40:02 +00:00
%-up: %$(PROJ_SUFFX)
2018-09-13 23:00:30 +00:00
$(call sudocompose,up -d $(x))
.PHONY: %-down
ifeq ($(x),)
%-down: %$(PROJ_SUFFX)
2018-09-13 23:00:30 +00:00
$(call sudocompose,down)
else
%-down: %$(PROJ_SUFFX)
2018-09-13 23:00:30 +00:00
$(call sudocompose,stop $(x))
$(call sudocompose,rm -f $(x))
endif
.PHONY: %-pull
%-pull: %$(PROJ_SUFFX)
2018-09-13 23:00:30 +00:00
$(call sudocompose,pull $(x))
.PHONY: %-build
%-build: %$(PROJ_SUFFX)
2018-09-13 23:00:30 +00:00
$(call sudocompose,build --pull $(x))
.PHONY: %-logs
%-logs: %$(PROJ_SUFFX)
2018-09-13 23:00:30 +00:00
$(call sudocompose,logs -t $(x)) 2>/dev/null | less -R +G
.PHONY: %-logf
%-logf: %$(PROJ_SUFFX)
2018-09-13 23:00:30 +00:00
$(call sudocompose,logs -tf --tail=10 $(x)) ||:
s?=bash
.PHONY: %-sh
%-sh: %$(PROJ_SUFFX)
2018-09-13 23:00:30 +00:00
$(call sudocompose,exec $(x) $(s)) ||:
# enabling and disabling
.PHONY: %-enable %-disable
%-enable: %$(PROJ_SUFFX)$(DOWN_SUFFX)
2018-09-13 23:00:30 +00:00
mv "$<" "$(basename $<)"
%-disable: %$(PROJ_SUFFX)
mv "$<" "$<$(DOWN_SUFFX)"
2018-09-13 23:00:30 +00:00
# Combinations
.PHONY: %-update
%-update: %$(PROJ_SUFFX) %-build %-pull
2018-09-13 23:00:30 +00:00
$(MAKE) $(basename $<)-up
# Arbitrary compose command
.PHONY: %-cmd
%-cmd: %$(PROJ_SUFFX)
2018-09-13 23:00:30 +00:00
$(call sudocompose,$(x))
#########
# project creation
.PHONY: %-new
%-new:
$(eval proj_dir:=$(patsubst %-new,%$(PROJ_SUFFX)$(DOWN_SUFFX),$@))
mkdir $(proj_dir)
$(eval export COMPOSEFILE)
echo -e "$$COMPOSEFILE" > $(proj_dir)/docker-compose.yml
# default compose file
define COMPOSEFILE
2018-09-14 08:51:40 +00:00
version: "3"
networks:
default:
external:
name: $$DOCKERNET
services:
something:
image: maintainer/repo:tag
restart: unless-stopped
[...]
endef