Files
2023_WEBSITE_jipf/Makefile
2024-02-04 12:00:33 +01:00

162 lines
5.7 KiB
Makefile

GRAY = "\e[0;30m"
RED = "\e[0;31m"
GREEN = "\e[0;32m"
YELLOW = "\e[0;33m"
BLUE = "\e[0;34m"
PURPLE = "\e[0;35m"
CYAN = "\e[0;36m"
WHITE = "\e[0;37m"
B_GRAY = "\e[1;30m"
B_RED = "\e[1;31m"
B_GREEN = "\e[1;32m"
B_YELLOW = "\e[1;33m"
B_BLUE = "\e[1;34m"
B_PURPLE = "\e[1;35m"
B_CYAN = "\e[1;36m"
B_WHITE = "\e[1;37m"
RESET = "\e[0m"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
COMPOSE = ./srcs/docker-compose.yml
# in makefile you can use an env variable directly as a make variable :
# -> https://ftp.gnu.org/old-gnu/Manuals/make-3.79.1/html_chapter/make_6.html#SEC68
# so if you want to get the home directory you can use $(HOME)
# however, this will not give the same result if you run make in sudo, ex :
# make : /home/asususus
# sudo make : /root
# but you can use this command `eval echo "~$SUDO_USER"` to get
# the home directory of the user using sudo, it works in non-sudo also :
# - echo "$SUDO_USER" :
# - in normal mode it output : ""
# - in sudo mode it output the user : "username"
# - same as $USER in normal mode
# - echo "~$SUDO_USER" :
# - in linux "~USER" is the home directory of a user
# - but echo "something" will treat ~ as a string litteral
# - so the output in mormal mode will be : "~"
# - and in sudo mode it will be : "~username"
# - eval echo "~$SUDO_USER" :
# - eval will evaluate the expression and perform expansion one more time
# - so it will evaluate the output of `echo "~$SUDO_USER"`
# - in normal mode :
# - it will evaluate : "~"
# - and ouptput : "/home/username"
# - in sudo mode :
# - it will evaluate : "~username"
# - and output : "/home/username"
# - because "~username" expand in the home (~) directory of given user
# https://stackoverflow.com/questions/77088135/makefile-subst-doesnt-use-make-variable-as-expected
USER_HOME := $(shell eval echo "~$$SUDO_USER")
# extract env variables in .env file
# then expend the home path
# then expend the pwd path
# and finally remove the leading "EXPEND_" word
EXPENDED_ENV_VAR := $(shell grep "^#EXPEND_" ./srcs/.env)
EXPENDED_ENV_VAR := $(subst $$HOME_PATH,$(USER_HOME),$(EXPENDED_ENV_VAR))
EXPENDED_ENV_VAR := $(subst $$PWD_PATH,$(shell pwd),$(EXPENDED_ENV_VAR))
EXPENDED_ENV_VAR := $(EXPENDED_ENV_VAR:#EXPEND_%=%)
# this creates a list of the path from the list of the variables :
# VAR_1=/path/to_1 VAR_2=/path/to_2
# becomes :
# /path/to_1 /path/to_2
# first, foreach execute an action on each space separated parts : the variables
# - VAR_1=/path/to_1
# - VAR_2=/path/to_2
# then on each of them, it substitute the "=" with a space " " :
# - VAR_1 /path/to_1
# - VAR_2 /path/to_2
# and it only keeps the second word :
# - /path/to_1
# - /path/to_2
VOLUMES_D = $(foreach val,$(EXPENDED_ENV_VAR),$(word 2, $(subst =, ,$(val))))
# url for wordpress, use in makefile to change local
WP_URL = $(shell grep "WP_URL" ./srcs/.env | cut -d "=" -f 2)
# list of running containers, see : https://stackoverflow.com/questions/10024279/how-to-use-shell-commands-in-makefile
RUNNING = $(shell docker ps -q)
# list of volumes
VOLUMES = $(shell docker volume ls -q)
# ln -s ${WP_VOLUME_PLUGINS}/$dir ${WP_VOLUME_DIR}/wp-content/plugins/$dir
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
all: require build up
require:
# remove all the lines starting with "HOST_VOLUME_" in .env
@echo $(B_PURPLE)"removes all lines starting with 'HOST_VOLUMES' in .env"$(RESET)
@sed -i "/^HOST_VOLUME_/d" ./srcs/.env
# add new expended lines starting with "HOST_VOLUME_" after the line "# EXPENDED LINES :" in .env
@echo $(B_PURPLE)"add new expended lines starting with 'HOST_VOLUME_' in .env"$(RESET)
@$(foreach val,$(EXPENDED_ENV_VAR),sed -i "/^# EXPENDED LINES/a\$(val)" ./srcs/.env;)
# create the volumes directories
@echo $(B_PURPLE)"create the volumes directories"$(RESET)
@mkdir -p $(VOLUMES_D)
# create the ssl folder to avoid pbm at nginx docker creation
@echo $(B_PURPLE)"create the ssl folder"$(RESET)
@mkdir -p ./srcs/requirements/nginx/conf/ssl
# verify if the wordpress url is added to the local path
@echo $(B_PURPLE)"verify if the wordpress url is added to the local path"$(RESET)
@- if ! grep "127.0.0.1 $(WP_URL)" /etc/hosts 2> /dev/null; then \
@echo $(B_PURPLE)"nop ! adding it"$(RESET) \
bash -c 'echo -e "\n# adding for lejourduprof (you can delete it)\n127.0.0.1 $(WP_URL)" >> /etc/hosts'; \
fi
build:
docker compose -f $(COMPOSE) build
up:
docker compose -f $(COMPOSE) up -d
@echo $(B_PURPLE)"you can now connect at "$(B_YELLOW)"https://$(WP_URL)"$(B_PURPLE)" or 127.0.0.1"$(RESET)
down:
docker compose -f $(COMPOSE) down
# list images, containers, volumes
list:
@echo $(B_YELLOW)"\nimages:"$(RESET)
@docker images -a
@echo $(B_YELLOW)"\nvolumes:"$(RESET)
@docker volume ls
@echo $(B_YELLOW)"\nvolumes content:"$(RESET)
- @ls $(VOLUMES_D)
@echo $(B_YELLOW)"\nnetworks:"$(RESET)
@docker network ls
@echo $(B_YELLOW)"\ncontainers:"$(RESET)
@docker ps -a
@echo ""
# remove project images and containers not used
clean:
- docker stop $(RUNNING)
docker network prune -f
docker system prune -f
# remove everything except local volumes data
fclean-images: clean
- docker stop $(RUNNING)
docker system prune -af
fclean-volumes: clean
- docker volume rm $(VOLUMES)
fclean: fclean-images fclean-volumes
re: fclean all
# !! remove everything everything
erase_v:
- rm -rf $(VOLUMES_D)
new: erase_v re
.PHONY : all $(VOLUMES_D) require build up list clean fclean re erase_v new