Skip to content

Commit

Permalink
chore: improve makeFile setup (#697)
Browse files Browse the repository at this point in the history
  • Loading branch information
Davidmattei authored Dec 31, 2023
1 parent 6156739 commit 320eb45
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 527 deletions.
11 changes: 6 additions & 5 deletions .env
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# Project namespace (defaults to the current folder name if not set)
COMPOSE_PROJECT_NAME=elasticms_demo

EMS_VERSION=5.12.0
EMS_VERSION=5.12.3
INSTANCE_ID='ems_promo_v2_'
EMS_BACKEND_URL=http://local.ems-demo-admin:9000
EMS_ADMIN_URL=http://local.ems-demo-admin.localhost
EMSF_AUTHKEY=''

DB_USER=postgres
DB_USER=demo
DB_PASSWORD=s3cr3t
DB_NAME=postgres
DB_SCHEMA=public
DB_NAME=db_demo
DB_SCHEMA=schema_demo_adm
DB_HOST=postgres

S3_BUCKET_NAME=demo
Expand Down Expand Up @@ -37,4 +38,4 @@ ELK8_STACK_VERSION=8.5.3
# Set to 'basic' or 'trial' to automatically start the 30-day trial
ELK8_LICENSE=basic
#ELK8_LICENSE=trial
###< ELK8 variables ###
###< ELK8 variables ###
181 changes: 80 additions & 101 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
include .env
export $(grep -v '^#' .env | xargs)

ELK_VERSION ?= elk7
ENVIRONMENT ?= local
DOCKER_USER ?= $UID
PWD = $(shell pwd)

DEMO_DIR ?= /opt/src
RUN_ADMIN ?= docker compose exec -u ${DOCKER_USER}:0 admin-${ENVIRONMENT} ems-demo
RUN_WEB ?= docker compose exec -u ${DOCKER_USER}:0 web-${ENVIRONMENT} preview
RUN_PSQL ?= docker compose exec -u ${DOCKER_USER}:0 -e PGUSER=postgres -e PGPASSWORD=adminpg -T postgres psql
RUN_NPM ?= docker run -u ${DOCKER_USER} --rm -it -v ${PWD}:/opt/src --workdir /opt/src elasticms/base-php:8.1-cli-dev npm
ELK_VERSION ?= elk7
ENVIRONMENT ?= local
DOCKER_USER ?= $(shell id -u)
PWD = $(shell pwd)
RUN_ADMIN = docker compose exec admin-${ENVIRONMENT} ems-demo
RUN_WEB = docker compose exec web-${ENVIRONMENT} preview
RUN_POSTGRES = docker compose exec -e PGUSER=postgres -e PGPASSWORD=adminpg -T postgres
RUN_NPM = docker run -u ${DOCKER_USER}:0 --rm -it -v ${PWD}:/opt/src --workdir /opt/src elasticms/base-php:8.1-cli-dev npm

.DEFAULT_GOAL := help
.PHONY: help npm
Expand All @@ -36,34 +34,49 @@ help: # Show help for each of the Makefile recipes.
@grep -E '(^[a-zA-Z0-9_-]+:.*?##.*$$)|(^##)' Makefile | awk 'BEGIN {FS = ":.*?## "}{printf "\033[32m%-30s\033[0m %s\n", $$1, $$2}' | sed -e 's/\[32m##/[33m/'

## —— Demo —————————————————————————————————————————————————————————————————————————————————————————————————————————————
start: ## start
@$(MAKE) -s docker-start
stop: ## stop
@$(MAKE) -s docker-stop
init: ## init
start: ## start docker
@docker compose up -d
restart: ## restart docker and recreate
@docker compose up -d --force-recreate
stop: ## stop docker
@docker compose down
status: ## status docker
@docker compose ps
logs: ## logs
@docker compose logs -f
update: ## update docker images
@docker compose pull
@docker compose up -d
dump: ## create db dump
@$(MAKE) -s _db-dump
clear-cache: ## clear cache
@$(RUN_ADMIN) cache:clear
@$(RUN_WEB) cache:clear
init: ## init demo (fresh db)
@$(MAKE) -s npm-install
@$(MAKE) -s npm-prod
@$(MAKE) -s start
@$(MAKE) -s db-setup
@$(MAKE) -s cache:clear
@$(MAKE) -s _db-setup
@$(MAKE) -s _init-create-users
@$(MAKE) -s load
load: ## load
@$(MAKE) -s _init-create-users
@$(MAKE) -s web-login
@$(MAKE) -s web-restore-configs
@$(MAKE) -s login
@$(MAKE) -s restore-configs
@$(MAKE) -s admin-activate
@$(MAKE) -s admin-rebuild
@$(MAKE) -s _init-create-managed-aliases
@$(MAKE) -s web-health-check
@$(MAKE) -s health-check
@$(MAKE) -s _init-switch_default-env
@$(MAKE) -s web-login
@$(MAKE) -s web-upload-folder-assets
@$(MAKE) -s web-local-upload
@$(MAKE) -s web-local-push
@$(MAKE) -s web-health-check
@$(MAKE) -s web-restore-documents
@$(MAKE) -s emsch-folder-upload
@$(MAKE) -s emsch-assets
@$(MAKE) -s emsch-push
@$(MAKE) -s health-check
@$(MAKE) -s restore-documents
@$(MAKE) -s admin-align

## —— Admin ————————————————————————————————————————————————————————————————————————————————————————————————————————————
admin/%: ## run admin command
@$(RUN_ADMIN) $*
admin-rebuild: ## rebuild all environments
@$(RUN_ADMIN) ems:environment:rebuild --all --no-debug
admin-activate: ## activate content types
Expand All @@ -72,86 +85,32 @@ admin-align: ## align preview with live
@$(RUN_ADMIN) ems:environment:align preview live --force --no-debug

## —— Web ——————————————————————————————————————————————————————————————————————————————————————————————————————————————
web-login: ## web login (ems & emsch)
web/%: ## run web command
@$(RUN_WEB) $*
login: ## login
@$(RUN_WEB) emsch:local:login demo demo
@$(RUN_WEB) ems:admin:login --username=demo --password=demo
web-local-status: ## web local status
emsch-status: ## local status
@$(RUN_WEB) emsch:local:status
web-local-push: ## web local push
emsch-push: ## local push
@$(RUN_WEB) emsch:local:push --force
web-local-pull: ## web local pull
emsch-pull: ## local pull
@$(RUN_WEB) emsch:local:pull
web-local-upload: ## web local upload (bundle.zip)
ifeq (${DEMO_DIR},/opt/src)
@$(RUN_WEB) emsch:local:upload --filename=/opt/src/local/skeleton/template/asset_hash.twig
else
@$(RUN_WEB) emsch:local:upload ../../demo/dist/ --filename=${DEMO_DIR}/skeleton/template/asset_hash.twig
endif
web-backup-configs:
ifeq (${DEMO_DIR},/opt/src)
@$(RUN_WEB) ems:admin:backup --configs --export
else
@$(RUN_WEB) ems:admin:backup --configs-folder=${DEMO_DIR}/configs/admin --configs --export
endif
web-backup-documents:
ifeq (${DEMO_DIR},/opt/src)
@$(RUN_WEB) ems:admin:backup --configs --export
else
@$(RUN_WEB) ems:admin:backup --documents-folder=${DEMO_DIR}/configs/document --documents --export
endif
web-restore-configs:
ifeq (${DEMO_DIR},/opt/src)
@$(RUN_WEB) ems:admin:restore --configs --force
else
@$(RUN_WEB) ems:admin:restore --configs-folder=${DEMO_DIR}/configs/admin --configs --force
endif
web-restore-documents:
ifeq (${DEMO_DIR},/opt/src)
@$(RUN_WEB) ems:admin:restore --documents --force
else
@$(RUN_WEB) ems:admin:restore --documents-folder=${DEMO_DIR}/configs/document --documents --force
endif
web-upload-folder-assets: ## web upload folder assets
ifeq (${DEMO_DIR},/opt/src)
@$(RUN_WEB) emsch:local:folder-upload /opt/src/admin/assets
else
@$(RUN_WEB) emsch:local:folder-upload ${DEMO_DIR}/configs/admin/assets
endif
web-health-check:
emsch-assets: ## local upload (bundle.zip)
@$(RUN_WEB) emsch:local:upload --filename=/opt/src/local/skeleton/template/asset_hash.twig
emsch-folder-upload: ## upload folder assets
@$(RUN_WEB) emsch:local:folder-upload /opt/src/admin/assets
backup-configs: ## backup configs
@$(RUN_WEB) ems:admin:backup --configs --export
backup-documents: ## backup documents
@$(RUN_WEB) ems:admin:backup --documents --export
restore-configs: ## restore configs
@$(RUN_WEB) ems:admin:restore --configs --force
restore-documents: ## restore documents
@$(RUN_WEB) ems:admin:restore --documents --force
health-check: ## health check
@$(RUN_WEB) emsch:health-check -g --no-debug

## —— Docker ———————————————————————————————————————————————————————————————————————————————————————————————————————————
docker-start: ## start docker
@docker compose up -d
docker-restart: ## restart docker
@docker compose up -d --force-recreate
docker-stop: ## stop docker
@docker compose down
docker-status: ## status docker
@docker compose ps
docker-update: ## update docker
@docker compose pull
@docker compose up -d

## —— Database —————————————————————————————————————————————————————————————————————————————————————————————————————————
db-setup: ## setup fresh demo (drops database)
@$(MAKE) -s _db-drop
@$(MAKE) -s _db-create
@$(MAKE) -s db-migrate
db-migrate: ## run doctrine migrations
@$(RUN_ADMIN) doctrine:migrations:migrate --no-interaction
_db-drop:
@$(RUN_PSQL) -c "DROP DATABASE IF EXISTS ${DB_NAME};"
@$(RUN_PSQL) -c "DROP USER IF EXISTS ${DB_USER};"
_db-create:
@$(RUN_PSQL) -c "CREATE USER ${DB_USER} WITH ENCRYPTED PASSWORD '${DB_PASSWORD}';"
@$(RUN_PSQL) -c "CREATE DATABASE ${DB_NAME} WITH OWNER ${DB_USER};"
@$(RUN_PSQL) -c "GRANT ALL PRIVILEGES ON DATABASE ${DB_NAME} TO ${DB_USER};"
@$(RUN_PSQL) -d ${DB_NAME} -c "ALTER SCHEMA public OWNER TO ${DB_USER};"
@$(RUN_PSQL) -d ${DB_NAME} -c "ALTER SCHEMA public RENAME TO ${DB_SCHEMA}"
@$(RUN_PSQL) -d ${DB_NAME} -c "ALTER USER ${DB_USER} SET search_path TO ${DB_SCHEMA};"
@$(RUN_PSQL) -d ${DB_NAME} -c "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA ${DB_SCHEMA} TO ${DB_USER};"

## —— Npm ——————————————————————————————————————————————————————————————————————————————————————————————————————————————
npm/%:
@$(RUN_NPM) $*
Expand All @@ -164,6 +123,26 @@ npm-watch: ## npm run watch
npm-dev: ## npm run dev
@$(MAKE) npm/"run dev"

_db-setup:
@$(MAKE) -s _db-drop
@$(MAKE) -s _db-create
@$(MAKE) -s _db-migrate
_db-dump:
@$(RUN_POSTGRES) pg_dump ${DB_NAME} -w --clean -Fp -O --schema=${DB_SCHEMA} | sed "/^\(DROP\|ALTER\|CREATE\) SCHEMA.*\$$/d" > dump_demo_$$(date +%Y%m%d%H%M%S).sql
_db-migrate:
@$(RUN_ADMIN) doctrine:migrations:migrate --no-interaction
_db-drop:
@$(RUN_POSTGRES) psql -c "DROP DATABASE IF EXISTS ${DB_NAME};"
@$(RUN_POSTGRES) psql -c "DROP USER IF EXISTS ${DB_USER};"
_db-create:
@$(RUN_POSTGRES) psql -c "CREATE USER ${DB_USER} WITH ENCRYPTED PASSWORD '${DB_PASSWORD}';"
@$(RUN_POSTGRES) psql -c "CREATE DATABASE ${DB_NAME} WITH OWNER ${DB_USER};"
@$(RUN_POSTGRES) psql -c "GRANT ALL PRIVILEGES ON DATABASE ${DB_NAME} TO ${DB_USER};"
@$(RUN_POSTGRES) psql -d ${DB_NAME} -c "ALTER SCHEMA public OWNER TO ${DB_USER};"
@$(RUN_POSTGRES) psql -d ${DB_NAME} -c "ALTER SCHEMA public RENAME TO ${DB_SCHEMA}"
@$(RUN_POSTGRES) psql -d ${DB_NAME} -c "ALTER USER ${DB_USER} SET search_path TO ${DB_SCHEMA};"
@$(RUN_POSTGRES) psql -d ${DB_NAME} -c "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA ${DB_SCHEMA} TO ${DB_USER};"

_init-create-managed-aliases:
@$(RUN_ADMIN) ems:managed-alias:add-environment ma_preview preview
@$(RUN_ADMIN) ems:managed-alias:add-environment ma_preview default
Expand All @@ -190,4 +169,4 @@ _init-create-users:
@$(RUN_ADMIN) emsco:user:promote demo ROLE_FORM_CRM
@$(RUN_ADMIN) emsco:user:promote demo ROLE_TASK_MANAGER
_init-switch_default-env:
@$(RUN_ADMIN) emsco:contenttype:switch-default-env media_file default
@$(RUN_ADMIN) emsco:contenttype:switch-default-env media_file default
Loading

0 comments on commit 320eb45

Please sign in to comment.