This commit is contained in:
aynic.os
2021-05-17 03:40:02 +02:00
parent 82ce98bb2c
commit 7271c1160f
20 changed files with 125 additions and 180 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ build: docker-compose-build ## Build application docker images
# target build@%: Build application docker images to deploy
.PHONY: build@% app-build
build@%: infra-base
build@%: myos-base
$(eval DRYRUN_IGNORE := true)
$(eval SERVICES ?= $(shell $(call docker-compose,--log-level critical config --services)))
$(eval DRYRUN_IGNORE := false)
+12 -12
View File
@@ -2,20 +2,20 @@
# COMMON
.PHONY: bootstrap
bootstrap: bootstrap-git bootstrap-docker ## Bootstrap application
bootstrap: bootstrap-git bootstrap-docker app-bootstrap ## Bootstrap application
.PHONY: boostrap-docker
bootstrap-docker: docker-network-create
$(if $(filter bootstrap-$(APP),$(MAKETARGETS)),$(call make,bootstrap-$(APP)))
$(call make,docker-compose-up)
.PHONY: bootstrap-git
bootstrap-git:
ifneq ($(SUBREPO),)
if ! git config remote.subrepo/$(SUBREPO).url > /dev/null ; \
then git remote add subrepo/$(SUBREPO) $(GIT_REPOSITORY); \
bootstrap-git: bootstrap-git-$(APP_DIR)
.PHONY: bootstrap-git-%
bootstrap-git-%:
if ! git config remote.origin.url > /dev/null ; \
then git clone $(GIT_REPOSITORY) $*; \
fi
endif
.PHONY: config
config: docker-compose-config ## View docker compose file
@@ -26,7 +26,7 @@ connect: docker-compose-connect ## Connect to docker $(SERVICE)
.PHONY: connect@%
connect@%: SERVICE ?= $(DOCKER_SERVICE)
connect@%: ## Connect to docker $(SERVICE) on first remote host
$(call make,ssh-connect,../infra,APP SERVICE)
$(call make,ssh-connect,$(MYOS),APP SERVICE)
.PHONY: down
down: docker-compose-down ## Remove application dockers
@@ -42,7 +42,7 @@ endif
.PHONY: exec@%
exec@%: SERVICE ?= $(DOCKER_SERVICE)
exec@%: ## Exec a command in docker $(SERVICE) on all remote hosts
$(call make,ssh-exec,../infra,APP ARGS SERVICE)
$(call make,ssh-exec,$(MYOS),APP ARGS SERVICE)
.PHONY: logs
logs: docker-compose-logs ## Display application dockers logs
@@ -72,14 +72,14 @@ endif
.PHONY: run@%
run@%: SERVICE ?= $(DOCKER_SERVICE)
run@%: ## Run a command on all remote hosts
$(call make,ssh-run,../infra,APP ARGS)
$(call make,ssh-run,$(MYOS),APP ARGS)
.PHONY: scale
scale: docker-compose-scale ## Scale application to NUM dockers
.PHONY: ssh@%
ssh@%: ## Connect to first remote host
$(call make,ssh,../infra,APP)
$(call make,ssh,$(MYOS),APP)
# target stack: Call docker-stack function with each value of $(STACK)
.PHONY: stack
@@ -96,7 +96,7 @@ stack-%:
$(eval command := $(lastword $(subst -, ,$*)))
$(if $(findstring -,$*), \
$(if $(filter $(command),$(filter-out %-%,$(patsubst docker-compose-%,%,$(filter docker-compose-%,$(MAKETARGETS))))), \
$(call make,docker-compose-$(command) STACK="$(stack)" $(if $(filter node,$(stack)),COMPOSE_PROJECT_NAME=$(COMPOSE_PROJECT_NAME_INFRA_NODE)),,ARGS COMPOSE_IGNORE_ORPHANS SERVICE)))
$(call make,docker-compose-$(command) STACK="$(stack)" $(if $(filter node,$(stack)),COMPOSE_PROJECT_NAME=$(COMPOSE_PROJECT_NAME_NODE)),,ARGS COMPOSE_IGNORE_ORPHANS SERVICE)))
.PHONY: start
start: docker-compose-start ## Start application dockers
+2 -2
View File
@@ -27,7 +27,7 @@ DOCKER_BUILD_VARS ?= APP BRANCH DOCKER_GID DOCKER_REPOSITORY GID G
DOCKER_COMPOSE_DOWN_OPTIONS ?=
DOCKER_COMPOSE_UP_OPTIONS ?= -d
DOCKER_GID ?= $(call getent-group,docker)
DOCKER_IMAGE_TAG ?= $(if $(filter-out $(APP),infra),$(if $(filter $(ENV),$(ENV_DEPLOY)),$(VERSION),$(if $(DRONE_BUILD_NUMBER),$(DRONE_BUILD_NUMBER),latest)),latest)
DOCKER_IMAGE_TAG ?= $(if $(filter-out $(APP),myos),$(if $(filter $(ENV),$(ENV_DEPLOY)),$(VERSION),$(if $(DRONE_BUILD_NUMBER),$(DRONE_BUILD_NUMBER),latest)),latest)
DOCKER_IMAGES ?= $(patsubst %/,%,$(patsubst docker/%,%,$(dir $(wildcard docker/*/Dockerfile))))
DOCKER_PLUGIN ?= rexray/s3fs:latest
DOCKER_PLUGIN_ARGS ?= $(foreach var,$(DOCKER_PLUGIN_VARS),$(if $(DOCKER_PLUGIN_$(var)),$(var)='$(DOCKER_PLUGIN_$(var))'))
@@ -62,7 +62,7 @@ DOCKER_COMPOSE_DOWN_OPTIONS := --rmi all -v
DOCKER_COMPOSE_UP_OPTIONS := -d --build
DOCKER_BUILD_CACHE := false
ENV_SUFFIX := $(DRONE_BUILD_NUMBER)
ifneq ($(APP), infra)
ifneq ($(APP), myos)
COMPOSE_PROJECT_NAME := $(USER)_$(ENV)$(ENV_SUFFIX)_$(APP)
COMPOSE_SERVICE_NAME := $(subst _,-,$(COMPOSE_PROJECT_NAME))
DOCKER_REPOSITORY := $(USER)/$(ENV)/$(APP)
+2 -65
View File
@@ -12,77 +12,14 @@ deploy: app-deploy ## Run post install hooks in the deployed application
## tag and push docker images to docker registry
## run ansible-pull on hosts to pull docker images from the registry
## tag and push docker images as latest to docker registry
deploy@%: infra-base build@% ## Deploy application docker images
deploy@%: myos-base build@% ## Deploy application docker images
$(call make,docker-login docker-tag docker-push)
$(call make,infra-ansible-pull@$(ENV) ANSIBLE_DOCKER_IMAGE_TAG=$(VERSION) ANSIBLE_TAGS=aws,,APP AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY)
$(call make,myos-ansible-pull@$(ENV) ANSIBLE_DOCKER_IMAGE_TAG=$(VERSION) ANSIBLE_TAGS=aws,,APP AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY)
$(call make,docker-tag-latest docker-push-latest)
.PHONY: deploy-aws-codedeploy-%
deploy-aws-codedeploy-%:
$(call exec,git fetch subrepo/$(SUBREPO))
ifneq ($(wildcard ../infra),)
$(call make,aws-codedeploy,../infra,CODEDEPLOY_APP_NAME CODEDEPLOY_DEPLOYMENT_CONFIG CODEDEPLOY_DEPLOYMENT_GROUP CODEDEPLOY_DESCRIPTION CODEDEPLOY_GITHUB_REPO CODEDEPLOY_GITHUB_COMMIT_ID)
endif
.PHONY: deploy-assets-install
deploy-assets-install:
su -s /bin/sh www-data -c "php app/console --no-interaction assets:install --env=prod"
su -s /bin/sh www-data -c "php app/console --no-interaction assetic:dump --env=prod"
.PHONY: deploy-cache-clear
deploy-cache-clear:
su -s /bin/sh www-data -c "php app/console --no-interaction cache:clear --env=prod"
.PHONY: deploy-cache-warmup
deploy-cache-warmup:
su -s /bin/sh www-data -c "php app/console --no-interaction cache:warmup --env=prod"
.PHONY: deploy-composer
deploy-composer:
su -s /bin/sh www-data -c "composer install --prefer-dist --optimize-autoloader --no-progress --no-interaction --no-dev"
.PHONY: deploy-doctrine-migrations-migrate
deploy-doctrine-migrations-migrate:
su -s /bin/sh www-data -c "php app/console --no-interaction doctrine:migrations:migrate"
.PHONY: deploy-npm
deploy-npm: deploy-npm-install deploy-npm-run-build
.PHONY: deploy-npm-install
deploy-npm-install:
npm set progress=false
npm install -s
.PHONY: deploy-npm-run-build
deploy-npm-run-build:
npm run build:prod
.PHONY: deploy-ping
deploy-ping: deploy-ping-slack
.PHONY: deploy-ping-slack
deploy-ping-slack:
curl -X POST --data-urlencode 'payload={"text": "$(DEPLOY_PING_TEXT)"}' $(DEPLOY_SLACK_HOOK) ||:
.PHONY: deploy-supervisorctl-restart-all
deploy-supervisorctl-restart-all:
supervisorctl restart all
.PHONY: deploy-supervisorctl-start-all
deploy-supervisorctl-start-all:
supervisorctl start all
.PHONY: deploy-supervisorctl-stop-all
deploy-supervisorctl-stop-all:
supervisorctl stop all
.PHONY: deploy-yarn
deploy-yarn: deploy-yarn-install
.PHONY: deploy-yarn-build
deploy-yarn-build:
yarn build:prod
.PHONY: deploy-yarn-install
deploy-yarn-install:
yarn install
+32 -9
View File
@@ -2,12 +2,12 @@
# DOCKER
.PHONY: docker-build
docker-build: docker-images-infra
docker-build: docker-images-myos
$(foreach image,$(or $(SERVICE),$(DOCKER_IMAGES)),$(call make,docker-build-$(image)))
.PHONY: docker-build-%
docker-build-%:
if grep -q DOCKER_REPOSITORY docker/$*/Dockerfile 2>/dev/null; then $(eval DOCKER_BUILD_ARGS:=$(subst $(DOCKER_REPOSITORY),$(DOCKER_REPOSITORY_INFRA),$(DOCKER_BUILD_ARGS))) true; fi
if grep -q DOCKER_REPOSITORY docker/$*/Dockerfile 2>/dev/null; then $(eval DOCKER_BUILD_ARGS:=$(subst $(DOCKER_REPOSITORY),$(DOCKER_REPOSITORY_MYOS),$(DOCKER_BUILD_ARGS))) true; fi
$(if $(wildcard docker/$*/Dockerfile),$(call docker-build,docker/$*))
$(if $(findstring :,$*),$(eval DOCKERFILES := $(wildcard docker/$(subst :,/,$*)/Dockerfile)),$(eval DOCKERFILES := $(wildcard docker/$*/*/Dockerfile)))
$(foreach dockerfile,$(DOCKERFILES),$(call docker-build,$(dir $(dockerfile)),$(DOCKER_REPOSITORY)/$(word 2,$(subst /, ,$(dir $(dockerfile)))):$(lastword $(subst /, ,$(dir $(dockerfile)))),"") && true)
@@ -27,7 +27,7 @@ docker-commit-%:
$(foreach service,$(or $(SERVICE),$(SERVICES)),$(call docker-commit,$(service),,,$*))
.PHONY: docker-compose-build
docker-compose-build: docker-images-infra
docker-compose-build: docker-images-myos
$(eval DRYRUN_IGNORE := true)
$(eval SERVICES ?= $(shell $(call docker-compose,--log-level critical config --services)))
$(eval DRYRUN_IGNORE := false)
@@ -66,7 +66,7 @@ docker-compose-ps:
$(call docker-compose,ps)
.PHONY: docker-compose-rebuild
docker-compose-rebuild: docker-images-infra
docker-compose-rebuild: docker-images-myos
$(call make,docker-compose-build DOCKER_BUILD_NO_CACHE=true)
.PHONY: docker-compose-recreate
@@ -111,18 +111,27 @@ docker-compose-stop:
$(call docker-compose,stop $(if $(filter $(SERVICE),$(SERVICES)),$(SERVICE)))
.PHONY: docker-compose-up
docker-compose-up: docker-images-infra
docker-compose-up: docker-images-myos
$(eval DRYRUN_IGNORE := true)
$(eval SERVICES ?= $(shell $(call docker-compose,--log-level critical config --services)))
$(eval DRYRUN_IGNORE := false)
$(call docker-compose,up $(DOCKER_COMPOSE_UP_OPTIONS) $(if $(filter $(SERVICE),$(SERVICES)),$(SERVICE)))
.PHONY: docker-images-infra
docker-images-infra:
$(foreach image,$(subst $(quote),,$(DOCKER_IMAGES_INFRA)),$(call make,infra-docker-build-$(image)))
.PHONY: docker-images-myos
docker-images-myos:
$(foreach image,$(subst $(quote),,$(DOCKER_IMAGES_MYOS)),$(call make,myos-docker-build-$(image)))
.PHONY: docker-images-rm
docker-images-rm:
$(call make,docker-images-rm-$(DOCKER_REPOSITORY)/)
.PHONY: docker-images-rm-%
docker-images-rm-%:
docker images |awk '$$1 ~ /^$(subst /,\/,$*)/ {print $$3}' |sort -u |while read image; do docker rmi -f $$image; done
.PHONY: docker-login
docker-login: infra-aws-ecr-login
docker-login: myos-base
$(ECHO) docker login
.PHONY: docker-network-create
docker-network-create: docker-network-create-$(DOCKER_NETWORK)
@@ -175,6 +184,13 @@ docker-rebuild:
docker-rebuild-%:
$(call make,docker-build-$* DOCKER_BUILD_CACHE=false)
.PHONY: docker-rm
docker-rm: docker-rm-$(COMPOSE_PROJECT_NAME)
.PHONY: docker-rm-%
docker-rm-%:
docker ps -a |awk '$$NF ~ /^$*/ {print $$NF}' |while read docker; do docker rm -f $$docker; done
.PHONY: docker-run
docker-run: SERVICE ?= $(DOCKER_SERVICE)
docker-run:
@@ -209,3 +225,10 @@ ifneq ($(filter $(DEPLOY),true),)
else
printf "${COLOR_BROWN}WARNING${COLOR_RESET}: ${COLOR_GREEN}target${COLOR_RESET} $@ ${COLOR_GREEN}not enabled in${COLOR_RESET} $(APP).\n" >&2
endif
.PHONY: docker-volume-rm
docker-volume-rm: docker-volume-rm-$(COMPOSE_PROJECT_NAME)
.PHONY: docker-volume-rm-%
docker-volume-rm-%:
docker volume ls |awk '$$2 ~ /^$*/ {print $$2}' |sort -u |while read volume; do docker volume rm $$volume; done
-18
View File
@@ -1,18 +0,0 @@
##
# CLEAN
.PHONY: clean-docker-%
clean-docker-%:
docker ps -a |awk '$$NF ~ /_$*/ && $$NF !~ /_infra_/ {print $$NF}' |while read docker; do docker rm -f $$docker; done
.PHONY: clean-elasticsearch-%
clean-elasticsearch-%:
docker ps |awk '$$NF ~ /infra_elasticsearch/' |sed 's/^.*:\([0-9]*\)->9200\/tcp.*$$/\1/' |while read port; do echo -e "DELETE /$* HTTP/1.0\n\n" |nc localhost $$port; done
.PHONY: clean-images-%
clean-images-%:
docker images |awk '$$1 ~ /\/$*/ && $$1 !~ /\/infra\// {print $$3}' |sort -u |while read image; do docker rmi -f $$image; done
.PHONY: clean-volumes-%
clean-volumes-%:
docker volume ls |awk '$$2 ~ /_$*/ && $$2 !~ /_infra_/ {print $$2}' |sort -u |while read volume; do docker volume rm $$volume; done
+2 -5
View File
@@ -5,21 +5,18 @@
install: app-install ## Install application
.PHONY: install-mysql-database-%
install-mysql-database-%: infra-base
install-mysql-database-%: myos-base
$(call exec,mysql -h mysql -u root -proot $* -e "use $*" >/dev/null 2>&1 || mysql -h mysql -u root -proot mysql -e "create database $* character set utf8 collate utf8_unicode_ci;")
$(call exec,mysql -h mysql -u $* -p$* $* -e "use $*" >/dev/null 2>&1 || mysql -h mysql -u root -proot mysql -e "grant all privileges on $*.* to '\''$*'\''@'\''%'\'' identified by '\''$*'\''; flush privileges;")
$(call exec,[ $$(mysql -h mysql -u $* -p$* $* -e "show tables" 2>/dev/null |wc -l) -eq 0 ] && [ -f "${APP_DIR}/$*.mysql.gz" ] && gzip -cd "${APP_DIR}/$*.mysql.gz" |mysql -h mysql -u root -proot $* || true)
.PHONY: install-pgsql-database-%
install-pgsql-database-%: infra-base
install-pgsql-database-%: myos-base
$(call exec,PGPASSWORD=$* psql -h postgres -U $* template1 -c "\q" >/dev/null 2>&1 || PGPASSWORD=postgres psql -h postgres -U postgres -c "create user $* with createdb password '\''$*'\'';")
$(call exec,PGPASSWORD=$* psql -h postgres -U $* -d $* -c "" >/dev/null 2>&1 || PGPASSWORD=postgres psql -h postgres -U postgres -c "create database $* owner $* ;")
$(call exec,[ $$(PGPASSWORD=$* psql -h postgres -U $* -d $* -c "\d" 2>/dev/null |wc -l) -eq 0 ] && [ -f "${APP_DIR}/$*.pgsql.gz" ] && gzip -cd "${APP_DIR}/$*.pgsql.gz" |PGPASSWORD="postgres" psql -h postgres -U postgres -d $* || true)
$(call exec,[ $$(PGPASSWORD=$* psql -h postgres -U $* -d $* -c "\d" 2>/dev/null |wc -l) -eq 0 ] && [ -f "${APP_DIR}/$*.pgsql" ] && PGPASSWORD="postgres" psql -h postgres -U postgres -c "ALTER ROLE $* WITH SUPERUSER" && PGPASSWORD="postgres" pg_restore -h postgres --no-owner --role=$* -U postgres -d $* ${APP_DIR}/$*.pgsql && PGPASSWORD="postgres" psql -h postgres -U postgres -c "ALTER ROLE $* WITH NOSUPERUSER" || true)
.PHONY: install-infra
install-infra: infra-install
.PHONY: install-parameters
install-parameters:
$(call install-parameters)