## # Makefile to help manage docker-compose services # # Include environment files include .env export # Variables THIS_FILE := $(lastword $(MAKEFILE_LIST)) DOCKER := $(shell which docker) DOCKER_COMPOSE := $(shell which docker-compose) IMAGE_DEFAULT := bskydj-app CONTAINER_DEFAULT := bskydj-app-1 BACKUP_SERVICE := backup RESTORE_SERVICE := restore SHELL_CMD := /bin/bash # Docker compose files DOCKER_COMPOSE_FILES := -f docker-compose.yml ifeq ($(DOCKER_ENV),local) DOCKER_COMPOSE_FILES := -f docker-compose.yml endif ifeq ($(DOCKER_ENV),production) DOCKER_COMPOSE_FILES := -f docker-compose.yml endif # Services SERVICES_DEFAULT := app db ifeq ($(DOCKER_ENV),local) SERVICES_DEFAULT := app db endif ifeq ($(DOCKER_ENV),production) SERVICES_DEFAULT := app db endif SERVICE_DEFAULT := app container ?= $(CONTAINER_DEFAULT) image ?= $(IMAGE_DEFAULT) service ?= services ?= $(SERVICES_DEFAULT) .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 service=app" @echo " " "services" "\t" "Target services for docker-compose actions (default=all-services, space separated)" @echo " " " " "\t" " - make stop services='app db'" @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)" ## # 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) ## # down # down: ## Removes containers (preserves images and volumes) @$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) down ## # build # build: ## Builds service images [service|services] @$(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 [service|services] @$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) ps $(services) ## # 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 ## # 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 --rm $(BACKUP_SERVICE) ## # restore # restore: ## Run 'restore' service @echo @echo "Running '$(BACKUP_SERVICE)'" @$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) run --rm $(RESTORE_SERVICE) ## # migrate # migrate: ## Run 'migrate', django migrations, needs the container to be up and running @$(DOCKER) exec -it "$(container)" /entrypoint.sh run-migrations cleandb: @prefix="bsky.local-"; \ path="_data/backup/*" ; \ suffix=".sql.gz"; \ days_to_keep=10; \ current_month=$$(date "+%m"); \ current_day=$$(date "+%d"); \ for file in $$path; do \ filename=$$(basename "$$file") ; \ if [ "$$filename" != "latest$$suffix" ]; then \ file_year=$$(echo "$$filename" | cut -d"-" -f2) ; \ file_month=$$(echo "$$filename" | cut -d"-" -f3) ; \ file_day=$$(echo "$$filename" | cut -d"-" -f4 | cut -d"." -f1) ; \ if [ "$$file_month" != "$$current_month" ]; then \ last_day=$$(date -d "$$file_year/$$file_month/01 +1 month -1 day" +"%Y-%m-%d") ; \ last_file=$$(echo "$$prefix$$last_day$$suffix") ; \ if [ "$$filename" != "$$last_file" ]; then \ echo "Cleaning: $$file" ; \ rm $$file ; \ else \ echo "Storing: $$file" ; \ fi ; \ else \ day_diff=$$(( $$days_to_keep - $$current_day + $$file_day )) ; \ if [ $$day_diff -lt 0 ]; then \ echo "Cleaning: $$file" ; \ rm $$file ; \ else \ echo "Storing: $$file" ; \ fi ; \ fi ; \ fi ; \ done ; %: @: .PHONY: %