export ARQUITECTURA := $(shell lscpu |grep itectur | tr -d ' '| cut -f2 -d':')
export REGISTRY=registry.reymota.es
export IMG_VERSION = 0.46
export IMG_NGINX_VERSION = 1.4

# limpia todo
all: imagen clean install

imagen:
	cd ../; make

install:
	-kubectl create -f namespace.yaml
	-kubectl create -f reg-secret.yaml
	-kubectl create -f env-prod-configmap.yaml 
	-kubectl create -f env-prod-db-configmap.yaml 

	-kubectl create -f pv-local-finanzas.yaml
	-kubectl create -f finanzas-prod-persistentvolumeclaim.yaml
	-kubectl create -f static-volume-persistentvolumeclaim.yaml
	-kubectl create -f postgres-data-persistentvolumeclaim.yaml

	-kubectl create -f db-deployment.yaml
	-kubectl create -f db-service.yaml

	-envsubst < finanzas-deployment.yaml |kubectl create -f -
	-kubectl create -f finanzas-ingress.yaml

clean:
	-envsubst < finanzas-deployment.yaml |kubectl delete -f -

	-kubectl delete -f db-deployment.yaml
	-kubectl delete -f db-service.yaml

	-kubectl delete -f env-prod-configmap.yaml 
	-kubectl delete -f env-prod-db-configmap.yaml 

	-kubectl delete -f postgres-data-persistentvolumeclaim.yaml
	-kubectl delete -f static-volume-persistentvolumeclaim.yaml
	-kubectl delete -f finanzas-prod-persistentvolumeclaim.yaml
	-kubectl delete -f pv-local-finanzas.yaml
	-kubectl delete -f reg-secret.yaml
	-kubectl delete -f finanzas-ingress.yaml
	-kubectl delete -f namespace.yaml

nginx:
	cd ../nginx; make

backup:
	kubectl --kubeconfig /home/creylopez/.kube/config -n finanzas exec -ti deployment.apps/db -- /usr/lib/postgresql/15/bin/pg_dump --username=creylopez --dbname=finanzas > finanzas-$(IMG_VERSION).sql

fleet:
	-cp namespace.yaml ../Fleet/15-namespace.yaml
	-cp reg-secret.yaml ../Fleet/20-reg-secret.yaml
	-cp env-prod-configmap.yaml  ../Fleet/25-env-prod-configmap.yaml
	-cp env-prod-db-configmap.yaml  ../Fleet/30-env-prod-db-configmap.yaml
	-cp pv-local-finanzas.yaml ../Fleet/35-pv-local-finanzas.yaml
	-cp finanzas-prod-persistentvolumeclaim.yaml ../Fleet/40-finanzas-prod-persistentvolumeclaim.yaml
	-cp static-volume-persistentvolumeclaim.yaml ../Fleet/43-static-volume-persistentvolumeclaim.yaml
	-cp postgres-data-persistentvolumeclaim.yaml ../Fleet/45-postgres-data-persistentvolumeclaim.yaml
	-cp db-deployment.yaml ../Fleet/55-db-deployment.yaml
	-cp db-service.yaml ../Fleet/60-db-service.yaml
	-cp nginx-service.yaml ../Fleet/65-nginx-service.yaml
	-envsubst < finanzas-deployment.yaml > ../Fleet/70-finanzas-deployment.yaml
	-envsubst < nginx-deployment.yaml > ../Fleet/75-nginx-deployment.yaml
	-cp finanzas-ingress.yaml ../Fleet/80-finanzas-ingress.yaml
