Onyx-API/docker/Makefile
2023-09-15 18:21:04 +02:00

319 lines
7.9 KiB
Makefile

##
# Makefile to help manage docker-compose services
#
# Built on top of those resources:
# https://gist.github.com/iNamik/cafef08f9d931cbf7acb012075052912
# https://github.com/krom/docker-compose-makefile/tree/master/samples
#
# Include environment files
include ../.env
include .env
export
THIS_FILE := Makefile
DOCKER := docker
DOCKER_COMPOSE := docker-compose
DOCKER_COMPOSE_FILES := -f docker-compose.yml
ifeq ($(DOCKER_ENV),staging)
DOCKER_COMPOSE_FILES := -f docker-compose.yml
endif
ifeq ($(DOCKER_ENV),production)
DOCKER_COMPOSE_FILES := -f docker-compose.yml
endif
CERT_DOMAIN := api.onyx.local
IMAGE_DEFAULT := onyx-api
CONTAINER_DEFAULT := onyx-api-1
SERVICES_DEFAULT := api
SERVICE_DEFAULT := api
NETWORK := app
NETWORK_PROXY := nginx-proxy
BACKUP_SERVICE := backup
RESTORE_SERVICE := restore
SHELL_CMD := /bin/bash
container ?= $(CONTAINER_DEFAULT)
image ?= $(IMAGE_DEFAULT)
service ?=
services ?= $(SERVICES_DEFAULT)
test ?= A
package ?= chargers
.DEFAULT_GOAL := help
##
# help
#
help:
ifeq ($(CONTAINER_DEFAULT),)
$(warning WARNING: CONTAINER_DEFAULT is not set. Please edit makefile)
endif
@echo
@echo "Make targets:"
@echo
@cat $(THIS_FILE) | \
sed -n -E 's/^([^.][^: ]+)\s*:(([^=#]*##\s*(.*[^[:space:]])\s*)|[^=].*)$$/ \1 \4/p' | \
sort -u | \
expand -t15
@echo
@echo
@echo "Target arguments:"
@echo
@echo " " "service" "\t" "Target service for docker-compose actions (default=all-services)"
@echo " " " " "\t" " - make start"
@echo " " " " "\t" " - make start services=app"
@echo " " "services" "\t" "Target services for docker-compose actions (default=all-services, space separated)"
@echo " " " " "\t" " - make stop services='app mysql'"
@echo " " "container""\t" "Target container for docker actions (default='$(CONTAINER_DEFAULT)')"
@echo " " " " "\t" " - make bash container=$(container)"
@echo " " "image" "\t" "Target image for interactive shell (default='$(IMAGE_DEFAULT)')"
@echo " " " " "\t" " - make it image=$(image)"
@echo " " "test" "\t" "Run custom test (default='$(test)')"
@echo " " " " "\t" " - make test test=$(test)"
@echo " " "testdlv" "\t" "Run a 'dlv test' with a custom package (default='$(package)')"
@echo " " " " "\t" " - make testdlv package=$(package)"
##
# services
#
services: ## Lists services
@$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) ps --services
##
# start
#
all: dev ## See 'dev'
start: dev ## See 'dev'
dev: ## Start containers for development [service|services]
@$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) up -d $(services)
$(MAKE) logs
##
# stop
#
stop: ## Stop containers [service|services]
@$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) stop $(services)
##
# restart
#
restart: ## Restart containers [service|services]
@$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) restart $(services)
$(MAKE) logs
##
# down
#
down: ## Removes containers (preserves images and volumes)
@$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) down
create-network:
ifeq ($(DOCKER_ENV),local)
@echo
@echo "Creating proper network ($(NETWORK)) for the project"
@$(DOCKER) network create $(NETWORK) 2> /dev/null || true
@echo "Creating proper network ($(NETWORK_PROXY)) for the nginx proxy"
@$(DOCKER) network create $(NETWORK_PROXY) 2> /dev/null || true
endif
##
# build
#
build: create-network ## Builds service images [service|services]
ifeq ($(DOCKER_ENV),local)
if [ ! -d "_data/certs" ]; then mkdir -p "_data/certs"; fi
test -f _data/certs/$(CERT_DOMAIN).key && test -f _data/certs/$(CERT_DOMAIN).crt || mkcert -key-file _data/certs/$(CERT_DOMAIN).key -cert-file _data/certs/$(CERT_DOMAIN).crt $(CERT_DOMAIN) "*.$(CERT_DOMAIN)"
endif
@$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) build $(services)
##
# rebuild
#
rebuild: ## Build containers without cache [service|services]
@$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) build --no-cache $(services)
##
# ps
#
status: ps ## See 'ps'
ps: ## Show status of containers
@$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) ps
##
# interact
#
interact: it ## See 'it'
it: ## Run a new container in interactive mode. Needs image name [image]
ifeq ($(image),)
$(error ERROR: 'image' is not set. Please provide 'image=' argument or edit makefile and set CONTAINER_DEFAULT)
endif
@echo
@echo "Starting interactive shell ($(SHELL_CMD)) in image container '$(image)'"
@$(DOCKER) run -it --entrypoint "$(SHELL_CMD)" $(image)
##
# bash
#
sh: bash ## See 'bash'
shell: bash ## See 'bash'
bash: ## Brings up a shell in default (or specified) container [container]
ifeq ($(container),)
$(error ERROR: 'container' is not set. Please provide 'container=' argument or edit makefile and set CONTAINER_DEFAULT)
endif
@echo
@echo "Starting shell ($(SHELL_CMD)) in container '$(container)'"
@$(DOCKER) exec -it "$(container)" "$(SHELL_CMD)"
##
# attach
#
at: attach ## See 'attach'
attach: ## Attach to a running container [container]
ifeq ($(container),)
$(error ERROR: 'container' is not set. Please provide 'container=' argument or edit makefile and set CONTAINER_DEFAULT)
endif
@echo
@echo "Attaching to '$(container)'"
@$(DOCKER) attach $(container)
##
# log
#
log: ## Shows log from a specific container (in 'follow' mode) [container]
ifeq ($(container),)
$(error ERROR: 'container' is not set. Please provide 'container=' argument or edit makefile and set CONTAINER_DEFAULT)
endif
@echo
@echo "Log in $(container)"
@$(DOCKER) logs -f $(container)
##
# logs
#
logs: ## Shows output of running containers (in 'follow' mode) [service|services]
@echo
@echo "Logs in $(services)"
@$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) logs -f $(services)
##
# rmimages
#
rmimages: ## Remove images
@echo
@echo "Remove local images"
@$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) down --rmi local
##
# rmvols
#
rmvols: ## Remove volumes
@echo
@echo "Remove named volumes declared in the volumes section of the Compose file and anonymous volumes attached to containers"
@$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) down -v
##
# rm
#
rm: rmimages rmvols ## Remove volumes and images
##
# clean
#
clean: ## Remove containers, images and volumes
@echo
@echo "Remove containers, images and volumes"
@$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) down --volumes --rmi all
##
# backup
#
backup: ## Run 'backup' service
@echo
@echo "Running '$(BACKUP_SERVICE)'"
@$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) run $(BACKUP_SERVICE)
##
# restore
#
restore: ## Run 'restore' service
@echo
@echo "Running '$(BACKUP_SERVICE)'"
@$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) run $(RESTORE_SERVICE)
##
# tests
#
tests: ## Run all the battery tests
@echo
@echo "Running tests"
@$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) run --rm tests
##
# test
#
test: ## Run 'tests' service with a custom test [test=A]
ifneq ($(DOCKER_ENV),production)
@echo
@echo "Running test $(test)"
@$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) run --rm tests gotest -v -run $(test) ./...
endif
##
# testdlv
#
testdlv: ## Run a 'dlv test' with a custom package for debug purposes [package=chargers]
ifneq ($(DOCKER_ENV),production)
@echo
@echo "Running test $(test)"
@$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) run --rm tests dlv test /code/internal/$(package)
endif
##
# recreate_test_db
#
recreate_test_db: ## Recrete test db
ifneq ($(DOCKER_ENV),production)
@echo
@echo "Recreate test db from init sql schemas"
@$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) stop $(MYSQL_TEST)
@$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) rm $(MYSQL_TEST)
@$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) up -d $(MYSQL_TEST)
endif
##
# dumplog
#
dumplog: ## Run 'dumplog' [service|services]
if [ ! -d "_data/logs" ]; then mkdir -p "_data/logs"; fi
@echo
@echo "Running 'dumplog'"
@$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) logs --no-color --since 24h --timestamps $(services)
.PHONY: help services all start dev stop restart down create-network build rebuild status ps interact it sh shell bash at attach log logs rm rmimages rmvols clean backup restore tests test testdlv recreate_test_db dumplog