Skip to content

Instantly share code, notes, and snippets.

@wtask
Last active May 9, 2021 18:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wtask/543061423e8b6ad77352ffd127d5e0c8 to your computer and use it in GitHub Desktop.
Save wtask/543061423e8b6ad77352ffd127d5e0c8 to your computer and use it in GitHub Desktop.
Orchestrate the orchestrator with Makefile
version: "3.8"
services:
postgres:
image: postgres:13.1
container_name: postgres13-${ENV:-dev}
ports:
- ${POSTGRES_PORT:-5432}:5432
environment:
POSTGRES_USER: ${POSTGRES_USER:-service}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-service}
POSTGRES_DB: ${POSTGRES_DB:-service}
mongo:
image: mongo:bionic
container_name: mongo-bionic-${ENV:-dev}
ports:
- ${MONGODB_PORT:-27017}:27017
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGODB_USER:-service}
MONGO_INITDB_ROOT_PASSWORD: ${MONGODB_PASSWORD:-service}
description := "Generic development dependency runner"
comma := ,
ENV ?= dev
# You may set credentials for all services with this single session var
# Example:
# CREDENTIALS=micro-service make postgres
# OR under Windows:
# wsl CREDENTIALS=micro-service make postgres
CREDENTIALS ?= service
# Or as before individually for every service
POSTGRES_PORT ?= 5432
POSTGRES_USER ?= $(CREDENTIALS)
POSTGRES_PASSWORD ?= $(CREDENTIALS)
POSTGRES_DB ?= $(CREDENTIALS)
MONGODB_PORT ?= 27017
MONGODB_USER ?= $(CREDENTIALS)
MONGODB_PASSWORD ?= $(CREDENTIALS)
.PHONY: all
all: about
@printf "%-16s %b\n" "config" "\e[0;90mPrint docker-compose config\e[0m"
@printf "%-16s %b\n" "postgres" "\e[0;90mRun Postgres database\e[0m"
@printf "%-16s %b\n" "postgres-logs" "\e[0;90mPrint Postgres container logs\e[0m"
@printf "%-16s %b\n" "postgres-stop" "\e[0;90mStop Postgres database\e[0m"
@printf "%-16s %b\n" "mongo" "\e[0;90mRun Mongo database\e[0m"
@printf "%-16s %b\n" "mongo-logs" "\e[0;90mPrint MongoDB container logs\e[0m"
@printf "%-16s %b\n" "mongo-stop" "\e[0;90mStop Mongo database\e[0m"
@printf "%-16s %b\n" "down" "\e[0;90mStop and remove ALL services\e[0m"
@echo
.PHONY: about
about:
@echo "$(description)"
@echo
##############
# internals #
##############
# env-settings
define env-settings
ENV=$(ENV) \
WSLENV=$$WSLENV:ENV/w
endef
define postgres-settings
POSTGRES_PORT=$(POSTGRES_PORT) POSTGRES_USER=$(POSTGRES_USER) POSTGRES_PASSWORD=$(POSTGRES_PASSWORD) POSTGRES_DB=$(POSTGRES_DB) \
WSLENV=$$WSLENV:POSTGRES_PORT/w:POSTGRES_USER/w:POSTGRES_PASSWORD/w:POSTGRES_DB/w
endef
define mongo-settings
MONGODB_PORT=$(MONGODB_PORT) MONGODB_USER=$(MONGODB_USER) MONGODB_PASSWORD=$(MONGODB_PASSWORD) \
WSLENV=$$WSLENV:MONGODB_PORT/w:MONGODB_USER/w:MONGODB_PASSWORD/w
endef
# wait-tcp: service,pattern,timeout-sec
define wait-tcp
@docker-compose exec $(1) sh -c ' \
printf "\033[0;90mWait\033[0m $(1) "; i=0; \
until grep -q "$(2)" /proc/net/tcp; do \
i=`expr $$i + 1`; \
if [ $$i -gt $(3) ]; then printf " \033[0;31mtimeout exceeded\033[0m\n"; exit 0; fi; \
printf "."; sleep 1; \
done; \
printf " \033[0;32mready\033[0m\n"; \
'
endef
############
# targets #
############
.PHONY: config
config:
@$(call env-settings) \
$(call postgres-settings) \
$(call mongo-settings) \
docker-compose config
.PHONY: postgres
postgres:
@$(call env-settings) \
$(call postgres-settings) \
docker-compose up -d postgres
@$(call wait-tcp,postgres,00000000:1538 00000000:0000,30)
.PHONY: postgres-logs
postgres-logs:
@$(call env-settings) \
$(call postgres-settings) \
docker-compose logs postgres
.PHONY: postgres-stop
postgres-stop:
@$(call env-settings) \
$(call postgres-settings) \
docker-compose stop postgres
.PHONY: mongo
mongo:
@$(call env-settings) \
$(call mongo-settings) \
docker-compose up -d mongo
@$(call wait-tcp,mongo,00000000:6989 00000000:0000,30)
.PHONY: mongo-logs
mongo-logs:
@$(call env-settings) \
$(call mongo-settings) \
docker-compose logs mongo
.PHONY: mongo-stop
mongo-stop:
@$(call env-settings) \
$(call mongo-settings) \
docker-compose stop mongo
.PHONY: down
down:
@$(call env-settings) \
$(call postgres-settings) \
$(call mongo-settings) \
docker-compose down
@wtask
Copy link
Author

wtask commented Apr 23, 2021

Аdd the up target by your own wishes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment