diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9222170 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +# Env file +*.env + +docker-compose.override.yml diff --git a/Makefile b/Makefile index 4eb2f45..8073828 100644 --- a/Makefile +++ b/Makefile @@ -1,20 +1,10 @@ -all: up passwd +default: help +include *.mk -.PHONY: pull -pull: - ./remote.sh docker-compose pull - -.PHONY: up -up: pull - ./remote.sh docker-compose up -d - -.PHONY: clean -clean: - ./remote.sh docker-compose down - ./remote.sh docker-compose kill - ./remote.sh docker-compose rm +start: docker-compose-start ##- Start +deploy: docker-compose-deploy ##- Deploy (start remotely) .PHONY: passwd -passwd: +passwd: environment @echo "Changing password for user 'docker'" - ./remote.sh docker-compose run --entrypoint htpasswd registry -cB /auth/htpasswd docker + $(load_env); docker-compose run --entrypoint htpasswd registry -cB /auth/htpasswd docker diff --git a/docker-compose.mk b/docker-compose.mk new file mode 100644 index 0000000..bfae70b --- /dev/null +++ b/docker-compose.mk @@ -0,0 +1,35 @@ +compose_files := + +.PHONY: docker-compose-pull +docker-compose-pull: environment ##- Pull latest containers + $(info *** Pulling containers ***) + -$(load_env); docker-compose ${compose_files} pull + +.PHONY: docker-compose-build +docker-compose-build: environment ##- Build containers + $(info *** Building containers ***) + $(load_env); docker-compose ${compose_files} build + +.PHONY: docker-compose-start +docker-compose-start: environment ##- Start containers + $(info *** Starting containers ***) + $(load_env); docker-compose ${compose_files} up -d + +.PHONY: docker-compose-stop +docker-compose-stop: environment ##- Stop containers + $(info *** Stopping containers ***) + $(load_env); docker-compose ${compose_files} down + +.PHONY: docker-compose-logs +docker-compose-logs: environment ##- Print containers logs + $(info *** Printing containers logs ***) + $(load_env); docker-compose ${compose_files} logs -f + +.PHONY: docker-compose-check-remote-env +docker-compose-check-remote-env: environment ##- Check environment variables + $(info *** Checking env variables ***) + $(load_env); test $$DOCKER_HOST + $(load_env); test $$COMPOSE_PROJECT_NAME + +.PHONY: docker-compose-deploy +docker-compose-deploy: docker-compose-check-remote-env docker-compose-start ##- Deploy containers diff --git a/docker-compose.yml b/docker-compose.yml index a9f6346..7ba2ebb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -24,9 +24,9 @@ services: - "traefik.port=5000" - "traefik.enable=true" # - "traefik.docker.network=traefik_network" - - "traefik.http.routers.registry.rule=Host(`${HOSTNAME:-localhost}`)" + - "traefik.http.routers.registry.rule=Host(`${APP_FQDN:-localhost}`)" - "traefik.http.routers.registry.entrypoints=web" - - "traefik.http.routers.registry-tls.rule=Host(`${HOSTNAME:-localhost}`)" + - "traefik.http.routers.registry-tls.rule=Host(`${APP_FQDN:-localhost}`)" - "traefik.http.routers.registry-tls.entrypoints=websecure" - "traefik.http.routers.registry-tls.tls.certresolver=default" diff --git a/env.sh b/env.sh new file mode 100755 index 0000000..3e2637e --- /dev/null +++ b/env.sh @@ -0,0 +1,25 @@ +#!/bin/sh +set -eu + +stage=${1:-'default'} + +cat <&2 + exit 1 + ;; +esac diff --git a/environment.mk b/environment.mk new file mode 100644 index 0000000..834a6c6 --- /dev/null +++ b/environment.mk @@ -0,0 +1,30 @@ +stage ?= default + +.PHONY: environment +environment: ${stage}.env ##- Define environment variables + @test ${stage} || (echo 'stage not set'; exit 1) + @$(eval ENV_FILE?=./${stage}.env) + @$(eval load_env=set -a;. ${ENV_FILE};set +a) + +%.env: env.sh + @echo "Env file $@ is not found or obsolete" + @echo "Please update it (review and touch, or call make [-e stage=${stage}] generate-env)"; exit 1 + +.PHONY: generate-env +generate-env: env.sh ##- Generate environment file ${stage}.env + @test ${stage} || (echo 'stage not set'; exit 1) + @./env.sh ${stage} > ${stage}.env + @$(eval OVERRIDE_ENV_FILE?=./override.env) + @[ -f "${OVERRIDE_ENV_FILE}" ] && echo "Appending environment override"; true + @(([ -x "${OVERRIDE_ENV_FILE}" ] && "${OVERRIDE_ENV_FILE}") || \ + ([ -r "${OVERRIDE_ENV_FILE}" ] && cat "${OVERRIDE_ENV_FILE}") || true) | tee -a ${stage}.env + @echo "Environment file ${stage}.env generated" + +.PHONY: dump-env +dump-env: environment ##- Dump environment + @echo "dump ENV_FILE: ${ENV_FILE}" + $(load_env); env + +.PHONY: shell-env +shell-env: environment ##- Start a local shell with environment + @$(load_env); PS1='env$$ ' ${SHELL} diff --git a/help.mk b/help.mk new file mode 100644 index 0000000..0e9dc7f --- /dev/null +++ b/help.mk @@ -0,0 +1,4 @@ +help: ##- Show this help. + @echo 'Usage: make (see target list below)' + @echo + @sed -e '/#\{2\}-/!d; s/\\$$//; s/:[^#\t]*/:/; s/#\{2\}-*//' $(MAKEFILE_LIST)